2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
31 * Demonstrates how to create FreeRTOS objects using pre-allocated memory,
\r
32 * rather than the normal dynamically allocated memory, and tests objects being
\r
33 * created and deleted with both statically allocated memory and dynamically
\r
36 * See http://www.FreeRTOS.org/Static_Vs_Dynamic_Memory_Allocation.html
\r
39 /* Scheduler include files. */
\r
40 #include "FreeRTOS.h"
\r
44 #include "event_groups.h"
\r
47 /* Demo program include files. */
\r
48 #include "StaticAllocation.h"
\r
50 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */
\r
51 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
53 /* The priority at which the task that performs the tests is created. */
\r
54 #define staticTASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
56 /* The length of the queue, in items, not bytes, used in the queue static
\r
57 allocation tests. */
\r
58 #define staticQUEUE_LENGTH_IN_ITEMS ( 5 )
\r
60 /* A block time of 0 simply means "don't block". */
\r
61 #define staticDONT_BLOCK ( ( TickType_t ) 0 )
\r
63 /* Binary semaphores have a maximum count of 1. */
\r
64 #define staticBINARY_SEMAPHORE_MAX_COUNT ( 1 )
\r
66 /* The size of the stack used by the task that runs the tests. */
\r
67 #define staticCREATOR_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE * 2 )
\r
69 /* The number of times the software timer will execute before stopping itself. */
\r
70 #define staticMAX_TIMER_CALLBACK_EXECUTIONS ( 5 )
\r
73 /*-----------------------------------------------------------*/
\r
76 * The task that repeatedly creates and deletes statically allocated tasks, and
\r
77 * other RTOS objects.
\r
79 static void prvStaticallyAllocatedCreator( void *pvParameters );
\r
82 * The callback function used by the software timer that is repeatedly created
\r
83 * and deleted using both static and dynamically allocated memory.
\r
85 static void prvTimerCallback( TimerHandle_t xExpiredTimer );
\r
88 * A task that is created and deleted multiple times, using both statically and
\r
89 * dynamically allocated stack and TCB.
\r
91 static void prvStaticallyAllocatedTask( void *pvParameters );
\r
94 * A function that demonstrates and tests the API functions that create and
\r
95 * delete tasks using both statically and dynamically allocated TCBs and stacks.
\r
97 static void prvCreateAndDeleteStaticallyAllocatedTasks( void );
\r
100 * A function that demonstrates and tests the API functions that create and
\r
101 * delete event groups using both statically and dynamically allocated RAM.
\r
103 static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void );
\r
106 * A function that demonstrates and tests the API functions that create and
\r
107 * delete queues using both statically and dynamically allocated RAM.
\r
109 static void prvCreateAndDeleteStaticallyAllocatedQueues( void );
\r
112 * A function that demonstrates and tests the API functions that create and
\r
113 * delete binary semaphores using both statically and dynamically allocated RAM.
\r
115 static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void );
\r
118 * A function that demonstrates and tests the API functions that create and
\r
119 * delete software timers using both statically and dynamically allocated RAM.
\r
121 static void prvCreateAndDeleteStaticallyAllocatedTimers( void );
\r
124 * A function that demonstrates and tests the API functions that create and
\r
125 * delete mutexes using both statically and dynamically allocated RAM.
\r
127 static void prvCreateAndDeleteStaticallyAllocatedMutexes( void );
\r
130 * A function that demonstrates and tests the API functions that create and
\r
131 * delete counting semaphores using both statically and dynamically allocated
\r
134 static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void );
\r
137 * A function that demonstrates and tests the API functions that create and
\r
138 * delete recursive mutexes using both statically and dynamically allocated RAM.
\r
140 static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void );
\r
143 * Utility function to create pseudo random numbers.
\r
145 static UBaseType_t prvRand( void );
\r
148 * The task that creates and deletes other tasks has to delay occasionally to
\r
149 * ensure lower priority tasks are not starved of processing time. A pseudo
\r
150 * random delay time is used just to add a little bit of randomisation into the
\r
151 * execution pattern. prvGetNextDelayTime() generates the pseudo random delay.
\r
153 static TickType_t prvGetNextDelayTime( void );
\r
156 * Checks the basic operation of a queue after it has been created.
\r
158 static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue );
\r
161 * Checks the basic operation of a recursive mutex after it has been created.
\r
163 static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore );
\r
166 * Checks the basic operation of a binary semaphore after it has been created.
\r
168 static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount );
\r
171 * Checks the basic operation of an event group after it has been created.
\r
173 static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup );
\r
175 /*-----------------------------------------------------------*/
\r
177 /* StaticTask_t is a publicly accessible structure that has the same size and
\r
178 alignment requirements as the real TCB structure. It is provided as a mechanism
\r
179 for applications to know the size of the TCB (which is dependent on the
\r
180 architecture and configuration file settings) without breaking the strict data
\r
181 hiding policy by exposing the real TCB. This StaticTask_t variable is passed
\r
182 into the xTaskCreateStatic() function that creates the
\r
183 prvStaticallyAllocatedCreator() task, and will hold the TCB of the created
\r
185 static StaticTask_t xCreatorTaskTCBBuffer;
\r
187 /* This is the stack that will be used by the prvStaticallyAllocatedCreator()
\r
188 task, which is itself created using statically allocated buffers (so without any
\r
189 dynamic memory allocation). */
\r
190 static StackType_t uxCreatorTaskStackBuffer[ staticCREATOR_TASK_STACK_SIZE ];
\r
192 /* Used by the pseudo random number generating function. */
\r
193 static uint32_t ulNextRand = 0;
\r
195 /* Used so a check task can ensure this test is still executing, and not
\r
197 static volatile UBaseType_t uxCycleCounter = 0;
\r
199 /* A variable that gets set to pdTRUE if an error is detected. */
\r
200 static volatile BaseType_t xErrorOccurred = pdFALSE;
\r
202 /*-----------------------------------------------------------*/
\r
204 void vStartStaticallyAllocatedTasks( void )
\r
206 /* Create a single task, which then repeatedly creates and deletes the other
\r
207 RTOS objects using both statically and dynamically allocated RAM. */
\r
208 xTaskCreateStatic( prvStaticallyAllocatedCreator, /* The function that implements the task being created. */
\r
209 "StatCreate", /* Text name for the task - not used by the RTOS, its just to assist debugging. */
\r
210 staticCREATOR_TASK_STACK_SIZE, /* Size of the buffer passed in as the stack - in words, not bytes! */
\r
211 NULL, /* Parameter passed into the task - not used in this case. */
\r
212 staticTASK_PRIORITY, /* Priority of the task. */
\r
213 &( uxCreatorTaskStackBuffer[ 0 ] ), /* The buffer to use as the task's stack. */
\r
214 &xCreatorTaskTCBBuffer ); /* The variable that will hold the task's TCB. */
\r
216 /*-----------------------------------------------------------*/
\r
218 static void prvStaticallyAllocatedCreator( void *pvParameters )
\r
220 /* Avoid compiler warnings. */
\r
221 ( void ) pvParameters;
\r
225 /* Loop, running functions that create and delete the various RTOS
\r
226 objects that can be optionally created using either static or dynamic
\r
227 memory allocation. */
\r
228 prvCreateAndDeleteStaticallyAllocatedTasks();
\r
229 prvCreateAndDeleteStaticallyAllocatedQueues();
\r
231 /* Delay to ensure lower priority tasks get CPU time, and increment the
\r
232 cycle counter so a 'check' task can determine that this task is still
\r
234 vTaskDelay( prvGetNextDelayTime() );
\r
237 prvCreateAndDeleteStaticallyAllocatedBinarySemaphores();
\r
238 prvCreateAndDeleteStaticallyAllocatedCountingSemaphores();
\r
240 vTaskDelay( prvGetNextDelayTime() );
\r
243 prvCreateAndDeleteStaticallyAllocatedMutexes();
\r
244 prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes();
\r
246 vTaskDelay( prvGetNextDelayTime() );
\r
249 prvCreateAndDeleteStaticallyAllocatedEventGroups();
\r
250 prvCreateAndDeleteStaticallyAllocatedTimers();
\r
253 /*-----------------------------------------------------------*/
\r
255 static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void )
\r
257 SemaphoreHandle_t xSemaphore;
\r
258 const UBaseType_t uxMaxCount = ( UBaseType_t ) 10;
\r
260 /* StaticSemaphore_t is a publicly accessible structure that has the same size
\r
261 and alignment requirements as the real semaphore structure. It is provided as a
\r
262 mechanism for applications to know the size of the semaphore (which is dependent
\r
263 on the architecture and configuration file settings) without breaking the strict
\r
264 data hiding policy by exposing the real semaphore internals. This
\r
265 StaticSemaphore_t variable is passed into the xSemaphoreCreateCountingStatic()
\r
266 function calls within this function. NOTE: In most usage scenarios now it is
\r
267 faster and more memory efficient to use a direct to task notification instead of
\r
268 a counting semaphore. http://www.freertos.org/RTOS-task-notifications.html */
\r
269 StaticSemaphore_t xSemaphoreBuffer;
\r
271 /* Create the semaphore. xSemaphoreCreateCountingStatic() has one more
\r
272 parameter than the usual xSemaphoreCreateCounting() function. The parameter
\r
273 is a pointer to the pre-allocated StaticSemaphore_t structure, which will
\r
274 hold information on the semaphore in an anonymous way. If the pointer is
\r
275 passed as NULL then the structure will be allocated dynamically, just as
\r
276 when xSemaphoreCreateCounting() is called. */
\r
277 xSemaphore = xSemaphoreCreateCountingStatic( uxMaxCount, 0, &xSemaphoreBuffer );
\r
279 /* The semaphore handle should equal the static semaphore structure passed
\r
280 into the xSemaphoreCreateBinaryStatic() function. */
\r
281 configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );
\r
283 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */
\r
284 prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount );
\r
286 /* Delete the semaphore again so the buffers can be reused. */
\r
287 vSemaphoreDelete( xSemaphore );
\r
289 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
291 /* Now do the same but using dynamically allocated buffers to ensure the
\r
292 delete functions are working correctly in both the static and dynamic
\r
293 allocation cases. */
\r
294 xSemaphore = xSemaphoreCreateCounting( uxMaxCount, 0 );
\r
295 configASSERT( xSemaphore != NULL );
\r
296 prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount );
\r
297 vSemaphoreDelete( xSemaphore );
\r
301 /*-----------------------------------------------------------*/
\r
303 static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void )
\r
305 SemaphoreHandle_t xSemaphore;
\r
307 /* StaticSemaphore_t is a publicly accessible structure that has the same size
\r
308 and alignment requirements as the real semaphore structure. It is provided as a
\r
309 mechanism for applications to know the size of the semaphore (which is dependent
\r
310 on the architecture and configuration file settings) without breaking the strict
\r
311 data hiding policy by exposing the real semaphore internals. This
\r
312 StaticSemaphore_t variable is passed into the
\r
313 xSemaphoreCreateRecursiveMutexStatic() function calls within this function. */
\r
314 StaticSemaphore_t xSemaphoreBuffer;
\r
316 /* Create the semaphore. xSemaphoreCreateRecursiveMutexStatic() has one
\r
317 more parameter than the usual xSemaphoreCreateRecursiveMutex() function.
\r
318 The parameter is a pointer to the pre-allocated StaticSemaphore_t structure,
\r
319 which will hold information on the semaphore in an anonymous way. If the
\r
320 pointer is passed as NULL then the structure will be allocated dynamically,
\r
321 just as when xSemaphoreCreateRecursiveMutex() is called. */
\r
322 xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xSemaphoreBuffer );
\r
324 /* The semaphore handle should equal the static semaphore structure passed
\r
325 into the xSemaphoreCreateBinaryStatic() function. */
\r
326 configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );
\r
328 /* Ensure the semaphore passes a few sanity checks as a valid
\r
329 recursive semaphore. */
\r
330 prvSanityCheckCreatedRecursiveMutex( xSemaphore );
\r
332 /* Delete the semaphore again so the buffers can be reused. */
\r
333 vSemaphoreDelete( xSemaphore );
\r
335 /* Now do the same using dynamically allocated buffers to ensure the delete
\r
336 functions are working correctly in both the static and dynamic memory
\r
337 allocation cases. */
\r
338 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
340 xSemaphore = xSemaphoreCreateRecursiveMutex();
\r
341 configASSERT( xSemaphore != NULL );
\r
342 prvSanityCheckCreatedRecursiveMutex( xSemaphore );
\r
343 vSemaphoreDelete( xSemaphore );
\r
347 /*-----------------------------------------------------------*/
\r
349 static void prvCreateAndDeleteStaticallyAllocatedQueues( void )
\r
351 QueueHandle_t xQueue;
\r
353 /* StaticQueue_t is a publicly accessible structure that has the same size and
\r
354 alignment requirements as the real queue structure. It is provided as a
\r
355 mechanism for applications to know the size of the queue (which is dependent on
\r
356 the architecture and configuration file settings) without breaking the strict
\r
357 data hiding policy by exposing the real queue internals. This StaticQueue_t
\r
358 variable is passed into the xQueueCreateStatic() function calls within this
\r
360 static StaticQueue_t xStaticQueue;
\r
362 /* The queue storage area must be large enough to hold the maximum number of
\r
363 items it is possible for the queue to hold at any one time, which equals the
\r
364 queue length (in items, not bytes) multiplied by the size of each item. In this
\r
365 case the queue will hold staticQUEUE_LENGTH_IN_ITEMS 64-bit items. See
\r
366 http://www.freertos.org/Embedded-RTOS-Queues.html */
\r
367 static uint8_t ucQueueStorageArea[ staticQUEUE_LENGTH_IN_ITEMS * sizeof( uint64_t ) ];
\r
369 /* Create the queue. xQueueCreateStatic() has two more parameters than the
\r
370 usual xQueueCreate() function. The first new parameter is a pointer to the
\r
371 pre-allocated queue storage area. The second new parameter is a pointer to
\r
372 the StaticQueue_t structure that will hold the queue state information in
\r
373 an anonymous way. If the two pointers are passed as NULL then the data
\r
374 will be allocated dynamically as if xQueueCreate() had been called. */
\r
375 xQueue = xQueueCreateStatic( staticQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */
\r
376 sizeof( uint64_t ), /* The size of each item. */
\r
377 ucQueueStorageArea, /* The buffer used to hold items within the queue. */
\r
378 &xStaticQueue ); /* The static queue structure that will hold the state of the queue. */
\r
380 /* The queue handle should equal the static queue structure passed into the
\r
381 xQueueCreateStatic() function. */
\r
382 configASSERT( xQueue == ( QueueHandle_t ) &xStaticQueue );
\r
384 /* Ensure the queue passes a few sanity checks as a valid queue. */
\r
385 prvSanityCheckCreatedQueue( xQueue );
\r
387 /* Delete the queue again so the buffers can be reused. */
\r
388 vQueueDelete( xQueue );
\r
390 /* Now do the same using a dynamically allocated queue to ensure the delete
\r
391 function is working correctly in both the static and dynamic memory
\r
392 allocation cases. */
\r
393 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
395 xQueue = xQueueCreate( staticQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */
\r
396 sizeof( uint64_t ) ); /* The size of each item. */
\r
398 /* The queue handle should equal the static queue structure passed into the
\r
399 xQueueCreateStatic() function. */
\r
400 configASSERT( xQueue != NULL );
\r
402 /* Ensure the queue passes a few sanity checks as a valid queue. */
\r
403 prvSanityCheckCreatedQueue( xQueue );
\r
405 /* Delete the queue again so the buffers can be reused. */
\r
406 vQueueDelete( xQueue );
\r
410 /*-----------------------------------------------------------*/
\r
412 static void prvCreateAndDeleteStaticallyAllocatedMutexes( void )
\r
414 SemaphoreHandle_t xSemaphore;
\r
415 BaseType_t xReturned;
\r
417 /* StaticSemaphore_t is a publicly accessible structure that has the same size
\r
418 and alignment requirements as the real semaphore structure. It is provided as a
\r
419 mechanism for applications to know the size of the semaphore (which is dependent
\r
420 on the architecture and configuration file settings) without breaking the strict
\r
421 data hiding policy by exposing the real semaphore internals. This
\r
422 StaticSemaphore_t variable is passed into the xSemaphoreCreateMutexStatic()
\r
423 function calls within this function. */
\r
424 StaticSemaphore_t xSemaphoreBuffer;
\r
426 /* Create the semaphore. xSemaphoreCreateMutexStatic() has one more
\r
427 parameter than the usual xSemaphoreCreateMutex() function. The parameter
\r
428 is a pointer to the pre-allocated StaticSemaphore_t structure, which will
\r
429 hold information on the semaphore in an anonymous way. If the pointer is
\r
430 passed as NULL then the structure will be allocated dynamically, just as
\r
431 when xSemaphoreCreateMutex() is called. */
\r
432 xSemaphore = xSemaphoreCreateMutexStatic( &xSemaphoreBuffer );
\r
434 /* The semaphore handle should equal the static semaphore structure passed
\r
435 into the xSemaphoreCreateMutexStatic() function. */
\r
436 configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );
\r
438 /* Take the mutex so the mutex is in the state expected by the
\r
439 prvSanityCheckCreatedSemaphore() function. */
\r
440 xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );
\r
442 if( xReturned != pdPASS )
\r
444 xErrorOccurred = pdTRUE;
\r
447 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */
\r
448 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
450 /* Delete the semaphore again so the buffers can be reused. */
\r
451 vSemaphoreDelete( xSemaphore );
\r
453 /* Now do the same using a dynamically allocated mutex to ensure the delete
\r
454 function is working correctly in both the static and dynamic allocation
\r
456 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
458 xSemaphore = xSemaphoreCreateMutex();
\r
460 /* The semaphore handle should equal the static semaphore structure
\r
461 passed into the xSemaphoreCreateMutexStatic() function. */
\r
462 configASSERT( xSemaphore != NULL );
\r
464 /* Take the mutex so the mutex is in the state expected by the
\r
465 prvSanityCheckCreatedSemaphore() function. */
\r
466 xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );
\r
468 if( xReturned != pdPASS )
\r
470 xErrorOccurred = pdTRUE;
\r
473 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */
\r
474 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
476 /* Delete the semaphore again so the buffers can be reused. */
\r
477 vSemaphoreDelete( xSemaphore );
\r
481 /*-----------------------------------------------------------*/
\r
483 static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void )
\r
485 SemaphoreHandle_t xSemaphore;
\r
487 /* StaticSemaphore_t is a publicly accessible structure that has the same size
\r
488 and alignment requirements as the real semaphore structure. It is provided as a
\r
489 mechanism for applications to know the size of the semaphore (which is dependent
\r
490 on the architecture and configuration file settings) without breaking the strict
\r
491 data hiding policy by exposing the real semaphore internals. This
\r
492 StaticSemaphore_t variable is passed into the xSemaphoreCreateBinaryStatic()
\r
493 function calls within this function. NOTE: In most usage scenarios now it is
\r
494 faster and more memory efficient to use a direct to task notification instead of
\r
495 a binary semaphore. http://www.freertos.org/RTOS-task-notifications.html */
\r
496 StaticSemaphore_t xSemaphoreBuffer;
\r
498 /* Create the semaphore. xSemaphoreCreateBinaryStatic() has one more
\r
499 parameter than the usual xSemaphoreCreateBinary() function. The parameter
\r
500 is a pointer to the pre-allocated StaticSemaphore_t structure, which will
\r
501 hold information on the semaphore in an anonymous way. If the pointer is
\r
502 passed as NULL then the structure will be allocated dynamically, just as
\r
503 when xSemaphoreCreateBinary() is called. */
\r
504 xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer );
\r
506 /* The semaphore handle should equal the static semaphore structure passed
\r
507 into the xSemaphoreCreateBinaryStatic() function. */
\r
508 configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );
\r
510 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */
\r
511 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
513 /* Delete the semaphore again so the buffers can be reused. */
\r
514 vSemaphoreDelete( xSemaphore );
\r
516 /* Now do the same using a dynamically allocated semaphore to check the
\r
517 delete function is working correctly in both the static and dynamic
\r
518 allocation cases. */
\r
519 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
521 xSemaphore = xSemaphoreCreateBinary();
\r
522 configASSERT( xSemaphore != NULL );
\r
523 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
524 vSemaphoreDelete( xSemaphore );
\r
528 /* There isn't a static version of the old and deprecated
\r
529 vSemaphoreCreateBinary() macro (because its deprecated!), but check it is
\r
530 still functioning correctly. */
\r
531 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
533 vSemaphoreCreateBinary( xSemaphore );
\r
535 /* The macro starts with the binary semaphore available, but the test
\r
536 function expects it to be unavailable. */
\r
537 if( xSemaphoreTake( xSemaphore, staticDONT_BLOCK ) == pdFAIL )
\r
539 xErrorOccurred = pdTRUE;
\r
542 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
543 vSemaphoreDelete( xSemaphore );
\r
547 /*-----------------------------------------------------------*/
\r
549 static void prvTimerCallback( TimerHandle_t xExpiredTimer )
\r
551 UBaseType_t *puxVariableToIncrement;
\r
552 BaseType_t xReturned;
\r
554 /* The timer callback just demonstrates it is executing by incrementing a
\r
555 variable - the address of which is passed into the timer as its ID. Obtain
\r
556 the address of the variable to increment. */
\r
557 puxVariableToIncrement = ( UBaseType_t * ) pvTimerGetTimerID( xExpiredTimer );
\r
559 /* Increment the variable to show the timer callback has executed. */
\r
560 ( *puxVariableToIncrement )++;
\r
562 /* If this callback has executed the required number of times, stop the
\r
564 if( *puxVariableToIncrement == staticMAX_TIMER_CALLBACK_EXECUTIONS )
\r
566 /* This is called from a timer callback so must not block. See
\r
567 http://www.FreeRTOS.org/FreeRTOS-timers-xTimerStop.html */
\r
568 xReturned = xTimerStop( xExpiredTimer, staticDONT_BLOCK );
\r
570 if( xReturned != pdPASS )
\r
572 xErrorOccurred = pdTRUE;
\r
576 /*-----------------------------------------------------------*/
\r
578 static void prvCreateAndDeleteStaticallyAllocatedTimers( void )
\r
580 TimerHandle_t xTimer;
\r
581 UBaseType_t uxVariableToIncrement;
\r
582 const TickType_t xTimerPeriod = pdMS_TO_TICKS( 20 );
\r
583 BaseType_t xReturned;
\r
585 /* StaticTimer_t is a publicly accessible structure that has the same size
\r
586 and alignment requirements as the real timer structure. It is provided as a
\r
587 mechanism for applications to know the size of the timer structure (which is
\r
588 dependent on the architecture and configuration file settings) without breaking
\r
589 the strict data hiding policy by exposing the real timer internals. This
\r
590 StaticTimer_t variable is passed into the xTimerCreateStatic() function calls
\r
591 within this function. */
\r
592 StaticTimer_t xTimerBuffer;
\r
594 /* Create the software time. xTimerCreateStatic() has an extra parameter
\r
595 than the normal xTimerCreate() API function. The parameter is a pointer to
\r
596 the StaticTimer_t structure that will hold the software timer structure. If
\r
597 the parameter is passed as NULL then the structure will be allocated
\r
598 dynamically, just as if xTimerCreate() had been called. */
\r
599 xTimer = xTimerCreateStatic( "T1", /* Text name for the task. Helps debugging only. Not used by FreeRTOS. */
\r
600 xTimerPeriod, /* The period of the timer in ticks. */
\r
601 pdTRUE, /* This is an auto-reload timer. */
\r
602 ( void * ) &uxVariableToIncrement, /* The variable incremented by the test is passed into the timer callback using the timer ID. */
\r
603 prvTimerCallback, /* The function to execute when the timer expires. */
\r
604 &xTimerBuffer ); /* The buffer that will hold the software timer structure. */
\r
606 /* The timer handle should equal the static timer structure passed into the
\r
607 xTimerCreateStatic() function. */
\r
608 configASSERT( xTimer == ( TimerHandle_t ) &xTimerBuffer );
\r
610 /* Set the variable to 0, wait for a few timer periods to expire, then check
\r
611 the timer callback has incremented the variable to the expected value. */
\r
612 uxVariableToIncrement = 0;
\r
614 /* This is a low priority so a block time should not be needed. */
\r
615 xReturned = xTimerStart( xTimer, staticDONT_BLOCK );
\r
617 if( xReturned != pdPASS )
\r
619 xErrorOccurred = pdTRUE;
\r
622 vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );
\r
624 /* By now the timer should have expired staticMAX_TIMER_CALLBACK_EXECUTIONS
\r
625 times, and then stopped itself. */
\r
626 if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS )
\r
628 xErrorOccurred = pdTRUE;
\r
631 /* Finished with the timer, delete it. */
\r
632 xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );
\r
634 /* Again, as this is a low priority task it is expected that the timer
\r
635 command will have been sent even without a block time being used. */
\r
636 if( xReturned != pdPASS )
\r
638 xErrorOccurred = pdTRUE;
\r
641 /* Just to show the check task that this task is still executing. */
\r
644 /* Now do the same using a dynamically allocated software timer to ensure
\r
645 the delete function is working correctly in both the static and dynamic
\r
646 allocation cases. */
\r
647 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
649 xTimer = xTimerCreate( "T1", /* Text name for the task. Helps debugging only. Not used by FreeRTOS. */
\r
650 xTimerPeriod, /* The period of the timer in ticks. */
\r
651 pdTRUE, /* This is an auto-reload timer. */
\r
652 ( void * ) &uxVariableToIncrement, /* The variable incremented by the test is passed into the timer callback using the timer ID. */
\r
653 prvTimerCallback ); /* The function to execute when the timer expires. */
\r
655 configASSERT( xTimer != NULL );
\r
657 uxVariableToIncrement = 0;
\r
658 xReturned = xTimerStart( xTimer, staticDONT_BLOCK );
\r
660 if( xReturned != pdPASS )
\r
662 xErrorOccurred = pdTRUE;
\r
665 vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );
\r
667 if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS )
\r
669 xErrorOccurred = pdTRUE;
\r
672 xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );
\r
674 if( xReturned != pdPASS )
\r
676 xErrorOccurred = pdTRUE;
\r
681 /*-----------------------------------------------------------*/
\r
683 static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void )
\r
685 EventGroupHandle_t xEventGroup;
\r
687 /* StaticEventGroup_t is a publicly accessible structure that has the same size
\r
688 and alignment requirements as the real event group structure. It is provided as
\r
689 a mechanism for applications to know the size of the event group (which is
\r
690 dependent on the architecture and configuration file settings) without breaking
\r
691 the strict data hiding policy by exposing the real event group internals. This
\r
692 StaticEventGroup_t variable is passed into the xSemaphoreCreateEventGroupStatic()
\r
693 function calls within this function. */
\r
694 StaticEventGroup_t xEventGroupBuffer;
\r
696 /* Create the event group. xEventGroupCreateStatic() has an extra parameter
\r
697 than the normal xEventGroupCreate() API function. The parameter is a
\r
698 pointer to the StaticEventGroup_t structure that will hold the event group
\r
699 structure. If the parameter is passed as NULL then the structure will be
\r
700 allocated dynamically, just as if xEventGroupCreate() had been called. */
\r
701 xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );
\r
703 /* The event group handle should equal the static event group structure
\r
704 passed into the xEventGroupCreateStatic() function. */
\r
705 configASSERT( xEventGroup == ( EventGroupHandle_t ) &xEventGroupBuffer );
\r
707 /* Ensure the event group passes a few sanity checks as a valid event
\r
709 prvSanityCheckCreatedEventGroup( xEventGroup );
\r
711 /* Delete the event group again so the buffers can be reused. */
\r
712 vEventGroupDelete( xEventGroup );
\r
714 /* Now do the same using a dynamically allocated event group to ensure the
\r
715 delete function is working correctly in both the static and dynamic
\r
716 allocation cases. */
\r
717 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
719 xEventGroup = xEventGroupCreate();
\r
720 configASSERT( xEventGroup != NULL );
\r
721 prvSanityCheckCreatedEventGroup( xEventGroup );
\r
722 vEventGroupDelete( xEventGroup );
\r
726 /*-----------------------------------------------------------*/
\r
728 static void prvCreateAndDeleteStaticallyAllocatedTasks( void )
\r
730 TaskHandle_t xCreatedTask;
\r
732 /* The variable that will hold the TCB of tasks created by this function. See
\r
733 the comments above the declaration of the xCreatorTaskTCBBuffer variable for
\r
734 more information. */
\r
735 StaticTask_t xTCBBuffer;
\r
737 /* This buffer that will be used as the stack of tasks created by this function.
\r
738 See the comments above the declaration of the uxCreatorTaskStackBuffer[] array
\r
739 above for more information. */
\r
740 static StackType_t uxStackBuffer[ configMINIMAL_STACK_SIZE ];
\r
742 /* Create the task. xTaskCreateStatic() has two more parameters than
\r
743 the usual xTaskCreate() function. The first new parameter is a pointer to
\r
744 the pre-allocated stack. The second new parameter is a pointer to the
\r
745 StaticTask_t structure that will hold the task's TCB. If both pointers are
\r
746 passed as NULL then the respective object will be allocated dynamically as
\r
747 if xTaskCreate() had been called. */
\r
748 xCreatedTask = xTaskCreateStatic(
\r
749 prvStaticallyAllocatedTask, /* Function that implements the task. */
\r
750 "Static", /* Human readable name for the task. */
\r
751 configMINIMAL_STACK_SIZE, /* Task's stack size, in words (not bytes!). */
\r
752 NULL, /* Parameter to pass into the task. */
\r
753 uxTaskPriorityGet( NULL ) + 1, /* The priority of the task. */
\r
754 &( uxStackBuffer[ 0 ] ), /* The buffer to use as the task's stack. */
\r
755 &xTCBBuffer ); /* The variable that will hold that task's TCB. */
\r
757 /* Check the task was created correctly, then delete the task. */
\r
758 if( xCreatedTask == NULL )
\r
760 xErrorOccurred = pdTRUE;
\r
762 else if( eTaskGetState( xCreatedTask ) != eSuspended )
\r
764 /* The created task had a higher priority so should have executed and
\r
765 suspended itself by now. */
\r
766 xErrorOccurred = pdTRUE;
\r
770 vTaskDelete( xCreatedTask );
\r
773 /* Now do the same using a dynamically allocated task to ensure the delete
\r
774 function is working correctly in both the static and dynamic allocation
\r
776 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
778 BaseType_t xReturned;
\r
780 xReturned = xTaskCreate(
\r
781 prvStaticallyAllocatedTask, /* Function that implements the task - the same function is used but is actually dynamically allocated this time. */
\r
782 "Static", /* Human readable name for the task. */
\r
783 configMINIMAL_STACK_SIZE, /* Task's stack size, in words (not bytes!). */
\r
784 NULL, /* Parameter to pass into the task. */
\r
785 uxTaskPriorityGet( NULL ) + 1, /* The priority of the task. */
\r
786 &xCreatedTask ); /* Handle of the task being created. */
\r
788 if( eTaskGetState( xCreatedTask ) != eSuspended )
\r
790 xErrorOccurred = pdTRUE;
\r
793 configASSERT( xReturned == pdPASS );
\r
794 if( xReturned != pdPASS )
\r
796 xErrorOccurred = pdTRUE;
\r
798 vTaskDelete( xCreatedTask );
\r
802 /*-----------------------------------------------------------*/
\r
804 static void prvStaticallyAllocatedTask( void *pvParameters )
\r
806 ( void ) pvParameters;
\r
808 /* The created task just suspends itself to wait to get deleted. The task
\r
809 that creates this task checks this task is in the expected Suspended state
\r
810 before deleting it. */
\r
811 vTaskSuspend( NULL );
\r
813 /*-----------------------------------------------------------*/
\r
815 static UBaseType_t prvRand( void )
\r
817 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
\r
819 /* Utility function to generate a pseudo random number. */
\r
820 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
\r
821 return( ( ulNextRand >> 16UL ) & 0x7fffUL );
\r
823 /*-----------------------------------------------------------*/
\r
825 static TickType_t prvGetNextDelayTime( void )
\r
827 TickType_t xNextDelay;
\r
828 const TickType_t xMaxDelay = pdMS_TO_TICKS( ( TickType_t ) 150 );
\r
829 const TickType_t xMinDelay = pdMS_TO_TICKS( ( TickType_t ) 75 );
\r
830 const TickType_t xTinyDelay = pdMS_TO_TICKS( ( TickType_t ) 2 );
\r
832 /* Generate the next delay time. This is kept within a narrow band so as
\r
833 not to disturb the timing of other tests - but does add in some pseudo
\r
834 randomisation into the tests. */
\r
837 xNextDelay = prvRand() % xMaxDelay;
\r
839 /* Just in case this loop is executed lots of times. */
\r
840 vTaskDelay( xTinyDelay );
\r
842 } while ( xNextDelay < xMinDelay );
\r
846 /*-----------------------------------------------------------*/
\r
848 static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup )
\r
850 EventBits_t xEventBits;
\r
851 const EventBits_t xFirstTestBits = ( EventBits_t ) 0xaa, xSecondTestBits = ( EventBits_t ) 0x55;
\r
853 /* The event group should not have any bits set yet. */
\r
854 xEventBits = xEventGroupGetBits( xEventGroup );
\r
856 if( xEventBits != ( EventBits_t ) 0 )
\r
858 xErrorOccurred = pdTRUE;
\r
861 /* Some some bits, then read them back to check they are as expected. */
\r
862 xEventGroupSetBits( xEventGroup, xFirstTestBits );
\r
864 xEventBits = xEventGroupGetBits( xEventGroup );
\r
866 if( xEventBits != xFirstTestBits )
\r
868 xErrorOccurred = pdTRUE;
\r
871 xEventGroupSetBits( xEventGroup, xSecondTestBits );
\r
873 xEventBits = xEventGroupGetBits( xEventGroup );
\r
875 if( xEventBits != ( xFirstTestBits | xSecondTestBits ) )
\r
877 xErrorOccurred = pdTRUE;
\r
880 /* Finally try clearing some bits too and check that operation proceeds as
\r
882 xEventGroupClearBits( xEventGroup, xFirstTestBits );
\r
884 xEventBits = xEventGroupGetBits( xEventGroup );
\r
886 if( xEventBits != xSecondTestBits )
\r
888 xErrorOccurred = pdTRUE;
\r
891 /*-----------------------------------------------------------*/
\r
893 static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount )
\r
895 BaseType_t xReturned;
\r
897 const TickType_t xShortBlockTime = pdMS_TO_TICKS( 10 );
\r
898 TickType_t xTickCount;
\r
900 /* The binary semaphore should start 'empty', so a call to xSemaphoreTake()
\r
902 xTickCount = xTaskGetTickCount();
\r
903 xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );
\r
905 if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime )
\r
907 /* Did not block on the semaphore as long as expected. */
\r
908 xErrorOccurred = pdTRUE;
\r
911 if( xReturned != pdFAIL )
\r
913 xErrorOccurred = pdTRUE;
\r
916 /* Should be possible to 'give' the semaphore up to a maximum of uxMaxCount
\r
918 for( x = 0; x < uxMaxCount; x++ )
\r
920 xReturned = xSemaphoreGive( xSemaphore );
\r
922 if( xReturned == pdFAIL )
\r
924 xErrorOccurred = pdTRUE;
\r
928 /* Giving the semaphore again should fail, as it is 'full'. */
\r
929 xReturned = xSemaphoreGive( xSemaphore );
\r
931 if( xReturned != pdFAIL )
\r
933 xErrorOccurred = pdTRUE;
\r
936 configASSERT( uxSemaphoreGetCount( xSemaphore ) == uxMaxCount );
\r
938 /* Should now be possible to 'take' the semaphore up to a maximum of
\r
939 uxMaxCount times without blocking. */
\r
940 for( x = 0; x < uxMaxCount; x++ )
\r
942 xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );
\r
944 if( xReturned == pdFAIL )
\r
946 xErrorOccurred = pdTRUE;
\r
950 /* Back to the starting condition, where the semaphore should not be
\r
952 xTickCount = xTaskGetTickCount();
\r
953 xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );
\r
955 if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime )
\r
957 /* Did not block on the semaphore as long as expected. */
\r
958 xErrorOccurred = pdTRUE;
\r
961 if( xReturned != pdFAIL )
\r
963 xErrorOccurred = pdTRUE;
\r
966 configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );
\r
968 /*-----------------------------------------------------------*/
\r
970 static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue )
\r
972 uint64_t ull, ullRead;
\r
973 BaseType_t xReturned, xLoop;
\r
975 /* This test is done twice to ensure the queue storage area wraps. */
\r
976 for( xLoop = 0; xLoop < 2; xLoop++ )
\r
978 /* A very basic test that the queue can be written to and read from as
\r
979 expected. First the queue should be empty. */
\r
980 xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );
\r
981 if( xReturned != errQUEUE_EMPTY )
\r
983 xErrorOccurred = pdTRUE;
\r
986 /* Now it should be possible to write to the queue staticQUEUE_LENGTH_IN_ITEMS
\r
988 for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ )
\r
990 xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );
\r
991 if( xReturned != pdPASS )
\r
993 xErrorOccurred = pdTRUE;
\r
997 /* Should not now be possible to write to the queue again. */
\r
998 xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );
\r
999 if( xReturned != errQUEUE_FULL )
\r
1001 xErrorOccurred = pdTRUE;
\r
1004 /* Now read back from the queue to ensure the data read back matches that
\r
1006 for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ )
\r
1008 xReturned = xQueueReceive( xQueue, &ullRead, staticDONT_BLOCK );
\r
1010 if( xReturned != pdPASS )
\r
1012 xErrorOccurred = pdTRUE;
\r
1015 if( ullRead != ull )
\r
1017 xErrorOccurred = pdTRUE;
\r
1021 /* The queue should be empty again. */
\r
1022 xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );
\r
1023 if( xReturned != errQUEUE_EMPTY )
\r
1025 xErrorOccurred = pdTRUE;
\r
1029 /*-----------------------------------------------------------*/
\r
1031 static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore )
\r
1033 const BaseType_t xLoops = 5;
\r
1034 BaseType_t x, xReturned;
\r
1036 /* A very basic test that the recursive semaphore behaved like a recursive
\r
1037 semaphore. First the semaphore should not be able to be given, as it has not
\r
1038 yet been taken. */
\r
1039 xReturned = xSemaphoreGiveRecursive( xSemaphore );
\r
1041 if( xReturned != pdFAIL )
\r
1043 xErrorOccurred = pdTRUE;
\r
1046 /* Now it should be possible to take the mutex a number of times. */
\r
1047 for( x = 0; x < xLoops; x++ )
\r
1049 xReturned = xSemaphoreTakeRecursive( xSemaphore, staticDONT_BLOCK );
\r
1051 if( xReturned != pdPASS )
\r
1053 xErrorOccurred = pdTRUE;
\r
1057 /* Should be possible to give the semaphore the same number of times as it
\r
1058 was given in the loop above. */
\r
1059 for( x = 0; x < xLoops; x++ )
\r
1061 xReturned = xSemaphoreGiveRecursive( xSemaphore );
\r
1063 if( xReturned != pdPASS )
\r
1065 xErrorOccurred = pdTRUE;
\r
1069 /* No more gives should be possible though. */
\r
1070 xReturned = xSemaphoreGiveRecursive( xSemaphore );
\r
1072 if( xReturned != pdFAIL )
\r
1074 xErrorOccurred = pdTRUE;
\r
1077 /*-----------------------------------------------------------*/
\r
1079 BaseType_t xAreStaticAllocationTasksStillRunning( void )
\r
1081 static UBaseType_t uxLastCycleCounter = 0;
\r
1082 BaseType_t xReturn;
\r
1084 if( uxCycleCounter == uxLastCycleCounter )
\r
1086 xErrorOccurred = pdTRUE;
\r
1090 uxLastCycleCounter = uxCycleCounter;
\r
1093 if( xErrorOccurred != pdFALSE )
\r
1104 /*-----------------------------------------------------------*/
\r
1106 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */
\r
1107 #endif /* configSUPPORT_STATIC_ALLOCATION == 1 */
\r