rtt-f030/examples/kernel/mbox_send_wait.c

142 lines
3.0 KiB
C
Raw Normal View History

/*
* <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 tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƿ<EFBFBD> */
static struct rt_mailbox mb;
/* <20><><EFBFBD>ڷ<EFBFBD><DAB7>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD> */
static char mb_pool[32];
static char mb_str1[] = "I'm a mail!";
static char mb_str2[] = "this is another mail!";
/* <20>߳<EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD> */
static void thread1_entry(void* parameter)
{
unsigned char* str;
while (1)
{
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>ʼ<EFBFBD> */
if (rt_mb_recv(&mb, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str);
/* <20><>ʱ20<32><30>OS Tick */
rt_thread_delay(50);
}
}
}
/* <20>߳<EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD> */
static void thread2_entry(void* parameter)
{
rt_uint8_t count;
unsigned char *str;
count = 0;
while (1)
{
count ++;
if (count & 0x1)
{
/* <20><><EFBFBD><EFBFBD>mb_str1<72><31>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
str = mb_str1;
}
else
{
/* <20><><EFBFBD><EFBFBD>mb_str2<72><32>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
str = mb_str2;
}
/* <20><>ͣ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD>10<31><30>tick<63><6B>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD>ʱ */
if( rt_mb_send_wait(&mb, (rt_uint32_t)str,10) == RT_EOK )
rt_kprintf("thread2: sent a mail to mailbox, the content:%s\n", str);
else
rt_kprintf("thread2: timeout while waiting to send a mail.\n");
}
}
int mbox_send_wait_init()
{
/* <20><>ʼ<EFBFBD><CABC>һ<EFBFBD><D2BB>mailbox */
rt_mb_init(&mb,
"mbt", /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>mbt */
&mb_pool[0], /* <20><><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>mb_pool */
sizeof(mb_pool)/4, /* <20><>С<EFBFBD><D0A1>mb_pool<6F><6C>С<EFBFBD><D0A1><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD>Ϊһ<CEAA><D2BB><EFBFBD>ʼ<EFBFBD><CABC>Ĵ<EFBFBD>С<EFBFBD><D0A1>4<EFBFBD>ֽ<EFBFBD> */
RT_IPC_FLAG_FIFO); /* <20><><EFBFBD><EFBFBD>FIFO<46><4F>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>̵߳ȴ<CCB5> */
/* <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>1 */
tid1 = rt_thread_create("t1",
thread1_entry, RT_NULL, /* <20>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>thread1_entry, <20><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
/* <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>2 */
tid2 = rt_thread_create("t2",
thread2_entry, RT_NULL, /* <20>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>thread2_entry, <20><><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>RT_NULL */
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
else
tc_stat(TC_STAT_END | TC_STAT_FAILED);
return 0;
}
#ifdef RT_USING_TC
static void _tc_cleanup()
{
/* <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();
/* ɾ<><C9BE><EFBFBD>߳<EFBFBD> */
if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid1);
if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
rt_thread_delete(tid2);
/* ִ<><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
rt_mb_detach(&mb);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
rt_exit_critical();
/* <20><><EFBFBD><EFBFBD>TestCase״̬ */
tc_done(TC_STAT_PASSED);
}
int _tc_mbox_send_wait()
{
/* <20><><EFBFBD><EFBFBD>TestCase<73><65><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD> */
tc_cleanup(_tc_cleanup);
mbox_send_wait_init();
/* <20><><EFBFBD><EFBFBD>TestCase<73><65><EFBFBD>е<EFBFBD><D0B5>ʱ<EEB3A4><CAB1> */
return 300;
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>finsh shell<6C><6C> */
FINSH_FUNCTION_EXPORT(_tc_mbox_send_wait, a example of mailbox send wait);
#else
/* <20>û<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
int rt_application_init()
{
mbox_send_wait_init();
return 0;
}
#endif