天天看點

[單片機架構][bsp層][cx32l003][bsp_led] LED配置和使用

/********************************************************************************
* @file    bsp_led.c
* @author  jianqiang.xue
* @version V1.0.0
* @date    2021-04-03
* @brief   LED控制
********************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "RTE_Components.h"
#include CMSIS_device_header

#include "bsp_gpio.h"
#include "bsp_led.h"
/* Private Includes ----------------------------------------------------------*/
#include "business_gpio.h"
#include "business_function.h"

/* Private Define ------------------------------------------------------------*/

#define LEDx_GPIO_CLK_ENABLE(__INDEX__) \
do                                      \
{                                       \
    if ((__INDEX__) == 0)               \
        BS_LED0_GPIO_CLK_ENABLE();      \
    else if ((__INDEX__) == 1)          \
        BS_LED1_GPIO_CLK_ENABLE();      \
    else if ((__INDEX__) == 2)          \
        BS_LED2_GPIO_CLK_ENABLE();      \
    else if ((__INDEX__) == 3)          \
        BS_LED3_GPIO_CLK_ENABLE();      \
    else if ((__INDEX__) == 4)          \
        BS_LED4_GPIO_CLK_ENABLE();      \
    else if ((__INDEX__) == 5)          \
        BS_LED5_GPIO_CLK_ENABLE();      \
    else if ((__INDEX__) == 6)          \
        BS_LED6_GPIO_CLK_ENABLE();      \
    else if ((__INDEX__) == 7)          \
        BS_LED7_GPIO_CLK_ENABLE();      \
} while (0U)

/* Private Variables ---------------------------------------------------------*/
#if BS_LEDN != 0
static GPIO_Type *g_led_port[BS_LEDN] = {
#if (BS_LEDN > 0)
    BS_LED0_GPIO_PORT
#endif
#if (BS_LEDN > 1)
    ,BS_LED1_GPIO_PORT
#endif
#if (BS_LEDN > 2)
    ,BS_LED2_GPIO_PORT
#endif
#if (BS_LEDN > 3)
    ,BS_LED3_GPIO_PORT
#endif
#if (BS_LEDN > 4)
    ,BS_LED4_GPIO_PORT
#endif
#if (BS_LEDN > 5)
    ,BS_LED5_GPIO_PORT
#endif
#if (BS_LEDN > 6)
    ,BS_LED6_GPIO_PORT
#endif
#if (BS_LEDN > 7)
    ,BS_LED7_GPIO_PORT
#endif
};

static const uint16_t g_led_pin[BS_LEDN] = {
#if (BS_LEDN > 0)
    BS_LED0_PIN
#endif
#if (BS_LEDN > 1)
    ,BS_LED1_PIN
#endif
#if (BS_LEDN > 2)
    ,BS_LED2_PIN
#endif
#if (BS_LEDN > 3)
    ,BS_LED3_PIN
#endif
#if (BS_LEDN > 4)
    ,BS_LED4_PIN
#endif
#if (BS_LEDN > 5)
    ,BS_LED5_PIN
#endif
#if (BS_LEDN > 6)
    ,BS_LED6_PIN
#endif
#if (BS_LEDN > 7)
    ,BS_LED7_PIN
#endif
};

static const bool g_led_light_state[BS_LEDN] = {
#if (BS_LEDN > 0)
    BS_LED0_LIGHT_LEVEL
#endif
#if (BS_LEDN > 1)
    ,BS_LED1_LIGHT_LEVEL
#endif
#if (BS_LEDN > 2)
    ,BS_LED2_LIGHT_LEVEL
#endif
#if (BS_LEDN > 3)
    ,BS_LED3_LIGHT_LEVEL
#endif
#if (BS_LEDN > 4)
    ,BS_LED4_LIGHT_LEVEL
#endif
#if (BS_LEDN > 5)
    ,BS_LED5_LIGHT_LEVEL
#endif
#if (BS_LEDN > 6)
    ,BS_LED6_LIGHT_LEVEL
#endif
#if (BS_LEDN > 7)
    ,BS_LED7_LIGHT_LEVEL
#endif
};

