2 * FreeRTOS Kernel V10.0.1
\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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
30 * Demonstrates how to create FreeRTOS objects using pre-allocated memory,
\r
31 * rather than the normal dynamically allocated memory, and tests objects being
\r
32 * created and deleted with both statically allocated memory and dynamically
\r
35 * See http://www.FreeRTOS.org/Static_Vs_Dynamic_Memory_Allocation.html
\r
38 /* Scheduler include files. */
\r
39 #include "FreeRTOS.h"
\r
43 #include "event_groups.h"
\r
46 /* Demo program include files. */
\r
47 #include "StaticAllocation.h"
\r
49 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */
\r
50 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
52 /* The priority at which the task that performs the tests is created. */
\r
53 #define staticTASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
55 /* The length of the queue, in items, not bytes, used in the queue static
\r
56 allocation tests. */
\r
57 #define staticQUEUE_LENGTH_IN_ITEMS ( 5 )
\r
59 /* A block time of 0 simply means "don't block". */
\r
60 #define staticDONT_BLOCK ( ( TickType_t ) 0 )
\r
62 /* Binary semaphores have a maximum count of 1. */
\r
63 #define staticBINARY_SEMAPHORE_MAX_COUNT ( 1 )
\r
65 /* The size of the stack used by the task that runs the tests. */
\r
66 #define staticCREATOR_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE * 2 )
\r
68 /* The number of times the software timer will execute before stopping itself. */
\r
69 #define staticMAX_TIMER_CALLBACK_EXECUTIONS ( 5 )
\r
72 /*-----------------------------------------------------------*/
\r
75 * The task that repeatedly creates and deletes statically allocated tasks, and
\r
76 * other RTOS objects.
\r
78 static void prvStaticallyAllocatedCreator( void *pvParameters );
\r
81 * The callback function used by the software timer that is repeatedly created
\r
82 * and deleted using both static and dynamically allocated memory.
\r
84 static void prvTimerCallback( TimerHandle_t xExpiredTimer );
\r
87 * A task that is created and deleted multiple times, using both statically and
\r
88 * dynamically allocated stack and TCB.
\r
90 static void prvStaticallyAllocatedTask( void *pvParameters );
\r
93 * A function that demonstrates and tests the API functions that create and
\r
94 * delete tasks using both statically and dynamically allocated TCBs and stacks.
\r
96 static void prvCreateAndDeleteStaticallyAllocatedTasks( void );
\r
99 * A function that demonstrates and tests the API functions that create and
\r
100 * delete event groups using both statically and dynamically allocated RAM.
\r
102 static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void );
\r
105 * A function that demonstrates and tests the API functions that create and
\r
106 * delete queues using both statically and dynamically allocated RAM.
\r
108 static void prvCreateAndDeleteStaticallyAllocatedQueues( void );
\r
111 * A function that demonstrates and tests the API functions that create and
\r
112 * delete binary semaphores using both statically and dynamically allocated RAM.
\r
114 static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void );
\r
117 * A function that demonstrates and tests the API functions that create and
\r
118 * delete software timers using both statically and dynamically allocated RAM.
\r
120 static void prvCreateAndDeleteStaticallyAllocatedTimers( void );
\r
123 * A function that demonstrates and tests the API functions that create and
\r
124 * delete mutexes using both statically and dynamically allocated RAM.
\r
126 static void prvCreateAndDeleteStaticallyAllocatedMutexes( void );
\r
129 * A function that demonstrates and tests the API functions that create and
\r
130 * delete counting semaphores using both statically and dynamically allocated
\r
133 static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void );
\r
136 * A function that demonstrates and tests the API functions that create and
\r
137 * delete recursive mutexes using both statically and dynamically allocated RAM.
\r
139 static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void );
\r
142 * Utility function to create pseudo random numbers.
\r
144 static UBaseType_t prvRand( void );
\r
147 * The task that creates and deletes other tasks has to delay occasionally to
\r
148 * ensure lower priority tasks are not starved of processing time. A pseudo
\r
149 * random delay time is used just to add a little bit of randomisation into the
\r
150 * execution pattern. prvGetNextDelayTime() generates the pseudo random delay.
\r
152 static TickType_t prvGetNextDelayTime( void );
\r
155 * Checks the basic operation of a queue after it has been created.
\r
157 static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue );
\r
160 * Checks the basic operation of a recursive mutex after it has been created.
\r
162 static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore );
\r
165 * Checks the basic operation of a binary semaphore after it has been created.
\r
167 static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount );
\r
170 * Checks the basic operation of an event group after it has been created.
\r
172 static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup );
\r
174 /*-----------------------------------------------------------*/
\r
176 /* StaticTask_t is a publicly accessible structure that has the same size and
\r
177 alignment requirements as the real TCB structure. It is provided as a mechanism
\r
178 for applications to know the size of the TCB (which is dependent on the
\r
179 architecture and configuration file settings) without breaking the strict data
\r
180 hiding policy by exposing the real TCB. This StaticTask_t variable is passed
\r
181 into the xTaskCreateStatic() function that creates the
\r
182 prvStaticallyAllocatedCreator() task, and will hold the TCB of the created
\r
184 static StaticTask_t xCreatorTaskTCBBuffer;
\r
186 /* This is the stack that will be used by the prvStaticallyAllocatedCreator()
\r
187 task, which is itself created using statically allocated buffers (so without any
\r
188 dynamic memory allocation). */
\r
189 static StackType_t uxCreatorTaskStackBuffer[ staticCREATOR_TASK_STACK_SIZE ];
\r
191 /* Used by the pseudo random number generating function. */
\r
192 static uint32_t ulNextRand = 0;
\r
194 /* Used so a check task can ensure this test is still executing, and not
\r
196 static volatile UBaseType_t uxCycleCounter = 0;
\r
198 /* A variable that gets set to pdTRUE if an error is detected. */
\r
199 static volatile BaseType_t xErrorOccurred = pdFALSE;
\r
201 /*-----------------------------------------------------------*/
\r
203 void vStartStaticallyAllocatedTasks( void )
\r
205 /* Create a single task, which then repeatedly creates and deletes the other
\r
206 RTOS objects using both statically and dynamically allocated RAM. */
\r
207 xTaskCreateStatic( prvStaticallyAllocatedCreator, /* The function that implements the task being created. */
\r
208 "StatCreate", /* Text name for the task - not used by the RTOS, its just to assist debugging. */
\r
209 staticCREATOR_TASK_STACK_SIZE, /* Size of the buffer passed in as the stack - in words, not bytes! */
\r
210 NULL, /* Parameter passed into the task - not used in this case. */
\r
211 staticTASK_PRIORITY, /* Priority of the task. */
\r
212 &( uxCreatorTaskStackBuffer[ 0 ] ), /* The buffer to use as the task's stack. */
\r
213 &xCreatorTaskTCBBuffer ); /* The variable that will hold the task's TCB. */
\r
215 /*-----------------------------------------------------------*/
\r
217 static void prvStaticallyAllocatedCreator( void *pvParameters )
\r
219 /* Avoid compiler warnings. */
\r
220 ( void ) pvParameters;
\r
224 /* Loop, running functions that create and delete the various RTOS
\r
225 objects that can be optionally created using either static or dynamic
\r
226 memory allocation. */
\r
227 prvCreateAndDeleteStaticallyAllocatedTasks();
\r
228 prvCreateAndDeleteStaticallyAllocatedQueues();
\r
230 /* Delay to ensure lower priority tasks get CPU time, and increment the
\r
231 cycle counter so a 'check' task can determine that this task is still
\r
233 vTaskDelay( prvGetNextDelayTime() );
\r
236 prvCreateAndDeleteStaticallyAllocatedBinarySemaphores();
\r
237 prvCreateAndDeleteStaticallyAllocatedCountingSemaphores();
\r
239 vTaskDelay( prvGetNextDelayTime() );
\r
242 prvCreateAndDeleteStaticallyAllocatedMutexes();
\r
243 prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes();
\r
245 vTaskDelay( prvGetNextDelayTime() );
\r
248 prvCreateAndDeleteStaticallyAllocatedEventGroups();
\r
249 prvCreateAndDeleteStaticallyAllocatedTimers();
\r
252 /*-----------------------------------------------------------*/
\r
254 static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void )
\r
256 SemaphoreHandle_t xSemaphore;
\r
257 const UBaseType_t uxMaxCount = ( UBaseType_t ) 10;
\r
259 /* StaticSemaphore_t is a publicly accessible structure that has the same size
\r
260 and alignment requirements as the real semaphore structure. It is provided as a
\r
261 mechanism for applications to know the size of the semaphore (which is dependent
\r
262 on the architecture and configuration file settings) without breaking the strict
\r
263 data hiding policy by exposing the real semaphore internals. This
\r
264 StaticSemaphore_t variable is passed into the xSemaphoreCreateCountingStatic()
\r
265 function calls within this function. NOTE: In most usage scenarios now it is
\r
266 faster and more memory efficient to use a direct to task notification instead of
\r
267 a counting semaphore. http://www.freertos.org/RTOS-task-notifications.html */
\r
268 StaticSemaphore_t xSemaphoreBuffer;
\r
270 /* Create the semaphore. xSemaphoreCreateCountingStatic() has one more
\r
271 parameter than the usual xSemaphoreCreateCounting() function. The parameter
\r
272 is a pointer to the pre-allocated StaticSemaphore_t structure, which will
\r
273 hold information on the semaphore in an anonymous way. If the pointer is
\r
274 passed as NULL then the structure will be allocated dynamically, just as
\r
275 when xSemaphoreCreateCounting() is called. */
\r
276 xSemaphore = xSemaphoreCreateCountingStatic( uxMaxCount, 0, &xSemaphoreBuffer );
\r
278 /* The semaphore handle should equal the static semaphore structure passed
\r
279 into the xSemaphoreCreateBinaryStatic() function. */
\r
280 configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );
\r
282 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */
\r
283 prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount );
\r
285 /* Delete the semaphore again so the buffers can be reused. */
\r
286 vSemaphoreDelete( xSemaphore );
\r
288 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
290 /* Now do the same but using dynamically allocated buffers to ensure the
\r
291 delete functions are working correctly in both the static and dynamic
\r
292 allocation cases. */
\r
293 xSemaphore = xSemaphoreCreateCounting( uxMaxCount, 0 );
\r
294 configASSERT( xSemaphore != NULL );
\r
295 prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount );
\r
296 vSemaphoreDelete( xSemaphore );
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void )
\r
304 SemaphoreHandle_t xSemaphore;
\r
306 /* StaticSemaphore_t is a publicly accessible structure that has the same size
\r
307 and alignment requirements as the real semaphore structure. It is provided as a
\r
308 mechanism for applications to know the size of the semaphore (which is dependent
\r
309 on the architecture and configuration file settings) without breaking the strict
\r
310 data hiding policy by exposing the real semaphore internals. This
\r
311 StaticSemaphore_t variable is passed into the
\r
312 xSemaphoreCreateRecursiveMutexStatic() function calls within this function. */
\r
313 StaticSemaphore_t xSemaphoreBuffer;
\r
315 /* Create the semaphore. xSemaphoreCreateRecursiveMutexStatic() has one
\r
316 more parameter than the usual xSemaphoreCreateRecursiveMutex() function.
\r
317 The parameter is a pointer to the pre-allocated StaticSemaphore_t structure,
\r
318 which will hold information on the semaphore in an anonymous way. If the
\r
319 pointer is passed as NULL then the structure will be allocated dynamically,
\r
320 just as when xSemaphoreCreateRecursiveMutex() is called. */
\r
321 xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xSemaphoreBuffer );
\r
323 /* The semaphore handle should equal the static semaphore structure passed
\r
324 into the xSemaphoreCreateBinaryStatic() function. */
\r
325 configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );
\r
327 /* Ensure the semaphore passes a few sanity checks as a valid
\r
328 recursive semaphore. */
\r
329 prvSanityCheckCreatedRecursiveMutex( xSemaphore );
\r
331 /* Delete the semaphore again so the buffers can be reused. */
\r
332 vSemaphoreDelete( xSemaphore );
\r
334 /* Now do the same using dynamically allocated buffers to ensure the delete
\r
335 functions are working correctly in both the static and dynamic memory
\r
336 allocation cases. */
\r
337 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
339 xSemaphore = xSemaphoreCreateRecursiveMutex();
\r
340 configASSERT( xSemaphore != NULL );
\r
341 prvSanityCheckCreatedRecursiveMutex( xSemaphore );
\r
342 vSemaphoreDelete( xSemaphore );
\r
346 /*-----------------------------------------------------------*/
\r
348 static void prvCreateAndDeleteStaticallyAllocatedQueues( void )
\r
350 QueueHandle_t xQueue;
\r
352 /* StaticQueue_t is a publicly accessible structure that has the same size and
\r
353 alignment requirements as the real queue structure. It is provided as a
\r
354 mechanism for applications to know the size of the queue (which is dependent on
\r
355 the architecture and configuration file settings) without breaking the strict
\r
356 data hiding policy by exposing the real queue internals. This StaticQueue_t
\r
357 variable is passed into the xQueueCreateStatic() function calls within this
\r
359 static StaticQueue_t xStaticQueue;
\r
361 /* The queue storage area must be large enough to hold the maximum number of
\r
362 items it is possible for the queue to hold at any one time, which equals the
\r
363 queue length (in items, not bytes) multiplied by the size of each item. In this
\r
364 case the queue will hold staticQUEUE_LENGTH_IN_ITEMS 64-bit items. See
\r
365 http://www.freertos.org/Embedded-RTOS-Queues.html */
\r
366 static uint8_t ucQueueStorageArea[ staticQUEUE_LENGTH_IN_ITEMS * sizeof( uint64_t ) ];
\r
368 /* Create the queue. xQueueCreateStatic() has two more parameters than the
\r
369 usual xQueueCreate() function. The first new parameter is a pointer to the
\r
370 pre-allocated queue storage area. The second new parameter is a pointer to
\r
371 the StaticQueue_t structure that will hold the queue state information in
\r
372 an anonymous way. If the two pointers are passed as NULL then the data
\r
373 will be allocated dynamically as if xQueueCreate() had been called. */
\r
374 xQueue = xQueueCreateStatic( staticQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */
\r
375 sizeof( uint64_t ), /* The size of each item. */
\r
376 ucQueueStorageArea, /* The buffer used to hold items within the queue. */
\r
377 &xStaticQueue ); /* The static queue structure that will hold the state of the queue. */
\r
379 /* The queue handle should equal the static queue structure passed into the
\r
380 xQueueCreateStatic() function. */
\r
381 configASSERT( xQueue == ( QueueHandle_t ) &xStaticQueue );
\r
383 /* Ensure the queue passes a few sanity checks as a valid queue. */
\r
384 prvSanityCheckCreatedQueue( xQueue );
\r
386 /* Delete the queue again so the buffers can be reused. */
\r
387 vQueueDelete( xQueue );
\r
389 /* Now do the same using a dynamically allocated queue to ensure the delete
\r
390 function is working correctly in both the static and dynamic memory
\r
391 allocation cases. */
\r
392 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
394 xQueue = xQueueCreate( staticQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */
\r
395 sizeof( uint64_t ) ); /* The size of each item. */
\r
397 /* The queue handle should equal the static queue structure passed into the
\r
398 xQueueCreateStatic() function. */
\r
399 configASSERT( xQueue != NULL );
\r
401 /* Ensure the queue passes a few sanity checks as a valid queue. */
\r
402 prvSanityCheckCreatedQueue( xQueue );
\r
404 /* Delete the queue again so the buffers can be reused. */
\r
405 vQueueDelete( xQueue );
\r
409 /*-----------------------------------------------------------*/
\r
411 static void prvCreateAndDeleteStaticallyAllocatedMutexes( void )
\r
413 SemaphoreHandle_t xSemaphore;
\r
414 BaseType_t xReturned;
\r
416 /* StaticSemaphore_t is a publicly accessible structure that has the same size
\r
417 and alignment requirements as the real semaphore structure. It is provided as a
\r
418 mechanism for applications to know the size of the semaphore (which is dependent
\r
419 on the architecture and configuration file settings) without breaking the strict
\r
420 data hiding policy by exposing the real semaphore internals. This
\r
421 StaticSemaphore_t variable is passed into the xSemaphoreCreateMutexStatic()
\r
422 function calls within this function. */
\r
423 StaticSemaphore_t xSemaphoreBuffer;
\r
425 /* Create the semaphore. xSemaphoreCreateMutexStatic() has one more
\r
426 parameter than the usual xSemaphoreCreateMutex() function. The parameter
\r
427 is a pointer to the pre-allocated StaticSemaphore_t structure, which will
\r
428 hold information on the semaphore in an anonymous way. If the pointer is
\r
429 passed as NULL then the structure will be allocated dynamically, just as
\r
430 when xSemaphoreCreateMutex() is called. */
\r
431 xSemaphore = xSemaphoreCreateMutexStatic( &xSemaphoreBuffer );
\r
433 /* The semaphore handle should equal the static semaphore structure passed
\r
434 into the xSemaphoreCreateMutexStatic() function. */
\r
435 configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );
\r
437 /* Take the mutex so the mutex is in the state expected by the
\r
438 prvSanityCheckCreatedSemaphore() function. */
\r
439 xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );
\r
441 if( xReturned != pdPASS )
\r
443 xErrorOccurred = pdTRUE;
\r
446 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */
\r
447 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
449 /* Delete the semaphore again so the buffers can be reused. */
\r
450 vSemaphoreDelete( xSemaphore );
\r
452 /* Now do the same using a dynamically allocated mutex to ensure the delete
\r
453 function is working correctly in both the static and dynamic allocation
\r
455 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
457 xSemaphore = xSemaphoreCreateMutex();
\r
459 /* The semaphore handle should equal the static semaphore structure
\r
460 passed into the xSemaphoreCreateMutexStatic() function. */
\r
461 configASSERT( xSemaphore != NULL );
\r
463 /* Take the mutex so the mutex is in the state expected by the
\r
464 prvSanityCheckCreatedSemaphore() function. */
\r
465 xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );
\r
467 if( xReturned != pdPASS )
\r
469 xErrorOccurred = pdTRUE;
\r
472 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */
\r
473 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
475 /* Delete the semaphore again so the buffers can be reused. */
\r
476 vSemaphoreDelete( xSemaphore );
\r
480 /*-----------------------------------------------------------*/
\r
482 static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void )
\r
484 SemaphoreHandle_t xSemaphore;
\r
486 /* StaticSemaphore_t is a publicly accessible structure that has the same size
\r
487 and alignment requirements as the real semaphore structure. It is provided as a
\r
488 mechanism for applications to know the size of the semaphore (which is dependent
\r
489 on the architecture and configuration file settings) without breaking the strict
\r
490 data hiding policy by exposing the real semaphore internals. This
\r
491 StaticSemaphore_t variable is passed into the xSemaphoreCreateBinaryStatic()
\r
492 function calls within this function. NOTE: In most usage scenarios now it is
\r
493 faster and more memory efficient to use a direct to task notification instead of
\r
494 a binary semaphore. http://www.freertos.org/RTOS-task-notifications.html */
\r
495 StaticSemaphore_t xSemaphoreBuffer;
\r
497 /* Create the semaphore. xSemaphoreCreateBinaryStatic() has one more
\r
498 parameter than the usual xSemaphoreCreateBinary() function. The parameter
\r
499 is a pointer to the pre-allocated StaticSemaphore_t structure, which will
\r
500 hold information on the semaphore in an anonymous way. If the pointer is
\r
501 passed as NULL then the structure will be allocated dynamically, just as
\r
502 when xSemaphoreCreateBinary() is called. */
\r
503 xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer );
\r
505 /* The semaphore handle should equal the static semaphore structure passed
\r
506 into the xSemaphoreCreateBinaryStatic() function. */
\r
507 configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );
\r
509 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */
\r
510 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
512 /* Delete the semaphore again so the buffers can be reused. */
\r
513 vSemaphoreDelete( xSemaphore );
\r
515 /* Now do the same using a dynamically allocated semaphore to check the
\r
516 delete function is working correctly in both the static and dynamic
\r
517 allocation cases. */
\r
518 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
520 xSemaphore = xSemaphoreCreateBinary();
\r
521 configASSERT( xSemaphore != NULL );
\r
522 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
523 vSemaphoreDelete( xSemaphore );
\r
527 /* There isn't a static version of the old and deprecated
\r
528 vSemaphoreCreateBinary() macro (because its deprecated!), but check it is
\r
529 still functioning correctly. */
\r
530 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
532 vSemaphoreCreateBinary( xSemaphore );
\r
534 /* The macro starts with the binary semaphore available, but the test
\r
535 function expects it to be unavailable. */
\r
536 if( xSemaphoreTake( xSemaphore, staticDONT_BLOCK ) == pdFAIL )
\r
538 xErrorOccurred = pdTRUE;
\r
541 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );
\r
542 vSemaphoreDelete( xSemaphore );
\r
546 /*-----------------------------------------------------------*/
\r
548 static void prvTimerCallback( TimerHandle_t xExpiredTimer )
\r
550 UBaseType_t *puxVariableToIncrement;
\r
551 BaseType_t xReturned;
\r
553 /* The timer callback just demonstrates it is executing by incrementing a
\r
554 variable - the address of which is passed into the timer as its ID. Obtain
\r
555 the address of the variable to increment. */
\r
556 puxVariableToIncrement = ( UBaseType_t * ) pvTimerGetTimerID( xExpiredTimer );
\r
558 /* Increment the variable to show the timer callback has executed. */
\r
559 ( *puxVariableToIncrement )++;
\r
561 /* If this callback has executed the required number of times, stop the
\r
563 if( *puxVariableToIncrement == staticMAX_TIMER_CALLBACK_EXECUTIONS )
\r
565 /* This is called from a timer callback so must not block. See
\r
566 http://www.FreeRTOS.org/FreeRTOS-timers-xTimerStop.html */
\r
567 xReturned = xTimerStop( xExpiredTimer, staticDONT_BLOCK );
\r
569 if( xReturned != pdPASS )
\r
571 xErrorOccurred = pdTRUE;
\r
575 /*-----------------------------------------------------------*/
\r
577 static void prvCreateAndDeleteStaticallyAllocatedTimers( void )
\r
579 TimerHandle_t xTimer;
\r
580 UBaseType_t uxVariableToIncrement;
\r
581 const TickType_t xTimerPeriod = pdMS_TO_TICKS( 20 );
\r
582 BaseType_t xReturned;
\r
584 /* StaticTimer_t is a publicly accessible structure that has the same size
\r
585 and alignment requirements as the real timer structure. It is provided as a
\r
586 mechanism for applications to know the size of the timer structure (which is
\r
587 dependent on the architecture and configuration file settings) without breaking
\r
588 the strict data hiding policy by exposing the real timer internals. This
\r
589 StaticTimer_t variable is passed into the xTimerCreateStatic() function calls
\r
590 within this function. */
\r
591 StaticTimer_t xTimerBuffer;
\r
593 /* Create the software time. xTimerCreateStatic() has an extra parameter
\r
594 than the normal xTimerCreate() API function. The parameter is a pointer to
\r
595 the StaticTimer_t structure that will hold the software timer structure. If
\r
596 the parameter is passed as NULL then the structure will be allocated
\r
597 dynamically, just as if xTimerCreate() had been called. */
\r
598 xTimer = xTimerCreateStatic( "T1", /* Text name for the task. Helps debugging only. Not used by FreeRTOS. */
\r
599 xTimerPeriod, /* The period of the timer in ticks. */
\r
600 pdTRUE, /* This is an auto-reload timer. */
\r
601 ( void * ) &uxVariableToIncrement, /* The variable incremented by the test is passed into the timer callback using the timer ID. */
\r
602 prvTimerCallback, /* The function to execute when the timer expires. */
\r
603 &xTimerBuffer ); /* The buffer that will hold the software timer structure. */
\r
605 /* The timer handle should equal the static timer structure passed into the
\r
606 xTimerCreateStatic() function. */
\r
607 configASSERT( xTimer == ( TimerHandle_t ) &xTimerBuffer );
\r
609 /* Set the variable to 0, wait for a few timer periods to expire, then check
\r
610 the timer callback has incremented the variable to the expected value. */
\r
611 uxVariableToIncrement = 0;
\r
613 /* This is a low priority so a block time should not be needed. */
\r
614 xReturned = xTimerStart( xTimer, staticDONT_BLOCK );
\r
616 if( xReturned != pdPASS )
\r
618 xErrorOccurred = pdTRUE;
\r
621 vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );
\r
623 /* By now the timer should have expired staticMAX_TIMER_CALLBACK_EXECUTIONS
\r
624 times, and then stopped itself. */
\r
625 if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS )
\r
627 xErrorOccurred = pdTRUE;
\r
630 /* Finished with the timer, delete it. */
\r
631 xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );
\r
633 /* Again, as this is a low priority task it is expected that the timer
\r
634 command will have been sent even without a block time being used. */
\r
635 if( xReturned != pdPASS )
\r
637 xErrorOccurred = pdTRUE;
\r
640 /* Just to show the check task that this task is still executing. */
\r
643 /* Now do the same using a dynamically allocated software timer to ensure
\r
644 the delete function is working correctly in both the static and dynamic
\r
645 allocation cases. */
\r
646 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
648 xTimer = xTimerCreate( "T1", /* Text name for the task. Helps debugging only. Not used by FreeRTOS. */
\r
649 xTimerPeriod, /* The period of the timer in ticks. */
\r
650 pdTRUE, /* This is an auto-reload timer. */
\r
651 ( void * ) &uxVariableToIncrement, /* The variable incremented by the test is passed into the timer callback using the timer ID. */
\r
652 prvTimerCallback ); /* The function to execute when the timer expires. */
\r
654 configASSERT( xTimer != NULL );
\r
656 uxVariableToIncrement = 0;
\r
657 xReturned = xTimerStart( xTimer, staticDONT_BLOCK );
\r
659 if( xReturned != pdPASS )
\r
661 xErrorOccurred = pdTRUE;
\r
664 vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );
\r
666 if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS )
\r
668 xErrorOccurred = pdTRUE;
\r
671 xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );
\r
673 if( xReturned != pdPASS )
\r
675 xErrorOccurred = pdTRUE;
\r
680 /*-----------------------------------------------------------*/
\r
682 static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void )
\r
684 EventGroupHandle_t xEventGroup;
\r
686 /* StaticEventGroup_t is a publicly accessible structure that has the same size
\r
687 and alignment requirements as the real event group structure. It is provided as
\r
688 a mechanism for applications to know the size of the event group (which is
\r
689 dependent on the architecture and configuration file settings) without breaking
\r
690 the strict data hiding policy by exposing the real event group internals. This
\r
691 StaticEventGroup_t variable is passed into the xSemaphoreCreateEventGroupStatic()
\r
692 function calls within this function. */
\r
693 StaticEventGroup_t xEventGroupBuffer;
\r
695 /* Create the event group. xEventGroupCreateStatic() has an extra parameter
\r
696 than the normal xEventGroupCreate() API function. The parameter is a
\r
697 pointer to the StaticEventGroup_t structure that will hold the event group
\r
699 xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );
\r
701 /* The event group handle should equal the static event group structure
\r
702 passed into the xEventGroupCreateStatic() function. */
\r
703 configASSERT( xEventGroup == ( EventGroupHandle_t ) &xEventGroupBuffer );
\r
705 /* Ensure the event group passes a few sanity checks as a valid event
\r
707 prvSanityCheckCreatedEventGroup( xEventGroup );
\r
709 /* Delete the event group again so the buffers can be reused. */
\r
710 vEventGroupDelete( xEventGroup );
\r
712 /* Now do the same using a dynamically allocated event group to ensure the
\r
713 delete function is working correctly in both the static and dynamic
\r
714 allocation cases. */
\r
715 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
717 xEventGroup = xEventGroupCreate();
\r
718 configASSERT( xEventGroup != NULL );
\r
719 prvSanityCheckCreatedEventGroup( xEventGroup );
\r
720 vEventGroupDelete( xEventGroup );
\r
724 /*-----------------------------------------------------------*/
\r
726 static void prvCreateAndDeleteStaticallyAllocatedTasks( void )
\r
728 TaskHandle_t xCreatedTask;
\r
730 /* The variable that will hold the TCB of tasks created by this function. See
\r
731 the comments above the declaration of the xCreatorTaskTCBBuffer variable for
\r
732 more information. */
\r
733 StaticTask_t xTCBBuffer;
\r
735 /* This buffer that will be used as the stack of tasks created by this function.
\r
736 See the comments above the declaration of the uxCreatorTaskStackBuffer[] array
\r
737 above for more information. */
\r
738 static StackType_t uxStackBuffer[ configMINIMAL_STACK_SIZE ];
\r
740 /* Create the task. xTaskCreateStatic() has two more parameters than
\r
741 the usual xTaskCreate() function. The first new parameter is a pointer to
\r
742 the pre-allocated stack. The second new parameter is a pointer to the
\r
743 StaticTask_t structure that will hold the task's TCB. If both pointers are
\r
744 passed as NULL then the respective object will be allocated dynamically as
\r
745 if xTaskCreate() had been called. */
\r
746 xCreatedTask = xTaskCreateStatic(
\r
747 prvStaticallyAllocatedTask, /* Function that implements the task. */
\r
748 "Static", /* Human readable name for the task. */
\r
749 configMINIMAL_STACK_SIZE, /* Task's stack size, in words (not bytes!). */
\r
750 NULL, /* Parameter to pass into the task. */
\r
751 uxTaskPriorityGet( NULL ) + 1, /* The priority of the task. */
\r
752 &( uxStackBuffer[ 0 ] ), /* The buffer to use as the task's stack. */
\r
753 &xTCBBuffer ); /* The variable that will hold that task's TCB. */
\r
755 /* Check the task was created correctly, then delete the task. */
\r
756 if( xCreatedTask == NULL )
\r
758 xErrorOccurred = pdTRUE;
\r
760 else if( eTaskGetState( xCreatedTask ) != eSuspended )
\r
762 /* The created task had a higher priority so should have executed and
\r
763 suspended itself by now. */
\r
764 xErrorOccurred = pdTRUE;
\r
768 vTaskDelete( xCreatedTask );
\r
771 /* Now do the same using a dynamically allocated task to ensure the delete
\r
772 function is working correctly in both the static and dynamic allocation
\r
774 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
776 BaseType_t xReturned;
\r
778 xReturned = xTaskCreate(
\r
779 prvStaticallyAllocatedTask, /* Function that implements the task - the same function is used but is actually dynamically allocated this time. */
\r
780 "Static", /* Human readable name for the task. */
\r
781 configMINIMAL_STACK_SIZE, /* Task's stack size, in words (not bytes!). */
\r
782 NULL, /* Parameter to pass into the task. */
\r
783 uxTaskPriorityGet( NULL ) + 1, /* The priority of the task. */
\r
784 &xCreatedTask ); /* Handle of the task being created. */
\r
786 if( eTaskGetState( xCreatedTask ) != eSuspended )
\r
788 xErrorOccurred = pdTRUE;
\r
791 configASSERT( xReturned == pdPASS );
\r
792 if( xReturned != pdPASS )
\r
794 xErrorOccurred = pdTRUE;
\r
796 vTaskDelete( xCreatedTask );
\r
800 /*-----------------------------------------------------------*/
\r
802 static void prvStaticallyAllocatedTask( void *pvParameters )
\r
804 ( void ) pvParameters;
\r
806 /* The created task just suspends itself to wait to get deleted. The task
\r
807 that creates this task checks this task is in the expected Suspended state
\r
808 before deleting it. */
\r
809 vTaskSuspend( NULL );
\r
811 /*-----------------------------------------------------------*/
\r
813 static UBaseType_t prvRand( void )
\r
815 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
\r
817 /* Utility function to generate a pseudo random number. */
\r
818 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
\r
819 return( ( ulNextRand >> 16UL ) & 0x7fffUL );
\r
821 /*-----------------------------------------------------------*/
\r
823 static TickType_t prvGetNextDelayTime( void )
\r
825 TickType_t xNextDelay;
\r
826 const TickType_t xMaxDelay = pdMS_TO_TICKS( ( TickType_t ) 150 );
\r
827 const TickType_t xMinDelay = pdMS_TO_TICKS( ( TickType_t ) 75 );
\r
828 const TickType_t xTinyDelay = pdMS_TO_TICKS( ( TickType_t ) 2 );
\r
830 /* Generate the next delay time. This is kept within a narrow band so as
\r
831 not to disturb the timing of other tests - but does add in some pseudo
\r
832 randomisation into the tests. */
\r
835 xNextDelay = prvRand() % xMaxDelay;
\r
837 /* Just in case this loop is executed lots of times. */
\r
838 vTaskDelay( xTinyDelay );
\r
840 } while ( xNextDelay < xMinDelay );
\r
844 /*-----------------------------------------------------------*/
\r
846 static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup )
\r
848 EventBits_t xEventBits;
\r
849 const EventBits_t xFirstTestBits = ( EventBits_t ) 0xaa, xSecondTestBits = ( EventBits_t ) 0x55;
\r
851 /* The event group should not have any bits set yet. */
\r
852 xEventBits = xEventGroupGetBits( xEventGroup );
\r
854 if( xEventBits != ( EventBits_t ) 0 )
\r
856 xErrorOccurred = pdTRUE;
\r
859 /* Some some bits, then read them back to check they are as expected. */
\r
860 xEventGroupSetBits( xEventGroup, xFirstTestBits );
\r
862 xEventBits = xEventGroupGetBits( xEventGroup );
\r
864 if( xEventBits != xFirstTestBits )
\r
866 xErrorOccurred = pdTRUE;
\r
869 xEventGroupSetBits( xEventGroup, xSecondTestBits );
\r
871 xEventBits = xEventGroupGetBits( xEventGroup );
\r
873 if( xEventBits != ( xFirstTestBits | xSecondTestBits ) )
\r
875 xErrorOccurred = pdTRUE;
\r
878 /* Finally try clearing some bits too and check that operation proceeds as
\r
880 xEventGroupClearBits( xEventGroup, xFirstTestBits );
\r
882 xEventBits = xEventGroupGetBits( xEventGroup );
\r
884 if( xEventBits != xSecondTestBits )
\r
886 xErrorOccurred = pdTRUE;
\r
889 /*-----------------------------------------------------------*/
\r
891 static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount )
\r
893 BaseType_t xReturned;
\r
895 const TickType_t xShortBlockTime = pdMS_TO_TICKS( 10 );
\r
896 TickType_t xTickCount;
\r
898 /* The binary semaphore should start 'empty', so a call to xSemaphoreTake()
\r
900 xTickCount = xTaskGetTickCount();
\r
901 xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );
\r
903 if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime )
\r
905 /* Did not block on the semaphore as long as expected. */
\r
906 xErrorOccurred = pdTRUE;
\r
909 if( xReturned != pdFAIL )
\r
911 xErrorOccurred = pdTRUE;
\r
914 /* Should be possible to 'give' the semaphore up to a maximum of uxMaxCount
\r
916 for( x = 0; x < uxMaxCount; x++ )
\r
918 xReturned = xSemaphoreGive( xSemaphore );
\r
920 if( xReturned == pdFAIL )
\r
922 xErrorOccurred = pdTRUE;
\r
926 /* Giving the semaphore again should fail, as it is 'full'. */
\r
927 xReturned = xSemaphoreGive( xSemaphore );
\r
929 if( xReturned != pdFAIL )
\r
931 xErrorOccurred = pdTRUE;
\r
934 configASSERT( uxSemaphoreGetCount( xSemaphore ) == uxMaxCount );
\r
936 /* Should now be possible to 'take' the semaphore up to a maximum of
\r
937 uxMaxCount times without blocking. */
\r
938 for( x = 0; x < uxMaxCount; x++ )
\r
940 xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );
\r
942 if( xReturned == pdFAIL )
\r
944 xErrorOccurred = pdTRUE;
\r
948 /* Back to the starting condition, where the semaphore should not be
\r
950 xTickCount = xTaskGetTickCount();
\r
951 xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );
\r
953 if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime )
\r
955 /* Did not block on the semaphore as long as expected. */
\r
956 xErrorOccurred = pdTRUE;
\r
959 if( xReturned != pdFAIL )
\r
961 xErrorOccurred = pdTRUE;
\r
964 configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );
\r
966 /*-----------------------------------------------------------*/
\r
968 static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue )
\r
970 uint64_t ull, ullRead;
\r
971 BaseType_t xReturned, xLoop;
\r
973 /* This test is done twice to ensure the queue storage area wraps. */
\r
974 for( xLoop = 0; xLoop < 2; xLoop++ )
\r
976 /* A very basic test that the queue can be written to and read from as
\r
977 expected. First the queue should be empty. */
\r
978 xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );
\r
979 if( xReturned != errQUEUE_EMPTY )
\r
981 xErrorOccurred = pdTRUE;
\r
984 /* Now it should be possible to write to the queue staticQUEUE_LENGTH_IN_ITEMS
\r
986 for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ )
\r
988 xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );
\r
989 if( xReturned != pdPASS )
\r
991 xErrorOccurred = pdTRUE;
\r
995 /* Should not now be possible to write to the queue again. */
\r
996 xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );
\r
997 if( xReturned != errQUEUE_FULL )
\r
999 xErrorOccurred = pdTRUE;
\r
1002 /* Now read back from the queue to ensure the data read back matches that
\r
1004 for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ )
\r
1006 xReturned = xQueueReceive( xQueue, &ullRead, staticDONT_BLOCK );
\r
1008 if( xReturned != pdPASS )
\r
1010 xErrorOccurred = pdTRUE;
\r
1013 if( ullRead != ull )
\r
1015 xErrorOccurred = pdTRUE;
\r
1019 /* The queue should be empty again. */
\r
1020 xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );
\r
1021 if( xReturned != errQUEUE_EMPTY )
\r
1023 xErrorOccurred = pdTRUE;
\r
1027 /*-----------------------------------------------------------*/
\r
1029 static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore )
\r
1031 const BaseType_t xLoops = 5;
\r
1032 BaseType_t x, xReturned;
\r
1034 /* A very basic test that the recursive semaphore behaved like a recursive
\r
1035 semaphore. First the semaphore should not be able to be given, as it has not
\r
1036 yet been taken. */
\r
1037 xReturned = xSemaphoreGiveRecursive( xSemaphore );
\r
1039 if( xReturned != pdFAIL )
\r
1041 xErrorOccurred = pdTRUE;
\r
1044 /* Now it should be possible to take the mutex a number of times. */
\r
1045 for( x = 0; x < xLoops; x++ )
\r
1047 xReturned = xSemaphoreTakeRecursive( xSemaphore, staticDONT_BLOCK );
\r
1049 if( xReturned != pdPASS )
\r
1051 xErrorOccurred = pdTRUE;
\r
1055 /* Should be possible to give the semaphore the same number of times as it
\r
1056 was given in the loop above. */
\r
1057 for( x = 0; x < xLoops; x++ )
\r
1059 xReturned = xSemaphoreGiveRecursive( xSemaphore );
\r
1061 if( xReturned != pdPASS )
\r
1063 xErrorOccurred = pdTRUE;
\r
1067 /* No more gives should be possible though. */
\r
1068 xReturned = xSemaphoreGiveRecursive( xSemaphore );
\r
1070 if( xReturned != pdFAIL )
\r
1072 xErrorOccurred = pdTRUE;
\r
1075 /*-----------------------------------------------------------*/
\r
1077 BaseType_t xAreStaticAllocationTasksStillRunning( void )
\r
1079 static UBaseType_t uxLastCycleCounter = 0;
\r
1080 BaseType_t xReturn;
\r
1082 if( uxCycleCounter == uxLastCycleCounter )
\r
1084 xErrorOccurred = pdTRUE;
\r
1088 uxLastCycleCounter = uxCycleCounter;
\r
1091 if( xErrorOccurred != pdFALSE )
\r
1102 /*-----------------------------------------------------------*/
\r
1104 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */
\r
1105 #endif /* configSUPPORT_STATIC_ALLOCATION == 1 */
\r