2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * Creates two sets of two tasks. The tasks within a set share a variable, access
\r
98 * to which is guarded by a semaphore.
\r
100 * Each task starts by attempting to obtain the semaphore. On obtaining a
\r
101 * semaphore a task checks to ensure that the guarded variable has an expected
\r
102 * value. It then clears the variable to zero before counting it back up to the
\r
103 * expected value in increments of 1. After each increment the variable is checked
\r
104 * to ensure it contains the value to which it was just set. When the starting
\r
105 * value is again reached the task releases the semaphore giving the other task in
\r
106 * the set a chance to do exactly the same thing. The starting value is high
\r
107 * enough to ensure that a tick is likely to occur during the incrementing loop.
\r
109 * An error is flagged if at any time during the process a shared variable is
\r
110 * found to have a value other than that expected. Such an occurrence would
\r
111 * suggest an error in the mutual exclusion mechanism by which access to the
\r
112 * variable is restricted.
\r
114 * The first set of two tasks poll their semaphore. The second set use blocking
\r
120 #include <stdlib.h>
\r
122 /* Scheduler include files. */
\r
123 #include "FreeRTOS.h"
\r
125 #include "semphr.h"
\r
127 /* Demo app include files. */
\r
128 #include "semtest.h"
\r
130 /* The value to which the shared variables are counted. */
\r
131 #define semtstBLOCKING_EXPECTED_VALUE ( ( uint32_t ) 0xfff )
\r
132 #define semtstNON_BLOCKING_EXPECTED_VALUE ( ( uint32_t ) 0xff )
\r
134 #define semtstSTACK_SIZE configMINIMAL_STACK_SIZE
\r
136 #define semtstNUM_TASKS ( 4 )
\r
138 #define semtstDELAY_FACTOR ( ( TickType_t ) 10 )
\r
140 /* The task function as described at the top of the file. */
\r
141 static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters );
\r
143 /* Structure used to pass parameters to each task. */
\r
144 typedef struct SEMAPHORE_PARAMETERS
\r
146 SemaphoreHandle_t xSemaphore;
\r
147 volatile uint32_t *pulSharedVariable;
\r
148 TickType_t xBlockTime;
\r
149 } xSemaphoreParameters;
\r
151 /* Variables used to check that all the tasks are still running without errors. */
\r
152 static volatile short sCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
153 static volatile short sNextCheckVariable = 0;
\r
155 /*-----------------------------------------------------------*/
\r
157 void vStartSemaphoreTasks( UBaseType_t uxPriority )
\r
159 xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters;
\r
160 const TickType_t xBlockTime = ( TickType_t ) 100;
\r
162 /* Create the structure used to pass parameters to the first two tasks. */
\r
163 pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
165 if( pxFirstSemaphoreParameters != NULL )
\r
167 /* Create the semaphore used by the first two tasks. */
\r
168 pxFirstSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();
\r
169 xSemaphoreGive( pxFirstSemaphoreParameters->xSemaphore );
\r
171 if( pxFirstSemaphoreParameters->xSemaphore != NULL )
\r
173 /* Create the variable which is to be shared by the first two tasks. */
\r
174 pxFirstSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );
\r
176 /* Initialise the share variable to the value the tasks expect. */
\r
177 *( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
179 /* The first two tasks do not block on semaphore calls. */
\r
180 pxFirstSemaphoreParameters->xBlockTime = ( TickType_t ) 0;
\r
182 /* Spawn the first two tasks. As they poll they operate at the idle priority. */
\r
183 xTaskCreate( prvSemaphoreTest, "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
184 xTaskCreate( prvSemaphoreTest, "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( TaskHandle_t * ) NULL );
\r
188 /* Do exactly the same to create the second set of tasks, only this time
\r
189 provide a block time for the semaphore calls. */
\r
190 pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) );
\r
191 if( pxSecondSemaphoreParameters != NULL )
\r
193 pxSecondSemaphoreParameters->xSemaphore = xSemaphoreCreateBinary();
\r
194 xSemaphoreGive( pxSecondSemaphoreParameters->xSemaphore );
\r
196 if( pxSecondSemaphoreParameters->xSemaphore != NULL )
\r
198 pxSecondSemaphoreParameters->pulSharedVariable = ( uint32_t * ) pvPortMalloc( sizeof( uint32_t ) );
\r
199 *( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE;
\r
200 pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_PERIOD_MS;
\r
202 xTaskCreate( prvSemaphoreTest, "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );
\r
203 xTaskCreate( prvSemaphoreTest, "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( TaskHandle_t * ) NULL );
\r
207 /* vQueueAddToRegistry() adds the semaphore to the registry, if one is
\r
208 in use. The registry is provided as a means for kernel aware
\r
209 debuggers to locate semaphores and has no purpose if a kernel aware debugger
\r
210 is not being used. The call to vQueueAddToRegistry() will be removed
\r
211 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
212 defined to be less than 1. */
\r
213 vQueueAddToRegistry( ( QueueHandle_t ) pxFirstSemaphoreParameters->xSemaphore, "Counting_Sem_1" );
\r
214 vQueueAddToRegistry( ( QueueHandle_t ) pxSecondSemaphoreParameters->xSemaphore, "Counting_Sem_2" );
\r
216 /*-----------------------------------------------------------*/
\r
218 static portTASK_FUNCTION( prvSemaphoreTest, pvParameters )
\r
220 xSemaphoreParameters *pxParameters;
\r
221 volatile uint32_t *pulSharedVariable, ulExpectedValue;
\r
222 uint32_t ulCounter;
\r
223 short sError = pdFALSE, sCheckVariableToUse;
\r
225 /* See which check variable to use. sNextCheckVariable is not semaphore
\r
227 portENTER_CRITICAL();
\r
228 sCheckVariableToUse = sNextCheckVariable;
\r
229 sNextCheckVariable++;
\r
230 portEXIT_CRITICAL();
\r
232 /* A structure is passed in as the parameter. This contains the shared
\r
233 variable being guarded. */
\r
234 pxParameters = ( xSemaphoreParameters * ) pvParameters;
\r
235 pulSharedVariable = pxParameters->pulSharedVariable;
\r
237 /* If we are blocking we use a much higher count to ensure loads of context
\r
238 switches occur during the count. */
\r
239 if( pxParameters->xBlockTime > ( TickType_t ) 0 )
\r
241 ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE;
\r
245 ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE;
\r
250 /* Try to obtain the semaphore. */
\r
251 if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS )
\r
253 /* We have the semaphore and so expect any other tasks using the
\r
254 shared variable to have left it in the state we expect to find
\r
256 if( *pulSharedVariable != ulExpectedValue )
\r
261 /* Clear the variable, then count it back up to the expected value
\r
262 before releasing the semaphore. Would expect a context switch or
\r
263 two during this time. */
\r
264 for( ulCounter = ( uint32_t ) 0; ulCounter <= ulExpectedValue; ulCounter++ )
\r
266 *pulSharedVariable = ulCounter;
\r
267 if( *pulSharedVariable != ulCounter )
\r
273 /* Release the semaphore, and if no errors have occurred increment the check
\r
275 if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE )
\r
280 if( sError == pdFALSE )
\r
282 if( sCheckVariableToUse < semtstNUM_TASKS )
\r
284 ( sCheckVariables[ sCheckVariableToUse ] )++;
\r
288 /* If we have a block time then we are running at a priority higher
\r
289 than the idle priority. This task takes a long time to complete
\r
290 a cycle (deliberately so to test the guarding) so will be starving
\r
291 out lower priority tasks. Block for some time to allow give lower
\r
292 priority tasks some processor time. */
\r
293 vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR );
\r
297 if( pxParameters->xBlockTime == ( TickType_t ) 0 )
\r
299 /* We have not got the semaphore yet, so no point using the
\r
300 processor. We are not blocking when attempting to obtain the
\r
307 /*-----------------------------------------------------------*/
\r
309 /* This is called to check that all the created tasks are still running. */
\r
310 BaseType_t xAreSemaphoreTasksStillRunning( void )
\r
312 static short sLastCheckVariables[ semtstNUM_TASKS ] = { 0 };
\r
313 BaseType_t xTask, xReturn = pdTRUE;
\r
315 for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ )
\r
317 if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] )
\r
322 sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ];
\r