1- 簡(jiǎn)介
每個(gè)系統(tǒng)任務(wù)都會(huì)有一個(gè)任務(wù)通知。然后每個(gè)任務(wù)通知都具有掛起或者未掛起的狀態(tài),以及32位的通知。常量configTASK_NOTIFICATION_ARRAY_ENTRIES()是用來設(shè)置任務(wù)通知索引的數(shù)組。
任務(wù)通知是直接發(fā)送給任務(wù)事件,不是通過中間對(duì)象(隊(duì)列、事件組、信號(hào)量)間接發(fā)送給任務(wù)的。
當(dāng)任務(wù)發(fā)送任務(wù)通知時(shí),會(huì)將目標(biāo)任務(wù)通知的狀態(tài)設(shè)定位掛起狀態(tài)。就像任務(wù)阻塞中間對(duì)象一樣,例如,信號(hào)在等待信號(hào)量的可用情況,任務(wù)組織這個(gè)等待通知的狀態(tài)變?yōu)閽炱鸬臓顟B(tài)。
任務(wù)通知也可以是以下幾種方式:
- 值覆蓋,不管接受任務(wù)是否已經(jīng)讀取被覆蓋的值。
- 值覆蓋,僅當(dāng)接收任務(wù)已讀取被覆蓋的值時(shí)。
- 在設(shè)置值中設(shè)置一個(gè)或多個(gè)位。
- 遞增值(加1)
注意:
每個(gè)數(shù)組中的通知都是獨(dú)立的,一個(gè)任務(wù)一次只能阻塞數(shù)組中的一個(gè)通知,而且不會(huì)通過發(fā)送到任何其他數(shù)組索引的直通解除阻塞狀態(tài)。
1.1 優(yōu)勢(shì)和使用限制
任務(wù)通知的靈活性允許它們?cè)谛枰獎(jiǎng)?chuàng)建單獨(dú)隊(duì)列、二進(jìn)制信號(hào)量、計(jì)數(shù)信號(hào)量或事件組的情況下使用。使用直接通知解除RTOS任務(wù)的阻塞速度比使用中間對(duì)象(如二進(jìn)制信號(hào)量)解除阻塞的速度快45% *,并且使用的RAM更少。正如所料,這些性能優(yōu)勢(shì)需要一些用例限制。
1-當(dāng)只有一個(gè)任務(wù)可以接收事件時(shí),才可以使用RTOS任務(wù)通知。然而,實(shí)際應(yīng)用中的大多數(shù)用例都滿足這個(gè)條件,例如中斷解除阻塞,任務(wù)將處理由中斷接收到的數(shù)據(jù)。
2-只有在使用RTOS任務(wù)通知代替隊(duì)列的情況下:接收任務(wù)可以在阻塞狀態(tài)下等待通知(這樣不會(huì)消耗任何CPU時(shí)間),如果發(fā)送任務(wù)不能立即完成,則發(fā)送任務(wù)不能在阻塞狀態(tài)下等待發(fā)送完成。
1.2 用例
通知使用xTaskNotifyIndexed()和xTaskNotifyGiveIndexed() API函數(shù)(和它們的中斷安全等效)發(fā)送,并保持等待,直到接收RTOS任務(wù)調(diào)用xTaskNotifyWaitIndexed()或ulTaskNotifyTakeIndexed() API函數(shù)。這些API函數(shù)都有一個(gè)不帶“I索引”前綴的等價(jià)函數(shù)。非“索引”版本總是在數(shù)組索引0處的任務(wù)通知上操作。例如,xTaskNotifyGive(TargetTask)等價(jià)于xTaskNotifyGiveIndexed(TargetTask, 0) -兩者都在索引0處增加任務(wù)通知由任務(wù)處理的TargetTask引用的任務(wù)。
2-將 RTOS 任務(wù)通知用作輕量級(jí)二進(jìn)制信號(hào)量
與使用二進(jìn)制信號(hào)量解鎖任務(wù)相比,使用直接通知解鎖 RTOS 任務(wù)的速度快 45%,并且使用的 RAM 更少。
二進(jìn)制信號(hào)量是最大計(jì)數(shù)為 1 的信號(hào)量,因此稱為“二進(jìn)制”。任務(wù)只有在信號(hào)量可用時(shí)才可以“獲取”信號(hào)量,并且信號(hào)量?jī)H 如果計(jì)數(shù)為 1,則可用。
當(dāng)使用任務(wù)通知代替二進(jìn)制信號(hào)量時(shí),接收任務(wù)的通知值會(huì)代替二進(jìn)制信號(hào)量的count值,并且會(huì)使用ulTaskNotifyTake()(或ulTaskNotifyTakeIndexed()) API函數(shù)代替信號(hào)量的xSemaphoreTake() API函數(shù)。
ulTaskNotifyTake()函數(shù)的xClearOnExit參數(shù)被設(shè)置為pdTRUE,因此每次收到通知時(shí)count值都返回0——模擬二進(jìn)制信號(hào)量。
同樣,xTaskNotifyGive()(或xTaskNotifyGiveIndexed())或vTaskNotifyGiveFromISR()(或vTaskNotifyGiveIndexedFromISR())函數(shù)被用來代替信號(hào)量的xSemaphoreGive()和xSemaphoreGiveFromISR()函數(shù)。
2.1 使用范例
1/*這是通用外設(shè)驅(qū)動(dòng)程序中的傳輸函數(shù)的一個(gè)例子。
2RTOS任務(wù)調(diào)用傳輸函數(shù),然后處于阻塞狀態(tài)(因此不占用CPU時(shí)間),
3直到收到傳輸完成的通知。傳輸由DMA執(zhí)行,DMA端中斷用于通知任務(wù)。 */
4
5/* 存儲(chǔ)傳輸完成時(shí)將收到通知的任務(wù)句柄。 */
6static TaskHandle_t xTaskToNotify = NULL;
7
8/* 目標(biāo)任務(wù)要使用的任務(wù)通知數(shù)組中的索引。 */
9const UBaseType_t xArrayIndex = 1;
10
11/* 外設(shè)驅(qū)動(dòng)的傳輸功能 */
12void StartTransmission( uint8_t *pcData, size_t xDataLength )
13{
14/*此時(shí)xTaskToNotify應(yīng)該為NULL,因?yàn)闆]有正在進(jìn)行傳輸。
15如果有必要,可以使用互斥量來保護(hù)對(duì)外設(shè)的訪問。*/
16configASSERT( xTaskToNotify == NULL );
17
18/* 存儲(chǔ)調(diào)用任務(wù)的句柄。 */
19xTaskToNotify = xTaskGetCurrentTaskHandle();
20
21/* 開始傳輸:在傳輸完成時(shí)產(chǎn)生一個(gè)中斷。 */
22vStartTransmit( pcData, xDatalength );
23}
24/*-----------------------------------------------------------*/
25
26/* 結(jié)束中斷傳輸 */
27void vTransmitEndISR( void )
28{
29BaseType_t xHigherPriorityTaskWoken = pdFALSE;
30
31/* 此時(shí),xTaskToNotify不應(yīng)該為NULL,因?yàn)閭鬏斦谶M(jìn)行中。 */
32configASSERT( xTaskToNotify != NULL );
33
34/* Notify the task that the transmission is complete. */
35vTaskNotifyGiveIndexedFromISR( xTaskToNotify,
36xArrayIndex,
37&xHigherPriorityTaskWoken );
38
39/*此時(shí),xTaskToNotify不應(yīng)該為NULL,因?yàn)閭鬏斦谶M(jìn)行中。 */
40xTaskToNotify = NULL;
41
42/*如果xHigherPriorityTaskWoken現(xiàn)在設(shè)置為pdTRUE,
43那么應(yīng)該執(zhí)行切換,以確保中斷直接返回到最高優(yōu)先級(jí)的任務(wù)。
44用于該目的的宏取決于所使用的端口,可以稱為portEND_SWITCHING_ISR()。 */
45portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
46}
47/*-----------------------------------------------------------*/
48
49/* 發(fā)起傳輸?shù)娜蝿?wù),然后進(jìn)入阻塞狀態(tài)(因此不消耗任何CPU時(shí)間),以等待傳輸完成。 */
50void vAFunctionCalledFromATask( uint8_t ucDataToTransmit,
51size_t xDataLength )
52{
53uint32_t ulNotificationValue;
54const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 );
55
56/* 通過調(diào)用上面顯示的函數(shù)開始傳輸。 */
57StartTransmission( ucDataToTransmit, xDataLength );
58
59/* 等待傳輸完成的通知。注意,第一個(gè)參數(shù)是pdTRUE,
60它的作用是將任務(wù)的通知值清除回0,
61使通知值類似于二進(jìn)制(而不是計(jì)數(shù))信號(hào)量。 */
62ulNotificationValue = ulTaskNotifyTakeIndexed( xArrayIndex,
63pdTRUE,
64xMaxBlockTime );
65
66if( ulNotificationValue == 1 )
67{
68/* The transmission ended as expected. */
69}
70else
71{
72/* The call to ulTaskNotifyTake() timed out. */
73}
74}
3-使用 RTOS 任務(wù)通知作為輕量級(jí)計(jì)數(shù)信號(hào)量
與使用信號(hào)量解鎖任務(wù)相比,使用直接通知解鎖 RTOS 任務(wù)的速度快 45%,并且使用的 RAM 更少。
計(jì)數(shù)信號(hào)量是這樣一種信號(hào)量,其計(jì)數(shù)值可以為0,直到創(chuàng)建信號(hào)量時(shí)設(shè)置的最大值。只有當(dāng)信號(hào)量可用時(shí),任務(wù)才能獲取信號(hào)量,并且只有當(dāng)信號(hào)量的計(jì)數(shù)大于0時(shí),信號(hào)量才可用。
當(dāng)使用任務(wù)通知來代替計(jì)數(shù)信號(hào)量時(shí),接收任務(wù)的通知值會(huì)代替計(jì)數(shù)信號(hào)量的計(jì)數(shù)值,并且使用ulTaskNotifyTake()(或ulTaskNotifyTakeIndexed()) API函數(shù)來代替信號(hào)量的xSemaphoreTake() API函數(shù)。ulTaskNotifyTake()函數(shù)的xClearOnExit參數(shù)被設(shè)置為pdFALSE,因此每次收到通知時(shí),計(jì)數(shù)值只減少(而不是清除)——模擬計(jì)數(shù)信號(hào)量。
同樣,xTaskNotifyGive()(或xTaskNotifyGiveIndexed())或vTaskNotifyGiveFromISR()(或vTaskNotifyGiveIndexedFromISR())函數(shù)被用來代替信號(hào)量的xSemaphoreGive()和xSemaphoreGiveFromISR()函數(shù)。
下面通過兩個(gè)例子來看。
下面的第一個(gè)例子使用接收任務(wù)的通知值作為計(jì)數(shù)信號(hào)量。第二個(gè)示例提供了高效的實(shí)現(xiàn)方式。
3.1 Example 1:
1/* An interrupt handler that does not process interrupts directly,
2but instead defers processing to a high priority RTOS task. The
3ISR uses RTOS task notifications to both unblock the RTOS task
4and increment the RTOS task's notification value. */
5void vANInterruptHandler( void )
6{
7BaseType_t xHigherPriorityTaskWoken;
8
9/* Clear the interrupt. */
10prvClearInterruptSource();
11
12/* xHigherPriorityTaskWoken must be initialised to pdFALSE.
13If calling vTaskNotifyGiveFromISR() unblocks the handling
14task, and the priority of the handling task is higher than
15the priority of the currently running task, then
16xHigherPriorityTaskWoken will be automatically set to pdTRUE. */
17xHigherPriorityTaskWoken = pdFALSE;
18
19/* Unblock the handling task so the task can perform
20any processing necessitated by the interrupt. xHandlingTask
21is the task's handle, which was obtained when the task was
22created. vTaskNotifyGiveFromISR() also increments
23the receiving task's notification value. */
24vTaskNotifyGiveFromISR( xHandlingTask, &xHigherPriorityTaskWoken );
25
26/* Force a context switch if xHigherPriorityTaskWoken is now
27set to pdTRUE. The macro used to do this is dependent on
28the port and may be called portEND_SWITCHING_ISR. */
29portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
30}
31/*-----------------------------------------------------------*/
32
33/* A task that blocks waiting to be notified that the peripheral
34needs servicing. */
35void vHandlingTask( void *pvParameters )
36{
37BaseType_t xEvent;
38const TickType_t xBlockTime = pdMS_TO_TICS( 500 );
39uint32_t ulNotifiedValue;
40
41for( ;; )
42{
43/* Block to wait for a notification. Here the RTOS
44task notification is being used as a counting semaphore.
45The task's notification value is incremented each time
46the ISR calls vTaskNotifyGiveFromISR(), and decremented
47each time the RTOS task calls ulTaskNotifyTake() - so in
48effect holds a count of the number of outstanding interrupts.
49The first parameter is set to pdFALSE, so the notification
50value is only decremented and not cleared to zero, and one
51deferred interrupt event is processed at a time. See
52example 2 below for a more pragmatic approach. */
53ulNotifiedValue = ulTaskNotifyTake( pdFALSE,
54xBlockTime );
55
56if( ulNotifiedValue > 0 )
57{
58/* Perform any processing necessitated by the interrupt. */
59xEvent = xQueryPeripheral();
60
61if( xEvent != NO_MORE_EVENTS )
62{
63vProcessPeripheralEvent( xEvent );
64}
65}
66else
67{
68/* Did not receive a notification within the expected
69time. */
70vCheckForErrorConditions();
71}
72}
73}
3.2 Example 2:
1這個(gè)例子展示了一個(gè)更實(shí)用和高效的RTOS任務(wù)的實(shí)現(xiàn)。在這個(gè)實(shí)現(xiàn)中,ulTaskNotifyTake()的返回值用于知道有多少未處理的ISR事件必須被處理,允許RTOS任務(wù)的通知計(jì)數(shù)在每次ulTaskNotifyTake()被調(diào)用時(shí)被清除回零。中斷服務(wù)例程(ISR)假定如上例1所示。
2/* The index within the target task's array of task notifications
3to use. */
4const UBaseType_t xArrayIndex = 0;
5
6/* A task that blocks waiting to be notified that the peripheral
7needs servicing. */
8void vHandlingTask( void *pvParameters )
9{
10BaseType_t xEvent;
11const TickType_t xBlockTime = pdMS_TO_TICS( 500 );
12uint32_t ulNotifiedValue;
13
14for( ;; )
15{
16/* As before, block to wait for a notification form the ISR.
17This time however the first parameter is set to pdTRUE,
18clearing the task's notification value to 0, meaning each
19outstanding outstanding deferred interrupt event must be
20processed before ulTaskNotifyTake() is called again. */
21ulNotifiedValue = ulTaskNotifyTakeIndexed( xArrayIndex,
22pdTRUE,
23xBlockTime );
24
25if( ulNotifiedValue == 0 )
26{
27/* Did not receive a notification within the expected
28time. */
29vCheckForErrorConditions();
30}
31else
32{
33/* ulNotifiedValue holds a count of the number of
34outstanding interrupts. Process each in turn. */
35while( ulNotifiedValue > 0 )
36{
37xEvent = xQueryPeripheral();
38
39if( xEvent != NO_MORE_EVENTS )
40{
41vProcessPeripheralEvent( xEvent );
42ulNotifiedValue--;
43}
44else
45{
46break;
47}
48}
49}
50}
51}
4-將 RTOS 任務(wù)通知用作輕量級(jí)事件組
事件組是一組二進(jìn)制標(biāo)志(或位),應(yīng)用程序編寫人員可以為每個(gè)標(biāo)志指定含義。RTOS進(jìn)程可以進(jìn)入阻塞狀態(tài),等待組內(nèi)的一個(gè)或多個(gè)標(biāo)志變?yōu)榛顒?dòng)狀態(tài)。當(dāng)RTOS任務(wù)處于阻塞狀態(tài)時(shí),不會(huì)消耗任何CPU時(shí)間。
當(dāng)使用任務(wù)通知代替事件組時(shí),使用接收任務(wù)的通知值代替事件組,接收任務(wù)的通知值中的比特位用作事件標(biāo)志,并且使用xTaskNotifyWait() API函數(shù)代替事件組的xEventGroupWaitBits() API函數(shù)。
同樣,使用xTaskNotify()和xTaskNotifyFromISR() API函數(shù)(其eAction參數(shù)設(shè)置為eSetBits)來代替xEventGroupSetBits()和xEventGroupSetBitsFromISR()函數(shù)。
與xEventGroupSetBitsFromISR()相比,xTaskNotifyFromISR()具有顯著的性能優(yōu)勢(shì),因?yàn)閤TaskNotifyFromISR()完全在ISR中執(zhí)行,而xEventGroupSetBitsFromISR()必須推遲一些處理到RTOS進(jìn)程任務(wù)。
與使用事件組時(shí)不同,接收任務(wù)不能指定只在同時(shí)有多個(gè)比特位處于活動(dòng)狀態(tài)時(shí)才離開阻塞狀態(tài)。相反,在任何比特位處于活動(dòng)狀態(tài)時(shí),進(jìn)程將解除阻塞,并且必須自己測(cè)試比特位的組合。
1/* This example demonstrates a single RTOS task being used to process
2events that originate from two separate interrupt service routines -
3a transmit interrupt and a receive interrupt. Many peripherals will
4use the same handler for both, in which case the peripheral's
5interrupt status register can simply be bitwise ORed with the
6receiving task's notification value.
7
8First bits are defined to represent each interrupt source. */
9#define TX_BIT 0x01
10#define RX_BIT 0x02
11
12/* The handle of the task that will receive notifications from the
13interrupts. The handle was obtained when the task
14was created. */
15static TaskHandle_t xHandlingTask;
16
17/*-----------------------------------------------------------*/
18
19/* The implementation of the transmit interrupt service routine. */
20void vTxISR( void )
21{
22BaseType_t xHigherPriorityTaskWoken = pdFALSE;
23
24/* Clear the interrupt source. */
25prvClearInterrupt();
26
27/* Notify the task that the transmission is complete by setting the TX_BIT
28in the task's notification value. */
29xTaskNotifyFromISR( xHandlingTask,
30TX_BIT,
31eSetBits,
32&xHigherPriorityTaskWoken );
33
34/* If xHigherPriorityTaskWoken is now set to pdTRUE then a context switch
35should be performed to ensure the interrupt returns directly to the highest
36priority task. The macro used for this purpose is dependent on the port in
37use and may be called portEND_SWITCHING_ISR(). */
38portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
39}
40/*-----------------------------------------------------------*/
41
42/* The implementation of the receive interrupt service routine is identical
43except for the bit that gets set in the receiving task's notification value. */
44void vRxISR( void )
45{
46BaseType_t xHigherPriorityTaskWoken = pdFALSE;
47
48/* Clear the interrupt source. */
49prvClearInterrupt();
50
51/* Notify the task that the reception is complete by setting the RX_BIT
52in the task's notification value. */
53xTaskNotifyFromISR( xHandlingTask,
54RX_BIT,
55eSetBits,
56&xHigherPriorityTaskWoken );
57
58/* If xHigherPriorityTaskWoken is now set to pdTRUE then a context switch
59should be performed to ensure the interrupt returns directly to the highest
60priority task. The macro used for this purpose is dependent on the port in
61use and may be called portEND_SWITCHING_ISR(). */
62portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
63}
64/*-----------------------------------------------------------*/
65
66/* The implementation of the task that is notified by the interrupt service
67routines. */
68static void prvHandlingTask( void *pvParameter )
69{
70const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 500 );
71BaseType_t xResult;
72
73for( ;; )
74{
75/* Wait to be notified of an interrupt. */
76xResult = xTaskNotifyWait( pdFALSE, /* Don't clear bits on entry. */
77ULONG_MAX, /* Clear all bits on exit. */
78&ulNotifiedValue, /* Stores the notified value. */
79xMaxBlockTime );
80
81if( xResult == pdPASS )
82{
83/* A notification was received. See which bits were set. */
84if( ( ulNotifiedValue & TX_BIT ) != 0 )
85{
86/* The TX ISR has set a bit. */
87prvProcessTx();
88}
89
90if( ( ulNotifiedValue & RX_BIT ) != 0 )
91{
92/* The RX ISR has set a bit. */
93prvProcessRx();
94}
95}
96else
97{
98/* Did not receive a notification within the expected time. */
99prvCheckForErrors();
100}
101}
102}
5-將 RTOS 任務(wù)通知用作輕量級(jí)郵箱
RTOS任務(wù)通知可用于向任務(wù)發(fā)送數(shù)據(jù),但與RTOS隊(duì)列相比,發(fā)送數(shù)據(jù)的方式要嚴(yán)格得多,因?yàn)?
-
只能發(fā)送 32 位值
-
該值被保存為接收任務(wù)的通知值,并且在任何時(shí)間只能有一個(gè)通知值,因此短語“輕量級(jí)郵箱”優(yōu)先于“輕量級(jí)隊(duì)列”。任務(wù)的通知值是郵箱值。
-
數(shù)據(jù)使用xTaskNotify()(或xTaskNotifyIndexed())和xTaskNotifyFromISR()(或xTaskNotifyIndexedFromISR()) API函數(shù)發(fā)送到任務(wù),其eAction參數(shù)設(shè)置為eSetValueWithOverwrite或eSetValueWithoutOverwrite。如果eAction設(shè)置為eSetValueWithOverwrite,那么即使接收任務(wù)已經(jīng)有一個(gè)待處理的通知,也會(huì)更新接收任務(wù)的通知值。如果eAction設(shè)置為esetvaluewithoutooverwrite,則只有在接收任務(wù)沒有待處理通知時(shí)才更新接收任務(wù)的通知值——因?yàn)楦峦ㄖ祵⒏采w接收任務(wù)處理之前的值。
任務(wù)可以使用xTaskNotifyWait()(或xTaskNotifyWaitIndexed())讀取它自己的通知值。
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4344瀏覽量
62864 -
RTOS
+關(guān)注
關(guān)注
22文章
819瀏覽量
119807 -
FreeRTOS
+關(guān)注
關(guān)注
12文章
484瀏覽量
62315
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論