1.代码
# include <rtthread.h>
# include <rtdevice.h>
# include "drv_gpio.h"
# define THREAD_PRIORITY 30
# define THREAD_TIMESLICE 10
# define TASK_STACK_MAX 500
static struct rt_mailbox mb;
static char mb_pool[ 16 ] ;
static char mb_str1[ ] = "I'm a mail!" ;
static char mb_str2[ ] = "this is another mail!" ;
static char mb_str3[ ] = "over" ;
ALIGN ( RT_ALIGN_SIZE)
static char thread41_stack[ TASK_STACK_MAX] ;
static struct rt_thread thread41;
static void thread41_entry ( void * parameter)
{
char * str;
while ( 1 )
{
rt_kprintf ( "thread1: try to recv a mail\n" ) ;
if ( rt_mb_recv ( & mb, ( rt_ubase_t * ) & str, RT_WAITING_FOREVER) == RT_EOK)
{
rt_kprintf ( "thread1: get a mail from mailbox, the content:%s\n" , str) ;
if ( str == mb_str3)
break ;
rt_thread_mdelay ( 2000 ) ;
}
}
rt_mb_detach ( & mb) ;
}
void MbKeyScanCallBack ( void )
{
static rt_uint8_t count = 0 ;
rt_err_t result = RT_EOK;
count ++ ;
if ( count & 0x1 )
{
result = rt_mb_send ( & mb, ( rt_uint32_t ) & mb_str1) ;
}
else
{
result = rt_mb_send ( & mb, ( rt_uint32_t ) & mb_str2) ;
}
if ( result == RT_EOK)
{
rt_kprintf ( "send mailbox OK.\n" ) ;
}
else if ( result == - RT_EFULL)
{
rt_kprintf ( "mailbox Full.\n" ) ;
}
else
{
rt_kprintf ( "send mailbox fail.\n" ) ;
}
}
int MbTaskInit ( void )
{
rt_err_t result;
result = rt_mb_init ( & mb,
"mbt" ,
& mb_pool[ 0 ] ,
sizeof ( mb_pool) / 4 ,
RT_IPC_FLAG_FIFO) ;
if ( result != RT_EOK)
{
rt_kprintf ( "init mailbox failed.\n" ) ;
return - 1 ;
}
rt_thread_init ( & thread41,
"thread41" ,
thread41_entry,
RT_NULL,
& thread41_stack[ 0 ] ,
sizeof ( thread41_stack) ,
THREAD_PRIORITY, THREAD_TIMESLICE) ;
rt_thread_startup ( & thread41) ;
return 0 ;
}
2.输出
key press val is 1
send mailbox OK.
t3 take a static semaphore.
thread1: get a mail from mailbox, the content:I'm a mail!
key press val is 1
send mailbox OK.
key press val is 1
send mailbox OK.
key press val is 1
send mailbox OK.
t3 take a static semaphore.
key press val is 1
send mailbox OK.
key press val is 1
mailbox Full.