2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 * Simple demonstration of the usage of counting semaphore.
\r
72 /* Scheduler include files. */
\r
73 #include "FreeRTOS.h"
\r
77 /* Demo program include files. */
\r
78 #include "countsem.h"
\r
80 /* The maximum count value that the semaphore used for the demo can hold. */
\r
81 #define countMAX_COUNT_VALUE ( 200 )
\r
83 /* Constants used to indicate whether or not the semaphore should have been
\r
84 created with its maximum count value, or its minimum count value. These
\r
85 numbers are used to ensure that the pointers passed in as the task parameters
\r
87 #define countSTART_AT_MAX_COUNT ( 0xaa )
\r
88 #define countSTART_AT_ZERO ( 0x55 )
\r
90 /* Two tasks are created for the test. One uses a semaphore created with its
\r
91 count value set to the maximum, and one with the count value set to zero. */
\r
92 #define countNUM_TEST_TASKS ( 2 )
\r
93 #define countDONT_BLOCK ( 0 )
\r
95 /*-----------------------------------------------------------*/
\r
97 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
98 detected in any of the tasks. */
\r
99 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
\r
101 /*-----------------------------------------------------------*/
\r
104 * The demo task. This simply counts the semaphore up to its maximum value,
\r
105 * the counts it back down again. The result of each semaphore 'give' and
\r
106 * 'take' is inspected, with an error being flagged if it is found not to be
\r
107 * the expected result.
\r
109 static void prvCountingSemaphoreTask( void *pvParameters );
\r
112 * Utility function to increment the semaphore count value up from zero to
\r
113 * countMAX_COUNT_VALUE.
\r
115 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
\r
118 * Utility function to decrement the semaphore count value up from
\r
119 * countMAX_COUNT_VALUE to zero.
\r
121 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
\r
123 /*-----------------------------------------------------------*/
\r
125 /* The structure that is passed into the task as the task parameter. */
\r
126 typedef struct COUNT_SEM_STRUCT
\r
128 /* The semaphore to be used for the demo. */
\r
129 xSemaphoreHandle xSemaphore;
\r
131 /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
\r
132 its count value set to its max count value, or countSTART_AT_ZERO if it
\r
133 should have been created with its count value set to 0. */
\r
134 unsigned portBASE_TYPE uxExpectedStartCount;
\r
136 /* Incremented on each cycle of the demo task. Used to detect a stalled
\r
138 unsigned portBASE_TYPE uxLoopCounter;
\r
141 /* Two structures are defined, one is passed to each test task. */
\r
142 static volatile xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
\r
144 /*-----------------------------------------------------------*/
\r
146 void vStartCountingSemaphoreTasks( void )
\r
148 /* Create the semaphores that we are going to use for the test/demo. The
\r
149 first should be created such that it starts at its maximum count value,
\r
150 the second should be created such that it starts with a count value of zero. */
\r
151 xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
\r
152 xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
\r
153 xParameters[ 0 ].uxLoopCounter = 0;
\r
155 xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
\r
156 xParameters[ 1 ].uxExpectedStartCount = 0;
\r
157 xParameters[ 1 ].uxLoopCounter = 0;
\r
159 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
\r
160 in use. The registry is provided as a means for kernel aware
\r
161 debuggers to locate semaphores and has no purpose if a kernel aware debugger
\r
162 is not being used. The call to vQueueAddToRegistry() will be removed
\r
163 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
164 defined to be less than 1. */
\r
165 vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 0 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );
\r
166 vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 1 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );
\r
169 /* Were the semaphores created? */
\r
170 if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
\r
172 /* Create the demo tasks, passing in the semaphore to use as the parameter. */
\r
173 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
\r
174 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
\r
177 /*-----------------------------------------------------------*/
\r
179 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
\r
181 unsigned portBASE_TYPE ux;
\r
183 /* If the semaphore count is at its maximum then we should not be able to
\r
184 'give' the semaphore. */
\r
185 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
187 xErrorDetected = pdTRUE;
\r
190 /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
\r
191 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
193 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
\r
195 /* We expected to be able to take the semaphore. */
\r
196 xErrorDetected = pdTRUE;
\r
199 ( *puxLoopCounter )++;
\r
202 #if configUSE_PREEMPTION == 0
\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 /*-----------------------------------------------------------*/
\r
215 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
\r
217 unsigned portBASE_TYPE ux;
\r
219 /* If the semaphore count is zero then we should not be able to 'take'
\r
221 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
\r
223 xErrorDetected = pdTRUE;
\r
226 /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
\r
227 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
229 if( xSemaphoreGive( xSemaphore ) != pdPASS )
\r
231 /* We expected to be able to take the semaphore. */
\r
232 xErrorDetected = pdTRUE;
\r
235 ( *puxLoopCounter )++;
\r
238 #if configUSE_PREEMPTION == 0
\r
242 /* If the semaphore count is at its maximum then we should not be able to
\r
243 'give' the semaphore. */
\r
244 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
246 xErrorDetected = pdTRUE;
\r
249 /*-----------------------------------------------------------*/
\r
251 static void prvCountingSemaphoreTask( void *pvParameters )
\r
253 xCountSemStruct *pxParameter;
\r
256 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
258 const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
\r
260 /* Queue a message for printing to say the task has started. */
\r
261 vPrintDisplayMessage( &pcTaskStartMsg );
\r
264 /* The semaphore to be used was passed as the parameter. */
\r
265 pxParameter = ( xCountSemStruct * ) pvParameters;
\r
267 /* Did we expect to find the semaphore already at its max count value, or
\r
269 if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
\r
271 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
274 /* Now we expect the semaphore count to be 0, so this time there is an
\r
275 error if we can take the semaphore. */
\r
276 if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
\r
278 xErrorDetected = pdTRUE;
\r
283 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
284 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
287 /*-----------------------------------------------------------*/
\r
289 portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )
\r
291 static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;
\r
292 portBASE_TYPE xReturn = pdPASS;
\r
294 /* Return fail if any 'give' or 'take' did not result in the expected
\r
296 if( xErrorDetected != pdFALSE )
\r
301 /* Return fail if either task is not still incrementing its loop counter. */
\r
302 if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
\r
308 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
\r
311 if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
\r
317 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;
\r