轉自:Mculover666
之前給大家分享過關于CMSIS的內容,比如:
接口標準CMSIS詳細內容" tab="innerlink" style="text-decoration: underline;" data-linktype="2" rel="nofollow">Cortex-M微控制器軟件接口標準CMSIS詳細內容
CMSIS-DAP和J-Link、ST-Link是什么關系?
今天繼續(xù)給大家分享由“Mculover666”整理的關于CMSIS的內容。
1. CMSIS-RTOS API
CMSIS-RTOS API是ARM公司為RTOS內核制定的一套通用接口協(xié)議,它提供了一套「標準的API接口」,可以移植到各種各樣的RTOS上,使得上層的軟件、中間件、庫以及其他組件在不同的RTOS之上都可以正常工作。
這套API表現(xiàn)為兩個文件:cmsis-os.h和cmsis-os.c,也就是說,不同的RTOS內核分別用自己的一套東西去適配.c文件中的接口,而用戶只需要調用.h文件中給出的API編寫應用。
本文會列舉性的給出CMSIS-RTOS有哪些API和宏定義,并給出每類API的使用demo,學習者只需要了解這些東西,能看懂用CMSIS-RTOS API編寫的應用程序即可~
在TencentOS-tiny中如下。
-
基于TencentOS-tiny的CMSIS-RTOS API v1.02版本實現(xiàn): -
cmsis_os.h
-
cmsis_os.c
-
基于TencentOS-tiny的CMSIS-RTOS API v2.1.3版本實現(xiàn): -
cmsis_os2.h
-
cmsis_os2.c
CMSIS-RTOS API的整體架構如下圖:
2. CMSIS-RTOS API列表
下面列出了 CMSIS-RTOS API v1.02 版本提供的所有API。
CMSIS-RTOS 所有API使用的錯誤碼(cmsis-os.h):
typedef enum {
osOK = 0, ///< function completed; no error or event occurred.
osEventSignal = 0x08, ///< function completed; signal event occurred.
osEventMessage = 0x10, ///< function completed; message event occurred.
osEventMail = 0x20, ///< function completed; mail event occurred.
osEventTimeout = 0x40, ///< function completed; timeout occurred.
osErrorParameter = 0x80, ///< parameter error: a mandatory parameter was missing or specified an incorrect object.
osErrorResource = 0x81, ///< resource not available: a specified resource was not available.
osErrorTimeoutResource = 0xC1, ///< resource not available within given time: a specified resource was not available within the timeout period.
osErrorISR = 0x82, ///< not allowed in ISR context: the function cannot be called from interrupt service routines.
osErrorISRRecursive = 0x83, ///< function called multiple times from ISR with same object.
osErrorPriority = 0x84, ///< system cannot determine priority or thread has illegal priority.
osErrorNoMemory = 0x85, ///< system is out of memory: it was impossible to allocate or reserve memory for the operation.
osErrorValue = 0x86, ///< value of a parameter is out of range.
osErrorOS = 0xFF, ///< unspecified RTOS error: run-time error but no other error message fits.
os_status_reserved = 0x7FFFFFFF ///< prevent from enum down-size compiler optimization.
} osStatus;
CMSIS-RTOS API一些可選項控制是否開啟(cmsis-os.h):
#define osFeature_MainThread 1 ///< main thread 1=main can be thread, 0=not available
#define osFeature_Pool 1 ///< Memory Pools: 1=available, 0=not available
#define osFeature_MailQ 1 ///< Mail Queues: 1=available, 0=not available
#define osFeature_MessageQ 1 ///< Message Queues: 1=available, 0=not available
#define osFeature_Signals 0 ///< maximum number of Signal Flags available per thread
#define osFeature_Semaphore 30 ///< maximum count for \ref osSemaphoreCreate function
#define osFeature_Wait 0 ///< osWait function: 1=available, 0=not available
#define osFeature_SysTick 1 ///< osKernelSysTick functions: 1=available, 0=not available
2.1. 內核信息和控制
API | 描述 |
---|---|
osKernelInitialize | 初始化RTOS內核 |
osKernelStart | 啟動RTOS內核 |
osKernelRunning | Query if the RTOS kernel is running |
osKernelSysTick (可選) | Get RTOS kernel system timer counter |
osKernelSysTickFrequency (可選) | RTOS kernel system timer frequency in Hz |
osKernelSysTickMicroSec (可選) | Convert microseconds value to RTOS kernel system timer value |
-
osKernelInitialize
osStatus osKernelInitialize(void);
返回值:status code
-
osKernelStart
osStatus osKernelStart(void);
返回值:status code
-
osKernelRunning
int32_t osKernelRunning(void);
返回值:0表示RTOS未啟動,1表示RTOS已經啟動
-
osKernelSysTick
uint32_t osKernelSysTick(void);
返回值:RTOS內核系統(tǒng)當前的時間
2.2. 線程管理
##
連接符的作用是連接兩個字符串,合為一個字符串。
CMSIS-RTOS API 存放線程參數(shù)管理的結構體如下:
typedef struct os_thread_def {
char *name; ///< Thread name
os_pthread pthread; ///< start address of thread function
osPriority tpriority; ///< initial thread priority
uint32_t instances; ///< maximum number of instances of that thread function
k_stack_t *stackbase; ///< base address of task
uint32_t stacksize; ///< stack size requirements in bytes; 0 is default stack size
k_timeslice_t timeslice; ///< timeslice
k_task_t *task;
} osThreadDef_t;
CMSIS-RTOS API 定義線程的宏如下:
#define osThreadDef(name, priority, instances, stacksz) \
k_task_t task_handler_##name; \
k_stack_t task_stack_##name[(stacksz)]; \
const osThreadDef_t os_thread_def_##name = \
{ #name, (os_pthread)(name), (osPriority)(priority), (instances), \
(&((task_stack_##name)[0])), (stacksz), ((k_timeslice_t)0u), (&(task_handler_##name)) }
宏定義中的 instances 表示基于此任務參數(shù),創(chuàng)建出幾個任務實例,比如instances為2,則會創(chuàng)建出兩個任務。
CMSIS-RTOS API定義的獲取線程參數(shù)結構體的宏如下:
#define osThread(name) \
&os_thread_def_##name
管理線程參數(shù)的API如下:
API | 描述 |
---|---|
osThreadCreate | 創(chuàng)建線程并開始執(zhí)行 |
osThreadTerminate | 停止線程執(zhí)行 |
osThreadYield | 線程主動讓出 |
osThreadGetID | 獲取當前正在運行線程的ID |
osThreadSetPriority | 改變線程優(yōu)先級 |
osThreadGetPriority | 獲取線程優(yōu)先級 |
-
osThreadCreate
osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *argument);
其中osThreadId被定義為k_task_t指針類型:
typedef k_task_t *osThreadId;
返回值:TencentOS-tiny中的任務控制塊類型指針。
-
osThreadTerminate
osStatus osThreadTerminate(osThreadId thread_id);
返回值:osStatus
-
osThreadYield
osStatus osThreadYield(void);
返回值:osStatus
-
osThreadGetID
osThreadId osThreadGetId(void);
-
osThreadSetPriority
osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority);
-
osThreadGetPriority
osPriority osThreadGetPriority(osThreadId thread_id);
使用時需要特別注意,在TencentOS-tiny中,調用CMSIS-RTOS API提供的優(yōu)先級選項設置之后,實際設置的任務值是不同的。
CMSIS-RTOS API提供的線程優(yōu)先級宏定義如下:
typedef enum {
osPriorityIdle = -3, ///< priority: idle (lowest)
osPriorityLow = -2, ///< priority: low
osPriorityBelowNormal = -1, ///< priority: below normal
osPriorityNormal = 0, ///< priority: normal (default)
osPriorityAboveNormal = +1, ///< priority: above normal
osPriorityHigh = +2, ///< priority: high
osPriorityRealtime = +3, ///< priority: realtime (highest)
osPriorityError = 0x84 ///< system cannot determine priority or thread has illegal priority
} osPriority;
在TecentOS-tiny中實現(xiàn)的時候進行了轉化:
static k_prio_t priority_cmsis2knl(osPriority prio)
{
if (prio == osPriorityError) {
return K_TASK_PRIO_INVALID;
}
return (k_prio_t)(3 - prio);
}
static osPriority priority_knl2cmsis(k_prio_t prio)
{
return (osPriority)(3 - prio);
}
比如創(chuàng)建線程時設置為 osPriorityNormal=0,則「實際設置的任務優(yōu)先級為3」。
2.3. 通用等待函數(shù)
CMSIS-RTOS提供的等待函數(shù)API如下:
API | 描述 |
---|---|
osDelay | 等待指定的時間 |
osWait(可選) | 等待信號、消息、郵箱的某個事件 |
-
osDelay
osStatus osDelay(uint32_t millisec);
返回值:osStatus。
2.4. 軟件定時器管理
CMSIS-RTOS API提供的存儲定時器參數(shù)的結構體如下:
typedef struct os_timer_def {
os_ptimer cb; ///< start address of a timer function
k_timer_t *timer;
} osTimerDef_t;
CMSIS-RTOS API提供的定義一個軟件定時器的宏定義如下:
#define osTimerDef(name, function) \
k_timer_t timer_handler_##name; \
const osTimerDef_t os_timer_def_##name = \
{ (os_ptimer)(function), (&(timer_handler_##name)) }
CMSIS-RTOS API定義的獲取軟件定時器參數(shù)結構體的宏如下:
#define osTimer(name) \
&os_timer_def_##name
CMSIS-RTOS API提供的軟件定時器管理API如下:
API | 描述 |
---|---|
osTimerCreate | 創(chuàng)建一個軟件定時器 |
osTimerStart | 啟動軟件定時器 |
osTimerStop | 停止軟件定時器 |
osTimerDelete | 刪除軟件定時器 |
-
osTimerCreate
osTimerId osTimerCreate(const osTimerDef_t *timer_def, os_timer_type type, void *argument);
其中osTimerId被定義為k_timer_t指針類型:
typedef k_timer_t *osTimerId;
type參數(shù)為 os_timer_type 類型,表示軟件定時器的類型為單次觸發(fā)或者周期觸發(fā):
typedef enum {
osTimerOnce = 0, ///< one-shot timer
osTimerPeriodic = 1 ///< repeating timer
} os_timer_type;
-
osTimerStart
osStatus osTimerStart(osTimerId timer_id, uint32_t millisec);
返回值:osStatus。
-
osTimerStop
osStatus osTimerStop(osTimerId timer_id)
返回值:osStatus。
-
osTimerDelete
osStatus osTimerDelete(osTimerId timer_id);
返回值:osStatus。
2.5. 信號量管理
CMSIS-RTOS API提供的存儲信號量參數(shù)的結構體如下:
typedef struct os_semaphore_def {
uint32_t dummy; ///< dummy value.
k_sem_t *sem;
} osSemaphoreDef_t;
CMSIS-RTOS API提供的定義一個信號量的宏定義如下:
#define osSemaphoreDef(name) \
k_sem_t sem_handler_##name; \
const osSemaphoreDef_t os_semaphore_def_##name = { 0, (&(sem_handler_##name)) }
CMSIS-RTOS API定義的獲取信號量參數(shù)結構體的宏如下:
#define osSemaphore(name) \
&os_semaphore_def_##name
CMSIS-RTOS API提供的信號量管理API如下:
API | 描述 |
---|---|
osSemaphoreCreate | 創(chuàng)建一個信號量 |
osSemaphoreWait | 等待信號量 |
osSemaphoreRelease | 釋放信號量 |
osSemaphoreDelete | 刪除信號量 |
-
osSemaphoreCreate
osSemaphoreId osSemaphoreCreate(const osSemaphoreDef_t *semaphore_def, int32_t count);
其中 osSemaphoreId 被定義為k_sem_t指針類型:
typedef k_sem_t *osSemaphoreId;
-
osSemaphoreWait
int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec);
返回值:int32_t ,正常返回當前count數(shù),失敗返回-1。
如果需要阻塞延時,參數(shù)應該設置為CMSIS-RTOS API提供的宏定義 osWaitForever :
#define osWaitForever 0xFFFFFFFF ///< wait forever timeout value
-
osSemaphoreRelease
osStatus osSemaphoreRelease(osSemaphoreId semaphore_id);
返回值:osStatus。
-
osSemaphoreDelete
osStatus osSemaphoreDelete(osSemaphoreId semaphore_id);
返回值:osStatus。
2.6. 互斥鎖管理
CMSIS-RTOS API提供的存儲互斥鎖參數(shù)的結構體如下:
typedef struct os_mutex_def {
uint32_t dummy; ///< dummy value.
k_mutex_t *mutex;
} osMutexDef_t;
CMSIS-RTOS API提供的定義一個互斥鎖的宏定義如下:
#define osMutexDef(name) \
k_mutex_t mutex_handler_##name; \
const osMutexDef_t os_mutex_def_##name = { 0, (&(mutex_handler_##name)) }
CMSIS-RTOS API定義的獲取互斥鎖參數(shù)結構體的宏如下:
#define osMutex(name) \
&os_mutex_def_##name
CMSIS-RTOS API提供的互斥鎖管理API如下:
API | 描述 |
---|---|
osMutexCreate | 創(chuàng)建一個互斥鎖 |
osMutexWait | 等待獲取互斥鎖 |
osMutexRelease | 釋放互斥鎖 |
osMutexDelete | 刪除互斥鎖 |
-
osMutexCreate
osMutexId osMutexCreate(const osMutexDef_t *mutex_def);
其中 osMutexId 被定義為k_mutex_t指針類型:
typedef k_mutex_t *osMutexId;
-
osMutexWait
osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec);
返回值:osStatus 。
如果需要阻塞延時,參數(shù)應該設置為CMSIS-RTOS API提供的宏定義 osWaitForever :
#define osWaitForever 0xFFFFFFFF ///< wait forever timeout value
-
osMutexRelease
osStatus osMutexRelease(osMutexId mutex_id);
返回值:osStatus。
-
osMutexDelete
osStatus osMutexDelete(osMutexId mutex_id);
返回值:osStatus。
2.7. 靜態(tài)內存池管理
CMSIS-RTOS API提供的存儲靜態(tài)內存池參數(shù)的結構體如下:
typedef struct os_pool_def {
uint32_t pool_sz; ///< number of items (elements) in the pool
uint32_t item_sz; ///< size of an item
void *pool; ///< pointer to memory for pool
k_mmblk_pool_t *mmblk_pool; ///< memory blk pool handler
} osPoolDef_t;
CMSIS-RTOS API提供的定義一個互斥鎖的宏定義如下:
#define osPoolDef(name, no, type) \
k_mmblk_pool_t mmblk_pool_handler_##name; \
uint8_t mmblk_pool_buf_##name[(no) * sizeof(type)]; \
const osPoolDef_t os_pool_def_##name = \
{ (no), sizeof(type), (&((mmblk_pool_buf_##name)[0])), (&(mmblk_pool_handler_##name)) }
CMSIS-RTOS API定義的獲取互斥鎖參數(shù)結構體的宏如下:
#define osPool(name) \
&os_pool_def_##name
CMSIS-RTOS API提供的互斥鎖管理API如下:
API | 描述 |
---|---|
osPoolCreate | 創(chuàng)建一塊固定大小的靜態(tài)內存池 |
osPoolAlloc | 申請分配內存 |
osPoolCAlloc | 申請分配一塊內存并全部初始化為0 |
osPoolFree | 申請回收內存 |
-
osPoolCreate
osPoolId osPoolCreate(const osPoolDef_t *pool_def);
其中 osPoolId 被定義為 k_mmblk_pool_t 指針類型:
typedef k_mmblk_pool_t *osPoolId;
-
osPoolAlloc
void *osPoolAlloc(osPoolId pool_id);
-
osPoolCAlloc
void *osPoolCAlloc(osPoolId pool_id);
-
osPoolFree
osStatus osPoolFree(osPoolId pool_id, void *block);
返回值:osStatus。
2.8. 消息隊列管理
CMSIS-RTOS API提供的存儲消息隊列參數(shù)的結構體如下:
typedef struct os_messageQ_def {
uint32_t queue_sz; ///< number of elements in the queue
uint32_t item_sz; ///< size of an item
void *pool; ///< memory array for messages
k_msg_q_t *queue; ///< queue handler
} osMessageQDef_t;
CMSIS-RTOS API提供的定義一個消息隊列的宏定義如下:
#define osMessageQDef(name, queue_sz, type) \
k_msg_q_t msg_q_handler_##name; \
const osMessageQDef_t os_messageQ_def_##name = \
{ (queue_sz), sizeof(type), NULL, (&(msg_q_handler_##name)) }
CMSIS-RTOS API定義的獲取消息隊列參數(shù)結構體的宏如下:
#define osMessageQ(name) \
&os_messageQ_def_##name
CMSIS-RTOS API提供的消息隊列管理API如下:
API | 描述 |
---|---|
osMessageCreate | 初始化一個消息隊列 |
osMessagePut | 向消息隊列中加入數(shù)據(jù) |
osMessageGet | 從消息隊列中取出數(shù)據(jù) |
-
osMessageCreate
osMessageQId osMessageCreate(const osMessageQDef_t *queue_def, osThreadId thread_id);
其中 osMessageQId 被定義為 k_msg_q_t 指針類型:
typedef k_msg_q_t *osMessageQId;
-
osMessagePut
osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec);
返回值:osStatus 。
因為TencentOS-tiny中消息隊列實現(xiàn)機制的不同,此API中的 millisec 參數(shù)未用到。
-
osMessageGet
osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec);
返回值:osEvent ,其中包含了事件信息和錯誤碼,以及消息隊列收到的值。
如果需要阻塞延時,參數(shù)應該設置為CMSIS-RTOS API提供的宏定義 osWaitForever :
#define osWaitForever 0xFFFFFFFF ///< wait forever timeout value
3. 使用示例
3.1. 任務創(chuàng)建示例
#include <cmsis_os.h>
void task1_entry(void *arg)
{
while(1)
{
printf("task1 is running...\r\n");
osDelay(1000);
}
}
osThreadDef(task1_entry, osPriorityNormal, 1, 512);
void task2_entry(void *arg)
{
while(1)
{
printf("task2 is running...\r\n");
osDelay(1000);
}
}
osThreadDef(task2_entry, osPriorityNormal, 1, 512);
void application_entry(void *arg)
{
osThreadCreate(osThread(task1_entry), NULL);
osThreadCreate(osThread(task2_entry), NULL);
return;
}
任務運行結果如下:
task1 is running...
task2 is running...
task1 is running...
task2 is running...
task1 is running...
task2 is running...
3.2. 軟件定時器使用示例
#include <cmsis_os.h>
void timer1_cb(void *arg)
{
printf("timer1 is timeout!\r\n");
}
void timer2_cb(void *arg)
{
printf("timer2 is timeout!\r\n");
}
osTimerDef(timer1, timer1_cb);
osTimerDef(timer2, timer2_cb);
void application_entry(void *arg)
{
osTimerId timer1;
osTimerId timer2;
timer1 = osTimerCreate(osTimer(timer1), osTimerOnce, NULL);
timer2 = osTimerCreate(osTimer(timer2), osTimerPeriodic, NULL);
osTimerStart(timer1, 5000);
osTimerStart(timer2, 1000);
return;
}
運行結果如下:
timer2 is timeout!
timer2 is timeout!
timer2 is timeout!
timer2 is timeout!
timer1 is timeout!
timer2 is timeout!
timer2 is timeout!
timer2 is timeout!
timer2 is timeout!
3.3. 信號量使用示例
#include <cmsis_os.h>
osSemaphoreId sync_sem_id;
osSemaphoreDef(sync_sem);
void task1_entry(void *arg)
{
while(1)
{
printf("task1 is waiting sem forever...\r\n");
osSemaphoreWait(sync_sem_id, osWaitForever);
printf("task1 get sem!\r\n");
}
}
osThreadDef(task1_entry, osPriorityNormal, 1, 512);
void task2_entry(void *arg)
{
while(1)
{
printf("task2 will release a sem...\r\n");
osSemaphoreRelease(sync_sem_id);
osDelay(1000);
}
}
osThreadDef(task2_entry, osPriorityNormal, 1, 512);
void application_entry(void *arg)
{
sync_sem_id = osSemaphoreCreate(osSemaphore(sync_sem), 0);
osThreadCreate(osThread(task1_entry), NULL);
osThreadCreate(osThread(task2_entry), NULL);
return;
}
運行結果為:
task1 is waiting sem forever...
task1 get sem!
task1 is waiting sem forever...
task2 will release a sem...
task1 get sem!
task1 is waiting sem forever...
task2 will release a sem...
task1 get sem!
task1 is waiting sem forever...
task2 will release a sem...
task1 get sem!
task1 is waiting sem forever...
task2 will release a sem...
task1 get sem!
task1 is waiting sem forever...
3.4. 互斥鎖使用示例
#include <cmsis_os.h>
osMutexId sync_mutex_id;
osMutexDef(sync_mutex);
void task1_entry(void *arg)
{
while(1)
{
osMutexWait(sync_mutex_id, osWaitForever);
printf("task1 get mutex,doing sth...\r\n");
HAL_Delay(1000); //死循環(huán)占用CPU
printf("task1 finish do sth!\r\n");
osMutexRelease(sync_mutex_id);
osDelay(1000);
}
}
osThreadDef(task1_entry, osPriorityHigh, 1, 512);
void task2_entry(void *arg)
{
while(1)
{
osMutexWait(sync_mutex_id, osWaitForever);
printf("task2 get mutex,doing sth...\r\n");
HAL_Delay(2000); //死循環(huán)占用CPU
printf("task2 finish do sth!\r\n");
osMutexRelease(sync_mutex_id);
osDelay(1000);
}
}
osThreadDef(task2_entry, osPriorityNormal, 1, 512);
void application_entry(void *arg)
{
sync_mutex_id = osMutexCreate(osMutex(sync_mutex));
osThreadCreate(osThread(task1_entry), NULL);
osThreadCreate(osThread(task2_entry), NULL);
return;
}
運行結果為:
task1 get mutex,doing sth...
task1 finish do sth!
task2 get mutex,doing sth...
task2 finish do sth!
task1 get mutex,doing sth...
task1 finish do sth!
task1 get mutex,doing sth...
task1 finish do sth!
task2 get mutex,doing sth...
3.5. 動態(tài)內存使用示例
#include <cmsis_os.h>
typedef struct blk_st {
int id;
char* payload;
} blk_t;
#define MMBLK_BLK_NUM 10
osPoolDef (MemPool, MMBLK_BLK_NUM, blk_t);
osPoolId mem_pool_id;
void task1_entry(void *arg)
{
blk_t *ptr = NULL;
osStatus err;
/* 打印出一個塊的大小 */
printf("block size is %d bytes\r\n", sizeof(blk_t));
/* 申請一個塊 */
ptr = osPoolAlloc(mem_pool_id);
if (ptr == NULL) {
printf("a mmblk alloc fail\r\n");
return;
}
else {
printf("a mmblk alloc success\r\n");
}
/* 使用該塊 */
ptr->id = 1;
ptr->payload = "hello";
printf("mmblk id:%d payload:%s\r\n", ptr->id, ptr->payload);
/* 使用完畢之后釋放 */
err = osPoolFree(mem_pool_id, ptr);
if (err != osOK) {
printf("a mmblk free fail, err = %d\r\n", err);
return;
}
else {
printf("a mmblk free success\r\n");
}
while (1) {
tos_task_delay(1000);
}
}
#define STK_SIZE_TASK1 1024
osThreadDef(task1_entry, osPriorityNormal, 1, STK_SIZE_TASK1);
void application_entry(void *arg)
{
//初始化靜態(tài)內存池
mem_pool_id = osPoolCreate(osPool(MemPool));
if (mem_pool_id == NULL) {
printf("mmblk pool create fail\r\n");
return;
}
else {
printf("mmblk pool create success\r\n");
}
//創(chuàng)建任務
osThreadCreate(osThread(task1_entry), NULL);
return;
}
運行結果為:
mmblk pool create success
block size is 8 bytes
a mmblk alloc success
mmblk id:1 payload:hello
a mmblk free success
3.6. 消息隊列使用示例
#include <cmsis_os.h>
#define STK_SIZE_TASK_RECEIVER 512
#define STK_SIZE_TASK_SENDER 512
#define MESSAGE_MAX 10
osMessageQId msg_q_id;
osMessageQDef(msg_q,MESSAGE_MAX,uint32_t);
void task_receiver_entry(void *arg)
{
osEvent event;
osStatus ret;
uint32_t value;
while (1)
{
event = osMessageGet(msg_q_id, osWaitForever);
ret = event.status;
if (ret == osOK)
{
value = event.value.v;
printf("receiver: msg incoming[%s]\r\n", (char*)value);
}
}
}
osThreadDef(task_receiver_entry, osPriorityNormal, 1, STK_SIZE_TASK_RECEIVER);
void task_sender_entry(void *arg)
{
char *msg_prio_0 = "msg 0";
char *msg_prio_1 = "msg 1";
char *msg_prio_2 = "msg 2";
printf("sender: post a messgae:[%s]\r\n", msg_prio_2);
osMessagePut(msg_q_id,(uint32_t)msg_prio_2,0);
printf("sender: post a messgae:[%s]\r\n", msg_prio_1);
osMessagePut(msg_q_id,(uint32_t)msg_prio_1,0);
printf("sender: post a messgae:[%s]\r\n", msg_prio_0);
osMessagePut(msg_q_id,(uint32_t)msg_prio_0,0);
}
osThreadDef(task_sender_entry, osPriorityNormal, 1, STK_SIZE_TASK_SENDER);
void application_entry(void *arg)
{
msg_q_id = osMessageCreate(osMessageQ(msg_q),NULL);
osThreadCreate(osThread(task_receiver_entry), NULL);
osThreadCreate(osThread(task_sender_entry), NULL);
return;
}
運行結果為:
sender: post a messgae:[msg 2]
sender: post a messgae:[msg 1]
sender: post a messgae:[msg 0]
receiver: msg incoming[msg 2]
receiver: msg incoming[msg 1]
receiver: msg incoming[msg 0]
長按前往圖中包含的公眾號關注
免責聲明:本文內容由21ic獲得授權后發(fā)布,版權歸原作者所有,本平臺僅提供信息存儲服務。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!