2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
67 * Simple demonstration of the usage of counting semaphore.
\r
70 /* Scheduler include files. */
\r
71 #include "FreeRTOS.h"
\r
75 /* Demo program include files. */
\r
76 #include "countsem.h"
\r
78 /* The maximum count value that the semaphore used for the demo can hold. */
\r
79 #define countMAX_COUNT_VALUE ( 200 )
\r
81 /* Constants used to indicate whether or not the semaphore should have been
\r
82 created with its maximum count value, or its minimum count value. These
\r
83 numbers are used to ensure that the pointers passed in as the task parameters
\r
85 #define countSTART_AT_MAX_COUNT ( 0xaa )
\r
86 #define countSTART_AT_ZERO ( 0x55 )
\r
88 /* Two tasks are created for the test. One uses a semaphore created with its
\r
89 count value set to the maximum, and one with the count value set to zero. */
\r
90 #define countNUM_TEST_TASKS ( 2 )
\r
91 #define countDONT_BLOCK ( 0 )
\r
93 /*-----------------------------------------------------------*/
\r
95 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
96 detected in any of the tasks. */
\r
97 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
\r
99 /*-----------------------------------------------------------*/
\r
102 * The demo task. This simply counts the semaphore up to its maximum value,
\r
103 * the counts it back down again. The result of each semaphore 'give' and
\r
104 * 'take' is inspected, with an error being flagged if it is found not to be
\r
105 * the expected result.
\r
107 static void prvCountingSemaphoreTask( void *pvParameters );
\r
110 * Utility function to increment the semaphore count value up from zero to
\r
111 * countMAX_COUNT_VALUE.
\r
113 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
\r
116 * Utility function to decrement the semaphore count value up from
\r
117 * countMAX_COUNT_VALUE to zero.
\r
119 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter );
\r
121 /*-----------------------------------------------------------*/
\r
123 /* The structure that is passed into the task as the task parameter. */
\r
124 typedef struct COUNT_SEM_STRUCT
\r
126 /* The semaphore to be used for the demo. */
\r
127 xSemaphoreHandle xSemaphore;
\r
129 /* Set to countSTART_AT_MAX_COUNT if the semaphore should be created with
\r
130 its count value set to its max count value, or countSTART_AT_ZERO if it
\r
131 should have been created with its count value set to 0. */
\r
132 unsigned portBASE_TYPE uxExpectedStartCount;
\r
134 /* Incremented on each cycle of the demo task. Used to detect a stalled
\r
136 unsigned portBASE_TYPE uxLoopCounter;
\r
139 /* Two structures are defined, one is passed to each test task. */
\r
140 static volatile xCountSemStruct xParameters[ countNUM_TEST_TASKS ];
\r
142 /*-----------------------------------------------------------*/
\r
144 void vStartCountingSemaphoreTasks( void )
\r
146 /* Create the semaphores that we are going to use for the test/demo. The
\r
147 first should be created such that it starts at its maximum count value,
\r
148 the second should be created such that it starts with a count value of zero. */
\r
149 xParameters[ 0 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, countMAX_COUNT_VALUE );
\r
150 xParameters[ 0 ].uxExpectedStartCount = countSTART_AT_MAX_COUNT;
\r
151 xParameters[ 0 ].uxLoopCounter = 0;
\r
153 xParameters[ 1 ].xSemaphore = xSemaphoreCreateCounting( countMAX_COUNT_VALUE, 0 );
\r
154 xParameters[ 1 ].uxExpectedStartCount = 0;
\r
155 xParameters[ 1 ].uxLoopCounter = 0;
\r
157 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
\r
158 in use. The registry is provided as a means for kernel aware
\r
159 debuggers to locate semaphores and has no purpose if a kernel aware debugger
\r
160 is not being used. The call to vQueueAddToRegistry() will be removed
\r
161 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
162 defined to be less than 1. */
\r
163 vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 0 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" );
\r
164 vQueueAddToRegistry( ( xQueueHandle ) xParameters[ 1 ].xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" );
\r
167 /* Were the semaphores created? */
\r
168 if( ( xParameters[ 0 ].xSemaphore != NULL ) || ( xParameters[ 1 ].xSemaphore != NULL ) )
\r
170 /* Create the demo tasks, passing in the semaphore to use as the parameter. */
\r
171 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT1", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 0 ] ), tskIDLE_PRIORITY, NULL );
\r
172 xTaskCreate( prvCountingSemaphoreTask, ( signed portCHAR * ) "CNT2", configMINIMAL_STACK_SIZE, ( void * ) &( xParameters[ 1 ] ), tskIDLE_PRIORITY, NULL );
\r
175 /*-----------------------------------------------------------*/
\r
177 static void prvDecrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
\r
179 unsigned portBASE_TYPE ux;
\r
181 /* If the semaphore count is at its maximum then we should not be able to
\r
182 'give' the semaphore. */
\r
183 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
185 xErrorDetected = pdTRUE;
\r
188 /* We should be able to 'take' the semaphore countMAX_COUNT_VALUE times. */
\r
189 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
191 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) != pdPASS )
\r
193 /* We expected to be able to take the semaphore. */
\r
194 xErrorDetected = pdTRUE;
\r
197 ( *puxLoopCounter )++;
\r
200 #if configUSE_PREEMPTION == 0
\r
204 /* If the semaphore count is zero then we should not be able to 'take'
\r
206 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
\r
208 xErrorDetected = pdTRUE;
\r
211 /*-----------------------------------------------------------*/
\r
213 static void prvIncrementSemaphoreCount( xSemaphoreHandle xSemaphore, unsigned portBASE_TYPE *puxLoopCounter )
\r
215 unsigned portBASE_TYPE ux;
\r
217 /* If the semaphore count is zero then we should not be able to 'take'
\r
219 if( xSemaphoreTake( xSemaphore, countDONT_BLOCK ) == pdPASS )
\r
221 xErrorDetected = pdTRUE;
\r
224 /* We should be able to 'give' the semaphore countMAX_COUNT_VALUE times. */
\r
225 for( ux = 0; ux < countMAX_COUNT_VALUE; ux++ )
\r
227 if( xSemaphoreGive( xSemaphore ) != pdPASS )
\r
229 /* We expected to be able to take the semaphore. */
\r
230 xErrorDetected = pdTRUE;
\r
233 ( *puxLoopCounter )++;
\r
236 #if configUSE_PREEMPTION == 0
\r
240 /* If the semaphore count is at its maximum then we should not be able to
\r
241 'give' the semaphore. */
\r
242 if( xSemaphoreGive( xSemaphore ) == pdPASS )
\r
244 xErrorDetected = pdTRUE;
\r
247 /*-----------------------------------------------------------*/
\r
249 static void prvCountingSemaphoreTask( void *pvParameters )
\r
251 xCountSemStruct *pxParameter;
\r
254 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
256 const portCHAR * const pcTaskStartMsg = "Counting semaphore demo started.\r\n";
\r
258 /* Queue a message for printing to say the task has started. */
\r
259 vPrintDisplayMessage( &pcTaskStartMsg );
\r
262 /* The semaphore to be used was passed as the parameter. */
\r
263 pxParameter = ( xCountSemStruct * ) pvParameters;
\r
265 /* Did we expect to find the semaphore already at its max count value, or
\r
267 if( pxParameter->uxExpectedStartCount == countSTART_AT_MAX_COUNT )
\r
269 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
272 /* Now we expect the semaphore count to be 0, so this time there is an
\r
273 error if we can take the semaphore. */
\r
274 if( xSemaphoreTake( pxParameter->xSemaphore, 0 ) == pdPASS )
\r
276 xErrorDetected = pdTRUE;
\r
281 prvIncrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
282 prvDecrementSemaphoreCount( pxParameter->xSemaphore, &( pxParameter->uxLoopCounter ) );
\r
285 /*-----------------------------------------------------------*/
\r
287 portBASE_TYPE xAreCountingSemaphoreTasksStillRunning( void )
\r
289 static unsigned portBASE_TYPE uxLastCount0 = 0, uxLastCount1 = 0;
\r
290 portBASE_TYPE xReturn = pdPASS;
\r
292 /* Return fail if any 'give' or 'take' did not result in the expected
\r
294 if( xErrorDetected != pdFALSE )
\r
299 /* Return fail if either task is not still incrementing its loop counter. */
\r
300 if( uxLastCount0 == xParameters[ 0 ].uxLoopCounter )
\r
306 uxLastCount0 = xParameters[ 0 ].uxLoopCounter;
\r
309 if( uxLastCount1 == xParameters[ 1 ].uxLoopCounter )
\r
315 uxLastCount1 = xParameters[ 1 ].uxLoopCounter;
\r