配置三个灯为例
三个灯亮灯电平都为低电平
bsp_led.h
/*
LED assign
LD1 : PG13 (低电平点亮,高电平熄灭)
LD2 : PG14 (低电平点亮,高电平熄灭)
LD3 : PG15 (低电平点亮,高电平熄灭)
*/
#define LEDn 3
#define LED1_PIN GPIO_PIN_13
#define LED1_GPIO_PORT GPIOG
#define LED1_GPIO_CLK_ENABLE() __HAL_RCC_GPIOG_CLK_ENABLE()
#define LED1_GPIO_CLK_DISABLE() __HAL_RCC_GPIOG_CLK_DISABLE()
#define LED2_PIN GPIO_PIN_14
#define LED2_GPIO_PORT GPIOG
#define LED2_GPIO_CLK_ENABLE() __HAL_RCC_GPIOG_CLK_ENABLE()
#define LED2_GPIO_CLK_DISABLE() __HAL_RCC_GPIOG_CLK_DISABLE()
#define LED3_PIN GPIO_PIN_15
#define LED3_GPIO_PORT GPIOG
#define LED3_GPIO_CLK_ENABLE() __HAL_RCC_GPIOG_CLK_ENABLE()
#define LED3_GPIO_CLK_DISABLE() __HAL_RCC_GPIOG_CLK_DISABLE()
#define LEDx_GPIO_CLK_ENABLE(__INDEX__) do{if((__INDEX__) == 0) LED1_GPIO_CLK_ENABLE(); else \
if((__INDEX__) == 1) LED2_GPIO_CLK_ENABLE(); else \
if((__INDEX__) == 2) LED3_GPIO_CLK_ENABLE(); \
}while(0)
#define LEDx_GPIO_CLK_DISABLE(__INDEX__) do{if((__INDEX__) == 0) LED1_GPIO_CLK_DISABLE(); else \
if((__INDEX__) == 1) LED2_GPIO_CLK_DISABLE(); else \
if((__INDEX__) == 2) LED3_GPIO_CLK_DISABLE(); \
}while(0)
typedef enum
{
LED1=0,
LED2,
LED3,
}Led_TypeDef;
void BSP_LED_Init(Led_TypeDef Led);
void BSP_LED_DeInit(Led_TypeDef Led);
void BSP_LED_On(Led_TypeDef Led);
void BSP_LED_Off(Led_TypeDef Led);
void BSP_LED_Toggle(Led_TypeDef Led);
bsp_led.c
GPIO_TypeDef* GPIO_PORT[LEDn] = {LED1_GPIO_PORT,LED2_GPIO_PORT,LED3_GPIO_PORT};
const uint16_t GPIO_PIN[LEDn] = {LED1_PIN,LED2_PIN,LED3_PIN};
/**
* @brief Configures LED GPIO.
* @param Led: LED to be configured.
* This parameter can be one of the following values:
* @arg LED1
* @arg LED2
* @arg LED3
*/
void BSP_LED_Init(Led_TypeDef Led)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable the GPIO_LED clock */
LEDx_GPIO_CLK_ENABLE(Led);
/* Configure the GPIO_LED pin */
GPIO_InitStruct.Pin = GPIO_PIN[Led];
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
}
/**
* @brief Turns selected LED On.
* @param Led: LED to be set on
* This parameter can be one of the following values:
* @arg LED1
* @arg LED2
* @arg LED3
*/
void BSP_LED_On(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
}
/**
* @brief Turns selected LED Off.
* @param Led: LED to be set off
* This parameter can be one of the following values:
* @arg LED1
* @arg LED2
* @arg LED3
*/
void BSP_LED_Off(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
}
/**
* @brief Toggles the selected LED.
* @param Led: LED to be toggled
* This parameter can be one of the following values:
* @arg LED1
* @arg LED2
* @arg LED3
*/
void BSP_LED_Toggle(Led_TypeDef Led)
{
HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
}
/**
* @brief DeInit LEDs.
* @param Led: LED to be de-init.
* This parameter can be one of the following values:
* @arg LED1
* @arg LED2
* @arg LED3
* @note Led DeInit does not disable the GPIO clock nor disable the Mfx
*/
void BSP_LED_DeInit(Led_TypeDef Led)
{
GPIO_InitTypeDef gpio_init_structure;
/* Turn off LED */
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
/* DeInit the GPIO_LED pin */
gpio_init_structure.Pin = GPIO_PIN[Led];
HAL_GPIO_DeInit(GPIO_PORT[Led], gpio_init_structure.Pin);
}