本帖最后由 freeelectron 于 2022-3-18 17:00 编辑
1、基本思路
在上节的基础上,新增一个任务,任务中获取事件集标志,如果没有获取到则任务挂起,获取到后清除事件标志,在外部中断服务函数中,当按键按下的时候发送事件标志。
2、代码实现
(1)初始化按键io口为外部中断 void KeyInit(void)
{
gpio_init_type gpio_init_structure;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_structure);
/* configure the led gpio */
gpio_init_structure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_structure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_structure.gpio_mode = GPIO_MODE_INPUT;
gpio_init_structure.gpio_pins = GPIO_PINS_0;
gpio_init_structure.gpio_pull = GPIO_PULL_DOWN;
gpio_init(GPIOA, &gpio_init_structure);
exint_init_type exint_init_structure;
exint_default_para_init(&exint_init_structure);
exint_init_structure.line_enable= TRUE;
exint_init_structure.line_mode=EXINT_LINE_INTERRUPUT;
exint_init_structure.line_polarity=EXINT_TRIGGER_RISING_EDGE;
exint_init_structure.line_select=EXINT_LINE_0;
exint_init(&exint_init_structure);
nvic_irq_enable(EXINT1_0_IRQn,4,0);
}
(2)创建任务、事件集 #define EVENT_FLAG (1 << 0)
static rt_thread_t keythread=RT_NULL;
static rt_event_t keyevent=RT_NULL;
static void key_thread_entry(void *parameter)
{
while(1)
{
if(rt_event_recv(keyevent,
EVENT_FLAG,
RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR ,
RT_WAITING_FOREVER,
RT_NULL)==RT_EOK)
{
rt_kprintf("\r\nkey press\r\n\r\n");
}
}
}
void KeyThreadStart(void)
{
keyevent=rt_event_create("keyevent",RT_IPC_FLAG_PRIO);
keythread=rt_thread_create("key",key_thread_entry,RT_NULL,512,8,20);
rt_thread_startup(keythread);
}
(3)中断服务函数 void EXINT1_0_IRQHandler(void)
{
rt_interrupt_enter();
if(RESET!=exint_flag_get(EXINT_LINE_0))
{
exint_flag_clear(EXINT_LINE_0);
rt_event_send(keyevent, EVENT_FLAG);
}
rt_interrupt_leave();
}
3、现象
|