美文网首页
7. 驱动可配置化开发 --- KEY驱动

7. 驱动可配置化开发 --- KEY驱动

作者: mz8023yt | 来源:发表于2020-01-05 21:09 被阅读0次

    本博客中示例代码下载路径: https://github.com/maziot-stm32/A1/releases/tag/v0.7

    A1 设备按键原理图

    KEY原理图

    KEY 驱动可配置化抽象

    同样想一下, 按键模块硬件强相关的配置有哪些?
    目前我想到的有这些:

    /* key handle */
    typedef void (*MAZDRV_KEY_HANDLE)(void);
    
    typedef struct _MAZDRV_KEY_CTRL_
    {
        MAZDRV_GPIO_PORT port;                  // 连接KEY的GPIO PORT
        MAZDRV_GPIO_PIN pin;                    // 连接KEY的GPIO PIN
        MAZDRV_KEY_POLARITY polarity;           // 按下后GPIO是高电平还是低电平
        MAZDRV_KEY_MODE mode;                   // 检测方式是中断还是扫描
        MAZDRV_KEY_HANDLE handle;               // 中断回调函数
    } MAZDRV_KEY_CTRL;
    

    补充说明:

    • STM32中断优先级组配置为2位抢占优先级, 2位响应优先级
    • 按键驱动的优先级默认设置为最低优先级

    添加按键模块配置

    基于上述抽象, 添加一个按键模块仅需要增加以下代码:

    typedef enum _MAZDRV_KEY_
    {
        MAZDRV_KEY0 = 0,
        MAZDRV_KEY_MAX,
    } MAZDRV_KEY;
    
    void MAZ_Drv_key0_handle(void);
    
    static MAZDRV_KEY_CTRL g_mazdrv_key_ctrl[MAZDRV_KEY_MAX] =
    {
        [MAZDRV_KEY0] =
        {
            .port       = MAZDRV_GPIO_PORTC,
            .pin        = MAZDRV_GPIO_PIN1,
            .polarity   = MAZDRV_KEY_POLARITY_LOW,
            .mode       = MAZDRV_KEY_INTERRUPT,
            .handle     = MAZ_Drv_key0_handle,
        },
    };
    
    /* Interrupt handler */
    void MAZ_Drv_key0_handle(void)
    {
        if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_1) != 0x00u)
        {
            MAZ_Drv_led_set_status(MAZDRV_LED0, MAZDRV_LED_STATUS_TOGGLE);
            __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_1);
        }
    }
    

    添加三个按键模块仅需要增加以下代码:

    typedef enum _MAZDRV_KEY_
    {
        MAZDRV_KEY0 = 0,
        MAZDRV_KEY1,
        MAZDRV_KEY2,
        MAZDRV_KEY_MAX,
    } MAZDRV_KEY;
    
    void MAZ_Drv_key0_handle(void);
    void MAZ_Drv_key1_handle(void);
    void MAZ_Drv_key2_handle(void);
    
    static MAZDRV_KEY_CTRL g_mazdrv_key_ctrl[MAZDRV_KEY_MAX] =
    {
        [MAZDRV_KEY0] =
        {
            .port       = MAZDRV_GPIO_PORTC,
            .pin        = MAZDRV_GPIO_PIN1,
            .polarity   = MAZDRV_KEY_POLARITY_LOW,
            .mode       = MAZDRV_KEY_INTERRUPT,
            .handle     = MAZ_Drv_key0_handle,
        },
        [MAZDRV_KEY1] =
        {
            .port       = MAZDRV_GPIO_PORTC,
            .pin        = MAZDRV_GPIO_PIN13,
            .polarity   = MAZDRV_KEY_POLARITY_LOW,
            .mode       = MAZDRV_KEY_INTERRUPT,
            .handle     = MAZ_Drv_key1_handle,
        },
        [MAZDRV_KEY2] =
        {
            .port       = MAZDRV_GPIO_PORTA,
            .pin        = MAZDRV_GPIO_PIN0,
            .polarity   = MAZDRV_KEY_POLARITY_HIGH,
            .mode       = MAZDRV_KEY_INTERRUPT,
            .handle     = MAZ_Drv_key2_handle,
        },
    };
    
    /* Interrupt handler */
    void MAZ_Drv_key0_handle(void)
    {
        if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_1) != 0x00u)
        {
            MAZ_Drv_led_set_status(MAZDRV_LED0, MAZDRV_LED_STATUS_TOGGLE);
            __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_1);
        }
    }
    
    void MAZ_Drv_key1_handle(void)
    {
        if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_13) != 0x00u)
        {
            MAZ_Drv_led_set_status(MAZDRV_LED1, MAZDRV_LED_STATUS_TOGGLE);
            __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_13);
        }
    }
    
    void MAZ_Drv_key2_handle(void)
    {
        if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_0) != 0x00u)
        {
            MAZ_Drv_led_set_status(MAZDRV_LED0, MAZDRV_LED_STATUS_TOGGLE);
            MAZ_Drv_led_set_status(MAZDRV_LED1, MAZDRV_LED_STATUS_TOGGLE);
            __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0);
        }
    }
    

    代码仓库

    仓库路径: https://github.com/maziot-stm32/A1
    驱动文件: https://github.com/maziot-stm32/A1/tree/master/MAZ_Drivers/MAZ_Drv_key

    相关文章

      网友评论

          本文标题:7. 驱动可配置化开发 --- KEY驱动

          本文链接:https://www.haomeiwen.com/subject/hlyeoctx.html