本博客中示例代码下载路径: https://github.com/maziot-stm32/A1/releases/tag/v0.6
驱动可配置化开发
前面已经提到了代码要分层, 便于理解和阅读代码. 这里针对其中的驱动层引入一个新的概念, 驱动可配置化开发.
何为可配置化开发, 举个最简单的例子:
原来我的 STM32F103RC 小板子(简称A1设备)上 PA8 和 PD2 接了一个 LED 灯, 现在我重新画了一个板子(简称C3设备), 用的依然是 F103RC 主控, 但是 LED 灯的接在 PC13 引脚上. 现在我希望 C3 可以直接复用 A1 的驱动, 驱动代码中改动量控制在仅仅只需要修改硬件上外挂的 LED 灯个数, 有效电平, 连接的 GPIO 引脚等与硬件电路强相关的信息, 其余与硬件不相关的代码不需要做任何修改.
简单总结下:
- 可配置化开发目的是为了提高代码的复用性
- 可配置化开发将与硬件设计强相关的参数剥离出来, 针对不同的设备做不同的配置
LED 驱动可配置化抽象
基于上面的概念, 想一下, LED 灯与硬件强相关的配置有哪些?
目前我想到的有这些:
- 硬件连接: 接在STM32的哪个GPIO口上, 包括PORT和PIN.
- 有效电平: 高电平点亮还是低电平点亮.
- 默认状态: 上电后默认是点亮还是熄灭.
用结构体描述如下:
typedef struct _MAZDRV_LED_CTRL_
{
MAZDRV_LED_GPIO_PORT port; // 连接LED的GPIO PORT
MAZDRV_LED_GPIO_PIN pin; // 连接LED的GPIO PIN
MAZDRV_LED_GPIO_LEVEL light; // 高电平还是低电平点亮
MAZDRV_LED_STATUS status; // 默认状态是点亮还是熄灭
} MAZDRV_LED_CTRL;
每一个成员分别定义对应的枚举类型, 用于规范配置项, 枚举定义如下:
typedef enum _MAZDRV_LED_GPIO_PORT_
{
MAZDRV_LED_GPIO_PORTA = GPIOA_BASE,
MAZDRV_LED_GPIO_PORTB = GPIOB_BASE,
MAZDRV_LED_GPIO_PORTC = GPIOC_BASE,
MAZDRV_LED_GPIO_PORTD = GPIOD_BASE,
MAZDRV_LED_GPIO_PORTE = GPIOE_BASE,
MAZDRV_LED_GPIO_PORTF = GPIOF_BASE,
MAZDRV_LED_GPIO_PORTG = GPIOG_BASE,
} MAZDRV_LED_GPIO_PORT;
typedef enum _MAZDRV_LED_GPIO_PIN_
{
MAZDRV_LED_GPIO_PIN0 = GPIO_PIN_0,
MAZDRV_LED_GPIO_PIN1 = GPIO_PIN_1,
MAZDRV_LED_GPIO_PIN2 = GPIO_PIN_2,
MAZDRV_LED_GPIO_PIN3 = GPIO_PIN_3,
MAZDRV_LED_GPIO_PIN4 = GPIO_PIN_4,
MAZDRV_LED_GPIO_PIN5 = GPIO_PIN_5,
MAZDRV_LED_GPIO_PIN6 = GPIO_PIN_6,
MAZDRV_LED_GPIO_PIN7 = GPIO_PIN_7,
MAZDRV_LED_GPIO_PIN8 = GPIO_PIN_8,
MAZDRV_LED_GPIO_PIN9 = GPIO_PIN_9,
MAZDRV_LED_GPIO_PIN10 = GPIO_PIN_10,
MAZDRV_LED_GPIO_PIN11 = GPIO_PIN_11,
MAZDRV_LED_GPIO_PIN12 = GPIO_PIN_12,
MAZDRV_LED_GPIO_PIN13 = GPIO_PIN_13,
MAZDRV_LED_GPIO_PIN14 = GPIO_PIN_14,
MAZDRV_LED_GPIO_PIN15 = GPIO_PIN_15,
MAZDRV_LED_GPIO_PIN_ALL = GPIO_PIN_All,
} MAZDRV_LED_GPIO_PIN;
typedef enum _MAZDRV_LED_GPIO_LEVEL_
{
MAZDRV_LED_GPIO_LEVEL_HIGH = GPIO_PIN_SET,
MAZDRV_LED_GPIO_LEVEL_LOW = GPIO_PIN_RESET,
} MAZDRV_LED_GPIO_LEVEL;
typedef enum _MAZDRV_LED_STATUS_
{
MAZDRV_LED_STATUS_ON = 0,
MAZDRV_LED_STATUS_OFF,
MAZDRV_LED_STATUS_TOGGLE,
} MAZDRV_LED_STATUS;
基于上述剥离出来的参数, 硬件上不同的 LED 灯就可以用 MAZDRV_LED_CTRL 对象来描述了. 示例代码如下:
typedef enum _MAZDRV_LED_
{
MAZDRV_LED0 = 0,
MAZDRV_LED1,
MAZDRV_LED_MAX,
} MAZDRV_LED;
static MAZDRV_LED_CTRL g_mazdrv_led_ctrl[MAZDRV_LED_MAX] =
{
[MAZDRV_LED0] =
{
.port = MAZDRV_LED_GPIO_PORTA,
.pin = MAZDRV_LED_GPIO_PIN8,
.light = MAZDRV_LED_GPIO_LEVEL_LOW,
.status = MAZDRV_LED_STATUS_OFF,
},
[MAZDRV_LED1] =
{
.port = MAZDRV_LED_GPIO_PORTD,
.pin = MAZDRV_LED_GPIO_PIN2,
.light = MAZDRV_LED_GPIO_LEVEL_LOW,
.status = MAZDRV_LED_STATUS_OFF,
},
};
LED 可配置化驱动程序
上面通过 MAZDRV_LED_CTRL 对象将硬件上 LED 的信息描述出来了. 现在我们的驱动就必须以这些硬件信息为输入, 支持各种各样的配置情况.
LED 初始化:
int MAZ_Drv_led_init(void)
{
MAZDRV_LED led;
MAZDRV_LED_CTRL *ctrl = NULL;
GPIO_InitTypeDef GPIO_InitStruct = { 0 };
ctrl = g_mazdrv_led_ctrl;
for (led = MAZDRV_LED0; led < MAZDRV_LED_MAX; led++)
{
/* GPIO Ports Clock Enable */
MAZ_Drv_led_gpio_prot_clk_enable(led);
/* Configure GPIO pin */
GPIO_InitStruct.Pin = ctrl[led].pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pull = (ctrl[led].light == MAZDRV_LED_GPIO_LEVEL_HIGH) ? GPIO_PULLDOWN : GPIO_PULLUP;
HAL_GPIO_Init((GPIO_TypeDef*) ctrl[led].port, &GPIO_InitStruct);
/* Set default status */
MAZ_Drv_led_set_status(led, ctrl[led].status);
}
return MAZRET_NOERR;
}
int MAZ_Drv_led_gpio_prot_clk_enable(MAZDRV_LED led)
{
MAZDRV_LED_GPIO_PORT port;
MAZDRV_LED_CTRL *ctrl = NULL;
if (led < MAZDRV_LED0 || led >= MAZDRV_LED_MAX)
{
return MAZRET_EINVAL;
}
ctrl = g_mazdrv_led_ctrl;
port = ctrl[led].port;
/* GPIO Ports Clock Enable */
switch (port)
{
case MAZDRV_LED_GPIO_PORTA:
__HAL_RCC_GPIOA_CLK_ENABLE();
break;
case MAZDRV_LED_GPIO_PORTB:
__HAL_RCC_GPIOB_CLK_ENABLE();
break;
case MAZDRV_LED_GPIO_PORTC:
__HAL_RCC_GPIOC_CLK_ENABLE();
break;
case MAZDRV_LED_GPIO_PORTD:
__HAL_RCC_GPIOD_CLK_ENABLE();
break;
case MAZDRV_LED_GPIO_PORTE:
__HAL_RCC_GPIOE_CLK_ENABLE();
break;
case MAZDRV_LED_GPIO_PORTF:
__HAL_RCC_GPIOF_CLK_ENABLE();
break;
case MAZDRV_LED_GPIO_PORTG:
__HAL_RCC_GPIOG_CLK_ENABLE();
break;
}
return MAZRET_NOERR;
}
LED 设置状态:
int MAZ_Drv_led_set_status(MAZDRV_LED led, MAZDRV_LED_STATUS status)
{
MAZDRV_LED_GPIO_LEVEL level;
MAZDRV_LED_CTRL *ctrl = NULL;
if (led < MAZDRV_LED0 || led >= MAZDRV_LED_MAX)
{
return MAZRET_EINVAL;
}
if (status < MAZDRV_LED_STATUS_ON || status > MAZDRV_LED_STATUS_TOGGLE)
{
return MAZRET_EINVAL;
}
ctrl = g_mazdrv_led_ctrl;
switch (status)
{
case MAZDRV_LED_STATUS_ON:
level = ctrl[led].light;
HAL_GPIO_WritePin((GPIO_TypeDef*) ctrl[led].port, ctrl[led].pin, level);
break;
case MAZDRV_LED_STATUS_OFF:
if (MAZDRV_LED_GPIO_LEVEL_HIGH == ctrl[led].light)
level = MAZDRV_LED_GPIO_LEVEL_LOW;
else if (MAZDRV_LED_GPIO_LEVEL_LOW == ctrl[led].light)
level = MAZDRV_LED_GPIO_LEVEL_HIGH;
HAL_GPIO_WritePin((GPIO_TypeDef*) ctrl[led].port, ctrl[led].pin, level);
break;
case MAZDRV_LED_STATUS_TOGGLE:
HAL_GPIO_TogglePin((GPIO_TypeDef*) ctrl[led].port, ctrl[led].pin);
break;
}
return MAZRET_NOERR;
}
不难发现, 驱动函数中所有与硬件相关的部分, 全部由 g_mazdrv_led_ctrl 变量提供. 因此移植代码时, 仅需要修改 g_mazdrv_led_ctrl 变量即可实现快速移植.
代码仓库
仓库路径: https://github.com/maziot-stm32/A1
驱动文件: https://github.com/maziot-stm32/A1/tree/master/MAZ_Drivers/MAZ_Drv_led
网友评论