/* Public Function Prototypes -----------------------------------------------*/
/**
 * @brief  led初始化 使能引腳時鐘 配置為推免輸出
 * @note   
 * @retval None
 */
void bsp_led_init(void)
{
    uint8_t i;

    for (i = 0; i < BS_LEDN; i++)
    {
        /* Enable the GPIO_LED Clock */
        LEDx_GPIO_CLK_ENABLE(i);

        /* Configure the GPIO_LED pin */
        bsp_gpio_init_output(g_led_port[i], g_led_pin[i], BSP_GPIO_PIN_OUT_PP);

        /* Reset PIN to switch off the LED */
        bsp_gpio_set_pin(g_led_port[i], g_led_pin[i], (bsp_gpio_pin_state_t)!g_led_light_state[i]);
    }
}

/**
 * @brief  led反初始化 将引腳配置為浮空輸入
 * @note   NULL
 * @retval None
 */
void bsp_led_deinit(void)
{
    uint8_t i;

    if (BS_LEDN == 0)
    {
        return;
    }

    for (i = 0; i < BS_LEDN; i++)
    {
        /* Turn off LED */
        bsp_gpio_set_pin(g_led_port[i], g_led_pin[i], (bsp_gpio_pin_state_t)!g_led_light_state[i]);
        /* DeInit the GPIO_LED pin */
        bsp_gpio_deinit(g_led_port[i], g_led_pin[i]);
    }
}

/**
 * @brief  點亮LED
 * @note   NULL
 * @param  ch: LED組号
 * @retval None
 */
void bsp_led_on(bsp_led_t ch)
{
    if (ch >= BS_LEDN)
    {
        return;
    }
    bsp_gpio_set_pin(g_led_port[ch], g_led_pin[ch], (bsp_gpio_pin_state_t)!g_led_light_state[ch]);
}

/**
 * @brief  關閉LED
 * @note   NULL
 * @param  ch: LED組号
 * @retval None
 */
void bsp_led_off(bsp_led_t ch)
{
    if (ch >= BS_LEDN)
    {
        return;
    }
    bsp_gpio_set_pin(g_led_port[ch], g_led_pin[ch], (bsp_gpio_pin_state_t)!g_led_light_state[ch]);
}

/**
 * @brief  翻轉LED
 * @note   NULL
 * @param  ch: LED組号
 * @retval None
 */
void bsp_led_toggle(bsp_led_t ch)
{
    if (ch >= BS_LEDN)
    {
        return;
    }
    bsp_gpio_set_toggle(g_led_port[ch], g_led_pin[ch]);
}
#else
void bsp_led_init(void)
{
}
void bsp_led_deinit(void)
{
}
void bsp_led_on(bsp_led_t ch)
{
}
void bsp_led_off(bsp_led_t ch)
{
}
void bsp_led_toggle(bsp_led_t ch)
{
}
#endif      
/********************************************************************************
* @file    bsp_led.h
* @author  jianqiang.xue
* @version V1.0.0
* @date    2021-04-03
* @brief   NULL
********************************************************************************/

#ifndef __BSP_LED_H
#define __BSP_LED_H

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>

/* Public enum ---------------------------------------------------------------*/
typedef enum
{
    BSP_LED_0 = 0,
    BSP_LED_1,
    BSP_LED_2,
    BSP_LED_3,
    BSP_LED_4,
    BSP_LED_5,
    BSP_LED_6,
    BSP_LED_7
} bsp_led_t;

/* Public Function Prototypes ------------------------------------------------*/

void bsp_led_init(void);
void bsp_led_deinit(void);

void bsp_led_on(bsp_led_t ch);
void bsp_led_off(bsp_led_t ch);
void bsp_led_toggle(bsp_led_t ch);

#endif      

繼續閱讀