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