blob: ef6ac5b862a92f8c5b6be352975e643525fa45ee [file] [log] [blame]
Richard Barry86032592007-12-02 18:37:43 +00001/*
Richard Barrycc611262011-09-20 18:22:39 +00002 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
Richard Barry89bf1cf2011-04-08 18:30:58 +00003
Richard Barry86032592007-12-02 18:37:43 +00004
Richard Barry26bf0da2009-10-13 11:05:41 +00005 ***************************************************************************
Richard Barry89bf1cf2011-04-08 18:30:58 +00006 * *
7 * FreeRTOS tutorial books are available in pdf and paperback. *
8 * Complete, revised, and edited pdf reference manuals are also *
9 * available. *
10 * *
11 * Purchasing FreeRTOS documentation will not only help you, by *
12 * ensuring you get running as quickly as possible and with an *
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
14 * the FreeRTOS project to continue with its mission of providing *
15 * professional grade, cross platform, de facto standard solutions *
16 * for microcontrollers - completely free of charge! *
17 * *
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
19 * *
20 * Thank you for using FreeRTOS, and thank you for your support! *
21 * *
Richard Barry26bf0da2009-10-13 11:05:41 +000022 ***************************************************************************
23
Richard Barry89bf1cf2011-04-08 18:30:58 +000024
Richard Barry64c701a2009-10-05 10:23:06 +000025 This file is part of the FreeRTOS distribution.
Richard Barry86032592007-12-02 18:37:43 +000026
Richard Barry26bf0da2009-10-13 11:05:41 +000027 FreeRTOS is free software; you can redistribute it and/or modify it under
Richard Barry64c701a2009-10-05 10:23:06 +000028 the terms of the GNU General Public License (version 2) as published by the
Richard Barry26bf0da2009-10-13 11:05:41 +000029 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
Richard Barry89bf1cf2011-04-08 18:30:58 +000030 >>>NOTE<<< The modification to the GPL is included to allow you to
31 distribute a combined work that includes FreeRTOS without being obliged to
32 provide the source code for proprietary components outside of the FreeRTOS
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
Richard Barry26bf0da2009-10-13 11:05:41 +000039 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
Richard Barry2f40ad72009-03-14 19:20:12 +000041
Richard Barry64c701a2009-10-05 10:23:06 +000042 1 tab == 4 spaces!
Richard Barry86032592007-12-02 18:37:43 +000043
Richard Barry64c701a2009-10-05 10:23:06 +000044 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
Richard Barryc86dcf72008-02-03 19:45:58 +000046
Richard Barry64c701a2009-10-05 10:23:06 +000047 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
Richard Barryc86dcf72008-02-03 19:45:58 +000049
Richard Barry64c701a2009-10-05 10:23:06 +000050 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
Richard Barry86032592007-12-02 18:37:43 +000052*/
53
54/*
55 * This is a version of BlockTim.c that uses the light weight API.
56 *
57 * This file contains some test scenarios that ensure tasks do not exit queue
58 * send or receive functions prematurely. A description of the tests is
59 * included within the code.
60 */
61
62/* Kernel includes. */
63#include "FreeRTOS.h"
64#include "task.h"
65#include "queue.h"
66
67/* Demo includes. */
68#include "AltBlock.h"
69
70/* Task priorities. */
71#define bktPRIMARY_PRIORITY ( 3 )
72#define bktSECONDARY_PRIORITY ( 2 )
73
74/* Task behaviour. */
75#define bktQUEUE_LENGTH ( 5 )
76#define bktSHORT_WAIT ( ( ( portTickType ) 20 ) / portTICK_RATE_MS )
77#define bktPRIMARY_BLOCK_TIME ( 10 )
78#define bktALLOWABLE_MARGIN ( 12 )
79#define bktTIME_TO_BLOCK ( 175 )
80#define bktDONT_BLOCK ( ( portTickType ) 0 )
81#define bktRUN_INDICATOR ( ( unsigned portBASE_TYPE ) 0x55 )
82
83/* The queue on which the tasks block. */
84static xQueueHandle xTestQueue;
85
86/* Handle to the secondary task is required by the primary task for calls
87to vTaskSuspend/Resume(). */
88static xTaskHandle xSecondary;
89
90/* Used to ensure that tasks are still executing without error. */
91static portBASE_TYPE xPrimaryCycles = 0, xSecondaryCycles = 0;
92static portBASE_TYPE xErrorOccurred = pdFALSE;
93
94/* Provides a simple mechanism for the primary task to know when the
95secondary task has executed. */
96static volatile unsigned portBASE_TYPE xRunIndicator;
97
98/* The two test tasks. Their behaviour is commented within the files. */
99static void vPrimaryBlockTimeTestTask( void *pvParameters );
100static void vSecondaryBlockTimeTestTask( void *pvParameters );
101
102/*-----------------------------------------------------------*/
103
104void vCreateAltBlockTimeTasks( void )
105{
106 /* Create the queue on which the two tasks block. */
107 xTestQueue = xQueueCreate( bktQUEUE_LENGTH, sizeof( portBASE_TYPE ) );
108
Richard Barrya5f37972008-05-23 18:49:59 +0000109 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
110 in use. The queue registry is provided as a means for kernel aware
111 debuggers to locate queues and has no purpose if a kernel aware debugger
112 is not being used. The call to vQueueAddToRegistry() will be removed
113 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
114 defined to be less than 1. */
115 vQueueAddToRegistry( xTestQueue, ( signed portCHAR * ) "AltBlockQueue" );
116
117
Richard Barry86032592007-12-02 18:37:43 +0000118 /* Create the two test tasks. */
119 xTaskCreate( vPrimaryBlockTimeTestTask, ( signed portCHAR * )"FBTest1", configMINIMAL_STACK_SIZE, NULL, bktPRIMARY_PRIORITY, NULL );
120 xTaskCreate( vSecondaryBlockTimeTestTask, ( signed portCHAR * )"FBTest2", configMINIMAL_STACK_SIZE, NULL, bktSECONDARY_PRIORITY, &xSecondary );
121}
122/*-----------------------------------------------------------*/
123
124static void vPrimaryBlockTimeTestTask( void *pvParameters )
125{
126portBASE_TYPE xItem, xData;
127portTickType xTimeWhenBlocking;
128portTickType xTimeToBlock, xBlockedTime;
129
130 #ifdef USE_STDIO
131 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
132
133 const portCHAR * const pcTaskStartMsg = "Alt primary block time test started.\r\n";
134
135 /* Queue a message for printing to say the task has started. */
136 vPrintDisplayMessage( &pcTaskStartMsg );
137 #endif
138
139 ( void ) pvParameters;
140
141 for( ;; )
142 {
143 /*********************************************************************
144 Test 1
145
146 Simple block time wakeup test on queue receives. */
147 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
148 {
149 /* The queue is empty. Attempt to read from the queue using a block
150 time. When we wake, ensure the delta in time is as expected. */
151 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
152
153 /* A critical section is used to minimise the jitter in the time
154 measurements. */
155 portENTER_CRITICAL();
156 {
157 xTimeWhenBlocking = xTaskGetTickCount();
158
159 /* We should unblock after xTimeToBlock having not received
160 anything on the queue. */
161 if( xQueueAltReceive( xTestQueue, &xData, xTimeToBlock ) != errQUEUE_EMPTY )
162 {
163 xErrorOccurred = pdTRUE;
164 }
165
166 /* How long were we blocked for? */
167 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
168 }
169 portEXIT_CRITICAL();
170
171 if( xBlockedTime < xTimeToBlock )
172 {
173 /* Should not have blocked for less than we requested. */
174 xErrorOccurred = pdTRUE;
175 }
176
177 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
178 {
179 /* Should not have blocked for longer than we requested,
180 although we would not necessarily run as soon as we were
181 unblocked so a margin is allowed. */
182 xErrorOccurred = pdTRUE;
183 }
184 }
185
Richard Barrye0184222008-01-27 20:25:36 +0000186
187 #if configUSE_PREEMPTION == 0
188 taskYIELD();
189 #endif
190
191
Richard Barry86032592007-12-02 18:37:43 +0000192 /*********************************************************************
193 Test 2
194
195 Simple block time wakeup test on queue sends.
196
197 First fill the queue. It should be empty so all sends should pass. */
198 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
199 {
200 if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
201 {
202 xErrorOccurred = pdTRUE;
203 }
204 }
205
206 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
207 {
208 /* The queue is full. Attempt to write to the queue using a block
209 time. When we wake, ensure the delta in time is as expected. */
210 xTimeToBlock = bktPRIMARY_BLOCK_TIME << xItem;
211
212 portENTER_CRITICAL();
213 {
214 xTimeWhenBlocking = xTaskGetTickCount();
215
216 /* We should unblock after xTimeToBlock having not received
217 anything on the queue. */
218 if( xQueueAltSendToBack( xTestQueue, &xItem, xTimeToBlock ) != errQUEUE_FULL )
219 {
220 xErrorOccurred = pdTRUE;
221 }
222
223 /* How long were we blocked for? */
224 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
225 }
226 portEXIT_CRITICAL();
227
228 if( xBlockedTime < xTimeToBlock )
229 {
230 /* Should not have blocked for less than we requested. */
231 xErrorOccurred = pdTRUE;
232 }
233
234 if( xBlockedTime > ( xTimeToBlock + bktALLOWABLE_MARGIN ) )
235 {
236 /* Should not have blocked for longer than we requested,
237 although we would not necessarily run as soon as we were
238 unblocked so a margin is allowed. */
239 xErrorOccurred = pdTRUE;
240 }
241 }
242
Richard Barrye0184222008-01-27 20:25:36 +0000243 #if configUSE_PREEMPTION == 0
244 taskYIELD();
245 #endif
246
Richard Barry86032592007-12-02 18:37:43 +0000247
248 /*********************************************************************
249 Test 3
250
251 Wake the other task, it will block attempting to post to the queue.
252 When we read from the queue the other task will wake, but before it
253 can run we will post to the queue again. When the other task runs it
254 will find the queue still full, even though it was woken. It should
255 recognise that its block time has not expired and return to block for
256 the remains of its block time.
257
258 Wake the other task so it blocks attempting to post to the already
259 full queue. */
260 xRunIndicator = 0;
261 vTaskResume( xSecondary );
262
263 /* We need to wait a little to ensure the other task executes. */
264 while( xRunIndicator != bktRUN_INDICATOR )
265 {
266 /* The other task has not yet executed. */
267 vTaskDelay( bktSHORT_WAIT );
268 }
269 /* Make sure the other task is blocked on the queue. */
270 vTaskDelay( bktSHORT_WAIT );
271 xRunIndicator = 0;
272
273 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
274 {
275 /* Now when we make space on the queue the other task should wake
276 but not execute as this task has higher priority. */
277 if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
278 {
279 xErrorOccurred = pdTRUE;
280 }
281
282 /* Now fill the queue again before the other task gets a chance to
283 execute. If the other task had executed we would find the queue
284 full ourselves, and the other task have set xRunIndicator. */
285 if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
286 {
287 xErrorOccurred = pdTRUE;
288 }
289
290 if( xRunIndicator == bktRUN_INDICATOR )
291 {
292 /* The other task should not have executed. */
293 xErrorOccurred = pdTRUE;
294 }
295
296 /* Raise the priority of the other task so it executes and blocks
297 on the queue again. */
298 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
299
300 /* The other task should now have re-blocked without exiting the
301 queue function. */
302 if( xRunIndicator == bktRUN_INDICATOR )
303 {
304 /* The other task should not have executed outside of the
305 queue function. */
306 xErrorOccurred = pdTRUE;
307 }
308
309 /* Set the priority back down. */
310 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
311 }
312
313 /* Let the other task timeout. When it unblockes it will check that it
314 unblocked at the correct time, then suspend itself. */
315 while( xRunIndicator != bktRUN_INDICATOR )
316 {
317 vTaskDelay( bktSHORT_WAIT );
318 }
319 vTaskDelay( bktSHORT_WAIT );
320 xRunIndicator = 0;
321
Richard Barrye0184222008-01-27 20:25:36 +0000322 #if configUSE_PREEMPTION == 0
323 taskYIELD();
324 #endif
Richard Barry86032592007-12-02 18:37:43 +0000325
326 /*********************************************************************
327 Test 4
328
329 As per test 3 - but with the send and receive the other way around.
330 The other task blocks attempting to read from the queue.
331
332 Empty the queue. We should find that it is full. */
333 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
334 {
335 if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
336 {
337 xErrorOccurred = pdTRUE;
338 }
339 }
340
341 /* Wake the other task so it blocks attempting to read from the
342 already empty queue. */
343 vTaskResume( xSecondary );
344
345 /* We need to wait a little to ensure the other task executes. */
346 while( xRunIndicator != bktRUN_INDICATOR )
347 {
348 vTaskDelay( bktSHORT_WAIT );
349 }
350 vTaskDelay( bktSHORT_WAIT );
351 xRunIndicator = 0;
352
353 for( xItem = 0; xItem < bktQUEUE_LENGTH; xItem++ )
354 {
355 /* Now when we place an item on the queue the other task should
356 wake but not execute as this task has higher priority. */
357 if( xQueueAltSendToBack( xTestQueue, &xItem, bktDONT_BLOCK ) != pdPASS )
358 {
359 xErrorOccurred = pdTRUE;
360 }
361
362 /* Now empty the queue again before the other task gets a chance to
363 execute. If the other task had executed we would find the queue
364 empty ourselves, and the other task would be suspended. */
365 if( xQueueAltReceive( xTestQueue, &xData, bktDONT_BLOCK ) != pdPASS )
366 {
367 xErrorOccurred = pdTRUE;
368 }
369
370 if( xRunIndicator == bktRUN_INDICATOR )
371 {
372 /* The other task should not have executed. */
373 xErrorOccurred = pdTRUE;
374 }
375
376 /* Raise the priority of the other task so it executes and blocks
377 on the queue again. */
378 vTaskPrioritySet( xSecondary, bktPRIMARY_PRIORITY + 2 );
379
380 /* The other task should now have re-blocked without exiting the
381 queue function. */
382 if( xRunIndicator == bktRUN_INDICATOR )
383 {
384 /* The other task should not have executed outside of the
385 queue function. */
386 xErrorOccurred = pdTRUE;
387 }
388 vTaskPrioritySet( xSecondary, bktSECONDARY_PRIORITY );
389 }
390
391 /* Let the other task timeout. When it unblockes it will check that it
392 unblocked at the correct time, then suspend itself. */
393 while( xRunIndicator != bktRUN_INDICATOR )
394 {
395 vTaskDelay( bktSHORT_WAIT );
396 }
397 vTaskDelay( bktSHORT_WAIT );
398
399 xPrimaryCycles++;
400 }
401}
402/*-----------------------------------------------------------*/
403
404static void vSecondaryBlockTimeTestTask( void *pvParameters )
405{
406portTickType xTimeWhenBlocking, xBlockedTime;
407portBASE_TYPE xData;
408
409 #ifdef USE_STDIO
410 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
411
412 const portCHAR * const pcTaskStartMsg = "Alt secondary block time test started.\r\n";
413
414 /* Queue a message for printing to say the task has started. */
415 vPrintDisplayMessage( &pcTaskStartMsg );
416 #endif
417
418 ( void ) pvParameters;
419
420 for( ;; )
421 {
422 /*********************************************************************
423 Test 1 and 2
424
425 This task does does not participate in these tests. */
426 vTaskSuspend( NULL );
427
428 /*********************************************************************
429 Test 3
430
431 The first thing we do is attempt to read from the queue. It should be
432 full so we block. Note the time before we block so we can check the
433 wake time is as per that expected. */
434 portENTER_CRITICAL();
435 {
436 xTimeWhenBlocking = xTaskGetTickCount();
437
438 /* We should unblock after bktTIME_TO_BLOCK having not received
439 anything on the queue. */
440 xData = 0;
441 xRunIndicator = bktRUN_INDICATOR;
442 if( xQueueAltSendToBack( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_FULL )
443 {
444 xErrorOccurred = pdTRUE;
445 }
446
447 /* How long were we inside the send function? */
448 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
449 }
450 portEXIT_CRITICAL();
451
452 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
453 if( xBlockedTime < bktTIME_TO_BLOCK )
454 {
455 xErrorOccurred = pdTRUE;
456 }
457
458 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
459 either. A margin is permitted as we would not necessarily run as
460 soon as we unblocked. */
461 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
462 {
463 xErrorOccurred = pdTRUE;
464 }
465
466 /* Suspend ready for test 3. */
467 xRunIndicator = bktRUN_INDICATOR;
468 vTaskSuspend( NULL );
469
470 /*********************************************************************
471 Test 4
472
473 As per test three, but with the send and receive reversed. */
474 portENTER_CRITICAL();
475 {
476 xTimeWhenBlocking = xTaskGetTickCount();
477
478 /* We should unblock after bktTIME_TO_BLOCK having not received
479 anything on the queue. */
480 xRunIndicator = bktRUN_INDICATOR;
481 if( xQueueAltReceive( xTestQueue, &xData, bktTIME_TO_BLOCK ) != errQUEUE_EMPTY )
482 {
483 xErrorOccurred = pdTRUE;
484 }
485
486 xBlockedTime = xTaskGetTickCount() - xTimeWhenBlocking;
487 }
488 portEXIT_CRITICAL();
489
490 /* We should not have blocked for less time than bktTIME_TO_BLOCK. */
491 if( xBlockedTime < bktTIME_TO_BLOCK )
492 {
493 xErrorOccurred = pdTRUE;
494 }
495
496 /* We should of not blocked for much longer than bktALLOWABLE_MARGIN
497 either. A margin is permitted as we would not necessarily run as soon
498 as we unblocked. */
499 if( xBlockedTime > ( bktTIME_TO_BLOCK + bktALLOWABLE_MARGIN ) )
500 {
501 xErrorOccurred = pdTRUE;
502 }
503
504 xRunIndicator = bktRUN_INDICATOR;
505
506 xSecondaryCycles++;
507 }
508}
509/*-----------------------------------------------------------*/
510
511portBASE_TYPE xAreAltBlockTimeTestTasksStillRunning( void )
512{
513static portBASE_TYPE xLastPrimaryCycleCount = 0, xLastSecondaryCycleCount = 0;
514portBASE_TYPE xReturn = pdPASS;
515
516 /* Have both tasks performed at least one cycle since this function was
517 last called? */
518 if( xPrimaryCycles == xLastPrimaryCycleCount )
519 {
520 xReturn = pdFAIL;
521 }
522
523 if( xSecondaryCycles == xLastSecondaryCycleCount )
524 {
525 xReturn = pdFAIL;
526 }
527
528 if( xErrorOccurred == pdTRUE )
529 {
530 xReturn = pdFAIL;
531 }
532
533 xLastSecondaryCycleCount = xSecondaryCycles;
534 xLastPrimaryCycleCount = xPrimaryCycles;
535
536 return xReturn;
537}