其中,常用的有:
(1)OSStatus mico_rtos_init_queue(mico_queue_t* queue, const char* name, uint32_t message_size, uint32_tnumber_of_messages );
该函数为初始化一个消息队列,参数1为指向要初始化的消息队列的句柄指针;参数2为消息队列的名称,由用户定义;参数3为将要进入队列对象的最大字节数;参数4为队列的深度,即队列中对象的最大数量
(2)OSStatus mico_rtos_push_to_queue(mico_queue_t* queue, void* message, uint32_t
timeout_ms );
该函数为将一个数据对象推入消息队列,参数1为指向要初始化的消息队列的句柄的指针;参数2为推入队列的对象,对象大小在队列初始化mico_rtos_init_queue()中已指定;参数3为返回前等待的毫秒数
(3)OSStatus mico_rtos_pop_from_queue(mico_queue_t* queue, void* message, uint32_t timeout_ms );
该函数为从消息队列中取出一个数据对象,参数1为指向要初始化的消息队列的句柄的指针;参数2为在mico_rtos_init_queue()中指定的“消息”,因此必须保证此缓存区足够大,否则将导致内存崩溃;参数3为返回前等待的毫秒数。
三、MiCO消息队列示例
该示例参数官方的测试demo,参考示例代码位于SDK_MiCOKit_v2.3.0.2DemosCOM.MXCHIP.BASICos,官网示例代码说明http://mico.io/wiki/doku.php?id=basic_os。该示例演示了一个MiCO系统通过消息队列实现两个线程之间的通讯。
- static mico_queue_t os_queue = NULL;
- typedef struct _msg
- {
- int value;
- } msg_t;
- void receiver_thread(void *arg)
- {
- UNUSED_PARAMETER( arg );
- OSStatus err;
- msg_t received ={ 0 };
- while(1)
- {
- /*Wait until queue has data*/
- err = mico_rtos_pop_from_queue( &os_queue, &received, MICO_WAIT_FOREVER);
- require_noerr( err, exit );
- mico_os_log( "Received data from queue:value = %d",received.value );
- }
- exit:
- if( err != kNoErr )
- mico_os_log( "Receiver exit with err: %d", err );
- mico_rtos_delete_thread( NULL );
- }
- void sender_thread(void *arg)
- {
- UNUSED_PARAMETER( arg );
- OSStatus err = kNoErr;
- msg_t my_message = { 0 };
- while(1)
- {
- my_message.value++;
- mico_rtos_push_to_queue( &os_queue, &my_message, MICO_WAIT_FOREVER );
- require_noerr( err, exit );
- mico_os_log( "send data to queue" );
- mico_thread_sleep( 1 );
- }
- exit:
- if( err != kNoErr )
- mico_os_log( "Sender exit with err: %d", err );
- mico_rtos_delete_thread( NULL );
- }
复制代码
四、MiCO消息队列测试结果
0