Skip to content

Commit c8435b4

Browse files
committed
feat: add gpio pinmux
1 parent 76c9bd3 commit c8435b4

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

src/fw/drivers/sf32lb/gpio.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void gpio_release(GPIO_TypeDef *GPIOx) {
5050
void gpio_output_init(const OutputConfig *pin_config, GPIOOType_TypeDef otype,
5151
GPIOSpeed_TypeDef speed) {
5252
(void)speed;
53+
gpio_use(pin_config->gpio);
5354
GPIO_InitTypeDef GPIO_InitStruct;
5455
GPIO_InitStruct.Pin = pin_config->gpio_pin;
5556
if (otype == GPIO_OType_OD) {
@@ -59,7 +60,7 @@ void gpio_output_init(const OutputConfig *pin_config, GPIOOType_TypeDef otype,
5960
} else {
6061
WTF;
6162
}
62-
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT;
63+
HAL_PIN_Set(PAD_PA00 + pin_config->gpio_pin, GPIO_A0 + pin_config->gpio_pin, PIN_NOPULL, 1);
6364
GPIO_InitStruct.Pull = GPIO_NOPULL;
6465

6566
HAL_GPIO_Init(pin_config->gpio, &GPIO_InitStruct);
@@ -72,9 +73,35 @@ void gpio_input_init(const InputConfig *pin_config) {
7273
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
7374
GPIO_InitStruct.Pull = GPIO_NOPULL;
7475

76+
HAL_PIN_Set(PAD_PA00 + pin_config->gpio_pin, GPIO_A0 + pin_config->gpio_pin, PIN_NOPULL, 1);
7577
HAL_GPIO_Init(pin_config->gpio, &GPIO_InitStruct);
7678
}
7779

80+
void gpio_input_init_pull_up_down(const InputConfig *input_cfg, GPIOPuPd_TypeDef pupd) {
81+
gpio_use(input_cfg->gpio);
82+
GPIO_InitTypeDef GPIO_InitStruct;
83+
GPIO_InitStruct.Pin = input_cfg->gpio_pin;
84+
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
85+
GPIO_InitStruct.Pull = GPIO_NOPULL;
86+
87+
int flag = 0;
88+
if (pupd == GPIO_PuPd_UP) {
89+
flag = GPIO_PULLUP;
90+
} else if (pupd == GPIO_PuPd_DOWN) {
91+
flag = GPIO_PULLDOWN;
92+
} else {
93+
WTF;
94+
}
95+
96+
HAL_PIN_Set(PAD_PA00 + input_cfg->gpio_pin, GPIO_A0 + input_cfg->gpio_pin, flag, 1);
97+
HAL_GPIO_Init(input_cfg->gpio, &GPIO_InitStruct);
98+
}
99+
100+
bool gpio_input_read(const InputConfig *input_cfg) {
101+
bool value = HAL_GPIO_ReadPin(input_cfg->gpio, input_cfg->gpio_pin);
102+
return value;
103+
}
104+
78105
void gpio_output_set(const OutputConfig *pin_config, bool asserted) {
79106
HAL_GPIO_WritePin(pin_config->gpio, pin_config->gpio_pin, asserted);
80107
}

0 commit comments

Comments
 (0)