]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/IntQueue.c
Change to the file headers only.
[freertos] / Demo / Common / Minimal / IntQueue.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * This file defines one of the more complex set of demo/test tasks.  They are\r
56  * designed to stress test the queue implementation though pseudo simultaneous\r
57  * multiple reads and multiple writes from both tasks of varying priority and\r
58  * interrupts.  The interrupts are prioritised such to ensure that nesting\r
59  * occurs (for those ports that support it).\r
60  *\r
61  * The test ensures that, while being accessed from three tasks and two\r
62  * interrupts, all the data sent to the queues is also received from\r
63  * the same queue, and that no duplicate items are either sent or received.\r
64  * The tests also ensure that a low priority task is never able to successfully\r
65  * read from or write to a queue when a task of higher priority is attempting\r
66  * the same operation.\r
67  */\r
68 \r
69 /* Standard includes. */\r
70 #include <string.h>\r
71 \r
72 /* SafeRTOS includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "queue.h"\r
75 #include "task.h"\r
76 \r
77 /* Demo app includes. */\r
78 #include "IntQueue.h"\r
79 #include "IntQueueTimer.h"\r
80 \r
81 /* Priorities used by test tasks. */\r
82 #define intqHIGHER_PRIORITY             ( configMAX_PRIORITIES - 2 )\r
83 #define intqLOWER_PRIORITY              ( tskIDLE_PRIORITY )\r
84 \r
85 /* The number of values to send/receive before checking that all values were\r
86 processed as expected. */\r
87 #define intqNUM_VALUES_TO_LOG   ( 200 )\r
88 #define intqSHORT_DELAY                 ( 75 )\r
89 \r
90 /* The value by which the value being sent to or received from a queue should\r
91 increment past intqNUM_VALUES_TO_LOG before we check that all values have been\r
92 sent/received correctly.  This is done to ensure that all tasks and interrupts\r
93 accessing the queue have completed their accesses with the\r
94 intqNUM_VALUES_TO_LOG range. */\r
95 #define intqVALUE_OVERRUN               ( 50 )\r
96 \r
97 /* The delay used by the polling task.  A short delay is used for code\r
98 coverage. */\r
99 #define intqONE_TICK_DELAY              ( 1 )\r
100 \r
101 /* Each task and interrupt is given a unique identifier.  This value is used to\r
102 identify which task sent or received each value.  The identifier is also used\r
103 to distinguish between two tasks that are running the same task function. */\r
104 #define intqHIGH_PRIORITY_TASK1 ( ( unsigned portBASE_TYPE ) 1 )\r
105 #define intqHIGH_PRIORITY_TASK2 ( ( unsigned portBASE_TYPE ) 2 )\r
106 #define intqLOW_PRIORITY_TASK   ( ( unsigned portBASE_TYPE ) 3 )\r
107 #define intqFIRST_INTERRUPT             ( ( unsigned portBASE_TYPE ) 4 )\r
108 #define intqSECOND_INTERRUPT    ( ( unsigned portBASE_TYPE ) 5 )\r
109 #define intqQUEUE_LENGTH                ( ( unsigned portBASE_TYPE ) 10 )\r
110 \r
111 /* At least intqMIN_ACCEPTABLE_TASK_COUNT values should be sent to/received\r
112 from each queue by each task, otherwise an error is detected. */\r
113 #define intqMIN_ACCEPTABLE_TASK_COUNT           ( 5 )\r
114 \r
115 /* Send the next value to the queue that is normally empty.  This is called\r
116 from within the interrupts. */\r
117 #define timerNORMALLY_EMPTY_TX()                                                                                                                                                                                        \\r
118         if( xQueueIsQueueFullFromISR( xNormallyEmptyQueue ) != pdTRUE )                                                                                                                 \\r
119         {                                                                                                                                                                                                                                               \\r
120         unsigned portBASE_TYPE uxSavedInterruptStatus;                                                                                                                                                  \\r
121                 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();                                                                                                                     \\r
122                 {                                                                                                                                                                                                                                       \\r
123                         uxValueForNormallyEmptyQueue++;                                                                                                                                                                 \\r
124                         xQueueSendFromISR( xNormallyEmptyQueue, ( void * ) &uxValueForNormallyEmptyQueue, &xHigherPriorityTaskWoken );  \\r
125                 }                                                                                                                                                                                                                                       \\r
126                 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );                                                                                                            \\r
127         }                                                                                                                                                                                                                                               \\r
128 \r
129 /* Send the next value to the queue that is normally full.  This is called\r
130 from within the interrupts. */\r
131 #define timerNORMALLY_FULL_TX()                                                                                                                                                                                         \\r
132         if( xQueueIsQueueFullFromISR( xNormallyFullQueue ) != pdTRUE )                                                                                                                  \\r
133         {                                                                                                                                                                                                                                               \\r
134         unsigned portBASE_TYPE uxSavedInterruptStatus;                                                                                                                                                  \\r
135                 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();                                                                                                                     \\r
136                 {                                                                                                                                                                                                                                       \\r
137                         uxValueForNormallyFullQueue++;                                                                                                                                                                  \\r
138                         xQueueSendFromISR( xNormallyFullQueue, ( void * ) &uxValueForNormallyFullQueue, &xHigherPriorityTaskWoken );    \\r
139                 }                                                                                                                                                                                                                                       \\r
140                 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );                                                                                                            \\r
141         }                                                                                                                                                                                                                                               \\r
142 \r
143 /* Receive a value from the normally empty queue.  This is called from within\r
144 an interrupt. */\r
145 #define timerNORMALLY_EMPTY_RX()                                                                                                                                                        \\r
146         if( xQueueReceiveFromISR( xNormallyEmptyQueue, &uxRxedValue, &xHigherPriorityTaskWoken ) != pdPASS )    \\r
147         {                                                                                                                                                                                                               \\r
148                 prvQueueAccessLogError( __LINE__ );                                                                                                                                     \\r
149         }                                                                                                                                                                                                               \\r
150         else                                                                                                                                                                                                    \\r
151         {                                                                                                                                                                                                               \\r
152                 prvRecordValue_NormallyEmpty( uxRxedValue, intqSECOND_INTERRUPT );                                                                      \\r
153         }\r
154 \r
155 /* Receive a value from the normally full queue.  This is called from within\r
156 an interrupt. */\r
157 #define timerNORMALLY_FULL_RX()                                                                                                                                                         \\r
158         if( xQueueReceiveFromISR( xNormallyFullQueue, &uxRxedValue, &xHigherPriorityTaskWoken ) == pdPASS )             \\r
159         {                                                                                                                                                                                                               \\r
160                 prvRecordValue_NormallyFull( uxRxedValue, intqSECOND_INTERRUPT );                                                                       \\r
161         }                                                                                                                                                                                                               \\r
162 \r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /* The two queues used by the test. */\r
167 static xQueueHandle xNormallyEmptyQueue, xNormallyFullQueue;\r
168 \r
169 /* Variables used to detect a stall in one of the tasks. */\r
170 static unsigned portBASE_TYPE uxHighPriorityLoops1 = 0, uxHighPriorityLoops2 = 0, uxLowPriorityLoops1 = 0, uxLowPriorityLoops2 = 0;\r
171 \r
172 /* Any unexpected behaviour sets xErrorStatus to fail and log the line that\r
173 caused the error in xErrorLine. */\r
174 static portBASE_TYPE xErrorStatus = pdPASS;\r
175 static unsigned portBASE_TYPE xErrorLine = ( unsigned portBASE_TYPE ) 0;\r
176 \r
177 /* Used for sequencing between tasks. */\r
178 static portBASE_TYPE xWasSuspended = pdFALSE;\r
179 \r
180 /* The values that are sent to the queues.  An incremented value is sent each\r
181 time to each queue. */\r
182 volatile unsigned portBASE_TYPE uxValueForNormallyEmptyQueue = 0, uxValueForNormallyFullQueue = 0;\r
183 \r
184 /* A handle to some of the tasks is required so they can be suspended/resumed. */\r
185 xTaskHandle xHighPriorityNormallyEmptyTask1, xHighPriorityNormallyEmptyTask2, xHighPriorityNormallyFullTask1, xHighPriorityNormallyFullTask2;\r
186 \r
187 /* When a value is received in a queue the value is ticked off in the array\r
188 the array position of the value is set to a the identifier of the task or\r
189 interrupt that accessed the queue.  This way missing or duplicate values can be\r
190 detected. */\r
191 static unsigned portCHAR ucNormallyEmptyReceivedValues[ intqNUM_VALUES_TO_LOG ] = { 0 };\r
192 static unsigned portCHAR ucNormallyFullReceivedValues[ intqNUM_VALUES_TO_LOG ] = { 0 };\r
193 \r
194 /* The test tasks themselves. */\r
195 static void prvLowerPriorityNormallyEmptyTask( void *pvParameters );\r
196 static void prvLowerPriorityNormallyFullTask( void *pvParameters );\r
197 static void prvHigherPriorityNormallyEmptyTask( void *pvParameters );\r
198 static void prv1stHigherPriorityNormallyFullTask( void *pvParameters );\r
199 static void prv2ndHigherPriorityNormallyFullTask( void *pvParameters );\r
200 \r
201 /* Used to mark the positions within the ucNormallyEmptyReceivedValues and\r
202 ucNormallyFullReceivedValues arrays, while checking for duplicates. */\r
203 static void prvRecordValue_NormallyEmpty( unsigned portBASE_TYPE uxValue, unsigned portBASE_TYPE uxSource );\r
204 static void prvRecordValue_NormallyFull( unsigned portBASE_TYPE uxValue, unsigned portBASE_TYPE uxSource );\r
205 \r
206 /* Logs the line on which an error occurred. */\r
207 static void prvQueueAccessLogError( unsigned portBASE_TYPE uxLine );\r
208 \r
209 /*-----------------------------------------------------------*/\r
210 \r
211 void vStartInterruptQueueTasks( void )\r
212 {\r
213         /* Start the test tasks. */\r
214         xTaskCreate( prvHigherPriorityNormallyEmptyTask, ( signed portCHAR * ) "H1QRx", configMINIMAL_STACK_SIZE, ( void * ) intqHIGH_PRIORITY_TASK1, intqHIGHER_PRIORITY, &xHighPriorityNormallyEmptyTask1 );\r
215         xTaskCreate( prvHigherPriorityNormallyEmptyTask, ( signed portCHAR * ) "H2QRx", configMINIMAL_STACK_SIZE, ( void * ) intqHIGH_PRIORITY_TASK2, intqHIGHER_PRIORITY, &xHighPriorityNormallyEmptyTask2 );\r
216         xTaskCreate( prvLowerPriorityNormallyEmptyTask, ( signed portCHAR * ) "LQRx", configMINIMAL_STACK_SIZE, NULL, intqLOWER_PRIORITY, NULL );\r
217         xTaskCreate( prv1stHigherPriorityNormallyFullTask, ( signed portCHAR * ) "H1QTx", configMINIMAL_STACK_SIZE, ( void * ) intqHIGH_PRIORITY_TASK1, intqHIGHER_PRIORITY, &xHighPriorityNormallyFullTask1 );\r
218         xTaskCreate( prv2ndHigherPriorityNormallyFullTask, ( signed portCHAR * ) "H1QTx", configMINIMAL_STACK_SIZE, ( void * ) intqHIGH_PRIORITY_TASK2, intqHIGHER_PRIORITY, &xHighPriorityNormallyFullTask2 );\r
219         xTaskCreate( prvLowerPriorityNormallyFullTask, ( signed portCHAR * ) "LQRx", configMINIMAL_STACK_SIZE, NULL, intqLOWER_PRIORITY, NULL );\r
220 \r
221         /* Create the queues that are accessed by multiple tasks and multiple\r
222         interrupts. */\r
223         xNormallyFullQueue = xQueueCreate( intqQUEUE_LENGTH, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );\r
224         xNormallyEmptyQueue = xQueueCreate( intqQUEUE_LENGTH, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );\r
225 \r
226         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
227         in use.  The queue registry is provided as a means for kernel aware\r
228         debuggers to locate queues and has no purpose if a kernel aware debugger\r
229         is not being used.  The call to vQueueAddToRegistry() will be removed\r
230         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
231         defined to be less than 1. */\r
232         vQueueAddToRegistry( xNormallyFullQueue, ( signed portCHAR * ) "NormallyFull" );\r
233         vQueueAddToRegistry( xNormallyEmptyQueue, ( signed portCHAR * ) "NormallyEmpty" );\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvRecordValue_NormallyFull( unsigned portBASE_TYPE uxValue, unsigned portBASE_TYPE uxSource )\r
238 {\r
239         if( uxValue < intqNUM_VALUES_TO_LOG )\r
240         {\r
241                 /* We don't expect to receive the same value twice, so if the value\r
242                 has already been marked as received an error has occurred. */\r
243                 if( ucNormallyFullReceivedValues[ uxValue ] != 0x00 )\r
244                 {\r
245                         prvQueueAccessLogError( __LINE__ );\r
246                 }\r
247 \r
248                 /* Log that this value has been received. */\r
249                 ucNormallyFullReceivedValues[ uxValue ] = uxSource;\r
250         }\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 static void prvRecordValue_NormallyEmpty( unsigned portBASE_TYPE uxValue, unsigned portBASE_TYPE uxSource )\r
255 {\r
256         if( uxValue < intqNUM_VALUES_TO_LOG )\r
257         {\r
258                 /* We don't expect to receive the same value twice, so if the value\r
259                 has already been marked as received an error has occurred. */\r
260                 if( ucNormallyEmptyReceivedValues[ uxValue ] != 0x00 )\r
261                 {\r
262                         prvQueueAccessLogError( __LINE__ );\r
263                 }\r
264 \r
265                 /* Log that this value has been received. */\r
266                 ucNormallyEmptyReceivedValues[ uxValue ] = uxSource;\r
267         }\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 static void prvQueueAccessLogError( unsigned portBASE_TYPE uxLine )\r
272 {\r
273         /* Latch the line number that caused the error. */\r
274         xErrorLine = uxLine;\r
275         xErrorStatus = pdFAIL;\r
276 }\r
277 /*-----------------------------------------------------------*/\r
278 \r
279 static void prvHigherPriorityNormallyEmptyTask( void *pvParameters )\r
280 {\r
281 unsigned portBASE_TYPE uxRxed, ux, uxTask1, uxTask2, uxErrorCount1 = 0, uxErrorCount2 = 0;\r
282 \r
283         /* The timer should not be started until after the scheduler has started.\r
284         More than one task is running this code so we check the parameter value\r
285         to determine which task should start the timer. */\r
286         if( ( unsigned portBASE_TYPE ) pvParameters == intqHIGH_PRIORITY_TASK1 )\r
287         {\r
288                 vInitialiseTimerForIntQueueTest();\r
289         }\r
290 \r
291         for( ;; )\r
292         {\r
293                 /* Block waiting to receive a value from the normally empty queue.\r
294                 Interrupts will write to the queue so we should receive a value. */\r
295                 if( xQueueReceive( xNormallyEmptyQueue, &uxRxed, intqSHORT_DELAY ) != pdPASS )\r
296                 {\r
297                         prvQueueAccessLogError( __LINE__ );\r
298                 }\r
299                 else\r
300                 {\r
301                         /* Note which value was received so we can check all expected\r
302                         values are received and no values are duplicated. */\r
303                         prvRecordValue_NormallyEmpty( uxRxed, ( unsigned portBASE_TYPE ) pvParameters );\r
304                 }\r
305 \r
306                 /* Ensure the other task running this code gets a chance to execute. */\r
307                 taskYIELD();\r
308 \r
309                 if( ( unsigned portBASE_TYPE ) pvParameters == intqHIGH_PRIORITY_TASK1 )\r
310                 {\r
311                         /* Have we received all the expected values? */\r
312                         if( uxValueForNormallyEmptyQueue > ( intqNUM_VALUES_TO_LOG + intqVALUE_OVERRUN ) )\r
313                         {\r
314                                 vTaskSuspend( xHighPriorityNormallyEmptyTask2 );\r
315 \r
316                                 uxTask1 = 0;\r
317                                 uxTask2 = 0;\r
318 \r
319                                 /* Loop through the array, checking that both tasks have\r
320                                 placed values into the array, and that no values are missing.\r
321                                 Start at 1 as we expect position 0 to be unused. */\r
322                                 for( ux = 1; ux < intqNUM_VALUES_TO_LOG; ux++ )\r
323                                 {\r
324                                         if( ucNormallyEmptyReceivedValues[ ux ] == 0 )\r
325                                         {\r
326                                                 /* A value is missing. */\r
327                                                 prvQueueAccessLogError( __LINE__ );\r
328                                         }\r
329                                         else\r
330                                         {\r
331                                                 if( ucNormallyEmptyReceivedValues[ ux ] == intqHIGH_PRIORITY_TASK1 )\r
332                                                 {\r
333                                                         /* Value was placed into the array by task 1. */\r
334                                                         uxTask1++;\r
335                                                 }\r
336                                                 else if( ucNormallyEmptyReceivedValues[ ux ] == intqHIGH_PRIORITY_TASK2 )\r
337                                                 {\r
338                                                         /* Value was placed into the array by task 2. */\r
339                                                         uxTask2++;\r
340                                                 }\r
341                                         }\r
342                                 }\r
343 \r
344                                 if( uxTask1 < intqMIN_ACCEPTABLE_TASK_COUNT )\r
345                                 {\r
346                                         /* Only task 2 seemed to log any values. */\r
347                                         uxErrorCount1++;\r
348                                         if( uxErrorCount1 > 2 )\r
349                                         {\r
350                                                 prvQueueAccessLogError( __LINE__ );\r
351                                         }\r
352                                 }\r
353                                 else\r
354                                 {\r
355                                         uxErrorCount1 = 0;\r
356                                 }\r
357 \r
358                                 if( uxTask2 < intqMIN_ACCEPTABLE_TASK_COUNT  )\r
359                                 {\r
360                                         /* Only task 1 seemed to log any values. */\r
361                                         uxErrorCount2++;\r
362                                         if( uxErrorCount2 > 2 )\r
363                                         {\r
364                                                 prvQueueAccessLogError( __LINE__ );\r
365                                         }\r
366                                 }\r
367                                 else\r
368                                 {\r
369                                         uxErrorCount2 = 0;\r
370                                 }\r
371 \r
372                                 /* Clear the array again, ready to start a new cycle. */\r
373                                 memset( ucNormallyEmptyReceivedValues, 0x00, sizeof( ucNormallyEmptyReceivedValues ) );\r
374 \r
375                                 uxHighPriorityLoops1++;\r
376                                 uxValueForNormallyEmptyQueue = 0;\r
377 \r
378                                 /* Suspend ourselves, allowing the lower priority task to\r
379                                 actually receive something from the queue.  Until now it\r
380                                 will have been prevented from doing so by the higher\r
381                                 priority tasks.  The lower priority task will resume us\r
382                                 if it receives something.  We will then resume the other\r
383                                 higher priority task. */\r
384                                 vTaskSuspend( NULL );\r
385                                 vTaskResume( xHighPriorityNormallyEmptyTask2 );\r
386                         }\r
387                 }\r
388         }\r
389 }\r
390 /*-----------------------------------------------------------*/\r
391 \r
392 static void prvLowerPriorityNormallyEmptyTask( void *pvParameters )\r
393 {\r
394 unsigned portBASE_TYPE uxValue, uxRxed;\r
395 portBASE_TYPE xQueueStatus;\r
396 \r
397         /* The parameters are not being used so avoid compiler warnings. */\r
398         ( void ) pvParameters;\r
399 \r
400         for( ;; )\r
401         {\r
402                 if( ( xQueueStatus = xQueueReceive( xNormallyEmptyQueue, &uxRxed, intqONE_TICK_DELAY ) ) != errQUEUE_EMPTY )\r
403                 {\r
404                         /* We should only obtain a value when the high priority task is\r
405                         suspended. */\r
406                         if( xTaskIsTaskSuspended( xHighPriorityNormallyEmptyTask1 ) == pdFALSE )\r
407                         {\r
408                                 prvQueueAccessLogError( __LINE__ );\r
409                         }\r
410 \r
411                         prvRecordValue_NormallyEmpty( uxRxed, intqLOW_PRIORITY_TASK );\r
412 \r
413                         /* Wake the higher priority task again. */\r
414                         vTaskResume( xHighPriorityNormallyEmptyTask1 );\r
415                         uxLowPriorityLoops1++;\r
416                 }\r
417                 else\r
418                 {\r
419                         /* Raise our priority while we send so we can preempt the higher\r
420                         priority task, and ensure we get the Tx value into the queue. */\r
421                         vTaskPrioritySet( NULL, intqHIGHER_PRIORITY + 1 );\r
422 \r
423                         portENTER_CRITICAL();\r
424                         {\r
425                                 uxValueForNormallyEmptyQueue++;\r
426                                 uxValue = uxValueForNormallyEmptyQueue;\r
427                         }\r
428                         portEXIT_CRITICAL();\r
429 \r
430                         if( xQueueSend( xNormallyEmptyQueue, &uxValue, portMAX_DELAY ) != pdPASS )\r
431                         {\r
432                                 prvQueueAccessLogError( __LINE__ );\r
433                         }\r
434 \r
435                         vTaskPrioritySet( NULL, intqLOWER_PRIORITY );\r
436                 }\r
437         }\r
438 }\r
439 /*-----------------------------------------------------------*/\r
440 \r
441 static void prv1stHigherPriorityNormallyFullTask( void *pvParameters )\r
442 {\r
443 unsigned portBASE_TYPE uxValueToTx, ux;\r
444 portBASE_TYPE xQueueStatus;\r
445 \r
446         /* The parameters are not being used so avoid compiler warnings. */\r
447         ( void ) pvParameters;\r
448 \r
449         /* Make sure the queue starts full or near full.  >> 1 as there are two\r
450         high priority tasks. */\r
451         for( ux = 0; ux < ( intqQUEUE_LENGTH >> 1 ); ux++ )\r
452         {\r
453                 portENTER_CRITICAL();\r
454                 {\r
455                         uxValueForNormallyFullQueue++;\r
456                         uxValueToTx = uxValueForNormallyFullQueue;\r
457                 }\r
458                 portEXIT_CRITICAL();\r
459 \r
460                 xQueueSend( xNormallyFullQueue, &uxValueToTx, intqSHORT_DELAY );\r
461         }\r
462 \r
463         for( ;; )\r
464         {\r
465                 portENTER_CRITICAL();\r
466                 {\r
467                         uxValueForNormallyFullQueue++;\r
468                         uxValueToTx = uxValueForNormallyFullQueue;\r
469                 }\r
470                 portEXIT_CRITICAL();\r
471 \r
472                 if( ( xQueueStatus = xQueueSend( xNormallyFullQueue, &uxValueToTx, intqSHORT_DELAY ) ) != pdPASS )\r
473                 {\r
474                         /* intqHIGH_PRIORITY_TASK2 is never suspended so we would not\r
475                         expect it to ever time out. */\r
476                         prvQueueAccessLogError( __LINE__ );\r
477                 }\r
478 \r
479                 /* Allow the other task running this code to run. */\r
480                 taskYIELD();\r
481 \r
482                 /* Have all the expected values been sent to the queue? */\r
483                 if( uxValueToTx > ( intqNUM_VALUES_TO_LOG + intqVALUE_OVERRUN ) )\r
484                 {\r
485                         /* Make sure the other high priority task completes its send of\r
486                         any values below intqNUM_VALUE_TO_LOG. */\r
487                         vTaskDelay( intqSHORT_DELAY );\r
488 \r
489                         vTaskSuspend( xHighPriorityNormallyFullTask2 );\r
490 \r
491                         if( xWasSuspended == pdTRUE )\r
492                         {\r
493                                 /* We would have expected the other high priority task to have\r
494                                 set this back to false by now. */\r
495                                 prvQueueAccessLogError( __LINE__ );\r
496                         }\r
497 \r
498                         /* Set the suspended flag so an error is not logged if the other\r
499                         task recognises a time out when it is unsuspended. */\r
500                         xWasSuspended = pdTRUE;\r
501 \r
502                         /* Start at 1 as we expect position 0 to be unused. */\r
503                         for( ux = 1; ux < intqNUM_VALUES_TO_LOG; ux++ )\r
504                         {\r
505                                 if( ucNormallyFullReceivedValues[ ux ] == 0 )\r
506                                 {\r
507                                         /* A value was missing. */\r
508                                         prvQueueAccessLogError( __LINE__ );\r
509                                 }\r
510                         }\r
511 \r
512                         /* Reset the array ready for the next cycle. */\r
513                         memset( ucNormallyFullReceivedValues, 0x00, sizeof( ucNormallyFullReceivedValues ) );\r
514 \r
515                         uxHighPriorityLoops2++;\r
516                         uxValueForNormallyFullQueue = 0;\r
517 \r
518                         /* Suspend ourselves, allowing the lower priority task to\r
519                         actually receive something from the queue.  Until now it\r
520                         will have been prevented from doing so by the higher\r
521                         priority tasks.  The lower priority task will resume us\r
522                         if it receives something.  We will then resume the other\r
523                         higher priority task. */\r
524                         vTaskSuspend( NULL );\r
525                         vTaskResume( xHighPriorityNormallyFullTask2 );\r
526                 }\r
527         }\r
528 }\r
529 /*-----------------------------------------------------------*/\r
530 \r
531 static void prv2ndHigherPriorityNormallyFullTask( void *pvParameters )\r
532 {\r
533 unsigned portBASE_TYPE uxValueToTx, ux;\r
534 portBASE_TYPE xQueueStatus;\r
535 \r
536         /* The parameters are not being used so avoid compiler warnings. */\r
537         ( void ) pvParameters;\r
538 \r
539         /* Make sure the queue starts full or near full.  >> 1 as there are two\r
540         high priority tasks. */\r
541         for( ux = 0; ux < ( intqQUEUE_LENGTH >> 1 ); ux++ )\r
542         {\r
543                 portENTER_CRITICAL();\r
544                 {\r
545                         uxValueForNormallyFullQueue++;\r
546                         uxValueToTx = uxValueForNormallyFullQueue;\r
547                 }\r
548                 portEXIT_CRITICAL();\r
549 \r
550                 xQueueSend( xNormallyFullQueue, &uxValueToTx, intqSHORT_DELAY );\r
551         }\r
552 \r
553         for( ;; )\r
554         {\r
555                 portENTER_CRITICAL();\r
556                 {\r
557                         uxValueForNormallyFullQueue++;\r
558                         uxValueToTx = uxValueForNormallyFullQueue;\r
559                 }\r
560                 portEXIT_CRITICAL();\r
561 \r
562                 if( ( xQueueStatus = xQueueSend( xNormallyFullQueue, &uxValueToTx, intqSHORT_DELAY ) ) != pdPASS )\r
563                 {\r
564                         if( xWasSuspended != pdTRUE )\r
565                         {\r
566                                 /* It is ok to time out if the task has been suspended. */\r
567                                 prvQueueAccessLogError( __LINE__ );\r
568                         }\r
569                 }\r
570 \r
571                 xWasSuspended = pdFALSE;\r
572 \r
573                 taskYIELD();\r
574         }\r
575 }\r
576 /*-----------------------------------------------------------*/\r
577 \r
578 static void prvLowerPriorityNormallyFullTask( void *pvParameters )\r
579 {\r
580 unsigned portBASE_TYPE uxValue, uxTxed = 9999;\r
581 portBASE_TYPE xQueueStatus;\r
582 \r
583         /* The parameters are not being used so avoid compiler warnings. */\r
584         ( void ) pvParameters;\r
585 \r
586         for( ;; )\r
587         {\r
588                 if( ( xQueueStatus = xQueueSend( xNormallyFullQueue, &uxTxed, intqONE_TICK_DELAY ) ) != errQUEUE_FULL )\r
589                 {\r
590                         /* We would only expect to succeed when the higher priority task\r
591                         is suspended. */\r
592                         if( xTaskIsTaskSuspended( xHighPriorityNormallyFullTask1 ) == pdFALSE )\r
593                         {\r
594                                 prvQueueAccessLogError( __LINE__ );\r
595                         }\r
596 \r
597                         vTaskResume( xHighPriorityNormallyFullTask1 );\r
598                         uxLowPriorityLoops2++;\r
599                 }\r
600                 else\r
601                 {\r
602                         /* Raise our priority while we receive so we can preempt the higher\r
603                         priority task, and ensure we get the value from the queue. */\r
604                         vTaskPrioritySet( NULL, intqHIGHER_PRIORITY + 1 );\r
605 \r
606                         if( xQueueReceive( xNormallyFullQueue, &uxValue, portMAX_DELAY ) != pdPASS )\r
607                         {\r
608                                 prvQueueAccessLogError( __LINE__ );\r
609                         }\r
610                         else\r
611                         {\r
612                                 prvRecordValue_NormallyFull( uxValue, intqLOW_PRIORITY_TASK );\r
613                         }\r
614 \r
615                         vTaskPrioritySet( NULL, intqLOWER_PRIORITY );\r
616                 }\r
617         }\r
618 }\r
619 /*-----------------------------------------------------------*/\r
620 \r
621 portBASE_TYPE xFirstTimerHandler( void )\r
622 {\r
623 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, uxRxedValue;\r
624 static unsigned portBASE_TYPE uxNextOperation = 0;\r
625 \r
626         /* Called from a timer interrupt.  Perform various read and write\r
627         accesses on the queues. */\r
628 \r
629         uxNextOperation++;\r
630 \r
631         if( uxNextOperation & ( unsigned portBASE_TYPE ) 0x01 )\r
632         {\r
633                 timerNORMALLY_EMPTY_TX();\r
634                 timerNORMALLY_EMPTY_TX();\r
635                 timerNORMALLY_EMPTY_TX();\r
636         }\r
637         else\r
638         {\r
639                 timerNORMALLY_FULL_RX();\r
640                 timerNORMALLY_FULL_RX();\r
641                 timerNORMALLY_FULL_RX();\r
642         }\r
643 \r
644         return xHigherPriorityTaskWoken;\r
645 }\r
646 /*-----------------------------------------------------------*/\r
647 \r
648 portBASE_TYPE xSecondTimerHandler( void )\r
649 {\r
650 unsigned portBASE_TYPE uxRxedValue;\r
651 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
652 static unsigned portBASE_TYPE uxNextOperation = 0;\r
653 \r
654         /* Called from a timer interrupt.  Perform various read and write\r
655         accesses on the queues. */\r
656 \r
657         uxNextOperation++;\r
658 \r
659         if( uxNextOperation & ( unsigned portBASE_TYPE ) 0x01 )\r
660         {\r
661                 timerNORMALLY_EMPTY_TX();\r
662                 timerNORMALLY_EMPTY_TX();\r
663 \r
664                 timerNORMALLY_EMPTY_RX();\r
665                 timerNORMALLY_EMPTY_RX();\r
666         }\r
667         else\r
668         {\r
669                 timerNORMALLY_FULL_RX();\r
670                 timerNORMALLY_FULL_TX();\r
671                 timerNORMALLY_FULL_TX();\r
672                 timerNORMALLY_FULL_TX();\r
673                 timerNORMALLY_FULL_TX();\r
674         }\r
675 \r
676         return xHigherPriorityTaskWoken;\r
677 }\r
678 /*-----------------------------------------------------------*/\r
679 \r
680 \r
681 portBASE_TYPE xAreIntQueueTasksStillRunning( void )\r
682 {\r
683 static unsigned portBASE_TYPE uxLastHighPriorityLoops1 = 0, uxLastHighPriorityLoops2 = 0, uxLastLowPriorityLoops1 = 0, uxLastLowPriorityLoops2 = 0;\r
684 \r
685         /* xErrorStatus can be set outside of this function.  This function just\r
686         checks that all the tasks are still cycling. */\r
687 \r
688         if( uxHighPriorityLoops1 == uxLastHighPriorityLoops1 )\r
689         {\r
690                 /* The high priority 1 task has stalled. */\r
691                 prvQueueAccessLogError( __LINE__ );\r
692         }\r
693 \r
694         uxLastHighPriorityLoops1 = uxHighPriorityLoops1;\r
695 \r
696         if( uxHighPriorityLoops2 == uxLastHighPriorityLoops2 )\r
697         {\r
698                 /* The high priority 2 task has stalled. */\r
699                 prvQueueAccessLogError( __LINE__ );\r
700         }\r
701 \r
702         uxLastHighPriorityLoops2 = uxHighPriorityLoops2;\r
703 \r
704         if( uxLowPriorityLoops1 == uxLastLowPriorityLoops1 )\r
705         {\r
706                 /* The low priority 1 task has stalled. */\r
707                 prvQueueAccessLogError( __LINE__ );\r
708         }\r
709 \r
710         uxLastLowPriorityLoops1 = uxLowPriorityLoops1;\r
711 \r
712         if( uxLowPriorityLoops2 == uxLastLowPriorityLoops2 )\r
713         {\r
714                 /* The low priority 2 task has stalled. */\r
715                 prvQueueAccessLogError( __LINE__ );\r
716         }\r
717 \r
718         uxLastLowPriorityLoops2 = uxLowPriorityLoops2;\r
719 \r
720         return xErrorStatus;\r
721 }\r
722 \r