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