]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/AltBlock.c
Update version numbers to V4.8.0
[freertos] / Demo / Common / Minimal / AltBlock.c
1 /*\r
2         FreeRTOS.org V4.8.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!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 /*\r
50  * This is a version of BlockTim.c that uses the light weight API.\r
51  *\r
52  * This file contains some test scenarios that ensure tasks do not exit queue\r
53  * send or receive functions prematurely.  A description of the tests is\r
54  * included within the code.\r
55  */\r
56 \r
57 /* Kernel includes. */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 #include "queue.h"\r
61 \r
62 /* Demo includes. */\r
63 #include "AltBlock.h"\r
64 \r
65 /* Task priorities. */\r
66 #define bktPRIMARY_PRIORITY                     ( 3 )\r
67 #define bktSECONDARY_PRIORITY           ( 2 )\r
68 \r
69 /* Task behaviour. */\r
70 #define bktQUEUE_LENGTH                         ( 5 )\r
71 #define bktSHORT_WAIT                           ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )\r
72 #define bktPRIMARY_BLOCK_TIME           ( 10 )\r
73 #define bktALLOWABLE_MARGIN                     ( 12 )\r
74 #define bktTIME_TO_BLOCK                        ( 175 )\r
75 #define bktDONT_BLOCK                           ( ( portTickType ) 0 )\r
76 #define bktRUN_INDICATOR                        ( ( unsigned portBASE_TYPE ) 0x55 )\r
77 \r
78 /* The queue on which the tasks block. */\r
79 static xQueueHandle xTestQueue;\r
80 \r
81 /* Handle to the secondary task is required by the primary task for calls\r
82 to vTaskSuspend/Resume(). */\r
83 static xTaskHandle xSecondary;\r
84 \r
85 /* Used to ensure that tasks are still executing without error. */\r
86 static portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;\r
87 static portBASE_TYPE xErrorOccurred = pdFALSE;\r
88 \r
89 /* Provides a simple mechanism for the primary task to know when the\r
90 secondary task has executed. */\r
91 static volatile unsigned portBASE_TYPE xRunIndicator;\r
92 \r
93 /* The two test tasks.  Their behaviour is commented within the files. */\r
94 static void vPrimaryBlockTimeTestTask( void *pvParameters );\r
95 static void vSecondaryBlockTimeTestTask( void *pvParameters );\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 void vCreateAltBlockTimeTasks( void )\r
100 {\r
101         /* Create the queue on which the two tasks block. */\r
102     xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );\r
103 \r
104         /* Create the two test tasks. */\r
105         xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"FBTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );\r
106         xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"FBTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );\r
107 }\r
108 /*-----------------------------------------------------------*/\r
109 \r
110 static void vPrimaryBlockTimeTestTask( void *pvParameters )\r
111 {\r
112 portBASE_TYPE xItem, xData;\r
113 portTickType xTimeWhenBlocking;\r
114 portTickType xTimeToBlock, xBlockedTime;\r
115 \r
116         #ifdef USE_STDIO\r
117         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
118         \r
119                 const portCHAR * const pcTaskStartMsg = "Alt primary block time test started.\r\n";\r
120 \r
121                 /* Queue a message for printing to say the task has started. */\r
122                 vPrintDisplayMessage( &pcTaskStartMsg );\r
123         #endif\r
124 \r
125         ( void ) pvParameters;\r
126 \r
127         for( ;; )\r
128         {\r
129                 /*********************************************************************\r
130         Test 1\r
131 \r
132         Simple block time wakeup test on queue receives. */\r
133                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
134                 {\r
135                         /* The queue is empty. Attempt to read from the queue using a block\r
136                         time.  When we wake, ensure the delta in time is as expected. */\r
137                         xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;\r
138 \r
139                         /* A critical section is used to minimise the jitter in the time\r
140                         measurements. */\r
141                         portENTER_CRITICAL();\r
142                         {\r
143                                 xTimeWhenBlocking = xTaskGetTickCount();\r
144                                 \r
145                                 /* We should unblock after xTimeToBlock having not received\r
146                                 anything on the queue. */\r
147                                 if( xQueueAltReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )\r
148                                 {\r
149                                         xErrorOccurred = pdTRUE;\r
150                                 }\r
151 \r
152                                 /* How long were we blocked for? */\r
153                                 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
154                         }\r
155                         portEXIT_CRITICAL();\r
156 \r
157                         if( xBlockedTime < xTimeToBlock )\r
158                         {\r
159                                 /* Should not have blocked for less than we requested. */\r
160                                 xErrorOccurred = pdTRUE;\r
161                         }\r
162 \r
163                         if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )\r
164                         {\r
165                                 /* Should not have blocked for longer than we requested,\r
166                                 although we would not necessarily run as soon as we were\r
167                                 unblocked so a margin is allowed. */\r
168                                 xErrorOccurred = pdTRUE;\r
169                         }\r
170                 }\r
171 \r
172 \r
173                 #if configUSE_PREEMPTION == 0\r
174                         taskYIELD();\r
175                 #endif\r
176 \r
177 \r
178                 /*********************************************************************\r
179         Test 2\r
180 \r
181         Simple block time wakeup test on queue sends.\r
182 \r
183                 First fill the queue.  It should be empty so all sends should pass. */\r
184                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
185                 {\r
186                         if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )\r
187                         {\r
188                                 xErrorOccurred = pdTRUE;\r
189                         }\r
190                 }\r
191 \r
192                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
193                 {\r
194                         /* The queue is full. Attempt to write to the queue using a block\r
195                         time.  When we wake, ensure the delta in time is as expected. */\r
196                         xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;\r
197 \r
198                         portENTER_CRITICAL();\r
199                         {\r
200                                 xTimeWhenBlocking = xTaskGetTickCount();\r
201                                 \r
202                                 /* We should unblock after xTimeToBlock having not received\r
203                                 anything on the queue. */\r
204                                 if( xQueueAltSendToBack( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )\r
205                                 {\r
206                                         xErrorOccurred = pdTRUE;\r
207                                 }\r
208 \r
209                                 /* How long were we blocked for? */\r
210                                 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
211                         }\r
212                         portEXIT_CRITICAL();\r
213 \r
214                         if( xBlockedTime < xTimeToBlock )\r
215                         {\r
216                                 /* Should not have blocked for less than we requested. */\r
217                                 xErrorOccurred = pdTRUE;\r
218                         }\r
219 \r
220                         if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )\r
221                         {\r
222                                 /* Should not have blocked for longer than we requested,\r
223                                 although we would not necessarily run as soon as we were\r
224                                 unblocked so a margin is allowed. */\r
225                                 xErrorOccurred = pdTRUE;\r
226                         }\r
227                 }\r
228 \r
229                 #if configUSE_PREEMPTION == 0\r
230                         taskYIELD();\r
231                 #endif\r
232 \r
233                 \r
234                 /*********************************************************************\r
235         Test 3\r
236 \r
237                 Wake the other task, it will block attempting to post to the queue.\r
238                 When we read from the queue the other task will wake, but before it\r
239                 can run we will post to the queue again.  When the other task runs it\r
240                 will find the queue still full, even though it was woken.  It should\r
241                 recognise that its block time has not expired and return to block for\r
242                 the remains of its block time.\r
243 \r
244                 Wake the other task so it blocks attempting to post to the already\r
245                 full queue. */\r
246                 xRunIndicator = 0;\r
247                 vTaskResume( xSecondary );\r
248 \r
249                 /* We need to wait a little to ensure the other task executes. */\r
250                 while( xRunIndicator != bktRUN_INDICATOR )\r
251                 {\r
252                         /* The other task has not yet executed. */\r
253                         vTaskDelay( bktSHORT_WAIT );\r
254                 }\r
255                 /* Make sure the other task is blocked on the queue. */\r
256                 vTaskDelay( bktSHORT_WAIT );\r
257                 xRunIndicator = 0;\r
258 \r
259                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
260                 {\r
261                         /* Now when we make space on the queue the other task should wake\r
262                         but not execute as this task has higher priority. */                            \r
263                         if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )\r
264                         {\r
265                                 xErrorOccurred = pdTRUE;\r
266                         }\r
267 \r
268                         /* Now fill the queue again before the other task gets a chance to\r
269                         execute.  If the other task had executed we would find the queue\r
270                         full ourselves, and the other task have set xRunIndicator. */\r
271                         if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )\r
272                         {\r
273                                 xErrorOccurred = pdTRUE;\r
274                         }\r
275 \r
276                         if( xRunIndicator == bktRUN_INDICATOR )\r
277                         {\r
278                                 /* The other task should not have executed. */\r
279                                 xErrorOccurred = pdTRUE;\r
280                         }\r
281 \r
282                         /* Raise the priority of the other task so it executes and blocks\r
283                         on the queue again. */\r
284                         vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );\r
285 \r
286                         /* The other task should now have re-blocked without exiting the\r
287                         queue function. */\r
288                         if( xRunIndicator == bktRUN_INDICATOR )\r
289                         {\r
290                                 /* The other task should not have executed outside of the\r
291                                 queue function. */\r
292                                 xErrorOccurred = pdTRUE;\r
293                         }\r
294 \r
295                         /* Set the priority back down. */\r
296                         vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );                  \r
297                 }\r
298 \r
299                 /* Let the other task timeout.  When it unblockes it will check that it\r
300                 unblocked at the correct time, then suspend itself. */\r
301                 while( xRunIndicator != bktRUN_INDICATOR )\r
302                 {\r
303                         vTaskDelay( bktSHORT_WAIT );\r
304                 }\r
305                 vTaskDelay( bktSHORT_WAIT );\r
306                 xRunIndicator = 0;\r
307 \r
308                 #if configUSE_PREEMPTION == 0\r
309                         taskYIELD();\r
310                 #endif\r
311 \r
312                 /*********************************************************************\r
313         Test 4\r
314 \r
315                 As per test 3 - but with the send and receive the other way around.\r
316                 The other task blocks attempting to read from the queue.\r
317 \r
318                 Empty the queue.  We should find that it is full. */\r
319                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
320                 {\r
321                         if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )\r
322                         {\r
323                                 xErrorOccurred = pdTRUE;\r
324                         }\r
325                 }\r
326                 \r
327                 /* Wake the other task so it blocks attempting to read from  the\r
328                 already empty queue. */\r
329                 vTaskResume( xSecondary );\r
330 \r
331                 /* We need to wait a little to ensure the other task executes. */\r
332                 while( xRunIndicator != bktRUN_INDICATOR )\r
333                 {\r
334                         vTaskDelay( bktSHORT_WAIT );\r
335                 }\r
336                 vTaskDelay( bktSHORT_WAIT );\r
337                 xRunIndicator = 0;\r
338 \r
339                 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )\r
340                 {\r
341                         /* Now when we place an item on the queue the other task should\r
342                         wake but not execute as this task has higher priority. */                               \r
343                         if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )\r
344                         {\r
345                                 xErrorOccurred = pdTRUE;\r
346                         }\r
347 \r
348                         /* Now empty the queue again before the other task gets a chance to\r
349                         execute.  If the other task had executed we would find the queue\r
350                         empty ourselves, and the other task would be suspended. */\r
351                         if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )\r
352                         {\r
353                                 xErrorOccurred = pdTRUE;\r
354                         }\r
355 \r
356                         if( xRunIndicator == bktRUN_INDICATOR )\r
357                         {\r
358                                 /* The other task should not have executed. */\r
359                                 xErrorOccurred = pdTRUE;\r
360                         }\r
361 \r
362                         /* Raise the priority of the other task so it executes and blocks\r
363                         on the queue again. */\r
364                         vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );\r
365 \r
366                         /* The other task should now have re-blocked without exiting the\r
367                         queue function. */\r
368                         if( xRunIndicator == bktRUN_INDICATOR )\r
369                         {\r
370                                 /* The other task should not have executed outside of the\r
371                                 queue function. */\r
372                                 xErrorOccurred = pdTRUE;\r
373                         }\r
374                         vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );                  \r
375                 }\r
376 \r
377                 /* Let the other task timeout.  When it unblockes it will check that it\r
378                 unblocked at the correct time, then suspend itself. */\r
379                 while( xRunIndicator != bktRUN_INDICATOR )\r
380                 {\r
381                         vTaskDelay( bktSHORT_WAIT );\r
382                 }\r
383                 vTaskDelay( bktSHORT_WAIT );\r
384 \r
385                 xPrimaryCycles++;\r
386         }\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 static void vSecondaryBlockTimeTestTask( void *pvParameters )\r
391 {\r
392 portTickType xTimeWhenBlocking, xBlockedTime;\r
393 portBASE_TYPE xData;\r
394 \r
395         #ifdef USE_STDIO\r
396         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
397         \r
398                 const portCHAR * const pcTaskStartMsg = "Alt secondary block time test started.\r\n";\r
399 \r
400                 /* Queue a message for printing to say the task has started. */\r
401                 vPrintDisplayMessage( &pcTaskStartMsg );\r
402         #endif\r
403 \r
404         ( void ) pvParameters;\r
405 \r
406         for( ;; )\r
407         {\r
408                 /*********************************************************************\r
409         Test 1 and 2\r
410 \r
411                 This task does does not participate in these tests. */\r
412                 vTaskSuspend( NULL );\r
413 \r
414                 /*********************************************************************\r
415         Test 3\r
416 \r
417                 The first thing we do is attempt to read from the queue.  It should be\r
418                 full so we block.  Note the time before we block so we can check the\r
419                 wake time is as per that expected. */\r
420                 portENTER_CRITICAL();\r
421                 {\r
422                         xTimeWhenBlocking = xTaskGetTickCount();\r
423                         \r
424                         /* We should unblock after bktTIME_TO_BLOCK having not received\r
425                         anything on the queue. */\r
426                         xData = 0;\r
427                         xRunIndicator = bktRUN_INDICATOR;\r
428                         if( xQueueAltSendToBack( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )\r
429                         {\r
430                                 xErrorOccurred = pdTRUE;\r
431                         }\r
432 \r
433                         /* How long were we inside the send function? */\r
434                         xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
435                 }\r
436                 portEXIT_CRITICAL();\r
437 \r
438                 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */\r
439                 if( xBlockedTime < bktTIME_TO_BLOCK )\r
440                 {\r
441                         xErrorOccurred = pdTRUE;\r
442                 }\r
443 \r
444                 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN\r
445                 either.  A margin is permitted as we would not necessarily run as\r
446                 soon as we unblocked. */\r
447                 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )\r
448                 {\r
449                         xErrorOccurred = pdTRUE;\r
450                 }\r
451 \r
452                 /* Suspend ready for test 3. */\r
453                 xRunIndicator = bktRUN_INDICATOR;\r
454                 vTaskSuspend( NULL );\r
455 \r
456                 /*********************************************************************\r
457         Test 4\r
458 \r
459                 As per test three, but with the send and receive reversed. */\r
460                 portENTER_CRITICAL();\r
461                 {\r
462                         xTimeWhenBlocking = xTaskGetTickCount();\r
463                         \r
464                         /* We should unblock after bktTIME_TO_BLOCK having not received\r
465                         anything on the queue. */\r
466                         xRunIndicator = bktRUN_INDICATOR;\r
467                         if( xQueueAltReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )\r
468                         {\r
469                                 xErrorOccurred = pdTRUE;\r
470                         }\r
471 \r
472                         xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;\r
473                 }\r
474                 portEXIT_CRITICAL();\r
475 \r
476                 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */\r
477                 if( xBlockedTime < bktTIME_TO_BLOCK )\r
478                 {\r
479                         xErrorOccurred = pdTRUE;\r
480                 }\r
481 \r
482                 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN\r
483                 either.  A margin is permitted as we would not necessarily run as soon\r
484                 as we unblocked. */\r
485                 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )\r
486                 {\r
487                         xErrorOccurred = pdTRUE;\r
488                 }\r
489 \r
490                 xRunIndicator = bktRUN_INDICATOR;\r
491 \r
492                 xSecondaryCycles++;\r
493         }\r
494 }\r
495 /*-----------------------------------------------------------*/\r
496 \r
497 portBASE_TYPE xAreAltBlockTimeTestTasksStillRunning( void )\r
498 {\r
499 static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;\r
500 portBASE_TYPE xReturn = pdPASS;\r
501 \r
502         /* Have both tasks performed at least one cycle since this function was\r
503         last called? */\r
504         if( xPrimaryCycles == xLastPrimaryCycleCount )\r
505         {\r
506                 xReturn = pdFAIL;\r
507         }\r
508 \r
509         if( xSecondaryCycles == xLastSecondaryCycleCount )\r
510         {\r
511                 xReturn = pdFAIL;\r
512         }\r
513 \r
514         if( xErrorOccurred == pdTRUE )\r
515         {\r
516                 xReturn = pdFAIL;\r
517         }\r
518 \r
519         xLastSecondaryCycleCount = xSecondaryCycles;\r
520         xLastPrimaryCycleCount = xPrimaryCycles;\r
521 \r
522         return xReturn;\r
523 }\r