]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/TaskNotify.c
e2c6b37e863f9a3ee56634fa059656a86899f226
[freertos] / FreeRTOS / Demo / Common / Minimal / TaskNotify.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /*\r
72  * Tests the behaviour of direct task notifications.\r
73  */\r
74 \r
75 /* Standard includes. */\r
76 #include <limits.h>\r
77 \r
78 /* Scheduler include files. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "timers.h"\r
82 \r
83 /* Demo program include files. */\r
84 #include "TaskNotify.h"\r
85 \r
86 #define notifyTASK_PRIORITY             ( tskIDLE_PRIORITY )\r
87 #define notifyUINT32_MAX        ( ( uint32_t ) 0xffffffff )\r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /*\r
91  * Implementation of the task that gets notified.\r
92  */\r
93 static void prvNotifiedTask( void *pvParameters );\r
94 \r
95 /*\r
96  * Performs a few initial tests that can be done prior to creating the second\r
97  * task.\r
98  */\r
99 static void prvSingleTaskTests( void );\r
100 \r
101 /*\r
102  * Software timer callback function from which xTaskNotify() is called.\r
103  */\r
104 static void prvNotifyingTimer( TimerHandle_t xTimer );\r
105 \r
106 /*\r
107  * Utility function to create pseudo random numbers.\r
108  */\r
109 static UBaseType_t prvRand( void );\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /* Used to latch errors during the test's execution. */\r
114 static BaseType_t xErrorStatus = pdPASS;\r
115 \r
116 /* Used to ensure the task has not stalled. */\r
117 static volatile uint32_t ulNotifyCycleCount = 0;\r
118 \r
119 /* The handle of the task that receives the notifications. */\r
120 static TaskHandle_t xTaskToNotify = NULL;\r
121 \r
122 /* Used to count the notifications sent to the task from a software timer and\r
123 the number of notifications received by the task from the software timer.  The\r
124 two should stay synchronised. */\r
125 static uint32_t ulTimerNotificationsReceived = 0UL, ulTimerNotificationsSent = 0UL;\r
126 \r
127 /* The timer used to notify the task. */\r
128 static TimerHandle_t xTimer = NULL;\r
129 \r
130 /* Used by the pseudo random number generating function. */\r
131 static size_t uxNextRand = 0;\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 void vStartTaskNotifyTask( void  )\r
136 {\r
137         /* Create the task that performs some tests by itself, then loops around\r
138         being notified by both a software timer and an interrupt. */\r
139         xTaskCreate( prvNotifiedTask, "Notified", configMINIMAL_STACK_SIZE, NULL, notifyTASK_PRIORITY, &xTaskToNotify );\r
140 \r
141         /* Pseudo seed the random number generator. */\r
142         uxNextRand = ( size_t ) prvRand;\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 static void prvSingleTaskTests( void )\r
147 {\r
148 const TickType_t xTicksToWait = pdMS_TO_TICKS( 100UL );\r
149 BaseType_t xReturned;\r
150 uint32_t ulNotifiedValue, ulLoop, ulNotifyingValue, ulPreviousValue, ulExpectedValue;\r
151 TickType_t xTimeOnEntering;\r
152 const uint32_t ulFirstNotifiedConst = 100001UL, ulSecondNotifiedValueConst = 5555UL, ulMaxLoops = 5UL;\r
153 const uint32_t ulBit0 = 0x01UL, ulBit1 = 0x02UL;\r
154 \r
155         /* -------------------------------------------------------------------------\r
156         Check blocking when there are no notifications. */\r
157         xTimeOnEntering = xTaskGetTickCount();\r
158         xReturned = xTaskNotifyWait( notifyUINT32_MAX, 0, &ulNotifiedValue, xTicksToWait );\r
159 \r
160         /* Should have blocked for the entire block time. */\r
161         if( ( xTaskGetTickCount() - xTimeOnEntering ) < xTicksToWait )\r
162         {\r
163                 xErrorStatus = pdFAIL;\r
164         }\r
165         configASSERT( xReturned == pdFAIL );\r
166         configASSERT( ulNotifiedValue == 0UL );\r
167 \r
168 \r
169 \r
170 \r
171         /* -------------------------------------------------------------------------\r
172         Check no blocking when notifications are pending.  First notify itself -\r
173         this would not be a normal thing to do and is done here for test purposes\r
174         only. */\r
175         xReturned = xTaskNotifyAndQuery( xTaskToNotify, ulFirstNotifiedConst, eSetValueWithoutOverwrite, &ulPreviousValue );\r
176 \r
177         /* Even through the 'without overwrite' action was used the update should\r
178         have been successful. */\r
179         configASSERT( xReturned == pdPASS );\r
180 \r
181         /* No bits should have been pending previously. */\r
182         configASSERT( ulPreviousValue == 0 );\r
183 \r
184         /* The task should now have a notification pending, and so not time out. */\r
185         xTimeOnEntering = xTaskGetTickCount();\r
186         xReturned = xTaskNotifyWait( notifyUINT32_MAX, 0, &ulNotifiedValue, xTicksToWait );\r
187 \r
188         if( ( xTaskGetTickCount() - xTimeOnEntering ) >= xTicksToWait )\r
189         {\r
190                 xErrorStatus = pdFAIL;\r
191         }\r
192 \r
193         /* The task should have been notified, and the notified value should\r
194         be equal to ulFirstNotifiedConst. */\r
195         configASSERT( xReturned == pdPASS );\r
196         configASSERT( ulNotifiedValue == ulFirstNotifiedConst );\r
197 \r
198         /* Incremented to show the task is still running. */\r
199         ulNotifyCycleCount++;\r
200 \r
201 \r
202 \r
203 \r
204 \r
205         /*--------------------------------------------------------------------------\r
206         Check the non-overwriting functionality.  The notification is done twice\r
207         using two different notification values.  The action says don't overwrite so\r
208         only the first notification should pass and the value read back should also\r
209         be that used with the first notification. */\r
210         xReturned = xTaskNotify( xTaskToNotify, ulFirstNotifiedConst, eSetValueWithoutOverwrite );\r
211         configASSERT( xReturned == pdPASS );\r
212 \r
213         xReturned = xTaskNotify( xTaskToNotify, ulSecondNotifiedValueConst, eSetValueWithoutOverwrite );\r
214         configASSERT( xReturned == pdFAIL );\r
215 \r
216         /* Waiting for the notification should now return immediately so a block\r
217         time of zero is used. */\r
218         xReturned = xTaskNotifyWait( notifyUINT32_MAX, 0, &ulNotifiedValue, 0 );\r
219 \r
220         configASSERT( xReturned == pdPASS );\r
221         configASSERT( ulNotifiedValue == ulFirstNotifiedConst );\r
222 \r
223 \r
224 \r
225 \r
226 \r
227         /*--------------------------------------------------------------------------\r
228         Do the same again, only this time use the overwriting version.  This time\r
229         both notifications should pass, and the value written the second time should\r
230         overwrite the value written the first time, and so be the value that is read\r
231         back. */\r
232         xReturned = xTaskNotify( xTaskToNotify, ulFirstNotifiedConst, eSetValueWithOverwrite );\r
233         configASSERT( xReturned == pdPASS );\r
234         xReturned = xTaskNotify( xTaskToNotify, ulSecondNotifiedValueConst, eSetValueWithOverwrite );\r
235         configASSERT( xReturned == pdPASS );\r
236         xReturned = xTaskNotifyWait( notifyUINT32_MAX, 0, &ulNotifiedValue, 0 );\r
237         configASSERT( xReturned == pdPASS );\r
238         configASSERT( ulNotifiedValue == ulSecondNotifiedValueConst );\r
239 \r
240 \r
241 \r
242 \r
243         /*--------------------------------------------------------------------------\r
244         Check notifications with no action pass without updating the value.  Even\r
245         though ulFirstNotifiedConst is used as the value the value read back should\r
246         remain at ulSecondNotifiedConst. */\r
247         xReturned = xTaskNotify( xTaskToNotify, ulFirstNotifiedConst, eNoAction );\r
248         configASSERT( xReturned == pdPASS );\r
249         xReturned = xTaskNotifyWait( notifyUINT32_MAX, 0, &ulNotifiedValue, 0 );\r
250         configASSERT( ulNotifiedValue == ulSecondNotifiedValueConst );\r
251 \r
252 \r
253 \r
254 \r
255         /*--------------------------------------------------------------------------\r
256         Check incrementing values.  Send ulMaxLoop increment notifications, then\r
257         ensure the received value is as expected - which should be\r
258         ulSecondNotificationValueConst plus how ever many times to loop iterated. */\r
259         for( ulLoop = 0; ulLoop < ulMaxLoops; ulLoop++ )\r
260         {\r
261                 xReturned = xTaskNotify( xTaskToNotify, 0, eIncrement );\r
262                 configASSERT( xReturned == pdPASS );\r
263         }\r
264 \r
265         xReturned = xTaskNotifyWait( notifyUINT32_MAX, 0, &ulNotifiedValue, 0 );\r
266         configASSERT( xReturned == pdPASS );\r
267         configASSERT( ulNotifiedValue == ( ulSecondNotifiedValueConst + ulMaxLoops ) );\r
268 \r
269         /* Should not be any notifications pending now. */\r
270         xReturned = xTaskNotifyWait( 0, 0, &ulNotifiedValue, 0 );\r
271         configASSERT( xReturned == pdFAIL );\r
272 \r
273 \r
274 \r
275 \r
276         /*--------------------------------------------------------------------------\r
277         Check all bits can be set by notifying the task with one additional bit set\r
278         on each notification, and exiting the loop when all the bits are found to be\r
279         set.  As there are 32-bits the loop should execute 32 times before all the\r
280         bits are found to be set. */\r
281         ulNotifyingValue = 0x01;\r
282         ulLoop = 0;\r
283 \r
284         /* Start with all bits clear. */\r
285         xTaskNotifyWait( notifyUINT32_MAX, 0, &ulNotifiedValue, 0 );\r
286 \r
287         do\r
288         {\r
289                 /* Set the next bit in the task's notified value. */\r
290                 xTaskNotify( xTaskToNotify, ulNotifyingValue, eSetBits );\r
291 \r
292                 /* Wait for the notified value - which of course will already be\r
293                 available.  Don't clear the bits on entry or exit as this loop is exited\r
294                 when all the bits are set. */\r
295                 xReturned = xTaskNotifyWait( 0, 0, &ulNotifiedValue, 0 );\r
296                 configASSERT( xReturned == pdPASS );\r
297 \r
298                 ulLoop++;\r
299 \r
300                 /* Use the next bit on the next iteration around this loop. */\r
301                 ulNotifyingValue <<= 1UL;\r
302 \r
303         } while ( ulNotifiedValue != notifyUINT32_MAX );\r
304 \r
305         /* As a 32-bit value was used the loop should have executed 32 times before\r
306         all the bits were set. */\r
307         configASSERT( ulLoop == 32 );\r
308 \r
309 \r
310 \r
311 \r
312         /*--------------------------------------------------------------------------\r
313         Check bits are cleared on entry but not on exit when a notification fails\r
314         to arrive before timing out - both with and without a timeout value.  Wait\r
315         for the notification again - but this time it is not given by anything and\r
316         should return pdFAIL.  The parameters are set to clear bit zero on entry and\r
317         bit one on exit.  As no notification was received only the bit cleared on\r
318         entry should actually get cleared. */\r
319         xReturned = xTaskNotifyWait( ulBit0, ulBit1, &ulNotifiedValue, xTicksToWait );\r
320         configASSERT( xReturned == pdFAIL );\r
321 \r
322         /* Notify the task with no action so as not to update the bits even though\r
323         notifyUINT32_MAX is used as the notification value. */\r
324         xTaskNotify( xTaskToNotify, notifyUINT32_MAX, eNoAction );\r
325 \r
326         /* Reading back the value should should find bit 0 is clear, as this was\r
327         cleared on entry, but bit 1 is not clear as it will not have been cleared on\r
328         exit as no notification was received. */\r
329         xReturned = xTaskNotifyWait( 0x00UL, 0x00UL, &ulNotifiedValue, 0 );\r
330         configASSERT( xReturned == pdPASS );\r
331         configASSERT( ulNotifiedValue == ( notifyUINT32_MAX & ~ulBit0 ) );\r
332 \r
333 \r
334 \r
335 \r
336 \r
337         /*--------------------------------------------------------------------------\r
338         Now try clearing the bit on exit.  For that to happen a notification must be\r
339         received, so the task is notified first. */\r
340         xTaskNotify( xTaskToNotify, 0, eNoAction );\r
341         xTaskNotifyWait( 0x00, ulBit1, &ulNotifiedValue, 0 );\r
342 \r
343         /* However as the bit is cleared on exit, after the returned notification\r
344         value is set, the returned notification value should not have the bit\r
345         cleared... */\r
346         configASSERT( ulNotifiedValue == ( notifyUINT32_MAX & ~ulBit0 ) );\r
347 \r
348         /* ...but reading the value back again should find that the bit was indeed\r
349         cleared internally.  The returned value should be pdFAIL however as nothing\r
350         has notified the task in the mean time. */\r
351         xReturned = xTaskNotifyWait( 0x00, 0x00, &ulNotifiedValue, 0 );\r
352         configASSERT( xReturned == pdFAIL );\r
353         configASSERT( ulNotifiedValue == ( notifyUINT32_MAX & ~( ulBit0 | ulBit1 ) ) );\r
354 \r
355 \r
356 \r
357 \r
358         /*--------------------------------------------------------------------------\r
359         Now try querying the previus value while notifying a task. */\r
360         xTaskNotifyAndQuery( xTaskToNotify, 0x00, eSetBits, &ulPreviousValue );\r
361         configASSERT( ulNotifiedValue == ( notifyUINT32_MAX & ~( ulBit0 | ulBit1 ) ) );\r
362 \r
363         /* Clear all bits. */\r
364         xTaskNotifyWait( 0x00, notifyUINT32_MAX, &ulNotifiedValue, 0 );\r
365         xTaskNotifyAndQuery( xTaskToNotify, 0x00, eSetBits, &ulPreviousValue );\r
366         configASSERT( ulPreviousValue == 0 );\r
367 \r
368         ulExpectedValue = 0;\r
369         for( ulLoop = 0x01; ulLoop < 0x80UL; ulLoop <<= 1UL )\r
370         {\r
371                 /* Set the next bit up, and expect to receive the last bits set (so\r
372                 the previous value will not yet have the bit being set this time\r
373                 around). */\r
374                 xTaskNotifyAndQuery( xTaskToNotify, ulLoop, eSetBits, &ulPreviousValue );\r
375                 configASSERT( ulExpectedValue == ulPreviousValue );\r
376                 ulExpectedValue |= ulLoop;\r
377         }\r
378 \r
379 \r
380 \r
381         /* -------------------------------------------------------------------------\r
382         Clear the previous notifications. */\r
383         xTaskNotifyWait( notifyUINT32_MAX, 0, &ulNotifiedValue, 0 );\r
384 \r
385         /* The task should not have any notifications pending, so an attempt to clear\r
386         the notification state should fail. */\r
387         configASSERT( xTaskNotifyStateClear( NULL ) == pdFALSE );\r
388 \r
389         /* Get the task to notify itself.  This is not a normal thing to do, and is\r
390         only done here for test purposes. */\r
391         xTaskNotifyAndQuery( xTaskToNotify, ulFirstNotifiedConst, eSetValueWithoutOverwrite, &ulPreviousValue );\r
392 \r
393         /* Now the notification state should be eNotified, so it should now be\r
394         possible to clear the notification state. */\r
395         configASSERT( xTaskNotifyStateClear( NULL ) == pdTRUE );\r
396         configASSERT( xTaskNotifyStateClear( NULL ) == pdFALSE );\r
397 \r
398 \r
399 \r
400 \r
401         /* Incremented to show the task is still running. */\r
402         ulNotifyCycleCount++;\r
403 \r
404         /* Leave all bits cleared. */\r
405         xTaskNotifyWait( notifyUINT32_MAX, 0, NULL, 0 );\r
406 }\r
407 /*-----------------------------------------------------------*/\r
408 \r
409 static void prvNotifyingTimer( TimerHandle_t xNotUsed )\r
410 {\r
411         ( void ) xNotUsed;\r
412 \r
413         xTaskNotifyGive( xTaskToNotify );\r
414 \r
415         /* This value is also incremented from an interrupt. */\r
416         taskENTER_CRITICAL();\r
417         {\r
418                 ulTimerNotificationsSent++;\r
419         }\r
420         taskEXIT_CRITICAL();\r
421 }\r
422 /*-----------------------------------------------------------*/\r
423 \r
424 static void prvNotifiedTask( void *pvParameters )\r
425 {\r
426 const TickType_t xMaxPeriod = pdMS_TO_TICKS( 90 ), xMinPeriod = pdMS_TO_TICKS( 10 ), xDontBlock = 0;\r
427 TickType_t xPeriod;\r
428 const uint32_t ulCyclesToRaisePriority = 50UL;\r
429 \r
430         /* Remove compiler warnings about unused parameters. */\r
431         ( void ) pvParameters;\r
432 \r
433         /* Run a few tests that can be done from a single task before entering the\r
434         main loop. */\r
435         prvSingleTaskTests();\r
436 \r
437         /* Create the software timer that is used to send notifications to this\r
438         task.  Notifications are also received from an interrupt. */\r
439         xTimer = xTimerCreate( "Notifier", xMaxPeriod, pdFALSE, NULL, prvNotifyingTimer );\r
440 \r
441         for( ;; )\r
442         {\r
443                 /* Start the timer again with a different period.  Sometimes the period\r
444                 will be higher than the tasks block time, sometimes it will be lower\r
445                 than the tasks block time. */\r
446                 xPeriod = prvRand() % xMaxPeriod;\r
447                 if( xPeriod < xMinPeriod )\r
448                 {\r
449                         xPeriod = xMinPeriod;\r
450                 }\r
451 \r
452                 /* Change the timer period and start the timer. */\r
453                 xTimerChangePeriod( xTimer, xPeriod, portMAX_DELAY );\r
454 \r
455                 /* Block waiting for the notification again with a different period.\r
456                 Sometimes the period will be higher than the tasks block time, sometimes\r
457                 it will be lower than the tasks block time. */\r
458                 xPeriod = prvRand() % xMaxPeriod;\r
459                 if( xPeriod < xMinPeriod )\r
460                 {\r
461                         xPeriod = xMinPeriod;\r
462                 }\r
463 \r
464                 /* Block to wait for a notification but without clearing the\r
465                 notification count, so only add one to the count of received\r
466                 notifications as any other notifications will remain pending. */\r
467                 if( ulTaskNotifyTake( pdFALSE, xPeriod ) != 0 )\r
468                 {\r
469                         ulTimerNotificationsReceived++;\r
470                 }\r
471 \r
472 \r
473                 /* Take a notification without clearing again, but this time without a\r
474                 block time specified. */\r
475                 if( ulTaskNotifyTake( pdFALSE, xDontBlock ) != 0 )\r
476                 {\r
477                         ulTimerNotificationsReceived++;\r
478                 }\r
479 \r
480                 /* Wait for the next notification from the timer, clearing all\r
481                 notifications if one is received, so this time adding the total number\r
482                 of notifications that were pending as none will be left pending after\r
483                 the function call. */\r
484                 ulTimerNotificationsReceived += ulTaskNotifyTake( pdTRUE, xPeriod );\r
485 \r
486                 /* Occasionally raise the priority of the task being notified to test\r
487                 the path where the task is notified from an ISR and becomes the highest\r
488                 priority ready state task, but the pxHigherPriorityTaskWoken parameter\r
489                 is NULL (which it is in the tick hook that sends notifications to this\r
490                 task. */\r
491                 if( ( ulNotifyCycleCount % ulCyclesToRaisePriority ) == 0 )\r
492                 {\r
493                         vTaskPrioritySet( xTaskToNotify, configMAX_PRIORITIES - 1 );\r
494 \r
495                         /* Wait for the next notification again, clearing all notifications if\r
496                         one is received, but this time blocking indefinitely. */\r
497                         ulTimerNotificationsReceived += ulTaskNotifyTake( pdTRUE, portMAX_DELAY );\r
498 \r
499                         /* Reset the priority. */\r
500                         vTaskPrioritySet( xTaskToNotify, notifyTASK_PRIORITY );\r
501                 }\r
502                 else\r
503                 {\r
504                         /* Wait for the next notification again, clearing all notifications if\r
505                         one is received, but this time blocking indefinitely. */\r
506                         ulTimerNotificationsReceived += ulTaskNotifyTake( pdTRUE, portMAX_DELAY );\r
507                 }\r
508 \r
509                 /* Incremented to show the task is still running. */\r
510                 ulNotifyCycleCount++;\r
511         }\r
512 }\r
513 /*-----------------------------------------------------------*/\r
514 \r
515 void xNotifyTaskFromISR( void )\r
516 {\r
517 static BaseType_t xCallCount = 0, xAPIToUse = 0;\r
518 const BaseType_t xCallInterval = pdMS_TO_TICKS( 50 );\r
519 uint32_t ulPreviousValue;\r
520 const uint32_t ulUnexpectedValue = 0xff;\r
521 \r
522         /* The task performs some tests before starting the timer that gives the\r
523         notification from this interrupt.  If the timer has not been created yet\r
524         then the initial tests have not yet completed and the notification should\r
525         not be sent. */\r
526         if( xTimer != NULL )\r
527         {\r
528                 xCallCount++;\r
529 \r
530                 if( xCallCount >= xCallInterval )\r
531                 {\r
532                         /* It is time to 'give' the notification again. */\r
533                         xCallCount = 0;\r
534 \r
535                         /* Test using both vTaskNotifyGiveFromISR(), xTaskNotifyFromISR()\r
536                         and xTaskNotifyAndQueryFromISR(). */\r
537                         switch( xAPIToUse )\r
538                         {\r
539                                 case 0: vTaskNotifyGiveFromISR( xTaskToNotify, NULL );\r
540                                                 xAPIToUse++;\r
541                                                 break;\r
542 \r
543                                 case 1: xTaskNotifyFromISR( xTaskToNotify, 0, eIncrement, NULL );\r
544                                                 xAPIToUse++;\r
545                                                 break;\r
546 \r
547                                 case 2: ulPreviousValue = ulUnexpectedValue;\r
548                                                 xTaskNotifyAndQueryFromISR( xTaskToNotify, 0, eIncrement, &ulPreviousValue, NULL );\r
549                                                 configASSERT( ulPreviousValue != ulUnexpectedValue );\r
550                                                 xAPIToUse = 0;\r
551                                                 break;\r
552 \r
553                                 default:/* Should never get here!. */\r
554                                                 break;\r
555                         }\r
556 \r
557                         ulTimerNotificationsSent++;\r
558                 }\r
559         }\r
560 }\r
561 /*-----------------------------------------------------------*/\r
562 \r
563 /* This is called to check the created tasks are still running and have not\r
564 detected any errors. */\r
565 BaseType_t xAreTaskNotificationTasksStillRunning( void )\r
566 {\r
567 static uint32_t ulLastNotifyCycleCount = 0;\r
568 const uint32_t ulMaxSendReceiveDeviation = 5UL;\r
569 \r
570         /* Check the cycle count is still incrementing to ensure the task is still\r
571         actually running. */\r
572         if( ulLastNotifyCycleCount == ulNotifyCycleCount )\r
573         {\r
574                 xErrorStatus = pdFAIL;\r
575         }\r
576         else\r
577         {\r
578                 ulLastNotifyCycleCount = ulNotifyCycleCount;\r
579         }\r
580 \r
581         /* Check the count of 'takes' from the software timer is keeping track with\r
582         the amount of 'gives'. */\r
583         if( ulTimerNotificationsSent > ulTimerNotificationsReceived )\r
584         {\r
585                 if( ( ulTimerNotificationsSent - ulTimerNotificationsReceived ) > ulMaxSendReceiveDeviation )\r
586                 {\r
587                         xErrorStatus = pdFAIL;\r
588                 }\r
589         }\r
590 \r
591         return xErrorStatus;\r
592 }\r
593 /*-----------------------------------------------------------*/\r
594 \r
595 static UBaseType_t prvRand( void )\r
596 {\r
597 const size_t uxMultiplier = ( size_t ) 0x015a4e35, uxIncrement = ( size_t ) 1;\r
598 \r
599         /* Utility function to generate a pseudo random number. */\r
600         uxNextRand = ( uxMultiplier * uxNextRand ) + uxIncrement;\r
601         return( ( uxNextRand >> 16 ) & ( ( size_t ) 0x7fff ) );\r
602 }\r
603 /*-----------------------------------------------------------*/\r