2 FreeRTOS.org V4.7.0 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
39 * Simple demonstration of the usage of counting semaphore.
\r
42 /* Scheduler include files. */
\r
43 #include "FreeRTOS.h"
\r
47 /* Demo program include files. */
\r
48 #include "countsem.h"
\r
50 /* The maximum count value that the semaphore used for the demo can hold. */
\r
51 #define countMAX_COUNT_VALUE ( 200 )
\r
53 /* Constants used to indicate whether or not the semaphore should have been
\r
54 created with its maximum count value, or its minimum count value. These
\r
55 numbers are used to ensure that the pointers passed in as the task parameters
\r
57 #define countSTART_AT_MAX_COUNT ( 0xaa )
\r
58 #define countSTART_AT_ZERO ( 0x55 )
\r
60 /* Two tasks are created for the test. One uses a semaphore created with its
\r
61 count value set to the maximum, and one with the count value set to zero. */
\r
62 #define countNUM_TEST_TASKS ( 2 )
\r
63 #define countDONT_BLOCK ( 0 )
\r
65 /*-----------------------------------------------------------*/
\r
67 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
68 detected in any of the tasks. */
\r
69 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
71 /*-----------------------------------------------------------*/
\r
74 * The demo task. This simply counts the semaphore up to its maximum value,
\r
75 * the counts it back down again. The result of each semaphore 'give' and
\r
76 * 'take' is inspected, with an error being flagged if it is found not to be
\r
77 * the expected result.
\r
79 static void prvCountingSemaphoreTask( void *pvParameters );
\r
82 * Utility function to increment the semaphore count value up from zero to
\r
83 * countMAX_COUNT_VALUE.
\r
85 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
\r
88 * Utility function to decrement the semaphore count value up from
\r
89 * countMAX_COUNT_VALUE to zero.
\r
91 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
\r
93 /*-----------------------------------------------------------*/
\r
95 /* The structure that is passed into the task as the task parameter. */
\r
96 typedef struct COUNT_SEM_STRUCT
\r
98 /* The semaphore to be used for the demo. */
\r
99 xSemaphoreHandle xSemaphore;
\r
101 /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
\r
102 its count value set to its max count value, or countSTART_AT_ZERO if it
\r
103 should have been created with its count value set to 0. */
\r
104 unsigned portBASE_TYPE uxExpectedStartCount;
\r
106 /* Incremented on each cycle of the demo task. Used to detect a stalled
\r
108 unsigned portBASE_TYPE uxLoopCounter;
\r
111 /* Two structures are defined, one is passed to each test task. */
\r
112 static xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
\r
114 /*-----------------------------------------------------------*/
\r
116 void vStartCountingSemaphoreTasks( void )
\r
118 /* Create the semaphores that we are going to use for the test/demo. The
\r
119 first should be created such that it starts at its maximum count value,
\r
120 the second should be created such that it starts with a count value of zero. */
\r
121 xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
\r
122 xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
\r
123 xParameters[ 0 ].uxLoopCounter = 0;
\r
125 xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
\r
126 xParameters[ 1 ].uxExpectedStartCount = 0;
\r
127 xParameters[ 1 ].uxLoopCounter = 0;
\r
129 /* Were the semaphores created? */
\r
130 if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
\r
132 /* Create the demo tasks, passing in the semaphore to use as the parameter. */
\r
133 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
\r
134 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
\r
137 /*-----------------------------------------------------------*/
\r
139 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
\r
141 unsigned portBASE_TYPE ux;
\r
143 /* If the semaphore count is at its maximum then we should not be able to
\r
144 'give' the semaphore. */
\r
145 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
147 xErrorDetected = pdTRUE;
\r
150 /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
\r
151 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
153 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
\r
155 /* We expected to be able to take the semaphore. */
\r
156 xErrorDetected = pdTRUE;
\r
159 ( *puxLoopCounter )++;
\r
162 #if configUSE_PREEMPTION == 0
\r
166 /* If the semaphore count is zero then we should not be able to 'take'
\r
168 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
\r
170 xErrorDetected = pdTRUE;
\r
173 /*-----------------------------------------------------------*/
\r
175 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
\r
177 unsigned portBASE_TYPE ux;
\r
179 /* If the semaphore count is zero then we should not be able to 'take'
\r
181 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
\r
183 xErrorDetected = pdTRUE;
\r
186 /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
\r
187 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
189 if( xSemaphoreGive( xSemaphore ) != pdPASS )
\r
191 /* We expected to be able to take the semaphore. */
\r
192 xErrorDetected = pdTRUE;
\r
195 ( *puxLoopCounter )++;
\r
198 #if configUSE_PREEMPTION == 0
\r
202 /* If the semaphore count is at its maximum then we should not be able to
\r
203 'give' the semaphore. */
\r
204 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
206 xErrorDetected = pdTRUE;
\r
209 /*-----------------------------------------------------------*/
\r
211 static void prvCountingSemaphoreTask( void *pvParameters )
\r
213 xCountSemStruct *pxParameter;
\r
216 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
218 const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
\r
220 /* Queue a message for printing to say the task has started. */
\r
221 vPrintDisplayMessage( &pcTaskStartMsg );
\r
224 /* The semaphore to be used was passed as the parameter. */
\r
225 pxParameter = ( xCountSemStruct * ) pvParameters;
\r
227 /* Did we expect to find the semaphore already at its max count value, or
\r
229 if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
\r
231 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
234 /* Now we expect the semaphore count to be 0, so this time there is an
\r
235 error if we can take the semaphore. */
\r
236 if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
\r
238 xErrorDetected = pdTRUE;
\r
243 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
244 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
247 /*-----------------------------------------------------------*/
\r
249 portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )
\r
251 static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;
\r
252 portBASE_TYPE xReturn = pdPASS;
\r
254 /* Return fail if any 'give' or 'take' did not result in the expected
\r
256 if( xErrorDetected != pdFALSE )
\r
261 /* Return fail if either task is not still incrementing its loop counter. */
\r
262 if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
\r
268 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
\r
271 if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
\r
277 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;
\r