• RTT学习笔记11- gpio使用和GPIO设备驱动框架层


    1.GPIO框架层

    1.1框架层内宏

    1.1gpio 状态

    #define PIN_LOW                 0x00
    #define PIN_HIGH                0x01
    
    • 1
    • 2

    1.2gpio工作模式

    #define PIN_MODE_OUTPUT         0x00
    #define PIN_MODE_INPUT          0x01
    #define PIN_MODE_INPUT_PULLUP   0x02
    #define PIN_MODE_INPUT_PULLDOWN 0x03
    #define PIN_MODE_OUTPUT_OD      0x04
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.3中断触发模式

    #define PIN_IRQ_MODE_RISING             0x00
    #define PIN_IRQ_MODE_FALLING            0x01
    #define PIN_IRQ_MODE_RISING_FALLING     0x02
    #define PIN_IRQ_MODE_HIGH_LEVEL         0x03
    #define PIN_IRQ_MODE_LOW_LEVEL          0x04
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.4中断的使能失能

    #define PIN_IRQ_DISABLE                 0x00
    #define PIN_IRQ_ENABLE                  0x01
    
    • 1
    • 2

    1.5需要实现结构

    struct rt_pin_ops
    {
        void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode);
        void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value);
        int (*pin_read)(struct rt_device *device, rt_base_t pin);
        rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin,
                          rt_uint32_t mode, void (*hdr)(void *args), void *args);
        rt_err_t (*pin_detach_irq)(struct rt_device *device, rt_int32_t pin);
        rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
        rt_base_t (*pin_get)(const char *name);
    }; 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.6 向gpio框架注册函数

    int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data
    
    • 1

    1.2自定义一个gpio驱动

    1. 自定义一个外设gpio的数据结构
    2. 给每个gpio分配一个id
    3. 实现 struct rt_pin_ops 结构里的函数
    4. gpio框架注册函数 rt_device_pin_register

    2 RTT 用户GPIO 函数接口

    2.1 获取引脚编号

    使用函数

    pin_number = rt_pin_get("PF.9");
    
    • 1

    使用宏

    GET_PIN(port, pin)
    
    • 1

    2.2设置引脚模式

    void rt_pin_mode(rt_base_t pin, rt_base_t mode);
    
    • 1

    mode可选参数

    #define PIN_MODE_OUTPUT 0x00 /* 输出 /
    #define PIN_MODE_INPUT 0x01 /
    输入 /
    #define PIN_MODE_INPUT_PULLUP 0x02 /
    上拉输入 /
    #define PIN_MODE_INPUT_PULLDOWN 0x03 /
    下拉输入 /
    #define PIN_MODE_OUTPUT_OD 0x04 /
    开漏输出 */

    2.3设置引脚电平

    void rt_pin_write(rt_base_t pin, rt_base_t value);
    
    • 1

    PIN_LOW 低电平,PIN_HIGH 高电平

    2.4读取引脚电平

    int rt_pin_read(rt_base_t pin);
    
    • 1

    2.5绑定引脚中断回调函数

    rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
                               void (*hdr)(void *args), void *args);
    
    • 1
    • 2

    中断触发模式 mode

    #define PIN_IRQ_MODE_RISING 0x00 /* 上升沿触发 /
    #define PIN_IRQ_MODE_FALLING 0x01 /
    下降沿触发 /
    #define PIN_IRQ_MODE_RISING_FALLING 0x02 /
    边沿触发(上升沿和下降沿都触发)/
    #define PIN_IRQ_MODE_HIGH_LEVEL 0x03 /
    高电平触发 /
    #define PIN_IRQ_MODE_LOW_LEVEL 0x04 /
    低电平触发 */

    2.6使能引脚中断

    rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled);
    
    • 1

    2.7脱离引脚中断回调函数

    rt_err_t rt_pin_detach_irq(rt_int32_t pin);
    
    • 1

    2.8使用示例

    /*
     * 程序清单:这是一个 PIN 设备使用例程
     * 例程导出了 pin_beep_sample 命令到控制终端
     * 命令调用格式:pin_beep_sample
     * 程序功能:通过按键控制蜂鸣器对应引脚的电平状态控制蜂鸣器
    */
    
    #include 
    #include 
    
    /* 引脚编号,通过查看设备驱动文件drv_gpio.c确定 */
    #ifndef BEEP_PIN_NUM
        #define BEEP_PIN_NUM            35  /* PB0 */
    #endif
    #ifndef KEY0_PIN_NUM
        #define KEY0_PIN_NUM            55  /* PD8 */
    #endif
    #ifndef KEY1_PIN_NUM
        #define KEY1_PIN_NUM            56  /* PD9 */
    #endif
    
    void beep_on(void *args)
    {
        rt_kprintf("turn on beep!\n");
    
        rt_pin_write(BEEP_PIN_NUM, PIN_HIGH);
    }
    
    void beep_off(void *args)
    {
        rt_kprintf("turn off beep!\n");
    
        rt_pin_write(BEEP_PIN_NUM, PIN_LOW);
    }
    
    static void pin_beep_sample(void)
    {
        /* 蜂鸣器引脚为输出模式 */
        rt_pin_mode(BEEP_PIN_NUM, PIN_MODE_OUTPUT);
        /* 默认低电平 */
        rt_pin_write(BEEP_PIN_NUM, PIN_LOW);
    
        /* 按键0引脚为输入模式 */
        rt_pin_mode(KEY0_PIN_NUM, PIN_MODE_INPUT_PULLUP);
        /* 绑定中断,下降沿模式,回调函数名为beep_on */
        rt_pin_attach_irq(KEY0_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_on, RT_NULL);
        /* 使能中断 */
        rt_pin_irq_enable(KEY0_PIN_NUM, PIN_IRQ_ENABLE);
    
        /* 按键1引脚为输入模式 */
        rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP);
        /* 绑定中断,下降沿模式,回调函数名为beep_off */
        rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_off, RT_NULL);
        /* 使能中断 */
        rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE);
    }
    /* 导出到 msh 命令列表中 */
    MSH_CMD_EXPORT(pin_beep_sample, pin beep sample);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    pikachu平台SQL注入
    抖音快速涨粉的方法,快速涨粉软件的实操分享与心得分享
    【数据治理】数据治理之主数据管理
    Jmeter UI详细介绍及脚本生成,get,转发收藏
    个人数学建模算法库之图的最小生成树模型
    nodejs微信小程序+python+PHP-维斯公司财务管理系统的设计与实现-计算机毕业设计推荐
    基于httpd和lvs的dr模式简单测试
    Makefile相关操作
    Java 内存模型 JMM
    Java中的多线程如何理解——精简
  • 原文地址:https://blog.csdn.net/u010261063/article/details/126198348