信號(hào)量即Semaphore。信號(hào)量主要用于控制和保護(hù)任務(wù)對(duì)特定資源的訪問(wèn)。FreeRTOS的信號(hào)量分為二值信號(hào)量、計(jì)數(shù)型信號(hào)量和互斥信號(hào)量。其中互斥信號(hào)量即Mutex在CMSIS API中被獨(dú)立;本文主要講解二值信號(hào)量和計(jì)數(shù)型信號(hào)量。
在FreeRTOS中,二值信號(hào)量和計(jì)數(shù)信號(hào)量在創(chuàng)建方式和功能上沒(méi)有差異,兩者區(qū)別僅為二值信號(hào)量token數(shù)為1;而計(jì)數(shù)信號(hào)量token>1。
- 信號(hào)量工作原理
圖示為CMSIS-RTOS的信號(hào)量抽象原理圖。系統(tǒng)創(chuàng)建信號(hào)量,一并指定信號(hào)量?jī)?nèi)token(object)數(shù)量。線程(任務(wù))可進(jìn)行拿取/放入token的操作。
①放入token:線程可以向信號(hào)量中放入token。調(diào)用一次相應(yīng)函數(shù)即放入一個(gè)。若當(dāng)前信號(hào)量已滿則報(bào)錯(cuò)。
②拿取token: 線程向信號(hào)量中拿取token。和消息隊(duì)列一樣,取操作可以設(shè)置阻塞超時(shí)時(shí)間。當(dāng)消息量中無(wú)token時(shí),線程進(jìn)入**BLOCK**狀態(tài)等待消息量被放入token。**在此期間當(dāng)任務(wù)檢測(cè)到消息量放入token時(shí),將自動(dòng)由****BLOCK**態(tài)轉(zhuǎn)移為**READY**態(tài)。當(dāng)?shù)却臅r(shí)間超過(guò)了指定的阻塞時(shí)間,即使隊(duì)列中尚無(wú)數(shù)據(jù),任務(wù)也會(huì)自動(dòng)從阻塞態(tài)轉(zhuǎn)移為**READY**態(tài)。此時(shí)程序會(huì)返回**osErrorTimeout**錯(cuò)誤。若沒(méi)有設(shè)置**阻塞超時(shí)**且參數(shù)正確,返回**osErrorResource**錯(cuò)誤**。**
- Semaphore APIs
①創(chuàng)建信號(hào)量
可以通過(guò)函數(shù) **osSemaphoreNew() **創(chuàng)建信號(hào)量。在創(chuàng)建時(shí),可以選擇信號(hào)量可容納token的數(shù)量、初始token數(shù); 并且可以傳入配置結(jié)構(gòu)體。當(dāng)創(chuàng)建失敗時(shí)返回NULL。
當(dāng)max_count為1時(shí),將創(chuàng)建二值信號(hào)量。
osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr);/*
@param: max_count -信號(hào)量可容納token的數(shù)量
initial_count -信號(hào)量初始時(shí)刻含有的token數(shù)量;initial_count<=max_count
*attr -配置結(jié)構(gòu)體
@retval -信號(hào)量ID(句柄);若創(chuàng)建失敗返回NULL
*/
②獲取信號(hào)量中token 【可在中斷中使用】
※當(dāng)在中斷中使用該函數(shù)時(shí),阻塞延時(shí)時(shí)間timeout應(yīng)設(shè)置為0U,否則報(bào)Parameter錯(cuò)誤。
線程調(diào)用該函數(shù)時(shí),當(dāng)消息量中無(wú)token時(shí),線程進(jìn)入BLOCK狀態(tài)等待消息量被放入token。在此期間當(dāng)任務(wù)檢測(cè)到消息量放入token時(shí),將自動(dòng)由****BLOCK態(tài)轉(zhuǎn)移為READY態(tài)。當(dāng)?shù)却臅r(shí)間超過(guò)了指定的阻塞時(shí)間,即使隊(duì)列中尚無(wú)數(shù)據(jù),任務(wù)也會(huì)自動(dòng)從阻塞態(tài)轉(zhuǎn)移為READY態(tài)。此時(shí)程序會(huì)返回osErrorTimeout錯(cuò)誤。若沒(méi)有設(shè)置阻塞超時(shí)且參數(shù)正確,返回osErrorResource錯(cuò)誤**。**
osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout);/* 獲取一個(gè)token
@param: semaphore_id -傳入信號(hào)量ID(句柄)
timeout -阻塞延時(shí)時(shí)間
@retval:
osOK: 操作成功
osErrorTimeout: the token could not be obtained in the given time.
osErrorResource: the token could not be obtained when no timeout was specified.
osErrorParameter: the parameter semaphore_id is NULL or invalid.*/
timeout參數(shù):
== 0U //不設(shè)置阻塞超時(shí)時(shí)間
== osWaitForever //任務(wù)將一直阻塞直到空隊(duì)列被寫入/滿隊(duì)列被取出數(shù)據(jù)
== Ticks //設(shè)置具體等待時(shí)間,單位為RTOS心跳數(shù)(Ticks)
③ 向信號(hào)量放入一個(gè)token 【可在中斷中使用】
當(dāng)信號(hào)量溢出時(shí),函數(shù)返回**osErrorResource **。
osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id);/*放入一個(gè)token
@param: semaphore_id -傳入信號(hào)量ID(句柄)
@retval:
osOK: the token has been released and the count incremented.
osErrorResource: the token could not be released (maximum token count has been reached).
osErrorParameter: the parameter semaphore_id is NULL or invalid.
*/
④獲取狀態(tài)
uint32_t osSemaphoreGetCount (osSemaphoreId_t semaphore_id);/*獲取信號(hào)量中token數(shù)
*/
⑤清理(刪除)信號(hào)量
osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id);/*
@retval:
osOK: the semaphore object has been deleted.
osErrorParameter: the parameter semaphore_id is NULL or invalid.
osErrorResource: the semaphore is in an invalid state.
osErrorISR: osSemaphoreDelete cannot be called from interrupt service routines.
*/
-
CMSIS
+關(guān)注
關(guān)注
0文章
41瀏覽量
12239 -
FreeRTOS
+關(guān)注
關(guān)注
12文章
492瀏覽量
63839 -
延時(shí)器
+關(guān)注
關(guān)注
1文章
36瀏覽量
15426 -
串口中斷
+關(guān)注
關(guān)注
0文章
67瀏覽量
14262
發(fā)布評(píng)論請(qǐng)先 登錄
FreeRTOS信號(hào)量使用教程

轉(zhuǎn):freeRTOS信號(hào)量學(xué)習(xí)
第14章 信號(hào)量
第15章 互斥信號(hào)量
信號(hào)量–使用許可的概念
信號(hào)量semphere概述
新手請(qǐng)教信號(hào)量的概念問(wèn)題
信號(hào)量機(jī)制怎么理解

你了解Linux 各類信號(hào)量?

Linux IPC System V 信號(hào)量
詳解互斥信號(hào)量的概念和運(yùn)行

Linux信號(hào)量(2):POSIX 信號(hào)量
淺談鴻蒙內(nèi)核源碼的信號(hào)量運(yùn)作原理
FreeRTOS 隊(duì)列 信號(hào)量 互斥量

ThreadX(六)------信號(hào)量semaphore

評(píng)論