1. 代码
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"
#define THREAD_PRIORITY 28
#define THREAD_TIMESLICE 10
#define EVENT_FLAG3 (1 << 3)
#define EVENT_FLAG5 (1 << 5)
#define LED4_PIN GET_PIN(B, 4)
static struct rt_event event;
ALIGN(RT_ALIGN_SIZE)
static char thread31_stack[500];
static struct rt_thread thread31;
static void thread31_recv_event(void *param)
{
rt_uint32_t e;
while(1)
{
if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, &e) == RT_EOK)
{
rt_kprintf("thread1: recv event 0x%x\n", e);
rt_pin_write(LED4_PIN, PIN_HIGH);
rt_thread_mdelay(100);
rt_pin_write(LED4_PIN, PIN_LOW);
}
}
}
ALIGN(RT_ALIGN_SIZE)
static char thread32_stack[500];
static struct rt_thread thread32;
static void thread32_send_event(void *param)
{
while(1)
{
rt_kprintf("thread2: send event3\n");
rt_event_send(&event, EVENT_FLAG3);
rt_thread_mdelay(8000);
}
}
void EventKeyScanCallBack(void)
{
rt_event_send(&event, EVENT_FLAG5);
}
int EventTaskInit(void)
{
rt_err_t result;
result = rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
if(result != RT_EOK)
{
rt_kprintf("init event failed.\n");
return -1;
}
rt_pin_mode(LED4_PIN, PIN_MODE_OUTPUT);
rt_thread_init(&thread31,
"thread31",
thread31_recv_event,
RT_NULL,
&thread31_stack[0],
sizeof(thread31_stack),
THREAD_PRIORITY - 1, THREAD_TIMESLICE);
rt_thread_startup(&thread31);
rt_thread_init(&thread32,
"thread32",
thread32_send_event,
RT_NULL,
&thread32_stack[0],
sizeof(thread32_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
rt_thread_startup(&thread32);
return 0;
}
2.输出
key press val is 1
thread2: send event3
thread1: recv event 0x28