[活动专区] 【AT-START-F425测评】14、RT-Thread事件集从中断唤醒任务

[复制链接]
1084|1
 楼主| freeelectron 发表于 2022-3-18 16:59 | 显示全部楼层 |阅读模式
本帖最后由 freeelectron 于 2022-3-18 17:00 编辑

1、基本思路

在上节的基础上,新增一个任务,任务中获取事件集标志,如果没有获取到则任务挂起,获取到后清除事件标志,在外部中断服务函数中,当按键按下的时候发送事件标志。


2、代码实现

(1)初始化按键io口为外部中断

  1. void KeyInit(void)
  2. {
  3.         gpio_init_type gpio_init_structure;
  4.   
  5.     crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  6.         
  7.         gpio_default_para_init(&gpio_init_structure);

  8.         /* configure the led gpio */
  9.         gpio_init_structure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  10.         gpio_init_structure.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  11.         gpio_init_structure.gpio_mode = GPIO_MODE_INPUT;
  12.         gpio_init_structure.gpio_pins = GPIO_PINS_0;
  13.         gpio_init_structure.gpio_pull = GPIO_PULL_DOWN;
  14.         gpio_init(GPIOA, &gpio_init_structure);
  15.         
  16.         exint_init_type  exint_init_structure;
  17.         
  18.         exint_default_para_init(&exint_init_structure);
  19.         
  20.         exint_init_structure.line_enable= TRUE;
  21.         exint_init_structure.line_mode=EXINT_LINE_INTERRUPUT;
  22.         exint_init_structure.line_polarity=EXINT_TRIGGER_RISING_EDGE;
  23.         exint_init_structure.line_select=EXINT_LINE_0;
  24.         
  25.         exint_init(&exint_init_structure);
  26.         
  27.         nvic_irq_enable(EXINT1_0_IRQn,4,0);

  28. }

(2)创建任务、事件集

  1. #define EVENT_FLAG (1 << 0)

  2. static rt_thread_t keythread=RT_NULL;
  3. static rt_event_t keyevent=RT_NULL;


  4. static void key_thread_entry(void *parameter)
  5. {
  6.         while(1)
  7.         {
  8.                 if(rt_event_recv(keyevent,
  9.                            EVENT_FLAG,
  10.                            RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR ,
  11.                            RT_WAITING_FOREVER,
  12.                            RT_NULL)==RT_EOK)
  13.                 {
  14.                         rt_kprintf("\r\nkey press\r\n\r\n");
  15.                 }
  16.         }
  17. }


  18. void KeyThreadStart(void)
  19. {
  20.         keyevent=rt_event_create("keyevent",RT_IPC_FLAG_PRIO);
  21.         
  22.         keythread=rt_thread_create("key",key_thread_entry,RT_NULL,512,8,20);
  23.         
  24.         rt_thread_startup(keythread);
  25. }

(3)中断服务函数

  1. void EXINT1_0_IRQHandler(void)
  2. {
  3.         rt_interrupt_enter();

  4.         if(RESET!=exint_flag_get(EXINT_LINE_0))
  5.         {
  6.                 exint_flag_clear(EXINT_LINE_0);
  7.                 rt_event_send(keyevent, EVENT_FLAG);
  8.         }
  9.         
  10.         rt_interrupt_leave();
  11. }

3、现象

33647623449a1d2386.png








豌豆爹 发表于 2022-3-23 09:53 来自手机 | 显示全部楼层
话不多说上代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:stm32/LoRa物联网:304350312

66

主题

786

帖子

11

粉丝
快速回复 在线客服 返回列表 返回顶部