一、drv_gpio.c
1.1在drv_common,c中调用rt_hw_pin_init()函数

1.2 rt_hw_pin_init()
启用 STM32 微控制器中 GPIOA 外设的时钟

1.3 pins[]
为每个pin定义编号


这个宏会创建一个结构体,结构体中包含了三个成员:
index
:通常是一个标识符或索引,用于标识该引脚的具体位置。
GPIO##gpio
:指定了 GPIO 端口(如 GPIOA
、GPIOB
等)。
GPIO_PIN_##gpio_index
:指定了具体的引脚(如 GPIO_PIN_0
、GPIO_PIN_1
等)。
stm32_pin_write
1 2 3 4 5 6 7 8 9 10 11 12
| static void stm32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value) { const struct pin_index *index;
index = get_pin(pin); if (index == RT_NULL) { return; }
HAL_GPIO_WritePin(index->gpio, index->pin, (GPIO_PinState)value); }
|
stm32_pin_read
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| static int stm32_pin_read(rt_device_t dev, rt_base_t pin) { int value; const struct pin_index *index;
value = PIN_LOW;
index = get_pin(pin); if (index == RT_NULL) { return value; }
value = HAL_GPIO_ReadPin(index->gpio, index->pin);
return value; }
|
stm32_pin_mode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| atic void stm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode) { const struct pin_index *index; GPIO_InitTypeDef GPIO_InitStruct;
index = get_pin(pin); if (index == RT_NULL) { return; }
GPIO_InitStruct.Pin = index->pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
if (mode == PIN_MODE_OUTPUT) { GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; } else if (mode == PIN_MODE_INPUT) { GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; } else if (mode == PIN_MODE_INPUT_PULLUP) { GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; } else if (mode == PIN_MODE_INPUT_PULLDOWN) { GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLDOWN; } else if (mode == PIN_MODE_OUTPUT_OD) { GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; }
HAL_GPIO_Init(index->gpio, &GPIO_InitStruct); }
|