rt-thread/examples/kernel/timer_timeout.c

123 lines
2.9 KiB
C
Raw Normal View History

2013-01-08 21:05:02 +08:00
/*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD>̣߳<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>̻߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>̻߳ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Ϣ<EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>̻߳ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD>ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
*/
#include <rtthread.h>
#include "tc_comm.h"
/* ָ<><D6B8><EFBFBD>߳̿<DFB3><CCBF>ƿ<EFBFBD><C6BF><EFBFBD>ָ<EFBFBD><D6B8> */
static rt_thread_t tid = RT_NULL;
/* <20><>Ϣ<EFBFBD><CFA2><EFBFBD>п<EFBFBD><D0BF>ƿ<EFBFBD> */
static struct rt_messagequeue mq;
/* <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD> */
static char msg_pool[2048];
/* <20><>ʱ<EFBFBD><CAB1><EFBFBD>Ŀ<EFBFBD><C4BF>ƿ<EFBFBD> */
static struct rt_timer timer;
static rt_uint16_t no = 0;
static void timer_timeout(void* parameter)
{
2013-12-21 12:51:52 +08:00
char buf[32];
rt_uint32_t length;
2013-01-08 21:05:02 +08:00
2013-12-21 12:51:52 +08:00
length = rt_snprintf(buf, sizeof(buf), "message %d", no++);
rt_mq_send(&mq, &buf[0], length);
2013-01-08 21:05:02 +08:00
}
/* <20>߳<EFBFBD><DFB3><EFBFBD><EFBFBD>ں<EFBFBD><DABA><EFBFBD> */
static void thread_entry(void* parameter)
{
2013-12-21 12:51:52 +08:00
char buf[64];
rt_err_t result;
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1> */
rt_timer_init(&timer, "timer", /* <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> timer1 */
timer_timeout, /* <20><>ʱʱ<CAB1>ص<EFBFBD><D8B5>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
RT_NULL, /* <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD> */
1, /* <20><>ʱ<EFBFBD><CAB1><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>OS TickΪ<6B><CEAA>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>1<EFBFBD><31>OS Tick */
RT_TIMER_FLAG_PERIODIC); /* <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ʱ<EFBFBD><CAB1> */
while (1)
{
rt_memset(&buf[0], 0, sizeof(buf));
/* <20><><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD>Ϣ */
result = rt_mq_recv(&mq, &buf[0], sizeof(buf), 1);
if (result == RT_EOK)
{
rt_kprintf("recv msg: %s\n", buf);
}
else if (result == -RT_ETIMEOUT)
{
rt_kprintf("recv msg timeout\n");
}
}
2013-01-08 21:05:02 +08:00
}
int timer_timeout_init()
{
2013-12-21 12:51:52 +08:00
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD> */
rt_mq_init(&mq, "mqt",
&msg_pool[0], /* <20>ڴ<EFBFBD><DAB4><EFBFBD>ָ<EFBFBD><D6B8>msg_pool */
128 - sizeof(void*), /* ÿ<><C3BF><EFBFBD><EFBFBD>Ϣ<EFBFBD>Ĵ<EFBFBD>С<EFBFBD><D0A1> 128 - void* */
sizeof(msg_pool), /* <20>ڴ<EFBFBD><DAB4>صĴ<D8B5>С<EFBFBD><D0A1>msg_pool<6F>Ĵ<EFBFBD>С */
RT_IPC_FLAG_FIFO); /* <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>̵߳ȴ<CCB5><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵõ<C8B5><C3B5>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ */
/* <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD> */
tid = rt_thread_create("t",
thread_entry, RT_NULL, /* <20>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>thread_entry, <20><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid != RT_NULL)
rt_thread_startup(tid);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
2013-01-08 21:05:02 +08:00
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
2013-12-21 12:51:52 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣬽<EFBFBD><F3A3ACBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣߳<DFB3><CCA3><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ж<EFBFBD> */
rt_enter_critical();
2013-01-08 21:05:02 +08:00
2013-12-21 12:51:52 +08:00
/* ɾ<><C9BE><EFBFBD>߳<EFBFBD> */
if (tid != RT_NULL && tid->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid);
2013-01-08 21:05:02 +08:00
2013-12-21 12:51:52 +08:00
/* ִ<><D6B4><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
rt_mq_detach(&mq);
/* ִ<>ж<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
rt_timer_detach(&timer);
2013-01-08 21:05:02 +08:00
2013-12-21 12:51:52 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
rt_exit_critical();
2013-01-08 21:05:02 +08:00
2013-12-21 12:51:52 +08:00
/* <20><><EFBFBD><EFBFBD>TestCase״̬ */
tc_done(TC_STAT_PASSED);
2013-01-08 21:05:02 +08:00
}
int _tc_timer_timeout()
{
2013-12-21 12:51:52 +08:00
/* <20><><EFBFBD><EFBFBD>TestCase<73><65><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD> */
tc_cleanup(_tc_cleanup);
timer_timeout_init();
2013-01-08 21:05:02 +08:00
2013-12-21 12:51:52 +08:00
/* <20><><EFBFBD><EFBFBD>TestCase<73><65><EFBFBD>е<EFBFBD><D0B5>ʱ<EEB3A4><CAB1> */
return 100;
2013-01-08 21:05:02 +08:00
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>finsh shell<6C><6C> */
FINSH_FUNCTION_EXPORT(_tc_timer_timeout, a thread timer testcase);
#else
/* <20>û<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int rt_application_init()
{
2013-12-21 12:51:52 +08:00
timer_timeout_init();
2013-01-08 21:05:02 +08:00
2013-12-21 12:51:52 +08:00
return 0;
2013-01-08 21:05:02 +08:00
}
#endif