FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / Minimal / countsem.c
blobd5dfd9f6152b24b5a547e0309ecde996c8897c4d
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 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
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
55 /*
56 * Simple demonstration of the usage of counting semaphore.
59 /* Scheduler include files. */
60 #include "FreeRTOS.h"
61 #include "task.h"
62 #include "semphr.h"
64 /* Demo program include files. */
65 #include "countsem.h"
67 /* The maximum count value that the semaphore used for the demo can hold. */
68 #define countMAX_COUNT_VALUE ( 200 )
70 /* Constants used to indicate whether or not the semaphore should have been
71 created with its maximum count value, or its minimum count value. These
72 numbers are used to ensure that the pointers passed in as the task parameters
73 are valid. */
74 #define countSTART_AT_MAX_COUNT ( 0xaa )
75 #define countSTART_AT_ZERO ( 0x55 )
77 /* Two tasks are created for the test. One uses a semaphore created with its
78 count value set to the maximum, and one with the count value set to zero. */
79 #define countNUM_TEST_TASKS ( 2 )
80 #define countDONT_BLOCK ( 0 )
82 /*-----------------------------------------------------------*/
84 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
85 detected in any of the tasks. */
86 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
88 /*-----------------------------------------------------------*/
91 * The demo task. This simply counts the semaphore up to its maximum value,
92 * the counts it back down again. The result of each semaphore 'give' and
93 * 'take' is inspected, with an error being flagged if it is found not to be
94 * the expected result.
96 static void prvCountingSemaphoreTask( void *pvParameters );
99 * Utility function to increment the semaphore count value up from zero to
100 * countMAX_COUNT_VALUE.
102 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
105 * Utility function to decrement the semaphore count value up from
106 * countMAX_COUNT_VALUE to zero.
108 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
110 /*-----------------------------------------------------------*/
112 /* The structure that is passed into the task as the task parameter. */
113 typedef struct COUNT_SEM_STRUCT
115 /* The semaphore to be used for the demo. */
116 xSemaphoreHandle xSemaphore;
118 /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
119 its count value set to its max count value, or countSTART_AT_ZERO if it
120 should have been created with its count value set to 0. */
121 unsigned portBASE_TYPE uxExpectedStartCount;
123 /* Incremented on each cycle of the demo task. Used to detect a stalled
124 task. */
125 unsigned portBASE_TYPE uxLoopCounter;
126 } xCountSemStruct;
128 /* Two structures are defined, one is passed to each test task. */
129 static volatile xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
131 /*-----------------------------------------------------------*/
133 void vStartCountingSemaphoreTasks( void )
135 /* Create the semaphores that we are going to use for the test/demo. The
136 first should be created such that it starts at its maximum count value,
137 the second should be created such that it starts with a count value of zero. */
138 xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
139 xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
140 xParameters[ 0 ].uxLoopCounter = 0;
142 xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
143 xParameters[ 1 ].uxExpectedStartCount = 0;
144 xParameters[ 1 ].uxLoopCounter = 0;
146 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
147 in use. The registry is provided as a means for kernel aware
148 debuggers to locate semaphores and has no purpose if a kernel aware debugger
149 is not being used. The call to vQueueAddToRegistry() will be removed
150 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
151 defined to be less than 1. */
152 vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 0 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );
153 vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 1 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );
156 /* Were the semaphores created? */
157 if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
159 /* Create the demo tasks, passing in the semaphore to use as the parameter. */
160 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
161 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
164 /*-----------------------------------------------------------*/
166 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
168 unsigned portBASE_TYPE ux;
170 /* If the semaphore count is at its maximum then we should not be able to
171 'give' the semaphore. */
172 if( xSemaphoreGive( xSemaphore ) == pdPASS )
174 xErrorDetected = pdTRUE;
177 /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
178 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
180 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
182 /* We expected to be able to take the semaphore. */
183 xErrorDetected = pdTRUE;
186 ( *puxLoopCounter )++;
189 #if configUSE_PREEMPTION == 0
190 taskYIELD();
191 #endif
193 /* If the semaphore count is zero then we should not be able to 'take'
194 the semaphore. */
195 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
197 xErrorDetected = pdTRUE;
200 /*-----------------------------------------------------------*/
202 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
204 unsigned portBASE_TYPE ux;
206 /* If the semaphore count is zero then we should not be able to 'take'
207 the semaphore. */
208 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
210 xErrorDetected = pdTRUE;
213 /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
214 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
216 if( xSemaphoreGive( xSemaphore ) != pdPASS )
218 /* We expected to be able to take the semaphore. */
219 xErrorDetected = pdTRUE;
222 ( *puxLoopCounter )++;
225 #if configUSE_PREEMPTION == 0
226 taskYIELD();
227 #endif
229 /* If the semaphore count is at its maximum then we should not be able to
230 'give' the semaphore. */
231 if( xSemaphoreGive( xSemaphore ) == pdPASS )
233 xErrorDetected = pdTRUE;
236 /*-----------------------------------------------------------*/
238 static void prvCountingSemaphoreTask( void *pvParameters )
240 xCountSemStruct *pxParameter;
242 #ifdef USE_STDIO
243 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
245 const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
247 /* Queue a message for printing to say the task has started. */
248 vPrintDisplayMessage( &pcTaskStartMsg );
249 #endif
251 /* The semaphore to be used was passed as the parameter. */
252 pxParameter = ( xCountSemStruct * ) pvParameters;
254 /* Did we expect to find the semaphore already at its max count value, or
255 at zero? */
256 if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
258 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
261 /* Now we expect the semaphore count to be 0, so this time there is an
262 error if we can take the semaphore. */
263 if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
265 xErrorDetected = pdTRUE;
268 for( ;; )
270 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
271 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
274 /*-----------------------------------------------------------*/
276 portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )
278 static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;
279 portBASE_TYPE xReturn = pdPASS;
281 /* Return fail if any 'give' or 'take' did not result in the expected
282 behaviour. */
283 if( xErrorDetected != pdFALSE )
285 xReturn = pdFAIL;
288 /* Return fail if either task is not still incrementing its loop counter. */
289 if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
291 xReturn = pdFAIL;
293 else
295 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
298 if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
300 xReturn = pdFAIL;
302 else
304 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;
307 return xReturn;