完善我的文章《MCU BSP Driver分层设计》,本次提供GPIO控制驱动。
本片完成了GPIO控制驱动操作。
本人是基于野火指南者STM32开发板。
本驱动基于STM32F10X官方固件库。
主程序
int main(void)
{
WHT_LED_Init_Config();
for(;;)
{
WHT_LED_Color_Set(LED_RED);
Delay(500);
WHT_LED_Color_Set(LED_GREEN);
Delay(500);
WHT_LED_Color_Set(LED_BLUE);
Delay(500);
WHT_LED_Color_Set(LED_RGBOFF);
Delay(100);
}
}
LED灯驱动程序
led头文件如下:
#ifndef __LED_DRIVER_H__
#define __LED_DRIVER_H__
#include "../../BSP/GPIO_BSP/gpio_bsp.h"
//Red
#define LED0_GPIO_Port PortB
#define LED0_GPIO_Pin Pin5
//Green
#define LED1_GPIO_Port PortB
#define LED1_GPIO_Pin Pin0
//Blue
#define LED2_GPIO_Port PortB
#define LED2_GPIO_Pin Pin1
typedef enum
{
LED_RED = 0,//红色
LED_GREEN = 1,//绿色
LED_BLUE = 2,//蓝色
LED_YELLOW = 3,//黄色(红+绿)
LED_PURPLE = 4,//紫色(红+蓝)
LED_CYAN = 5,//青色(绿+蓝)
LED_WHITE = 6,//白色(红+绿+蓝)
LED_RGBOFF = 7,//黑色(全部关闭)
}WHT_LED_Color_enum;
extern void WHT_LED_Init_Config(void);
extern void WHT_LED_Color_Set(WHT_LED_Color_enum LED_Color);
#endif /* __LED_DRIVER_H__ */
led源文件如下:
#include "led_driver.h"
static WHT_GPIO_Device_t led_device = { 0 };
static void wht_gpio_init_config(void)
{
WHT_LED_Color_Set(LED_WHITE);
led_device.WHT_Set_GPIO_Mode(LED0_GPIO_Port, LED0_GPIO_Pin, Mode_Out_PP);
led_device.WHT_Set_GPIO_Mode(LED1_GPIO_Port, LED1_GPIO_Pin, Mode_Out_PP);
led_device.WHT_Set_GPIO_Mode(LED2_GPIO_Port, LED2_GPIO_Pin, Mode_Out_PP);
}
void WHT_LED_Color_Set(WHT_LED_Color_enum LED_Color)
{
WHT_GPIO_State_enum LED_Red_State;
WHT_GPIO_State_enum LED_Green_State;
WHT_GPIO_State_enum LED_Blue_State;
switch (LED_Color)
{
case LED_RED:
LED_Red_State = Low;
LED_Green_State = Hig;
LED_Blue_State = Hig;
break;
case LED_GREEN:
LED_Red_State = Hig;
LED_Green_State = Low;
LED_Blue_State = Hig;
break;
case LED_BLUE:
LED_Red_State = Hig;
LED_Green_State = Hig;
LED_Blue_State = Low;
break;
case LED_YELLOW:
LED_Red_State = Low;
LED_Green_State = Low;
LED_Blue_State = Hig;
break;
case LED_PURPLE:
LED_Red_State = Low;
LED_Green_State = Hig;
LED_Blue_State = Low;
break;
case LED_CYAN:
LED_Red_State = Hig;
LED_Green_State = Low;
LED_Blue_State = Low;
break;
case LED_WHITE:
LED_Red_State = Low;
LED_Green_State = Low;
LED_Blue_State = Low;
break;
case LED_RGBOFF:
LED_Red_State = Hig;
LED_Green_State = Hig;
LED_Blue_State = Hig;
break;
default:
return;
}
led_device.WHT_Set_GPIO_State(LED0_GPIO_Port, LED0_GPIO_Pin, LED_Red_State);
led_device.WHT_Set_GPIO_State(LED1_GPIO_Port, LED1_GPIO_Pin, LED_Green_State);
led_device.WHT_Set_GPIO_State(LED2_GPIO_Port, LED2_GPIO_Pin, LED_Blue_State);
}
void WHT_LED_Init_Config(void)
{
WHT_GPIO_Init_Config(&led_device);
wht_gpio_init_config();
}
GPIO驱动如下
GPIO头文件:
#ifndef __GPIO_BSP_H__
#define __GPIO_BSP_H__
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
typedef enum
{
PortA = (uint32_t)GPIOA,
PortB = (uint32_t)GPIOB,
PortC = (uint32_t)GPIOC,
PortD = (uint32_t)GPIOD,
PortE = (uint32_t)GPIOE,
PortF = (uint32_t)GPIOF,
PortG = (uint32_t)GPIOG,
}WHT_GPIO_Port_enum;
typedef enum
{
Pin0 = GPIO_Pin_0,
Pin1 = GPIO_Pin_1,
Pin2 = GPIO_Pin_2,
Pin3 = GPIO_Pin_3,
Pin4 = GPIO_Pin_4,
Pin5 = GPIO_Pin_5,
Pin6 = GPIO_Pin_6,
Pin7 = GPIO_Pin_7,
Pin8 = GPIO_Pin_8,
Pin9 = GPIO_Pin_9,
Pin10 = GPIO_Pin_10,
Pin11 = GPIO_Pin_11,
Pin12 = GPIO_Pin_12,
Pin13 = GPIO_Pin_13,
Pin14 = GPIO_Pin_14,
Pin15 = GPIO_Pin_15,
PinAll =GPIO_Pin_All,
}WHT_GPIO_Pin_enum;
typedef enum
{
Low = 0,
Hig = !Low,
}WHT_GPIO_State_enum;
typedef enum
{
Mode_Ain = (uint8_t)GPIO_Mode_AIN, //模拟输入
Mode_IN_FLOATING = (uint8_t)GPIO_Mode_IN_FLOATING,//浮空输入
Mode_IPD = (uint8_t)GPIO_Mode_IPD, //下拉输入
Mode_IPU = (uint8_t)GPIO_Mode_IPU, //上拉输入
Mode_Out_OD = (uint8_t)GPIO_Mode_Out_OD, //开漏输出
Mode_Out_PP = (uint8_t)GPIO_Mode_Out_PP, //推挽输出
Mode_AF_OD = (uint8_t)GPIO_Mode_AF_OD, //开漏复用输出
Mode_AF_PP = (uint8_t)GPIO_Mode_AF_PP, //推挽复用输出
}WHT_GPIO_Mode_enum;
typedef struct
{
void (*WHT_Set_GPIO_Mode)(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx, WHT_GPIO_Mode_enum mode);
void (*WHT_Set_GPIO_State)(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx, WHT_GPIO_State_enum state);
WHT_GPIO_State_enum(*WHT_Get_GPIO_State)(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx);
}WHT_GPIO_Device_t;
extern void WHT_GPIO_Init_Config(WHT_GPIO_Device_t* wht_gpio_device);
#endif /*__GPIO_BSP_H__*/
GPIO源文件:
#include "gpio_bsp.h"
static void wht_set_gpio_mode(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx, WHT_GPIO_Mode_enum mode)
{
GPIO_InitTypeDef GPIO_InitStruct;
uint32_t RCC_APB2Periph;
switch (portx)
{
case PortA:RCC_APB2Periph = RCC_APB2Periph_GPIOA; break;
case PortB:RCC_APB2Periph = RCC_APB2Periph_GPIOB; break;
case PortC:RCC_APB2Periph = RCC_APB2Periph_GPIOC; break;
case PortD:RCC_APB2Periph = RCC_APB2Periph_GPIOD; break;
case PortE:RCC_APB2Periph = RCC_APB2Periph_GPIOE; break;
case PortF:RCC_APB2Periph = RCC_APB2Periph_GPIOF; break;
case PortG:RCC_APB2Periph = RCC_APB2Periph_GPIOG; break;
default:
return;
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph, ENABLE);
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = (GPIOMode_TypeDef)mode;
GPIO_InitStruct.GPIO_Pin = pinx;
GPIO_Init((GPIO_TypeDef*)portx, &GPIO_InitStruct);
}
static void wht_set_gpio_state(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx, WHT_GPIO_State_enum state)
{
if (state == Hig)
GPIO_SetBits((GPIO_TypeDef*)portx, pinx);
else
GPIO_ResetBits((GPIO_TypeDef*)portx, pinx);
}
static WHT_GPIO_State_enum wht_get_gpio_state(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx)
{
if (GPIO_ReadInputDataBit((GPIO_TypeDef*)portx, pinx) == Bit_SET)
return Hig;
else
return Low;
}
void WHT_GPIO_Init_Config(WHT_GPIO_Device_t* wht_gpio_device)
{
wht_gpio_device->WHT_Set_GPIO_Mode = wht_set_gpio_mode;
wht_gpio_device->WHT_Get_GPIO_State = wht_get_gpio_state;
wht_gpio_device->WHT_Set_GPIO_State = wht_set_gpio_state;
}
最终的固件库就不贴了,基于官方固件库的。