]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/StaticAllocation.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / Common / Minimal / StaticAllocation.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /*\r
72  * Demonstrates how to create FreeRTOS objects using pre-allocated memory,\r
73  * rather than the normal dynamically allocated memory, and tests objects being\r
74  * created and deleted with both statically allocated memory and dynamically\r
75  * allocated memory.\r
76  *\r
77  * See http://www.FreeRTOS.org/Static_Vs_Dynamic_Memory_Allocation.html\r
78  */\r
79 \r
80 /* Scheduler include files. */\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 #include "queue.h"\r
84 #include "semphr.h"\r
85 #include "event_groups.h"\r
86 #include "timers.h"\r
87 \r
88 /* Demo program include files. */\r
89 #include "StaticAllocation.h"\r
90 \r
91 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */\r
92 #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
93 \r
94 /* The priority at which the task that performs the tests is created. */\r
95 #define staticTASK_PRIORITY                                     ( tskIDLE_PRIORITY + 2 )\r
96 \r
97 /* The length of the queue, in items, not bytes, used in the queue static\r
98 allocation tests. */\r
99 #define staticQUEUE_LENGTH_IN_ITEMS                     ( 5 )\r
100 \r
101 /* A block time of 0 simply means "don't block". */\r
102 #define staticDONT_BLOCK                                        ( ( TickType_t ) 0 )\r
103 \r
104 /* Binary semaphores have a maximum count of 1. */\r
105 #define staticBINARY_SEMAPHORE_MAX_COUNT        ( 1 )\r
106 \r
107 /* The size of the stack used by the task that runs the tests. */\r
108 #define staticCREATOR_TASK_STACK_SIZE           ( configMINIMAL_STACK_SIZE * 2 )\r
109 \r
110 /* The number of times the software timer will execute before stopping itself. */\r
111 #define staticMAX_TIMER_CALLBACK_EXECUTIONS     ( 5 )\r
112 \r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /*\r
117  * The task that repeatedly creates and deletes statically allocated tasks, and\r
118  * other RTOS objects.\r
119  */\r
120 static void prvStaticallyAllocatedCreator( void *pvParameters );\r
121 \r
122 /*\r
123  * The callback function used by the software timer that is repeatedly created\r
124  * and deleted using both static and dynamically allocated memory.\r
125  */\r
126 static void prvTimerCallback( TimerHandle_t xExpiredTimer );\r
127 \r
128 /*\r
129  * A task that is created and deleted multiple times, using both statically and\r
130  * dynamically allocated stack and TCB.\r
131  */\r
132 static void prvStaticallyAllocatedTask( void *pvParameters );\r
133 \r
134 /*\r
135  * A function that demonstrates and tests the API functions that create and\r
136  * delete tasks using both statically and dynamically allocated TCBs and stacks.\r
137  */\r
138 static void prvCreateAndDeleteStaticallyAllocatedTasks( void );\r
139 \r
140 /*\r
141  * A function that demonstrates and tests the API functions that create and\r
142  * delete event groups using both statically and dynamically allocated RAM.\r
143  */\r
144 static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void );\r
145 \r
146 /*\r
147  * A function that demonstrates and tests the API functions that create and\r
148  * delete queues using both statically and dynamically allocated RAM.\r
149  */\r
150 static void prvCreateAndDeleteStaticallyAllocatedQueues( void );\r
151 \r
152 /*\r
153  * A function that demonstrates and tests the API functions that create and\r
154  * delete binary semaphores using both statically and dynamically allocated RAM.\r
155  */\r
156 static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void );\r
157 \r
158 /*\r
159  * A function that demonstrates and tests the API functions that create and\r
160  * delete software timers using both statically and dynamically allocated RAM.\r
161  */\r
162 static void prvCreateAndDeleteStaticallyAllocatedTimers( void );\r
163 \r
164 /*\r
165  * A function that demonstrates and tests the API functions that create and\r
166  * delete mutexes using both statically and dynamically allocated RAM.\r
167  */\r
168 static void prvCreateAndDeleteStaticallyAllocatedMutexes( void );\r
169 \r
170 /*\r
171  * A function that demonstrates and tests the API functions that create and\r
172  * delete counting semaphores using both statically and dynamically allocated\r
173  * RAM.\r
174  */\r
175 static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void );\r
176 \r
177 /*\r
178  * A function that demonstrates and tests the API functions that create and\r
179  * delete recursive mutexes using both statically and dynamically allocated RAM.\r
180  */\r
181 static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void );\r
182 \r
183 /*\r
184  * Utility function to create pseudo random numbers.\r
185  */\r
186 static UBaseType_t prvRand( void );\r
187 \r
188 /*\r
189  * The task that creates and deletes other tasks has to delay occasionally to\r
190  * ensure lower priority tasks are not starved of processing time.  A pseudo\r
191  * random delay time is used just to add a little bit of randomisation into the\r
192  * execution pattern.  prvGetNextDelayTime() generates the pseudo random delay.\r
193  */\r
194 static TickType_t prvGetNextDelayTime( void );\r
195 \r
196 /*\r
197  * Checks the basic operation of a queue after it has been created.\r
198  */\r
199 static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue );\r
200 \r
201 /*\r
202  * Checks the basic operation of a recursive mutex after it has been created.\r
203  */\r
204 static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore );\r
205 \r
206 /*\r
207  * Checks the basic operation of a binary semaphore after it has been created.\r
208  */\r
209 static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount );\r
210 \r
211 /*\r
212  * Checks the basic operation of an event group after it has been created.\r
213  */\r
214 static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup );\r
215 \r
216 /*-----------------------------------------------------------*/\r
217 \r
218 /* StaticTask_t is a publicly accessible structure that has the same size and\r
219 alignment requirements as the real TCB structure.  It is provided as a mechanism\r
220 for applications to know the size of the TCB (which is dependent on the\r
221 architecture and configuration file settings) without breaking the strict data\r
222 hiding policy by exposing the real TCB.  This StaticTask_t variable is passed\r
223 into the xTaskCreateStatic() function that creates the\r
224 prvStaticallyAllocatedCreator() task, and will hold the TCB of the created\r
225 tasks. */\r
226 static StaticTask_t xCreatorTaskTCBBuffer;\r
227 \r
228 /* This is the stack that will be used by the prvStaticallyAllocatedCreator()\r
229 task, which is itself created using statically allocated buffers (so without any\r
230 dynamic memory allocation). */\r
231 static StackType_t uxCreatorTaskStackBuffer[ staticCREATOR_TASK_STACK_SIZE ];\r
232 \r
233 /* Used by the pseudo random number generating function. */\r
234 static uint32_t ulNextRand = 0;\r
235 \r
236 /* Used so a check task can ensure this test is still executing, and not\r
237 stalled. */\r
238 static volatile UBaseType_t uxCycleCounter = 0;\r
239 \r
240 /* A variable that gets set to pdTRUE if an error is detected. */\r
241 static volatile BaseType_t xErrorOccurred = pdFALSE;\r
242 \r
243 /*-----------------------------------------------------------*/\r
244 \r
245 void vStartStaticallyAllocatedTasks( void  )\r
246 {\r
247         /* Create a single task, which then repeatedly creates and deletes the other\r
248         RTOS objects using both statically and dynamically allocated RAM. */\r
249         xTaskCreateStatic( prvStaticallyAllocatedCreator,               /* The function that implements the task being created. */\r
250                                            "StatCreate",                                                /* Text name for the task - not used by the RTOS, its just to assist debugging. */\r
251                                            staticCREATOR_TASK_STACK_SIZE,               /* Size of the buffer passed in as the stack - in words, not bytes! */\r
252                                            NULL,                                                                /* Parameter passed into the task - not used in this case. */\r
253                                            staticTASK_PRIORITY,                                 /* Priority of the task. */\r
254                                            NULL,                                                                /* Handle of the task being created, not used in this case. */\r
255                                            &( uxCreatorTaskStackBuffer[ 0 ] ),  /* The buffer to use as the task's stack. */\r
256                                            &xCreatorTaskTCBBuffer );                    /* The variable that will hold the task's TCB. */\r
257 \r
258         /* Pseudo seed the random number generator. */\r
259         ulNextRand = ( uint32_t ) prvRand;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 static void prvStaticallyAllocatedCreator( void *pvParameters )\r
264 {\r
265         /* Avoid compiler warnings. */\r
266         ( void ) pvParameters;\r
267 \r
268         for( ;; )\r
269         {\r
270                 /* Loop, running functions that create and delete the various RTOS\r
271                 objects that can be optionally created using either static or dynamic\r
272                 memory allocation. */\r
273                 prvCreateAndDeleteStaticallyAllocatedTasks();\r
274                 prvCreateAndDeleteStaticallyAllocatedQueues();\r
275 \r
276                 /* Delay to ensure lower priority tasks get CPU time, and increment the\r
277                 cycle counter so a 'check' task can determine that this task is still\r
278                 executing. */\r
279                 vTaskDelay( prvGetNextDelayTime() );\r
280                 uxCycleCounter++;\r
281 \r
282                 prvCreateAndDeleteStaticallyAllocatedBinarySemaphores();\r
283                 prvCreateAndDeleteStaticallyAllocatedCountingSemaphores();\r
284 \r
285                 vTaskDelay( prvGetNextDelayTime() );\r
286                 uxCycleCounter++;\r
287 \r
288                 prvCreateAndDeleteStaticallyAllocatedMutexes();\r
289                 prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes();\r
290 \r
291                 vTaskDelay( prvGetNextDelayTime() );\r
292                 uxCycleCounter++;\r
293 \r
294                 prvCreateAndDeleteStaticallyAllocatedEventGroups();\r
295                 prvCreateAndDeleteStaticallyAllocatedTimers();\r
296         }\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void )\r
301 {\r
302 SemaphoreHandle_t xSemaphore;\r
303 const UBaseType_t uxMaxCount = ( UBaseType_t ) 10;\r
304 \r
305 /* StaticSemaphore_t is a publicly accessible structure that has the same size\r
306 and alignment requirements as the real semaphore structure.  It is provided as a\r
307 mechanism for applications to know the size of the semaphore (which is dependent\r
308 on the architecture and configuration file settings) without breaking the strict\r
309 data hiding policy by exposing the real semaphore internals.  This\r
310 StaticSemaphore_t variable is passed into the xSemaphoreCreateCountingStatic()\r
311 function calls within this function.  NOTE: In most usage scenarios now it is\r
312 faster and more memory efficient to use a direct to task notification instead of\r
313 a counting semaphore.  http://www.freertos.org/RTOS-task-notifications.html */\r
314 StaticSemaphore_t xSemaphoreBuffer;\r
315 \r
316         /* Create the semaphore.  xSemaphoreCreateCountingStatic() has one more\r
317         parameter than the usual xSemaphoreCreateCounting() function.  The parameter\r
318         is a pointer to the pre-allocated StaticSemaphore_t structure, which will\r
319         hold information on the semaphore in an anonymous way.  If the pointer is\r
320         passed as NULL then the structure will be allocated dynamically, just as\r
321         when xSemaphoreCreateCounting() is called. */\r
322         xSemaphore = xSemaphoreCreateCountingStatic( uxMaxCount, 0, &xSemaphoreBuffer );\r
323 \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
327 \r
328         /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */\r
329         prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount );\r
330 \r
331         /* Delete the semaphore again so the buffers can be reused. */\r
332         vSemaphoreDelete( xSemaphore );\r
333 \r
334         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
335         {\r
336                 /* Now do the same but using dynamically allocated buffers to ensure the\r
337                 delete functions are working correctly in both the static and dynamic\r
338                 allocation cases. */\r
339                 xSemaphore = xSemaphoreCreateCounting( uxMaxCount, 0 );\r
340                 configASSERT( xSemaphore != NULL );\r
341                 prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount );\r
342                 vSemaphoreDelete( xSemaphore );\r
343         }\r
344         #endif\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void )\r
349 {\r
350 SemaphoreHandle_t xSemaphore;\r
351 \r
352 /* StaticSemaphore_t is a publicly accessible structure that has the same size\r
353 and alignment requirements as the real semaphore structure.  It is provided as a\r
354 mechanism for applications to know the size of the semaphore (which is dependent\r
355 on the architecture and configuration file settings) without breaking the strict\r
356 data hiding policy by exposing the real semaphore internals.  This\r
357 StaticSemaphore_t variable is passed into the\r
358 xSemaphoreCreateRecursiveMutexStatic() function calls within this function. */\r
359 StaticSemaphore_t xSemaphoreBuffer;\r
360 \r
361         /* Create the semaphore.  xSemaphoreCreateRecursiveMutexStatic() has one\r
362         more parameter than the usual xSemaphoreCreateRecursiveMutex() function.\r
363         The parameter is a pointer to the pre-allocated StaticSemaphore_t structure,\r
364         which will hold information on the semaphore in an anonymous way.  If the\r
365         pointer is passed as NULL then the structure will be allocated dynamically,\r
366         just as when xSemaphoreCreateRecursiveMutex() is called. */\r
367         xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xSemaphoreBuffer );\r
368 \r
369         /* The semaphore handle should equal the static semaphore structure passed\r
370         into the xSemaphoreCreateBinaryStatic() function. */\r
371         configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );\r
372 \r
373         /* Ensure the semaphore passes a few sanity checks as a valid\r
374         recursive semaphore. */\r
375         prvSanityCheckCreatedRecursiveMutex( xSemaphore );\r
376 \r
377         /* Delete the semaphore again so the buffers can be reused. */\r
378         vSemaphoreDelete( xSemaphore );\r
379 \r
380         /* Now do the same using dynamically allocated buffers to ensure the delete\r
381         functions are working correctly in both the static and dynamic memory\r
382         allocation cases. */\r
383         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
384         {\r
385                 xSemaphore = xSemaphoreCreateRecursiveMutex();\r
386                 configASSERT( xSemaphore != NULL );\r
387                 prvSanityCheckCreatedRecursiveMutex( xSemaphore );\r
388                 vSemaphoreDelete( xSemaphore );\r
389         }\r
390         #endif\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \r
394 static void prvCreateAndDeleteStaticallyAllocatedQueues( void )\r
395 {\r
396 QueueHandle_t xQueue;\r
397 \r
398 /* StaticQueue_t is a publicly accessible structure that has the same size and\r
399 alignment requirements as the real queue structure.  It is provided as a\r
400 mechanism for applications to know the size of the queue (which is dependent on\r
401 the architecture and configuration file settings) without breaking the strict\r
402 data hiding policy by exposing the real queue internals.  This StaticQueue_t\r
403 variable is passed into the xQueueCreateStatic() function calls within this\r
404 function. */\r
405 static StaticQueue_t xStaticQueue;\r
406 \r
407 /* The queue storage area must be large enough to hold the maximum number of\r
408 items it is possible for the queue to hold at any one time, which equals the\r
409 queue length (in items, not bytes) multiplied by the size of each item.  In this\r
410 case the queue will hold staticQUEUE_LENGTH_IN_ITEMS 64-bit items.  See\r
411 http://www.freertos.org/Embedded-RTOS-Queues.html */\r
412 static uint8_t ucQueueStorageArea[ staticQUEUE_LENGTH_IN_ITEMS * sizeof( uint64_t ) ];\r
413 \r
414         /* Create the queue.  xQueueCreateStatic() has two more parameters than the\r
415         usual xQueueCreate() function.  The first new parameter is a pointer to the\r
416         pre-allocated queue storage area.  The second new parameter is a pointer to\r
417         the StaticQueue_t structure that will hold the queue state information in\r
418         an anonymous way.  If the two pointers are passed as NULL then the data\r
419         will be allocated dynamically as if xQueueCreate() had been called. */\r
420         xQueue = xQueueCreateStatic( staticQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */\r
421                                                                  sizeof( uint64_t ), /* The size of each item. */\r
422                                                                  ucQueueStorageArea, /* The buffer used to hold items within the queue. */\r
423                                                                  &xStaticQueue );        /* The static queue structure that will hold the state of the queue. */\r
424 \r
425         /* The queue handle should equal the static queue structure passed into the\r
426         xQueueCreateStatic() function. */\r
427         configASSERT( xQueue == ( QueueHandle_t ) &xStaticQueue );\r
428 \r
429         /* Ensure the queue passes a few sanity checks as a valid queue. */\r
430         prvSanityCheckCreatedQueue( xQueue );\r
431 \r
432         /* Delete the queue again so the buffers can be reused. */\r
433         vQueueDelete( xQueue );\r
434 \r
435         /* Now do the same using a dynamically allocated queue to ensure the delete\r
436         function is working correctly in both the static and dynamic memory\r
437         allocation cases. */\r
438         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
439         {\r
440                 xQueue = xQueueCreate( staticQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */\r
441                                                            sizeof( uint64_t ) );                /* The size of each item. */\r
442 \r
443                 /* The queue handle should equal the static queue structure passed into the\r
444                 xQueueCreateStatic() function. */\r
445                 configASSERT( xQueue != NULL );\r
446 \r
447                 /* Ensure the queue passes a few sanity checks as a valid queue. */\r
448                 prvSanityCheckCreatedQueue( xQueue );\r
449 \r
450                 /* Delete the queue again so the buffers can be reused. */\r
451                 vQueueDelete( xQueue );\r
452         }\r
453         #endif\r
454 }\r
455 /*-----------------------------------------------------------*/\r
456 \r
457 static void prvCreateAndDeleteStaticallyAllocatedMutexes( void )\r
458 {\r
459 SemaphoreHandle_t xSemaphore;\r
460 BaseType_t xReturned;\r
461 \r
462 /* StaticSemaphore_t is a publicly accessible structure that has the same size\r
463 and alignment requirements as the real semaphore structure.  It is provided as a\r
464 mechanism for applications to know the size of the semaphore (which is dependent\r
465 on the architecture and configuration file settings) without breaking the strict\r
466 data hiding policy by exposing the real semaphore internals.  This\r
467 StaticSemaphore_t variable is passed into the xSemaphoreCreateMutexStatic()\r
468 function calls within this function. */\r
469 StaticSemaphore_t xSemaphoreBuffer;\r
470 \r
471         /* Create the semaphore.  xSemaphoreCreateMutexStatic() has one more\r
472         parameter than the usual xSemaphoreCreateMutex() function.  The parameter\r
473         is a pointer to the pre-allocated StaticSemaphore_t structure, which will\r
474         hold information on the semaphore in an anonymous way.  If the pointer is\r
475         passed as NULL then the structure will be allocated dynamically, just as\r
476         when xSemaphoreCreateMutex() is called. */\r
477         xSemaphore = xSemaphoreCreateMutexStatic( &xSemaphoreBuffer );\r
478 \r
479         /* The semaphore handle should equal the static semaphore structure passed\r
480         into the xSemaphoreCreateMutexStatic() function. */\r
481         configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );\r
482 \r
483         /* Take the mutex so the mutex is in the state expected by the\r
484         prvSanityCheckCreatedSemaphore() function. */\r
485         xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );\r
486 \r
487         if( xReturned != pdPASS )\r
488         {\r
489                 xErrorOccurred = pdTRUE;\r
490         }\r
491 \r
492         /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */\r
493         prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
494 \r
495         /* Delete the semaphore again so the buffers can be reused. */\r
496         vSemaphoreDelete( xSemaphore );\r
497 \r
498         /* Now do the same using a dynamically allocated mutex to ensure the delete\r
499         function is working correctly in both the static and dynamic allocation\r
500         cases. */\r
501         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
502         {\r
503                 xSemaphore = xSemaphoreCreateMutex();\r
504 \r
505                 /* The semaphore handle should equal the static semaphore structure\r
506                 passed into the xSemaphoreCreateMutexStatic() function. */\r
507                 configASSERT( xSemaphore != NULL );\r
508 \r
509                 /* Take the mutex so the mutex is in the state expected by the\r
510                 prvSanityCheckCreatedSemaphore() function. */\r
511                 xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );\r
512 \r
513                 if( xReturned != pdPASS )\r
514                 {\r
515                         xErrorOccurred = pdTRUE;\r
516                 }\r
517 \r
518                 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */\r
519                 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
520 \r
521                 /* Delete the semaphore again so the buffers can be reused. */\r
522                 vSemaphoreDelete( xSemaphore );\r
523         }\r
524         #endif\r
525 }\r
526 /*-----------------------------------------------------------*/\r
527 \r
528 static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void )\r
529 {\r
530 SemaphoreHandle_t xSemaphore;\r
531 \r
532 /* StaticSemaphore_t is a publicly accessible structure that has the same size\r
533 and alignment requirements as the real semaphore structure.  It is provided as a\r
534 mechanism for applications to know the size of the semaphore (which is dependent\r
535 on the architecture and configuration file settings) without breaking the strict\r
536 data hiding policy by exposing the real semaphore internals.  This\r
537 StaticSemaphore_t variable is passed into the xSemaphoreCreateBinaryStatic()\r
538 function calls within this function.  NOTE: In most usage scenarios now it is\r
539 faster and more memory efficient to use a direct to task notification instead of\r
540 a binary semaphore.  http://www.freertos.org/RTOS-task-notifications.html */\r
541 StaticSemaphore_t xSemaphoreBuffer;\r
542 \r
543         /* Create the semaphore.  xSemaphoreCreateBinaryStatic() has one more\r
544         parameter than the usual xSemaphoreCreateBinary() function.  The parameter\r
545         is a pointer to the pre-allocated StaticSemaphore_t structure, which will\r
546         hold information on the semaphore in an anonymous way.  If the pointer is\r
547         passed as NULL then the structure will be allocated dynamically, just as\r
548         when xSemaphoreCreateBinary() is called. */\r
549         xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer );\r
550 \r
551         /* The semaphore handle should equal the static semaphore structure passed\r
552         into the xSemaphoreCreateBinaryStatic() function. */\r
553         configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );\r
554 \r
555         /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */\r
556         prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
557 \r
558         /* Delete the semaphore again so the buffers can be reused. */\r
559         vSemaphoreDelete( xSemaphore );\r
560 \r
561         /* Now do the same using a dynamically allocated semaphore to check the\r
562         delete function is working correctly in both the static and dynamic\r
563         allocation cases. */\r
564         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
565         {\r
566                 xSemaphore = xSemaphoreCreateBinary();\r
567                 configASSERT( xSemaphore != NULL );\r
568                 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
569                 vSemaphoreDelete( xSemaphore );\r
570         }\r
571         #endif\r
572 \r
573         /* There isn't a static version of the old and deprecated\r
574         vSemaphoreCreateBinary() macro (because its deprecated!), but check it is\r
575         still functioning correctly. */\r
576         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
577         {\r
578                 vSemaphoreCreateBinary( xSemaphore );\r
579 \r
580                 /* The macro starts with the binary semaphore available, but the test\r
581                 function expects it to be unavailable. */\r
582                 if( xSemaphoreTake( xSemaphore, staticDONT_BLOCK ) == pdFAIL )\r
583                 {\r
584                         xErrorOccurred = pdTRUE;\r
585                 }\r
586 \r
587                 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
588                 vSemaphoreDelete( xSemaphore );\r
589         }\r
590         #endif\r
591 }\r
592 /*-----------------------------------------------------------*/\r
593 \r
594 static void prvTimerCallback( TimerHandle_t xExpiredTimer )\r
595 {\r
596 UBaseType_t *puxVariableToIncrement;\r
597 BaseType_t xReturned;\r
598 \r
599         /* The timer callback just demonstrates it is executing by incrementing a\r
600         variable - the address of which is passed into the timer as its ID.  Obtain\r
601         the address of the variable to increment. */\r
602         puxVariableToIncrement = ( UBaseType_t * ) pvTimerGetTimerID( xExpiredTimer );\r
603 \r
604         /* Increment the variable to show the timer callback has executed. */\r
605         ( *puxVariableToIncrement )++;\r
606 \r
607         /* If this callback has executed the required number of times, stop the\r
608         timer. */\r
609         if( *puxVariableToIncrement == staticMAX_TIMER_CALLBACK_EXECUTIONS )\r
610         {\r
611                 /* This is called from a timer callback so must not block.  See\r
612                 http://www.FreeRTOS.org/FreeRTOS-timers-xTimerStop.html */\r
613                 xReturned = xTimerStop( xExpiredTimer, staticDONT_BLOCK );\r
614 \r
615                 if( xReturned != pdPASS )\r
616                 {\r
617                         xErrorOccurred = pdTRUE;\r
618                 }\r
619         }\r
620 }\r
621 /*-----------------------------------------------------------*/\r
622 \r
623 static void prvCreateAndDeleteStaticallyAllocatedTimers( void )\r
624 {\r
625 TimerHandle_t xTimer;\r
626 UBaseType_t uxVariableToIncrement;\r
627 const TickType_t xTimerPeriod = pdMS_TO_TICKS( 20 );\r
628 BaseType_t xReturned;\r
629 \r
630 /* StaticTimer_t is a publicly accessible structure that has the same size\r
631 and alignment requirements as the real timer structure.  It is provided as a\r
632 mechanism for applications to know the size of the timer structure (which is\r
633 dependent on the architecture and configuration file settings) without breaking\r
634 the strict data hiding policy by exposing the real timer internals.  This\r
635 StaticTimer_t variable is passed into the xTimerCreateStatic() function calls\r
636 within this function. */\r
637 StaticTimer_t xTimerBuffer;\r
638 \r
639         /* Create the software time.  xTimerCreateStatic() has an extra parameter\r
640         than the normal xTimerCreate() API function.  The parameter is a pointer to\r
641         the StaticTimer_t structure that will hold the software timer structure.  If\r
642         the parameter is passed as NULL then the structure will be allocated\r
643         dynamically, just as if xTimerCreate() had been called. */\r
644         xTimer = xTimerCreateStatic( "T1",                                      /* Text name for the task.  Helps debugging only.  Not used by FreeRTOS. */\r
645                                                                  xTimerPeriod,                  /* The period of the timer in ticks. */\r
646                                                                  pdTRUE,                                /* This is an auto-reload timer. */\r
647                                                                  ( void * ) &uxVariableToIncrement,     /* The variable incremented by the test is passed into the timer callback using the timer ID. */\r
648                                                                  prvTimerCallback,              /* The function to execute when the timer expires. */\r
649                                                                  &xTimerBuffer );               /* The buffer that will hold the software timer structure. */\r
650 \r
651         /* The timer handle should equal the static timer structure passed into the\r
652         xTimerCreateStatic() function. */\r
653         configASSERT( xTimer == ( TimerHandle_t ) &xTimerBuffer );\r
654 \r
655         /* Set the variable to 0, wait for a few timer periods to expire, then check\r
656         the timer callback has incremented the variable to the expected value. */\r
657         uxVariableToIncrement = 0;\r
658 \r
659         /* This is a low priority so a block time should not be needed. */\r
660         xReturned = xTimerStart( xTimer, staticDONT_BLOCK );\r
661 \r
662         if( xReturned != pdPASS )\r
663         {\r
664                 xErrorOccurred = pdTRUE;\r
665         }\r
666 \r
667         vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );\r
668 \r
669         /* By now the timer should have expired staticMAX_TIMER_CALLBACK_EXECUTIONS\r
670         times, and then stopped itself. */\r
671         if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS )\r
672         {\r
673                 xErrorOccurred = pdTRUE;\r
674         }\r
675 \r
676         /* Finished with the timer, delete it. */\r
677         xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );\r
678 \r
679         /* Again, as this is a low priority task it is expected that the timer\r
680         command will have been sent even without a block time being used. */\r
681         if( xReturned != pdPASS )\r
682         {\r
683                 xErrorOccurred = pdTRUE;\r
684         }\r
685 \r
686         /* Just to show the check task that this task is still executing. */\r
687         uxCycleCounter++;\r
688 \r
689         /* Now do the same using a dynamically allocated software timer to ensure\r
690         the delete function is working correctly in both the static and dynamic\r
691         allocation cases. */\r
692         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
693         {\r
694                 xTimer = xTimerCreate( "T1",                                                            /* Text name for the task.  Helps debugging only.  Not used by FreeRTOS. */\r
695                                                             xTimerPeriod,                                               /* The period of the timer in ticks. */\r
696                                                                 pdTRUE,                                                         /* This is an auto-reload timer. */\r
697                                                                 ( void * ) &uxVariableToIncrement,      /* The variable incremented by the test is passed into the timer callback using the timer ID. */\r
698                                                                 prvTimerCallback );                                     /* The function to execute when the timer expires. */\r
699 \r
700                 configASSERT( xTimer != NULL );\r
701 \r
702                 uxVariableToIncrement = 0;\r
703                 xReturned = xTimerStart( xTimer, staticDONT_BLOCK );\r
704 \r
705                 if( xReturned != pdPASS )\r
706                 {\r
707                         xErrorOccurred = pdTRUE;\r
708                 }\r
709 \r
710                 vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );\r
711 \r
712                 if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS )\r
713                 {\r
714                         xErrorOccurred = pdTRUE;\r
715                 }\r
716 \r
717                 xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );\r
718 \r
719                 if( xReturned != pdPASS )\r
720                 {\r
721                         xErrorOccurred = pdTRUE;\r
722                 }\r
723         }\r
724         #endif\r
725 }\r
726 /*-----------------------------------------------------------*/\r
727 \r
728 static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void )\r
729 {\r
730 EventGroupHandle_t xEventGroup;\r
731 \r
732 /* StaticEventGroup_t is a publicly accessible structure that has the same size\r
733 and alignment requirements as the real event group structure.  It is provided as\r
734 a mechanism for applications to know the size of the event group (which is\r
735 dependent on the architecture and configuration file settings) without breaking\r
736 the strict data hiding policy by exposing the real event group internals.  This\r
737 StaticEventGroup_t variable is passed into the xSemaphoreCreateEventGroupStatic()\r
738 function calls within this function. */\r
739 StaticEventGroup_t xEventGroupBuffer;\r
740 \r
741         /* Create the event group.  xEventGroupCreateStatic() has an extra parameter\r
742         than the normal xEventGroupCreate() API function.  The parameter is a\r
743         pointer to the StaticEventGroup_t structure that will hold the event group\r
744         structure.  If the parameter is passed as NULL then the structure will be\r
745         allocated dynamically, just as if xEventGroupCreate() had been called. */\r
746         xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );\r
747 \r
748         /* The event group handle should equal the static event group structure\r
749         passed into the xEventGroupCreateStatic() function. */\r
750         configASSERT( xEventGroup == ( EventGroupHandle_t ) &xEventGroupBuffer );\r
751 \r
752         /* Ensure the event group passes a few sanity checks as a valid event\r
753         group. */\r
754         prvSanityCheckCreatedEventGroup( xEventGroup );\r
755 \r
756         /* Delete the event group again so the buffers can be reused. */\r
757         vEventGroupDelete( xEventGroup );\r
758 \r
759         /* Now do the same using a dynamically allocated event group to ensure the\r
760         delete function is working correctly in both the static and dynamic\r
761         allocation cases. */\r
762         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
763         {\r
764                 xEventGroup = xEventGroupCreate();\r
765                 configASSERT( xEventGroup != NULL );\r
766                 prvSanityCheckCreatedEventGroup( xEventGroup );\r
767                 vEventGroupDelete( xEventGroup );\r
768         }\r
769         #endif\r
770 }\r
771 /*-----------------------------------------------------------*/\r
772 \r
773 static void prvCreateAndDeleteStaticallyAllocatedTasks( void )\r
774 {\r
775 TaskHandle_t xCreatedTask;\r
776 BaseType_t xReturned;\r
777 \r
778 /* The variable that will hold the TCB of tasks created by this function.  See\r
779 the comments above the declaration of the xCreatorTaskTCBBuffer variable for\r
780 more information. */\r
781 StaticTask_t xTCBBuffer;\r
782 \r
783 /* This buffer that will be used as the stack of tasks created by this function.\r
784 See the comments above the declaration of the uxCreatorTaskStackBuffer[] array\r
785 above for more information. */\r
786 static StackType_t uxStackBuffer[ configMINIMAL_STACK_SIZE ];\r
787 \r
788         /* Create the task.  xTaskCreateStatic() has two more parameters than\r
789         the usual xTaskCreate() function.  The first new parameter is a pointer to\r
790         the pre-allocated stack.  The second new parameter is a pointer to the\r
791         StaticTask_t structure that will hold the task's TCB.  If both pointers are\r
792         passed as NULL then the respective object will be allocated dynamically as\r
793         if xTaskCreate() had been called. */\r
794         xReturned = xTaskCreateStatic(\r
795                                                 prvStaticallyAllocatedTask,     /* Function that implements the task. */\r
796                                                 "Static",                                               /* Human readable name for the task. */\r
797                                                 configMINIMAL_STACK_SIZE,               /* Task's stack size, in words (not bytes!). */\r
798                                                 NULL,                                                   /* Parameter to pass into the task. */\r
799                                                 uxTaskPriorityGet( NULL ) + 1,  /* The priority of the task. */\r
800                                                 &xCreatedTask,                                  /* Handle of the task being created. */\r
801                                                 &( uxStackBuffer[ 0 ] ),                /* The buffer to use as the task's stack. */\r
802                                                 &xTCBBuffer );                                  /* The variable that will hold that task's TCB. */\r
803 \r
804         /* The created task had a higher priority so should have executed and\r
805         suspended itself by now. */\r
806         if( eTaskGetState( xCreatedTask ) != eSuspended )\r
807         {\r
808                 xErrorOccurred = pdTRUE;\r
809         }\r
810 \r
811         /* Check the task was created correctly, then delete the task. */\r
812         configASSERT( xReturned == pdPASS );\r
813         if( xReturned != pdPASS )\r
814         {\r
815                 xErrorOccurred = pdTRUE;\r
816         }\r
817         vTaskDelete( xCreatedTask );\r
818 \r
819         /* Now do the same using a dynamically allocated task to ensure the delete\r
820         function is working correctly in both the static and dynamic allocation\r
821         cases. */\r
822         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
823         {\r
824                 xReturned = xTaskCreate(\r
825                                                                         prvStaticallyAllocatedTask,             /* Function that implements the task. */\r
826                                                                         "Static",                                               /* Human readable name for the task. */\r
827                                                                         configMINIMAL_STACK_SIZE,               /* Task's stack size, in words (not bytes!). */\r
828                                                                         NULL,                                                   /* Parameter to pass into the task. */\r
829                                                                         uxTaskPriorityGet( NULL ) + 1,  /* The priority of the task. */\r
830                                                                         &xCreatedTask );                                /* Handle of the task being created. */\r
831 \r
832                 if( eTaskGetState( xCreatedTask ) != eSuspended )\r
833                 {\r
834                         xErrorOccurred = pdTRUE;\r
835                 }\r
836 \r
837                 configASSERT( xReturned == pdPASS );\r
838                 if( xReturned != pdPASS )\r
839                 {\r
840                         xErrorOccurred = pdTRUE;\r
841                 }\r
842                 vTaskDelete( xCreatedTask );\r
843         }\r
844         #endif\r
845 }\r
846 /*-----------------------------------------------------------*/\r
847 \r
848 static void prvStaticallyAllocatedTask( void *pvParameters )\r
849 {\r
850         ( void ) pvParameters;\r
851 \r
852         /* The created task just suspends itself to wait to get deleted.  The task\r
853         that creates this task checks this task is in the expected Suspended state\r
854         before deleting it. */\r
855         vTaskSuspend( NULL );\r
856 }\r
857 /*-----------------------------------------------------------*/\r
858 \r
859 static UBaseType_t prvRand( void )\r
860 {\r
861 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;\r
862 \r
863         /* Utility function to generate a pseudo random number. */\r
864         ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
865         return( ( ulNextRand >> 16UL ) & 0x7fffUL );\r
866 }\r
867 /*-----------------------------------------------------------*/\r
868 \r
869 static TickType_t prvGetNextDelayTime( void )\r
870 {\r
871 TickType_t xNextDelay;\r
872 const TickType_t xMaxDelay = pdMS_TO_TICKS( ( TickType_t ) 150 );\r
873 const TickType_t xMinDelay = pdMS_TO_TICKS( ( TickType_t ) 75 );\r
874 const TickType_t xTinyDelay = pdMS_TO_TICKS( ( TickType_t ) 2 );\r
875 \r
876         /* Generate the next delay time.  This is kept within a narrow band so as\r
877         not to disturb the timing of other tests - but does add in some pseudo\r
878         randomisation into the tests. */\r
879         do\r
880         {\r
881                 xNextDelay = prvRand() % xMaxDelay;\r
882 \r
883                 /* Just in case this loop is executed lots of times. */\r
884                 vTaskDelay( xTinyDelay );\r
885 \r
886         } while ( xNextDelay < xMinDelay );\r
887 \r
888         return xNextDelay;\r
889 }\r
890 /*-----------------------------------------------------------*/\r
891 \r
892 static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup )\r
893 {\r
894 EventBits_t xEventBits;\r
895 const EventBits_t xFirstTestBits = ( EventBits_t ) 0xaa, xSecondTestBits = ( EventBits_t ) 0x55;\r
896 \r
897         /* The event group should not have any bits set yet. */\r
898         xEventBits = xEventGroupGetBits( xEventGroup );\r
899 \r
900         if( xEventBits != ( EventBits_t ) 0 )\r
901         {\r
902                 xErrorOccurred = pdTRUE;\r
903         }\r
904 \r
905         /* Some some bits, then read them back to check they are as expected. */\r
906         xEventGroupSetBits( xEventGroup, xFirstTestBits );\r
907 \r
908         xEventBits = xEventGroupGetBits( xEventGroup );\r
909 \r
910         if( xEventBits != xFirstTestBits )\r
911         {\r
912                 xErrorOccurred = pdTRUE;\r
913         }\r
914 \r
915         xEventGroupSetBits( xEventGroup, xSecondTestBits );\r
916 \r
917         xEventBits = xEventGroupGetBits( xEventGroup );\r
918 \r
919         if( xEventBits != ( xFirstTestBits | xSecondTestBits ) )\r
920         {\r
921                 xErrorOccurred = pdTRUE;\r
922         }\r
923 \r
924         /* Finally try clearing some bits too and check that operation proceeds as\r
925         expected. */\r
926         xEventGroupClearBits( xEventGroup, xFirstTestBits );\r
927 \r
928         xEventBits = xEventGroupGetBits( xEventGroup );\r
929 \r
930         if( xEventBits != xSecondTestBits )\r
931         {\r
932                 xErrorOccurred = pdTRUE;\r
933         }\r
934 }\r
935 /*-----------------------------------------------------------*/\r
936 \r
937 static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount )\r
938 {\r
939 BaseType_t xReturned;\r
940 UBaseType_t x;\r
941 const TickType_t xShortBlockTime = pdMS_TO_TICKS( 10 );\r
942 TickType_t xTickCount;\r
943 \r
944         /* The binary semaphore should start 'empty', so a call to xSemaphoreTake()\r
945         should fail. */\r
946         xTickCount = xTaskGetTickCount();\r
947         xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );\r
948 \r
949         if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime )\r
950         {\r
951                 /* Did not block on the semaphore as long as expected. */\r
952                 xErrorOccurred = pdTRUE;\r
953         }\r
954 \r
955         if( xReturned != pdFAIL )\r
956         {\r
957                 xErrorOccurred = pdTRUE;\r
958         }\r
959 \r
960         /* Should be possible to 'give' the semaphore up to a maximum of uxMaxCount\r
961         times. */\r
962         for( x = 0; x < uxMaxCount; x++ )\r
963         {\r
964                 xReturned = xSemaphoreGive( xSemaphore );\r
965 \r
966                 if( xReturned == pdFAIL )\r
967                 {\r
968                         xErrorOccurred = pdTRUE;\r
969                 }\r
970         }\r
971 \r
972         /* Giving the semaphore again should fail, as it is 'full'. */\r
973         xReturned = xSemaphoreGive( xSemaphore );\r
974 \r
975         if( xReturned != pdFAIL )\r
976         {\r
977                 xErrorOccurred = pdTRUE;\r
978         }\r
979 \r
980         configASSERT( uxSemaphoreGetCount( xSemaphore ) == uxMaxCount );\r
981 \r
982         /* Should now be possible to 'take' the semaphore up to a maximum of\r
983         uxMaxCount times without blocking. */\r
984         for( x = 0; x < uxMaxCount; x++ )\r
985         {\r
986                 xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );\r
987 \r
988                 if( xReturned == pdFAIL )\r
989                 {\r
990                         xErrorOccurred = pdTRUE;\r
991                 }\r
992         }\r
993 \r
994         /* Back to the starting condition, where the semaphore should not be\r
995         available. */\r
996         xTickCount = xTaskGetTickCount();\r
997         xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );\r
998 \r
999         if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime )\r
1000         {\r
1001                 /* Did not block on the semaphore as long as expected. */\r
1002                 xErrorOccurred = pdTRUE;\r
1003         }\r
1004 \r
1005         if( xReturned != pdFAIL )\r
1006         {\r
1007                 xErrorOccurred = pdTRUE;\r
1008         }\r
1009 \r
1010         configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );\r
1011 }\r
1012 /*-----------------------------------------------------------*/\r
1013 \r
1014 static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue )\r
1015 {\r
1016 uint64_t ull, ullRead;\r
1017 BaseType_t xReturned, xLoop;\r
1018 \r
1019         /* This test is done twice to ensure the queue storage area wraps. */\r
1020         for( xLoop = 0; xLoop < 2; xLoop++ )\r
1021         {\r
1022                 /* A very basic test that the queue can be written to and read from as\r
1023                 expected.  First the queue should be empty. */\r
1024                 xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );\r
1025                 if( xReturned != errQUEUE_EMPTY )\r
1026                 {\r
1027                         xErrorOccurred = pdTRUE;\r
1028                 }\r
1029 \r
1030                 /* Now it should be possible to write to the queue staticQUEUE_LENGTH_IN_ITEMS\r
1031                 times. */\r
1032                 for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ )\r
1033                 {\r
1034                         xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );\r
1035                         if( xReturned != pdPASS )\r
1036                         {\r
1037                                 xErrorOccurred = pdTRUE;\r
1038                         }\r
1039                 }\r
1040 \r
1041                 /* Should not now be possible to write to the queue again. */\r
1042                 xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );\r
1043                 if( xReturned != errQUEUE_FULL )\r
1044                 {\r
1045                         xErrorOccurred = pdTRUE;\r
1046                 }\r
1047 \r
1048                 /* Now read back from the queue to ensure the data read back matches that\r
1049                 written. */\r
1050                 for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ )\r
1051                 {\r
1052                         xReturned = xQueueReceive( xQueue, &ullRead, staticDONT_BLOCK );\r
1053 \r
1054                         if( xReturned != pdPASS )\r
1055                         {\r
1056                                 xErrorOccurred = pdTRUE;\r
1057                         }\r
1058 \r
1059                         if( ullRead != ull )\r
1060                         {\r
1061                                 xErrorOccurred = pdTRUE;\r
1062                         }\r
1063                 }\r
1064 \r
1065                 /* The queue should be empty again. */\r
1066                 xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );\r
1067                 if( xReturned != errQUEUE_EMPTY )\r
1068                 {\r
1069                         xErrorOccurred = pdTRUE;\r
1070                 }\r
1071         }\r
1072 }\r
1073 /*-----------------------------------------------------------*/\r
1074 \r
1075 static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore )\r
1076 {\r
1077 const BaseType_t xLoops = 5;\r
1078 BaseType_t x, xReturned;\r
1079 \r
1080         /* A very basic test that the recursive semaphore behaved like a recursive\r
1081         semaphore. First the semaphore should not be able to be given, as it has not\r
1082         yet been taken. */\r
1083         xReturned = xSemaphoreGiveRecursive( xSemaphore );\r
1084 \r
1085         if( xReturned != pdFAIL )\r
1086         {\r
1087                 xErrorOccurred = pdTRUE;\r
1088         }\r
1089 \r
1090         /* Now it should be possible to take the mutex a number of times. */\r
1091         for( x = 0; x < xLoops; x++ )\r
1092         {\r
1093                 xReturned = xSemaphoreTakeRecursive( xSemaphore, staticDONT_BLOCK );\r
1094 \r
1095                 if( xReturned != pdPASS )\r
1096                 {\r
1097                         xErrorOccurred = pdTRUE;\r
1098                 }\r
1099         }\r
1100 \r
1101         /* Should be possible to give the semaphore the same number of times as it\r
1102         was given in the loop above. */\r
1103         for( x = 0; x < xLoops; x++ )\r
1104         {\r
1105                 xReturned = xSemaphoreGiveRecursive( xSemaphore );\r
1106 \r
1107                 if( xReturned != pdPASS )\r
1108                 {\r
1109                         xErrorOccurred = pdTRUE;\r
1110                 }\r
1111         }\r
1112 \r
1113         /* No more gives should be possible though. */\r
1114         xReturned = xSemaphoreGiveRecursive( xSemaphore );\r
1115 \r
1116         if( xReturned != pdFAIL )\r
1117         {\r
1118                 xErrorOccurred = pdTRUE;\r
1119         }\r
1120 }\r
1121 /*-----------------------------------------------------------*/\r
1122 \r
1123 BaseType_t xAreStaticAllocationTasksStillRunning( void )\r
1124 {\r
1125 static UBaseType_t uxLastCycleCounter = 0;\r
1126 BaseType_t xReturn;\r
1127 \r
1128         if( uxCycleCounter == uxLastCycleCounter )\r
1129         {\r
1130                 xErrorOccurred = pdTRUE;\r
1131         }\r
1132         else\r
1133         {\r
1134                 uxLastCycleCounter = uxCycleCounter;\r
1135         }\r
1136 \r
1137         if( xErrorOccurred != pdFALSE )\r
1138         {\r
1139                 xReturn = pdFAIL;\r
1140         }\r
1141         else\r
1142         {\r
1143                 xReturn = pdPASS;\r
1144         }\r
1145 \r
1146         return xReturn;\r
1147 }\r
1148 /*-----------------------------------------------------------*/\r
1149 \r
1150 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */\r
1151 #endif /* configSUPPORT_STATIC_ALLOCATION == 1 */\r