【M61笔记】GPIO中断

[复制链接]
查看146 | 回复7 | 2024-6-13 19:20:30 | 显示全部楼层 |阅读模式

中断函数

bflb_gpio_int_init 外部中断初始化

gpio 外部中断初始化

/**bflb_gpio.h
 * @brief 配置 gpio 引脚中断
 *
 * @param [in] dev 设备句柄
 * @param [in] pin gpio pin, use @ref GPIO_PIN
 * @param [in] trig_mode 触发中断的模式
 */
void bflb_gpio_int_init(struct bflb_device_s *dev, uint8_t pin, uint8_t trig_mode);

bflb_gpio_int_mask 中断屏蔽开关

gpio 外部中断屏蔽开关

/**bflb_gpio.h
 * @brief 启用或禁用 gpio 引脚中断
 *
 * @param [in] dev 设备句柄
 * @param [in] pin gpio pin, use @ref GPIO_PIN
 * @param [in] mask 是否屏蔽中断
 */
void bflb_gpio_int_mask(struct bflb_device_s *dev, uint8_t pin, bool mask);

bflb_gpio_get_intstatus 获取 gpio 外部中断是否触发的标志

获取 gpio 外部中断是否触发的标志

/**bflb_gpio.h
 * @brief 获取 gpio 引脚中断状态
 *
 * @param [in] dev 设备句柄
 * @param [in] pin gpio pin, use @ref GPIO_PIN
 * @return true 为触发,false 未触发
 */
bool bflb_gpio_get_intstatus(struct bflb_device_s *dev, uint8_t pin);

bflb_gpio_int_clear 清除 gpio 中断标志

清除 gpio 中断标志

/**bflb_gpio.h
 * @brief 清除 gpio 引脚中断状态
 *
 * @param [in] dev 设备句柄
 * @param [in] pin gpio pin, use @ref GPIO_PIN
 */
void bflb_gpio_int_clear(struct bflb_device_s *dev, uint8_t pin);

bflb_irq_attach 设置触发中断后进入的回调函数

设置触发中断后进入的回调函数

/**bflb_irq.h
 * @brief Attach interrupt with callback.
 *
 * @param [in] irq 中断号
 * @param [in] isr 中断服务程序
 * @param [in] arg 用户数据(user data)
 * @return 函数调用失败时返回errno的相反数 (A negated errno value on failure)
 */
int bflb_irq_attach(int irq, irq_callback isr, void *arg);

bflb_irq_enable 中断使能

中断使能

/**bflb_irq.h
 * @brief 启用中断
 *
 * @param [in] irq 中断号
 */
void bflb_irq_enable(int irq);

bflb_irq_disable 中断失能

/**bflb_irq.h
 * @brief 关闭中断
 *
 * @param [in] irq 中断号
 */
void bflb_irq_disable(int irq);

示例

#include "bflb_gpio.h"   
#include "bflb_mtimer.h"
#include "board.h"

//设置控制的外设句柄,取名gpio
struct bflb_device_s *gpio;         

//中断服务函数
void gpio_isr(int irq, void *arg)
{
    //检测中断是否发生
    bool intstatus = bflb_gpio_get_intstatus(gpio, GPIO_PIN_2);
    //前面的instatus是bool类型,为true,也就是中断发生
    if (intstatus) {
        //清除中断源,也就是例子里的敲门声
        bflb_gpio_int_clear(gpio, GPIO_PIN_2);
        //输出信息,“吃完了”
        printf("Finished eating\r\n");
    }
}

int main(void)
{
    board_init();

    //给外设句柄复位gpio句柄
    gpio = bflb_device_get_by_name("gpio");   
    printf("gpio interrupt\r\n");

    //第三个参数设置为低电平触发
    bflb_gpio_int_init(gpio, GPIO_PIN_2, GPIO_INT_TRIG_MODE_SYNC_HIGH_LEVEL);
    //是否屏蔽中断,设置为false
    bflb_gpio_int_mask(gpio, GPIO_PIN_2, false);

    //第二个参数为中断服务函数的函数名
    bflb_irq_attach(gpio->irq_num, gpio_isr, gpio);
    //中断使能
    bflb_irq_enable(gpio->irq_num);

    while (1) {
        //在常规的while程序中输出“我在敲代码”
        printf("I am typing the code\r\n");
        bflb_mtimer_delay_ms(2000); 
    }
}
回复

使用道具 举报

1084504793 | 2024-6-14 08:12:35 | 显示全部楼层
回复

使用道具 举报

WT_0213 | 2024-6-14 08:41:46 | 显示全部楼层
回复

使用道具 举报

jkernet | 2024-6-14 09:45:53 | 显示全部楼层
民间文档
回复

使用道具 举报

爱笑 | 2024-6-14 13:53:53 | 显示全部楼层
用心做好保姆工作
回复

使用道具 举报

iiv | 2024-6-14 19:03:11 | 显示全部楼层
很棒
回复

使用道具 举报

a1286544151 | 2024-6-15 19:03:46 | 显示全部楼层
回复

使用道具 举报

wurong | 2024-7-29 11:53:13 | 显示全部楼层
厉害了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则