]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/StaticAllocation.c
Update version number ready for next release.
[freertos] / FreeRTOS / Demo / Common / Minimal / StaticAllocation.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /*\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
33  * allocated memory.\r
34  *\r
35  * See http://www.FreeRTOS.org/Static_Vs_Dynamic_Memory_Allocation.html\r
36  */\r
37 \r
38 /* Scheduler include files. */\r
39 #include "FreeRTOS.h"\r
40 #include "task.h"\r
41 #include "queue.h"\r
42 #include "semphr.h"\r
43 #include "event_groups.h"\r
44 #include "timers.h"\r
45 \r
46 /* Demo program include files. */\r
47 #include "StaticAllocation.h"\r
48 \r
49 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */\r
50 #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
51 \r
52 /* The priority at which the task that performs the tests is created. */\r
53 #define staticTASK_PRIORITY                                     ( tskIDLE_PRIORITY + 2 )\r
54 \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
58 \r
59 /* A block time of 0 simply means "don't block". */\r
60 #define staticDONT_BLOCK                                        ( ( TickType_t ) 0 )\r
61 \r
62 /* Binary semaphores have a maximum count of 1. */\r
63 #define staticBINARY_SEMAPHORE_MAX_COUNT        ( 1 )\r
64 \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
67 \r
68 /* The number of times the software timer will execute before stopping itself. */\r
69 #define staticMAX_TIMER_CALLBACK_EXECUTIONS     ( 5 )\r
70 \r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /*\r
75  * The task that repeatedly creates and deletes statically allocated tasks, and\r
76  * other RTOS objects.\r
77  */\r
78 static void prvStaticallyAllocatedCreator( void *pvParameters );\r
79 \r
80 /*\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
83  */\r
84 static void prvTimerCallback( TimerHandle_t xExpiredTimer );\r
85 \r
86 /*\r
87  * A task that is created and deleted multiple times, using both statically and\r
88  * dynamically allocated stack and TCB.\r
89  */\r
90 static void prvStaticallyAllocatedTask( void *pvParameters );\r
91 \r
92 /*\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
95  */\r
96 static void prvCreateAndDeleteStaticallyAllocatedTasks( void );\r
97 \r
98 /*\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
101  */\r
102 static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void );\r
103 \r
104 /*\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
107  */\r
108 static void prvCreateAndDeleteStaticallyAllocatedQueues( void );\r
109 \r
110 /*\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
113  */\r
114 static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void );\r
115 \r
116 /*\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
119  */\r
120 static void prvCreateAndDeleteStaticallyAllocatedTimers( void );\r
121 \r
122 /*\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
125  */\r
126 static void prvCreateAndDeleteStaticallyAllocatedMutexes( void );\r
127 \r
128 /*\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
131  * RAM.\r
132  */\r
133 static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void );\r
134 \r
135 /*\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
138  */\r
139 static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void );\r
140 \r
141 /*\r
142  * Utility function to create pseudo random numbers.\r
143  */\r
144 static UBaseType_t prvRand( void );\r
145 \r
146 /*\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
151  */\r
152 static TickType_t prvGetNextDelayTime( void );\r
153 \r
154 /*\r
155  * Checks the basic operation of a queue after it has been created.\r
156  */\r
157 static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue );\r
158 \r
159 /*\r
160  * Checks the basic operation of a recursive mutex after it has been created.\r
161  */\r
162 static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore );\r
163 \r
164 /*\r
165  * Checks the basic operation of a binary semaphore after it has been created.\r
166  */\r
167 static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount );\r
168 \r
169 /*\r
170  * Checks the basic operation of an event group after it has been created.\r
171  */\r
172 static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup );\r
173 \r
174 /*-----------------------------------------------------------*/\r
175 \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
183 tasks. */\r
184 static StaticTask_t xCreatorTaskTCBBuffer;\r
185 \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
190 \r
191 /* Used by the pseudo random number generating function. */\r
192 static uint32_t ulNextRand = 0;\r
193 \r
194 /* Used so a check task can ensure this test is still executing, and not\r
195 stalled. */\r
196 static volatile UBaseType_t uxCycleCounter = 0;\r
197 \r
198 /* A variable that gets set to pdTRUE if an error is detected. */\r
199 static volatile BaseType_t xErrorOccurred = pdFALSE;\r
200 \r
201 /*-----------------------------------------------------------*/\r
202 \r
203 void vStartStaticallyAllocatedTasks( void  )\r
204 {\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
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void prvStaticallyAllocatedCreator( void *pvParameters )\r
218 {\r
219         /* Avoid compiler warnings. */\r
220         ( void ) pvParameters;\r
221 \r
222         for( ;; )\r
223         {\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
229 \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
232                 executing. */\r
233                 vTaskDelay( prvGetNextDelayTime() );\r
234                 uxCycleCounter++;\r
235 \r
236                 prvCreateAndDeleteStaticallyAllocatedBinarySemaphores();\r
237                 prvCreateAndDeleteStaticallyAllocatedCountingSemaphores();\r
238 \r
239                 vTaskDelay( prvGetNextDelayTime() );\r
240                 uxCycleCounter++;\r
241 \r
242                 prvCreateAndDeleteStaticallyAllocatedMutexes();\r
243                 prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes();\r
244 \r
245                 vTaskDelay( prvGetNextDelayTime() );\r
246                 uxCycleCounter++;\r
247 \r
248                 prvCreateAndDeleteStaticallyAllocatedEventGroups();\r
249                 prvCreateAndDeleteStaticallyAllocatedTimers();\r
250         }\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void )\r
255 {\r
256 SemaphoreHandle_t xSemaphore;\r
257 const UBaseType_t uxMaxCount = ( UBaseType_t ) 10;\r
258 \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
269 \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
277 \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
281 \r
282         /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */\r
283         prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount );\r
284 \r
285         /* Delete the semaphore again so the buffers can be reused. */\r
286         vSemaphoreDelete( xSemaphore );\r
287 \r
288         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
289         {\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
297         }\r
298         #endif\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void )\r
303 {\r
304 SemaphoreHandle_t xSemaphore;\r
305 \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
314 \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
322 \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
326 \r
327         /* Ensure the semaphore passes a few sanity checks as a valid\r
328         recursive semaphore. */\r
329         prvSanityCheckCreatedRecursiveMutex( xSemaphore );\r
330 \r
331         /* Delete the semaphore again so the buffers can be reused. */\r
332         vSemaphoreDelete( xSemaphore );\r
333 \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
338         {\r
339                 xSemaphore = xSemaphoreCreateRecursiveMutex();\r
340                 configASSERT( xSemaphore != NULL );\r
341                 prvSanityCheckCreatedRecursiveMutex( xSemaphore );\r
342                 vSemaphoreDelete( xSemaphore );\r
343         }\r
344         #endif\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 static void prvCreateAndDeleteStaticallyAllocatedQueues( void )\r
349 {\r
350 QueueHandle_t xQueue;\r
351 \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
358 function. */\r
359 static StaticQueue_t xStaticQueue;\r
360 \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
367 \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
378 \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
382 \r
383         /* Ensure the queue passes a few sanity checks as a valid queue. */\r
384         prvSanityCheckCreatedQueue( xQueue );\r
385 \r
386         /* Delete the queue again so the buffers can be reused. */\r
387         vQueueDelete( xQueue );\r
388 \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
393         {\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
396 \r
397                 /* The queue handle should equal the static queue structure passed into the\r
398                 xQueueCreateStatic() function. */\r
399                 configASSERT( xQueue != NULL );\r
400 \r
401                 /* Ensure the queue passes a few sanity checks as a valid queue. */\r
402                 prvSanityCheckCreatedQueue( xQueue );\r
403 \r
404                 /* Delete the queue again so the buffers can be reused. */\r
405                 vQueueDelete( xQueue );\r
406         }\r
407         #endif\r
408 }\r
409 /*-----------------------------------------------------------*/\r
410 \r
411 static void prvCreateAndDeleteStaticallyAllocatedMutexes( void )\r
412 {\r
413 SemaphoreHandle_t xSemaphore;\r
414 BaseType_t xReturned;\r
415 \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
424 \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
432 \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
436 \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
440 \r
441         if( xReturned != pdPASS )\r
442         {\r
443                 xErrorOccurred = pdTRUE;\r
444         }\r
445 \r
446         /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */\r
447         prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
448 \r
449         /* Delete the semaphore again so the buffers can be reused. */\r
450         vSemaphoreDelete( xSemaphore );\r
451 \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
454         cases. */\r
455         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
456         {\r
457                 xSemaphore = xSemaphoreCreateMutex();\r
458 \r
459                 /* The semaphore handle should equal the static semaphore structure\r
460                 passed into the xSemaphoreCreateMutexStatic() function. */\r
461                 configASSERT( xSemaphore != NULL );\r
462 \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
466 \r
467                 if( xReturned != pdPASS )\r
468                 {\r
469                         xErrorOccurred = pdTRUE;\r
470                 }\r
471 \r
472                 /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */\r
473                 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
474 \r
475                 /* Delete the semaphore again so the buffers can be reused. */\r
476                 vSemaphoreDelete( xSemaphore );\r
477         }\r
478         #endif\r
479 }\r
480 /*-----------------------------------------------------------*/\r
481 \r
482 static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void )\r
483 {\r
484 SemaphoreHandle_t xSemaphore;\r
485 \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
496 \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
504 \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
508 \r
509         /* Ensure the semaphore passes a few sanity checks as a valid semaphore. */\r
510         prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
511 \r
512         /* Delete the semaphore again so the buffers can be reused. */\r
513         vSemaphoreDelete( xSemaphore );\r
514 \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
519         {\r
520                 xSemaphore = xSemaphoreCreateBinary();\r
521                 configASSERT( xSemaphore != NULL );\r
522                 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
523                 vSemaphoreDelete( xSemaphore );\r
524         }\r
525         #endif\r
526 \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
531         {\r
532                 vSemaphoreCreateBinary( xSemaphore );\r
533 \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
537                 {\r
538                         xErrorOccurred = pdTRUE;\r
539                 }\r
540 \r
541                 prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );\r
542                 vSemaphoreDelete( xSemaphore );\r
543         }\r
544         #endif\r
545 }\r
546 /*-----------------------------------------------------------*/\r
547 \r
548 static void prvTimerCallback( TimerHandle_t xExpiredTimer )\r
549 {\r
550 UBaseType_t *puxVariableToIncrement;\r
551 BaseType_t xReturned;\r
552 \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
557 \r
558         /* Increment the variable to show the timer callback has executed. */\r
559         ( *puxVariableToIncrement )++;\r
560 \r
561         /* If this callback has executed the required number of times, stop the\r
562         timer. */\r
563         if( *puxVariableToIncrement == staticMAX_TIMER_CALLBACK_EXECUTIONS )\r
564         {\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
568 \r
569                 if( xReturned != pdPASS )\r
570                 {\r
571                         xErrorOccurred = pdTRUE;\r
572                 }\r
573         }\r
574 }\r
575 /*-----------------------------------------------------------*/\r
576 \r
577 static void prvCreateAndDeleteStaticallyAllocatedTimers( void )\r
578 {\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
583 \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
592 \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
604 \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
608 \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
612 \r
613         /* This is a low priority so a block time should not be needed. */\r
614         xReturned = xTimerStart( xTimer, staticDONT_BLOCK );\r
615 \r
616         if( xReturned != pdPASS )\r
617         {\r
618                 xErrorOccurred = pdTRUE;\r
619         }\r
620 \r
621         vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );\r
622 \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
626         {\r
627                 xErrorOccurred = pdTRUE;\r
628         }\r
629 \r
630         /* Finished with the timer, delete it. */\r
631         xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );\r
632 \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
636         {\r
637                 xErrorOccurred = pdTRUE;\r
638         }\r
639 \r
640         /* Just to show the check task that this task is still executing. */\r
641         uxCycleCounter++;\r
642 \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
647         {\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
653 \r
654                 configASSERT( xTimer != NULL );\r
655 \r
656                 uxVariableToIncrement = 0;\r
657                 xReturned = xTimerStart( xTimer, staticDONT_BLOCK );\r
658 \r
659                 if( xReturned != pdPASS )\r
660                 {\r
661                         xErrorOccurred = pdTRUE;\r
662                 }\r
663 \r
664                 vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );\r
665 \r
666                 if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS )\r
667                 {\r
668                         xErrorOccurred = pdTRUE;\r
669                 }\r
670 \r
671                 xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );\r
672 \r
673                 if( xReturned != pdPASS )\r
674                 {\r
675                         xErrorOccurred = pdTRUE;\r
676                 }\r
677         }\r
678         #endif\r
679 }\r
680 /*-----------------------------------------------------------*/\r
681 \r
682 static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void )\r
683 {\r
684 EventGroupHandle_t xEventGroup;\r
685 \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
694 \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
698         structure. */\r
699         xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );\r
700 \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
704 \r
705         /* Ensure the event group passes a few sanity checks as a valid event\r
706         group. */\r
707         prvSanityCheckCreatedEventGroup( xEventGroup );\r
708 \r
709         /* Delete the event group again so the buffers can be reused. */\r
710         vEventGroupDelete( xEventGroup );\r
711 \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
716         {\r
717                 xEventGroup = xEventGroupCreate();\r
718                 configASSERT( xEventGroup != NULL );\r
719                 prvSanityCheckCreatedEventGroup( xEventGroup );\r
720                 vEventGroupDelete( xEventGroup );\r
721         }\r
722         #endif\r
723 }\r
724 /*-----------------------------------------------------------*/\r
725 \r
726 static void prvCreateAndDeleteStaticallyAllocatedTasks( void )\r
727 {\r
728 TaskHandle_t xCreatedTask;\r
729 \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.  NOTE:  This is not static so relies on the tasks that use it\r
733 being deleted before this function returns and deallocates its stack.  That will\r
734 only be the case if configUSE_PREEMPTION is set to 1. */\r
735 StaticTask_t xTCBBuffer;\r
736 \r
737 /* This buffer that will be used as the stack of tasks created by this function.\r
738 See the comments above the declaration of the uxCreatorTaskStackBuffer[] array\r
739 above for more information. */\r
740 static StackType_t uxStackBuffer[ configMINIMAL_STACK_SIZE ];\r
741 \r
742         /* Create the task.  xTaskCreateStatic() has two more parameters than\r
743         the usual xTaskCreate() function.  The first new parameter is a pointer to\r
744         the pre-allocated stack.  The second new parameter is a pointer to the\r
745         StaticTask_t structure that will hold the task's TCB.  If both pointers are\r
746         passed as NULL then the respective object will be allocated dynamically as\r
747         if xTaskCreate() had been called. */\r
748         xCreatedTask = xTaskCreateStatic(\r
749                                                 prvStaticallyAllocatedTask,     /* Function that implements the task. */\r
750                                                 "Static",                                               /* Human readable name for the task. */\r
751                                                 configMINIMAL_STACK_SIZE,               /* Task's stack size, in words (not bytes!). */\r
752                                                 NULL,                                                   /* Parameter to pass into the task. */\r
753                                                 uxTaskPriorityGet( NULL ) + 1,  /* The priority of the task. */\r
754                                                 &( uxStackBuffer[ 0 ] ),                /* The buffer to use as the task's stack. */\r
755                                                 &xTCBBuffer );                                  /* The variable that will hold that task's TCB. */\r
756 \r
757         /* Check the task was created correctly, then delete the task. */\r
758         if( xCreatedTask == NULL )\r
759         {\r
760                 xErrorOccurred = pdTRUE;\r
761         }\r
762         else if( eTaskGetState( xCreatedTask ) != eSuspended )\r
763         {\r
764                 /* The created task had a higher priority so should have executed and\r
765                 suspended itself by now. */\r
766                 xErrorOccurred = pdTRUE;\r
767         }\r
768         else\r
769         {\r
770                 vTaskDelete( xCreatedTask );\r
771         }\r
772 \r
773         /* Now do the same using a dynamically allocated task to ensure the delete\r
774         function is working correctly in both the static and dynamic allocation\r
775         cases. */\r
776         #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )\r
777         {\r
778         BaseType_t xReturned;\r
779 \r
780                 xReturned = xTaskCreate(\r
781                                                                         prvStaticallyAllocatedTask,             /* Function that implements the task - the same function is used but is actually dynamically allocated this time. */\r
782                                                                         "Static",                                               /* Human readable name for the task. */\r
783                                                                         configMINIMAL_STACK_SIZE,               /* Task's stack size, in words (not bytes!). */\r
784                                                                         NULL,                                                   /* Parameter to pass into the task. */\r
785                                                                         uxTaskPriorityGet( NULL ) + 1,  /* The priority of the task. */\r
786                                                                         &xCreatedTask );                                /* Handle of the task being created. */\r
787 \r
788                 if( eTaskGetState( xCreatedTask ) != eSuspended )\r
789                 {\r
790                         xErrorOccurred = pdTRUE;\r
791                 }\r
792 \r
793                 configASSERT( xReturned == pdPASS );\r
794                 if( xReturned != pdPASS )\r
795                 {\r
796                         xErrorOccurred = pdTRUE;\r
797                 }\r
798                 vTaskDelete( xCreatedTask );\r
799         }\r
800         #endif\r
801 }\r
802 /*-----------------------------------------------------------*/\r
803 \r
804 static void prvStaticallyAllocatedTask( void *pvParameters )\r
805 {\r
806         ( void ) pvParameters;\r
807 \r
808         /* The created task just suspends itself to wait to get deleted.  The task\r
809         that creates this task checks this task is in the expected Suspended state\r
810         before deleting it. */\r
811         vTaskSuspend( NULL );\r
812 }\r
813 /*-----------------------------------------------------------*/\r
814 \r
815 static UBaseType_t prvRand( void )\r
816 {\r
817 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;\r
818 \r
819         /* Utility function to generate a pseudo random number. */\r
820         ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
821         return( ( ulNextRand >> 16UL ) & 0x7fffUL );\r
822 }\r
823 /*-----------------------------------------------------------*/\r
824 \r
825 static TickType_t prvGetNextDelayTime( void )\r
826 {\r
827 TickType_t xNextDelay;\r
828 const TickType_t xMaxDelay = pdMS_TO_TICKS( ( TickType_t ) 150 );\r
829 const TickType_t xMinDelay = pdMS_TO_TICKS( ( TickType_t ) 75 );\r
830 const TickType_t xTinyDelay = pdMS_TO_TICKS( ( TickType_t ) 2 );\r
831 \r
832         /* Generate the next delay time.  This is kept within a narrow band so as\r
833         not to disturb the timing of other tests - but does add in some pseudo\r
834         randomisation into the tests. */\r
835         do\r
836         {\r
837                 xNextDelay = prvRand() % xMaxDelay;\r
838 \r
839                 /* Just in case this loop is executed lots of times. */\r
840                 vTaskDelay( xTinyDelay );\r
841 \r
842         } while ( xNextDelay < xMinDelay );\r
843 \r
844         return xNextDelay;\r
845 }\r
846 /*-----------------------------------------------------------*/\r
847 \r
848 static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup )\r
849 {\r
850 EventBits_t xEventBits;\r
851 const EventBits_t xFirstTestBits = ( EventBits_t ) 0xaa, xSecondTestBits = ( EventBits_t ) 0x55;\r
852 \r
853         /* The event group should not have any bits set yet. */\r
854         xEventBits = xEventGroupGetBits( xEventGroup );\r
855 \r
856         if( xEventBits != ( EventBits_t ) 0 )\r
857         {\r
858                 xErrorOccurred = pdTRUE;\r
859         }\r
860 \r
861         /* Some some bits, then read them back to check they are as expected. */\r
862         xEventGroupSetBits( xEventGroup, xFirstTestBits );\r
863 \r
864         xEventBits = xEventGroupGetBits( xEventGroup );\r
865 \r
866         if( xEventBits != xFirstTestBits )\r
867         {\r
868                 xErrorOccurred = pdTRUE;\r
869         }\r
870 \r
871         xEventGroupSetBits( xEventGroup, xSecondTestBits );\r
872 \r
873         xEventBits = xEventGroupGetBits( xEventGroup );\r
874 \r
875         if( xEventBits != ( xFirstTestBits | xSecondTestBits ) )\r
876         {\r
877                 xErrorOccurred = pdTRUE;\r
878         }\r
879 \r
880         /* Finally try clearing some bits too and check that operation proceeds as\r
881         expected. */\r
882         xEventGroupClearBits( xEventGroup, xFirstTestBits );\r
883 \r
884         xEventBits = xEventGroupGetBits( xEventGroup );\r
885 \r
886         if( xEventBits != xSecondTestBits )\r
887         {\r
888                 xErrorOccurred = pdTRUE;\r
889         }\r
890 }\r
891 /*-----------------------------------------------------------*/\r
892 \r
893 static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount )\r
894 {\r
895 BaseType_t xReturned;\r
896 UBaseType_t x;\r
897 const TickType_t xShortBlockTime = pdMS_TO_TICKS( 10 );\r
898 TickType_t xTickCount;\r
899 \r
900         /* The binary semaphore should start 'empty', so a call to xSemaphoreTake()\r
901         should fail. */\r
902         xTickCount = xTaskGetTickCount();\r
903         xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );\r
904 \r
905         if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime )\r
906         {\r
907                 /* Did not block on the semaphore as long as expected. */\r
908                 xErrorOccurred = pdTRUE;\r
909         }\r
910 \r
911         if( xReturned != pdFAIL )\r
912         {\r
913                 xErrorOccurred = pdTRUE;\r
914         }\r
915 \r
916         /* Should be possible to 'give' the semaphore up to a maximum of uxMaxCount\r
917         times. */\r
918         for( x = 0; x < uxMaxCount; x++ )\r
919         {\r
920                 xReturned = xSemaphoreGive( xSemaphore );\r
921 \r
922                 if( xReturned == pdFAIL )\r
923                 {\r
924                         xErrorOccurred = pdTRUE;\r
925                 }\r
926         }\r
927 \r
928         /* Giving the semaphore again should fail, as it is 'full'. */\r
929         xReturned = xSemaphoreGive( xSemaphore );\r
930 \r
931         if( xReturned != pdFAIL )\r
932         {\r
933                 xErrorOccurred = pdTRUE;\r
934         }\r
935 \r
936         configASSERT( uxSemaphoreGetCount( xSemaphore ) == uxMaxCount );\r
937 \r
938         /* Should now be possible to 'take' the semaphore up to a maximum of\r
939         uxMaxCount times without blocking. */\r
940         for( x = 0; x < uxMaxCount; x++ )\r
941         {\r
942                 xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );\r
943 \r
944                 if( xReturned == pdFAIL )\r
945                 {\r
946                         xErrorOccurred = pdTRUE;\r
947                 }\r
948         }\r
949 \r
950         /* Back to the starting condition, where the semaphore should not be\r
951         available. */\r
952         xTickCount = xTaskGetTickCount();\r
953         xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );\r
954 \r
955         if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime )\r
956         {\r
957                 /* Did not block on the semaphore as long as expected. */\r
958                 xErrorOccurred = pdTRUE;\r
959         }\r
960 \r
961         if( xReturned != pdFAIL )\r
962         {\r
963                 xErrorOccurred = pdTRUE;\r
964         }\r
965 \r
966         configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );\r
967 }\r
968 /*-----------------------------------------------------------*/\r
969 \r
970 static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue )\r
971 {\r
972 uint64_t ull, ullRead;\r
973 BaseType_t xReturned, xLoop;\r
974 \r
975         /* This test is done twice to ensure the queue storage area wraps. */\r
976         for( xLoop = 0; xLoop < 2; xLoop++ )\r
977         {\r
978                 /* A very basic test that the queue can be written to and read from as\r
979                 expected.  First the queue should be empty. */\r
980                 xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );\r
981                 if( xReturned != errQUEUE_EMPTY )\r
982                 {\r
983                         xErrorOccurred = pdTRUE;\r
984                 }\r
985 \r
986                 /* Now it should be possible to write to the queue staticQUEUE_LENGTH_IN_ITEMS\r
987                 times. */\r
988                 for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ )\r
989                 {\r
990                         xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );\r
991                         if( xReturned != pdPASS )\r
992                         {\r
993                                 xErrorOccurred = pdTRUE;\r
994                         }\r
995                 }\r
996 \r
997                 /* Should not now be possible to write to the queue again. */\r
998                 xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );\r
999                 if( xReturned != errQUEUE_FULL )\r
1000                 {\r
1001                         xErrorOccurred = pdTRUE;\r
1002                 }\r
1003 \r
1004                 /* Now read back from the queue to ensure the data read back matches that\r
1005                 written. */\r
1006                 for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ )\r
1007                 {\r
1008                         xReturned = xQueueReceive( xQueue, &ullRead, staticDONT_BLOCK );\r
1009 \r
1010                         if( xReturned != pdPASS )\r
1011                         {\r
1012                                 xErrorOccurred = pdTRUE;\r
1013                         }\r
1014 \r
1015                         if( ullRead != ull )\r
1016                         {\r
1017                                 xErrorOccurred = pdTRUE;\r
1018                         }\r
1019                 }\r
1020 \r
1021                 /* The queue should be empty again. */\r
1022                 xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );\r
1023                 if( xReturned != errQUEUE_EMPTY )\r
1024                 {\r
1025                         xErrorOccurred = pdTRUE;\r
1026                 }\r
1027         }\r
1028 }\r
1029 /*-----------------------------------------------------------*/\r
1030 \r
1031 static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore )\r
1032 {\r
1033 const BaseType_t xLoops = 5;\r
1034 BaseType_t x, xReturned;\r
1035 \r
1036         /* A very basic test that the recursive semaphore behaved like a recursive\r
1037         semaphore. First the semaphore should not be able to be given, as it has not\r
1038         yet been taken. */\r
1039         xReturned = xSemaphoreGiveRecursive( xSemaphore );\r
1040 \r
1041         if( xReturned != pdFAIL )\r
1042         {\r
1043                 xErrorOccurred = pdTRUE;\r
1044         }\r
1045 \r
1046         /* Now it should be possible to take the mutex a number of times. */\r
1047         for( x = 0; x < xLoops; x++ )\r
1048         {\r
1049                 xReturned = xSemaphoreTakeRecursive( xSemaphore, staticDONT_BLOCK );\r
1050 \r
1051                 if( xReturned != pdPASS )\r
1052                 {\r
1053                         xErrorOccurred = pdTRUE;\r
1054                 }\r
1055         }\r
1056 \r
1057         /* Should be possible to give the semaphore the same number of times as it\r
1058         was given in the loop above. */\r
1059         for( x = 0; x < xLoops; x++ )\r
1060         {\r
1061                 xReturned = xSemaphoreGiveRecursive( xSemaphore );\r
1062 \r
1063                 if( xReturned != pdPASS )\r
1064                 {\r
1065                         xErrorOccurred = pdTRUE;\r
1066                 }\r
1067         }\r
1068 \r
1069         /* No more gives should be possible though. */\r
1070         xReturned = xSemaphoreGiveRecursive( xSemaphore );\r
1071 \r
1072         if( xReturned != pdFAIL )\r
1073         {\r
1074                 xErrorOccurred = pdTRUE;\r
1075         }\r
1076 }\r
1077 /*-----------------------------------------------------------*/\r
1078 \r
1079 BaseType_t xAreStaticAllocationTasksStillRunning( void )\r
1080 {\r
1081 static UBaseType_t uxLastCycleCounter = 0;\r
1082 BaseType_t xReturn;\r
1083 \r
1084         if( uxCycleCounter == uxLastCycleCounter )\r
1085         {\r
1086                 xErrorOccurred = pdTRUE;\r
1087         }\r
1088         else\r
1089         {\r
1090                 uxLastCycleCounter = uxCycleCounter;\r
1091         }\r
1092 \r
1093         if( xErrorOccurred != pdFALSE )\r
1094         {\r
1095                 xReturn = pdFAIL;\r
1096         }\r
1097         else\r
1098         {\r
1099                 xReturn = pdPASS;\r
1100         }\r
1101 \r
1102         return xReturn;\r
1103 }\r
1104 /*-----------------------------------------------------------*/\r
1105 \r
1106 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */\r
1107 #endif /* configSUPPORT_STATIC_ALLOCATION == 1 */\r