03_rtthread_event_test

1.rtthread event typedef and macro

 2.rtthread event interface

 3.rtthread event demo test

/**

*******************************************************************************
*/
/* 包含头文件 ----------------------------------------------------------------*/	
#include "main.h"
#include "rtthread.h"

/* 私有类型定义 --------------------------------------------------------------*/

/* 私有宏定义 ----------------------------------------------------------------*/
#define	THREAD_PRIORITY		( 9 )
#define	THREAD_TIMESLICE	( 5 )

#define	EVENT_FLAG_03	( 1 << 3)
#define EVENT_FLAG_05	( 1 <<5 )

/* 私有变量 ------------------------------------------------------------------*/
static  struct rt_event event;

//ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t thread_recv_event_stack[1024];
static struct rt_thread thread_recv_event;

static rt_uint8_t thread_send_event_stack[1024];
static struct rt_thread thread_send_event;

/* 扩展变量 ------------------------------------------------------------------*/

/* 私有函数原形 --------------------------------------------------------------*/
static void thread_recv_event_entry(void *param);
static void thread_send_event_entry(void *param);

/* 函数体 --------------------------------------------------------------------*/
void event_sample_test(void)
{
	rt_err_t ret;
	ret = rt_event_init(&event,"event",RT_IPC_FLAG_PRIO);
	if(ret != RT_EOK)
	{
		rt_kprintf("event init failed!\n");
	}

	// initial the thread
	rt_thread_init(&thread_recv_event,
					"thread_recv_event",
					thread_recv_event_entry,
					RT_NULL,
					&thread_recv_event_stack[0],
					sizeof(thread_recv_event_stack),
					THREAD_PRIORITY-1,
					THREAD_TIMESLICE);	
	rt_thread_startup(&thread_recv_event);

	rt_thread_init(&thread_send_event,
					"thread_send_event",
					thread_send_event_entry,
					RT_NULL,
					&thread_send_event_stack[0],
					sizeof(thread_send_event_stack),
					THREAD_PRIORITY,
					THREAD_TIMESLICE);	
	rt_thread_startup(&thread_send_event);
}

static void thread_recv_event_entry(void *param)
{
	rt_uint32_t e;

	/*
        first time to receive event(s):
        EITHER Event3 OR Event5 happened can resume thread1
        and then clear conrresponding event(s)' flag
    */
	if(rt_event_recv(&event, 								// rt_event_t event
					(EVENT_FLAG_03 | EVENT_FLAG_05), 		// rt_uint32_t set
					RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,	// rt_uint8_t option 
					RT_WAITING_FOREVER, 					// rt_int32_t timeout
					&e) == RT_EOK)							//rt_uint32_t * recved
	{
		rt_kprintf("thread recv event: OR recv event 0x%x\n",e);
	}

	rt_kprintf("thread RECV: delay 1s to prepare the second event\n");
    rt_thread_mdelay(1000);

	/*
        second time to receive event(s):
        BOTH Event3 AND Event5 happened can resume thread1
        and then clear conrresponding event(s)' flag
    */
    if(rt_event_recv(&event, 									// rt_event_t event
					(EVENT_FLAG_03 | EVENT_FLAG_05), 			// rt_uint32_t set
					RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,	// rt_uint8_t option 
					RT_WAITING_FOREVER, 						// rt_int32_t timeout
					&e) == RT_EOK)								//rt_uint32_t * recved
	{
		rt_kprintf("thread recv event: AND recv event 0x%x\n",e);
	}
					
	rt_kprintf("thread RECV leave.\n");
    
}

static void thread_send_event_entry(void *param)
{
	rt_kprintf("thread SEND: send event3\n");
    rt_event_send(&event, EVENT_FLAG_03);
    rt_thread_mdelay(200);

    rt_kprintf("thread SEND: send event5\n");
    rt_event_send(&event, EVENT_FLAG_05);
    rt_thread_mdelay(200);

    rt_kprintf("thread SEND: send event3\n");
    rt_event_send(&event, EVENT_FLAG_03);
    rt_kprintf("thread SEND leave.\n");
}

4.rtthread event test demo

rtthread event test demo的测试效果如下图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值