2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
98 * Tests the behaviour of direct task notifications.
\r
101 /* Standard includes. */
\r
102 #include <limits.h>
\r
104 /* Scheduler include files. */
\r
105 #include "FreeRTOS.h"
\r
107 #include "timers.h"
\r
109 /* Demo program include files. */
\r
110 #include "TaskNotify.h"
\r
112 #define notifyTASK_PRIORITY ( tskIDLE_PRIORITY )
\r
114 /*-----------------------------------------------------------*/
\r
117 * Implementation of the task that gets notified.
\r
119 static void prvNotifiedTask( void *pvParameters );
\r
122 * Performs a few initial tests that can be done prior to creating the second
\r
125 static void prvSingleTaskTests( void );
\r
128 * Software timer callback function from which xTaskNotify() is called.
\r
130 static void prvNotifyingTimer( TimerHandle_t xTimer );
\r
133 * Utility function to create pseudo random numbers.
\r
135 static UBaseType_t prvRand( void );
\r
137 /*-----------------------------------------------------------*/
\r
139 /* Used to latch errors during the test's execution. */
\r
140 static BaseType_t xErrorStatus = pdPASS;
\r
142 /* Used to ensure the task has not stalled. */
\r
143 static volatile uint32_t ulNotifyCycleCount = 0;
\r
145 /* The handle of the task that receives the notifications. */
\r
146 static TaskHandle_t xTaskToNotify = NULL;
\r
148 /* Used to count the notifications sent to the task from a software timer and
\r
149 the number of notifications received by the task from the software timer. The
\r
150 two should stay synchronised. */
\r
151 static uint32_t ulTimerNotificationsReceived = 0UL, ulTimerNotificationsSent = 0UL;
\r
153 /* The timer used to notify the task. */
\r
154 static TimerHandle_t xTimer = NULL;
\r
156 /* Used by the pseudo random number generating function. */
\r
157 static uint32_t ulNextRand = 0;
\r
159 /*-----------------------------------------------------------*/
\r
161 void vStartTaskNotifyTask( void )
\r
163 /* Create the task that performs some tests by itself, then loops around
\r
164 being notified by both a software timer and an interrupt. */
\r
165 xTaskCreate( prvNotifiedTask, "Notified", configMINIMAL_STACK_SIZE, NULL, notifyTASK_PRIORITY, &xTaskToNotify );
\r
167 /* Pseudo seed the random number generator. */
\r
168 ulNextRand = ( uint32_t ) prvRand;
\r
170 /*-----------------------------------------------------------*/
\r
172 static void prvSingleTaskTests( void )
\r
174 const TickType_t xTicksToWait = pdMS_TO_TICKS( 100UL );
\r
175 BaseType_t xReturned;
\r
176 uint32_t ulNotifiedValue, ulLoop, ulNotifyingValue;
\r
177 TickType_t xTimeOnEntering;
\r
178 const uint32_t ulFirstNotifiedConst = 100001UL, ulSecondNotifiedValueConst = 5555UL, ulMaxLoops = 5UL;
\r
179 const uint32_t ulBit0 = 0x01UL, ulBit1 = 0x02UL;
\r
181 /* -------------------------------------------------------------------------
\r
182 Check blocking when there are no notifications. */
\r
183 xTimeOnEntering = xTaskGetTickCount();
\r
184 xReturned = xTaskNotifyWait( ULONG_MAX, 0, &ulNotifiedValue, xTicksToWait );
\r
186 /* Should have blocked for the entire block time. */
\r
187 if( ( xTaskGetTickCount() - xTimeOnEntering ) < xTicksToWait )
\r
189 xErrorStatus = pdFAIL;
\r
191 configASSERT( xReturned == pdFAIL );
\r
192 configASSERT( ulNotifiedValue == 0UL );
\r
197 /* -------------------------------------------------------------------------
\r
198 Check no blocking when notifications are pending. First notify itself -
\r
199 this would not be a normal thing to do and is done here for test purposes
\r
201 xReturned = xTaskNotify( xTaskToNotify, ulFirstNotifiedConst, eSetValueWithoutOverwrite );
\r
203 /* Even through the 'without overwrite' action was used the update should
\r
204 have been successful. */
\r
205 configASSERT( xReturned == pdPASS );
\r
207 /* The task should now have a notification pending, and so not time out. */
\r
208 xTimeOnEntering = xTaskGetTickCount();
\r
209 xReturned = xTaskNotifyWait( ULONG_MAX, 0, &ulNotifiedValue, xTicksToWait );
\r
211 if( ( xTaskGetTickCount() - xTimeOnEntering ) >= xTicksToWait )
\r
213 xErrorStatus = pdFAIL;
\r
216 /* The task should have been notified, and the notified value should
\r
217 be equal to ulFirstNotifiedConst. */
\r
218 configASSERT( xReturned == pdPASS );
\r
219 configASSERT( ulNotifiedValue == ulFirstNotifiedConst );
\r
221 /* Incremented to show the task is still running. */
\r
222 ulNotifyCycleCount++;
\r
228 /*--------------------------------------------------------------------------
\r
229 Check the non-overwriting functionality. The notification is done twice
\r
230 using two different notification values. The action says don't overwrite so
\r
231 only the first notification should pass and the value read back should also
\r
232 be that used with the first notification. */
\r
233 xReturned = xTaskNotify( xTaskToNotify, ulFirstNotifiedConst, eSetValueWithoutOverwrite );
\r
234 configASSERT( xReturned == pdPASS );
\r
236 xReturned = xTaskNotify( xTaskToNotify, ulSecondNotifiedValueConst, eSetValueWithoutOverwrite );
\r
237 configASSERT( xReturned == pdFAIL );
\r
239 /* Waiting for the notification should now return immediately so a block
\r
240 time of zero is used. */
\r
241 xReturned = xTaskNotifyWait( ULONG_MAX, 0, &ulNotifiedValue, 0 );
\r
243 configASSERT( xReturned == pdPASS );
\r
244 configASSERT( ulNotifiedValue == ulFirstNotifiedConst );
\r
250 /*--------------------------------------------------------------------------
\r
251 Do the same again, only this time use the overwriting version. This time
\r
252 both notifications should pass, and the value written the second time should
\r
253 overwrite the value written the first time, and so be the value that is read
\r
255 xReturned = xTaskNotify( xTaskToNotify, ulFirstNotifiedConst, eSetValueWithOverwrite );
\r
256 configASSERT( xReturned == pdPASS );
\r
257 xReturned = xTaskNotify( xTaskToNotify, ulSecondNotifiedValueConst, eSetValueWithOverwrite );
\r
258 configASSERT( xReturned == pdPASS );
\r
259 xReturned = xTaskNotifyWait( ULONG_MAX, 0, &ulNotifiedValue, 0 );
\r
260 configASSERT( xReturned == pdPASS );
\r
261 configASSERT( ulNotifiedValue == ulSecondNotifiedValueConst );
\r
266 /*--------------------------------------------------------------------------
\r
267 Check notifications with no action pass without updating the value. Even
\r
268 though ulFirstNotifiedConst is used as the value the value read back should
\r
269 remain at ulSecondNotifiedConst. */
\r
270 xReturned = xTaskNotify( xTaskToNotify, ulFirstNotifiedConst, eNoAction );
\r
271 configASSERT( xReturned == pdPASS );
\r
272 xReturned = xTaskNotifyWait( ULONG_MAX, 0, &ulNotifiedValue, 0 );
\r
273 configASSERT( ulNotifiedValue == ulSecondNotifiedValueConst );
\r
278 /*--------------------------------------------------------------------------
\r
279 Check incrementing values. Send ulMaxLoop increment notifications, then
\r
280 ensure the received value is as expected - which should be
\r
281 ulSecondNotificationValueConst plus how ever many times to loop iterated. */
\r
282 for( ulLoop = 0; ulLoop < ulMaxLoops; ulLoop++ )
\r
284 xReturned = xTaskNotify( xTaskToNotify, 0, eIncrement );
\r
285 configASSERT( xReturned == pdPASS );
\r
288 xReturned = xTaskNotifyWait( ULONG_MAX, 0, &ulNotifiedValue, 0 );
\r
289 configASSERT( xReturned == pdPASS );
\r
290 configASSERT( ulNotifiedValue == ( ulSecondNotifiedValueConst + ulMaxLoops ) );
\r
292 /* Should not be any notifications pending now. */
\r
293 xReturned = xTaskNotifyWait( 0, 0, &ulNotifiedValue, 0 );
\r
294 configASSERT( xReturned == pdFAIL );
\r
299 /*--------------------------------------------------------------------------
\r
300 Check all bits can be set by notifying the task with one additional bit set
\r
301 on each notification, and exiting the loop when all the bits are found to be
\r
302 set. As there are 32-bits the loop should execute 32 times before all the
\r
303 bits are found to be set. */
\r
304 ulNotifyingValue = 0x01;
\r
307 /* Start with all bits clear. */
\r
308 xTaskNotifyWait( ULONG_MAX, 0, &ulNotifiedValue, 0 );
\r
312 /* Set the next bit in the task's notified value. */
\r
313 xTaskNotify( xTaskToNotify, ulNotifyingValue, eSetBits );
\r
315 /* Wait for the notified value - which of course will already be
\r
316 available. Don't clear the bits on entry or exit as this loop is exited
\r
317 when all the bits are set. */
\r
318 xReturned = xTaskNotifyWait( 0, 0, &ulNotifiedValue, 0 );
\r
319 configASSERT( xReturned == pdPASS );
\r
323 /* Use the next bit on the next iteration around this loop. */
\r
324 ulNotifyingValue <<= 1UL;
\r
326 } while ( ulNotifiedValue != ULONG_MAX );
\r
328 /* As a 32-bit value was used the loop should have executed 32 times before
\r
329 all the bits were set. */
\r
330 configASSERT( ulLoop == 32 );
\r
335 /*--------------------------------------------------------------------------
\r
336 Check bits are cleared on entry but not on exit when a notification fails
\r
337 to arrive before timing out - both with and without a timeout value. Wait
\r
338 for the notification again - but this time it is not given by anything and
\r
339 should return pdFAIL. The parameters are set to clear bit zero on entry and
\r
340 bit one on exit. As no notification was received only the bit cleared on
\r
341 entry should actually get cleared. */
\r
342 xReturned = xTaskNotifyWait( ulBit0, ulBit1, &ulNotifiedValue, xTicksToWait );
\r
343 configASSERT( xReturned == pdFAIL );
\r
345 /* Notify the task with no action so as not to update the bits even though
\r
346 ULONG_MAX is used as the notification value. */
\r
347 xTaskNotify( xTaskToNotify, ULONG_MAX, eNoAction );
\r
349 /* Reading back the value should should find bit 0 is clear, as this was
\r
350 cleared on entry, but bit 1 is not clear as it will not have been cleared on
\r
351 exit as no notification was received. */
\r
352 xReturned = xTaskNotifyWait( 0x00UL, 0x00UL, &ulNotifiedValue, 0 );
\r
353 configASSERT( xReturned == pdPASS );
\r
354 configASSERT( ulNotifiedValue == ( ULONG_MAX & ~ulBit0 ) );
\r
360 /*--------------------------------------------------------------------------
\r
361 Now try clearing the bit on exit. For that to happen a notification must be
\r
362 received, so the task is notified first. */
\r
363 xTaskNotify( xTaskToNotify, 0, eNoAction );
\r
364 xTaskNotifyWait( 0x00, ulBit1, &ulNotifiedValue, 0 );
\r
366 /* However as the bit is cleared on exit, after the returned notification
\r
367 value is set, the returned notification value should not have the bit
\r
369 configASSERT( ulNotifiedValue == ( ULONG_MAX & ~ulBit0 ) );
\r
371 /* ...but reading the value back again should find that the bit was indeed
\r
372 cleared internally. The returned value should be pdFAIL however as nothing
\r
373 has notified the task in the mean time. */
\r
374 xReturned = xTaskNotifyWait( 0x00, 0x00, &ulNotifiedValue, 0 );
\r
375 configASSERT( xReturned == pdFAIL );
\r
376 configASSERT( ulNotifiedValue == ( ULONG_MAX & ~( ulBit0 | ulBit1 ) ) );
\r
380 /* Incremented to show the task is still running. */
\r
381 ulNotifyCycleCount++;
\r
383 /* Leave all bits cleared. */
\r
384 xTaskNotifyWait( ULONG_MAX, 0, NULL, 0 );
\r
386 /*-----------------------------------------------------------*/
\r
388 static void prvNotifyingTimer( TimerHandle_t xNotUsed )
\r
392 xTaskNotifyGive( xTaskToNotify );
\r
394 /* This value is also incremented from an interrupt. */
\r
395 taskENTER_CRITICAL();
\r
397 ulTimerNotificationsSent++;
\r
399 taskEXIT_CRITICAL();
\r
401 /*-----------------------------------------------------------*/
\r
403 static void prvNotifiedTask( void *pvParameters )
\r
405 const TickType_t xMaxPeriod = pdMS_TO_TICKS( 90 ), xMinPeriod = pdMS_TO_TICKS( 10 ), xDontBlock = 0;
\r
406 TickType_t xPeriod;
\r
408 /* Remove compiler warnings about unused parameters. */
\r
409 ( void ) pvParameters;
\r
411 /* Run a few tests that can be done from a single task before entering the
\r
413 prvSingleTaskTests();
\r
415 /* Create the software timer that is used to send notifications to this
\r
416 task. Notifications are also received from an interrupt. */
\r
417 xTimer = xTimerCreate( "Notifier", xMaxPeriod, pdFALSE, NULL, prvNotifyingTimer );
\r
421 /* Start the timer again with a different period. Sometimes the period
\r
422 will be higher than the tasks block time, sometimes it will be lower
\r
423 than the tasks block time. */
\r
424 xPeriod = prvRand() % xMaxPeriod;
\r
425 if( xPeriod < xMinPeriod )
\r
427 xPeriod = xMinPeriod;
\r
430 xTimerChangePeriod( xTimer, xPeriod, portMAX_DELAY );
\r
431 xTimerStart( xTimer, portMAX_DELAY );
\r
433 /* Block waiting for the notification again with a different period.
\r
434 Sometimes the period will be higher than the tasks block time, sometimes
\r
435 it will be lower than the tasks block time. */
\r
436 xPeriod = prvRand() % xMaxPeriod;
\r
437 if( xPeriod < xMinPeriod )
\r
439 xPeriod = xMinPeriod;
\r
442 /* Block to wait for a notification but without clearing the
\r
443 notification count, so only add one to the count of received
\r
444 notifications as any other notifications will remain pending. */
\r
445 if( ulTaskNotifyTake( pdFALSE, xPeriod ) != 0 )
\r
447 ulTimerNotificationsReceived++;
\r
451 /* Take a notification without clearing again, but this time without a
\r
452 block time specified. */
\r
453 if( ulTaskNotifyTake( pdFALSE, xDontBlock ) != 0 )
\r
455 ulTimerNotificationsReceived++;
\r
458 /* Wait for the next notification from the timer, clearing all
\r
459 notifications if one is received, so this time adding the total number
\r
460 of notifications that were pending as none will be left pending after
\r
461 the function call. */
\r
462 ulTimerNotificationsReceived += ulTaskNotifyTake( pdTRUE, xPeriod );
\r
464 /* Wait for the next notification again, clearing all notifications if
\r
465 one is received, but this time blocking indefinitely. */
\r
466 ulTimerNotificationsReceived += ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
\r
468 /* Incremented to show the task is still running. */
\r
469 ulNotifyCycleCount++;
\r
472 /*-----------------------------------------------------------*/
\r
474 void xNotifyTaskFromISR( void )
\r
476 static BaseType_t xCallCount = 0;
\r
477 const BaseType_t xCallInterval = pdMS_TO_TICKS( 50 );
\r
479 /* The task performs some tests before starting the timer that gives the
\r
480 notification from this interrupt. If the timer has not been created yet
\r
481 then the initial tests have not yet completed and the notification should
\r
483 if( xTimer != NULL )
\r
487 if( xCallCount >= xCallInterval )
\r
489 /* It is time to 'give' the notification again. */
\r
492 vTaskNotifyGiveFromISR( xTaskToNotify, NULL );
\r
493 ulTimerNotificationsSent++;
\r
497 /*-----------------------------------------------------------*/
\r
499 /* This is called to check the created tasks are still running and have not
\r
500 detected any errors. */
\r
501 BaseType_t xAreTaskNotificationTasksStillRunning( void )
\r
503 static uint32_t ulLastNotifyCycleCount = 0;
\r
504 const uint32_t ulMaxSendReceiveDeviation = 5UL;
\r
506 /* Check the cycle count is still incrementing to ensure the task is still
\r
507 actually running. */
\r
508 if( ulLastNotifyCycleCount == ulNotifyCycleCount )
\r
510 xErrorStatus = pdFAIL;
\r
514 ulLastNotifyCycleCount = ulNotifyCycleCount;
\r
517 /* Check the count of 'takes' from the software timer is keeping track with
\r
518 the amount of 'gives'. */
\r
519 if( ulTimerNotificationsSent > ulTimerNotificationsSent )
\r
521 if( ( ulTimerNotificationsSent - ulTimerNotificationsReceived ) > ulMaxSendReceiveDeviation )
\r
523 xErrorStatus = pdFAIL;
\r
527 return xErrorStatus;
\r
529 /*-----------------------------------------------------------*/
\r
531 static UBaseType_t prvRand( void )
\r
533 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
\r
535 /* Utility function to generate a pseudo random number. */
\r
536 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
\r
537 return( ( int ) ( ulNextRand >> 16UL ) & 0x7fffUL );
\r
539 /*-----------------------------------------------------------*/
\r