1 |
/* |
---|
2 |
FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd. |
---|
3 |
|
---|
4 |
This file is part of the FreeRTOS distribution. |
---|
5 |
|
---|
6 |
FreeRTOS is free software; you can redistribute it and/or modify it under |
---|
7 |
the terms of the GNU General Public License (version 2) as published by the |
---|
8 |
Free Software Foundation and modified by the FreeRTOS exception. |
---|
9 |
**NOTE** The exception to the GPL is included to allow you to distribute a |
---|
10 |
combined work that includes FreeRTOS without being obliged to provide the |
---|
11 |
source code for proprietary components outside of the FreeRTOS kernel. |
---|
12 |
Alternative commercial license and support terms are also available upon |
---|
13 |
request. See the licensing section of http://www.FreeRTOS.org for full |
---|
14 |
license details. |
---|
15 |
|
---|
16 |
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT |
---|
17 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
18 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
19 |
more details. |
---|
20 |
|
---|
21 |
You should have received a copy of the GNU General Public License along |
---|
22 |
with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59 |
---|
23 |
Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
---|
24 |
|
---|
25 |
|
---|
26 |
*************************************************************************** |
---|
27 |
* * |
---|
28 |
* Looking for a quick start? Then check out the FreeRTOS eBook! * |
---|
29 |
* See http://www.FreeRTOS.org/Documentation for details * |
---|
30 |
* * |
---|
31 |
*************************************************************************** |
---|
32 |
|
---|
33 |
1 tab == 4 spaces! |
---|
34 |
|
---|
35 |
Please ensure to read the configuration and relevant port sections of the |
---|
36 |
online documentation. |
---|
37 |
|
---|
38 |
http://www.FreeRTOS.org - Documentation, latest information, license and |
---|
39 |
contact details. |
---|
40 |
|
---|
41 |
http://www.SafeRTOS.com - A version that is certified for use in safety |
---|
42 |
critical systems. |
---|
43 |
|
---|
44 |
http://www.OpenRTOS.com - Commercial support, development, porting, |
---|
45 |
licensing and training services. |
---|
46 |
*/ |
---|
47 |
|
---|
48 |
/* |
---|
49 |
* Creates two sets of two tasks. The tasks within a set share a variable, access |
---|
50 |
* to which is guarded by a semaphore. |
---|
51 |
* |
---|
52 |
* Each task starts by attempting to obtain the semaphore. On obtaining a |
---|
53 |
* semaphore a task checks to ensure that the guarded variable has an expected |
---|
54 |
* value. It then clears the variable to zero before counting it back up to the |
---|
55 |
* expected value in increments of 1. After each increment the variable is checked |
---|
56 |
* to ensure it contains the value to which it was just set. When the starting |
---|
57 |
* value is again reached the task releases the semaphore giving the other task in |
---|
58 |
* the set a chance to do exactly the same thing. The starting value is high |
---|
59 |
* enough to ensure that a tick is likely to occur during the incrementing loop. |
---|
60 |
* |
---|
61 |
* An error is flagged if at any time during the process a shared variable is |
---|
62 |
* found to have a value other than that expected. Such an occurrence would |
---|
63 |
* suggest an error in the mutual exclusion mechanism by which access to the |
---|
64 |
* variable is restricted. |
---|
65 |
* |
---|
66 |
* The first set of two tasks poll their semaphore. The second set use blocking |
---|
67 |
* calls. |
---|
68 |
* |
---|
69 |
*/ |
---|
70 |
|
---|
71 |
|
---|
72 |
#include <stdlib.h> |
---|
73 |
|
---|
74 |
/* Scheduler include files. */ |
---|
75 |
#include "FreeRTOS.h" |
---|
76 |
#include "task.h" |
---|
77 |
#include "semphr.h" |
---|
78 |
|
---|
79 |
/* Demo app include files. */ |
---|
80 |
#include "semtest.h" |
---|
81 |
|
---|
82 |
/* The value to which the shared variables are counted. */ |
---|
83 |
#define semtstBLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xfff ) |
---|
84 |
#define semtstNON_BLOCKING_EXPECTED_VALUE ( ( unsigned portLONG ) 0xff ) |
---|
85 |
|
---|
86 |
#define semtstSTACK_SIZE configMINIMAL_STACK_SIZE |
---|
87 |
|
---|
88 |
#define semtstNUM_TASKS ( 4 ) |
---|
89 |
|
---|
90 |
#define semtstDELAY_FACTOR ( ( portTickType ) 10 ) |
---|
91 |
|
---|
92 |
/* The task function as described at the top of the file. */ |
---|
93 |
static portTASK_FUNCTION_PROTO( prvSemaphoreTest, pvParameters ); |
---|
94 |
|
---|
95 |
/* Structure used to pass parameters to each task. */ |
---|
96 |
typedef struct SEMAPHORE_PARAMETERS |
---|
97 |
{ |
---|
98 |
xSemaphoreHandle xSemaphore; |
---|
99 |
volatile unsigned portLONG *pulSharedVariable; |
---|
100 |
portTickType xBlockTime; |
---|
101 |
} xSemaphoreParameters; |
---|
102 |
|
---|
103 |
/* Variables used to check that all the tasks are still running without errors. */ |
---|
104 |
static volatile portSHORT sCheckVariables[ semtstNUM_TASKS ] = { 0 }; |
---|
105 |
static volatile portSHORT sNextCheckVariable = 0; |
---|
106 |
|
---|
107 |
/*-----------------------------------------------------------*/ |
---|
108 |
|
---|
109 |
void vStartSemaphoreTasks( unsigned portBASE_TYPE uxPriority ) |
---|
110 |
{ |
---|
111 |
xSemaphoreParameters *pxFirstSemaphoreParameters, *pxSecondSemaphoreParameters; |
---|
112 |
const portTickType xBlockTime = ( portTickType ) 100; |
---|
113 |
|
---|
114 |
/* Create the structure used to pass parameters to the first two tasks. */ |
---|
115 |
pxFirstSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) ); |
---|
116 |
|
---|
117 |
if( pxFirstSemaphoreParameters != NULL ) |
---|
118 |
{ |
---|
119 |
/* Create the semaphore used by the first two tasks. */ |
---|
120 |
vSemaphoreCreateBinary( pxFirstSemaphoreParameters->xSemaphore ); |
---|
121 |
|
---|
122 |
if( pxFirstSemaphoreParameters->xSemaphore != NULL ) |
---|
123 |
{ |
---|
124 |
/* Create the variable which is to be shared by the first two tasks. */ |
---|
125 |
pxFirstSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) ); |
---|
126 |
|
---|
127 |
/* Initialise the share variable to the value the tasks expect. */ |
---|
128 |
*( pxFirstSemaphoreParameters->pulSharedVariable ) = semtstNON_BLOCKING_EXPECTED_VALUE; |
---|
129 |
|
---|
130 |
/* The first two tasks do not block on semaphore calls. */ |
---|
131 |
pxFirstSemaphoreParameters->xBlockTime = ( portTickType ) 0; |
---|
132 |
|
---|
133 |
/* Spawn the first two tasks. As they poll they operate at the idle priority. */ |
---|
134 |
xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "PolSEM1", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL ); |
---|
135 |
xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "PolSEM2", semtstSTACK_SIZE, ( void * ) pxFirstSemaphoreParameters, tskIDLE_PRIORITY, ( xTaskHandle * ) NULL ); |
---|
136 |
} |
---|
137 |
} |
---|
138 |
|
---|
139 |
/* Do exactly the same to create the second set of tasks, only this time |
---|
140 |
provide a block time for the semaphore calls. */ |
---|
141 |
pxSecondSemaphoreParameters = ( xSemaphoreParameters * ) pvPortMalloc( sizeof( xSemaphoreParameters ) ); |
---|
142 |
if( pxSecondSemaphoreParameters != NULL ) |
---|
143 |
{ |
---|
144 |
vSemaphoreCreateBinary( pxSecondSemaphoreParameters->xSemaphore ); |
---|
145 |
|
---|
146 |
if( pxSecondSemaphoreParameters->xSemaphore != NULL ) |
---|
147 |
{ |
---|
148 |
pxSecondSemaphoreParameters->pulSharedVariable = ( unsigned portLONG * ) pvPortMalloc( sizeof( unsigned portLONG ) ); |
---|
149 |
*( pxSecondSemaphoreParameters->pulSharedVariable ) = semtstBLOCKING_EXPECTED_VALUE; |
---|
150 |
pxSecondSemaphoreParameters->xBlockTime = xBlockTime / portTICK_RATE_MS; |
---|
151 |
|
---|
152 |
xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "BlkSEM1", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL ); |
---|
153 |
xTaskCreate( prvSemaphoreTest, ( signed portCHAR * ) "BlkSEM2", semtstSTACK_SIZE, ( void * ) pxSecondSemaphoreParameters, uxPriority, ( xTaskHandle * ) NULL ); |
---|
154 |
} |
---|
155 |
} |
---|
156 |
|
---|
157 |
/* vQueueAddToRegistry() adds the semaphore to the registry, if one is |
---|
158 |
in use. The registry is provided as a means for kernel aware |
---|
159 |
debuggers to locate semaphores and has no purpose if a kernel aware debugger |
---|
160 |
is not being used. The call to vQueueAddToRegistry() will be removed |
---|
161 |
by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is |
---|
162 |
defined to be less than 1. */ |
---|
163 |
vQueueAddToRegistry( ( xQueueHandle ) pxFirstSemaphoreParameters->xSemaphore, ( signed portCHAR * ) "Counting_Sem_1" ); |
---|
164 |
vQueueAddToRegistry( ( xQueueHandle ) pxSecondSemaphoreParameters->xSemaphore, ( signed portCHAR * ) "Counting_Sem_2" ); |
---|
165 |
} |
---|
166 |
/*-----------------------------------------------------------*/ |
---|
167 |
|
---|
168 |
static portTASK_FUNCTION( prvSemaphoreTest, pvParameters ) |
---|
169 |
{ |
---|
170 |
xSemaphoreParameters *pxParameters; |
---|
171 |
volatile unsigned portLONG *pulSharedVariable, ulExpectedValue; |
---|
172 |
unsigned portLONG ulCounter; |
---|
173 |
portSHORT sError = pdFALSE, sCheckVariableToUse; |
---|
174 |
|
---|
175 |
/* See which check variable to use. sNextCheckVariable is not semaphore |
---|
176 |
protected! */ |
---|
177 |
portENTER_CRITICAL(); |
---|
178 |
sCheckVariableToUse = sNextCheckVariable; |
---|
179 |
sNextCheckVariable++; |
---|
180 |
portEXIT_CRITICAL(); |
---|
181 |
|
---|
182 |
/* A structure is passed in as the parameter. This contains the shared |
---|
183 |
variable being guarded. */ |
---|
184 |
pxParameters = ( xSemaphoreParameters * ) pvParameters; |
---|
185 |
pulSharedVariable = pxParameters->pulSharedVariable; |
---|
186 |
|
---|
187 |
/* If we are blocking we use a much higher count to ensure loads of context |
---|
188 |
switches occur during the count. */ |
---|
189 |
if( pxParameters->xBlockTime > ( portTickType ) 0 ) |
---|
190 |
{ |
---|
191 |
ulExpectedValue = semtstBLOCKING_EXPECTED_VALUE; |
---|
192 |
} |
---|
193 |
else |
---|
194 |
{ |
---|
195 |
ulExpectedValue = semtstNON_BLOCKING_EXPECTED_VALUE; |
---|
196 |
} |
---|
197 |
|
---|
198 |
for( ;; ) |
---|
199 |
{ |
---|
200 |
/* Try to obtain the semaphore. */ |
---|
201 |
if( xSemaphoreTake( pxParameters->xSemaphore, pxParameters->xBlockTime ) == pdPASS ) |
---|
202 |
{ |
---|
203 |
/* We have the semaphore and so expect any other tasks using the |
---|
204 |
shared variable to have left it in the state we expect to find |
---|
205 |
it. */ |
---|
206 |
if( *pulSharedVariable != ulExpectedValue ) |
---|
207 |
{ |
---|
208 |
sError = pdTRUE; |
---|
209 |
} |
---|
210 |
|
---|
211 |
/* Clear the variable, then count it back up to the expected value |
---|
212 |
before releasing the semaphore. Would expect a context switch or |
---|
213 |
two during this time. */ |
---|
214 |
for( ulCounter = ( unsigned portLONG ) 0; ulCounter <= ulExpectedValue; ulCounter++ ) |
---|
215 |
{ |
---|
216 |
*pulSharedVariable = ulCounter; |
---|
217 |
if( *pulSharedVariable != ulCounter ) |
---|
218 |
{ |
---|
219 |
sError = pdTRUE; |
---|
220 |
} |
---|
221 |
} |
---|
222 |
|
---|
223 |
/* Release the semaphore, and if no errors have occurred increment the check |
---|
224 |
variable. */ |
---|
225 |
if( xSemaphoreGive( pxParameters->xSemaphore ) == pdFALSE ) |
---|
226 |
{ |
---|
227 |
sError = pdTRUE; |
---|
228 |
} |
---|
229 |
|
---|
230 |
if( sError == pdFALSE ) |
---|
231 |
{ |
---|
232 |
if( sCheckVariableToUse < semtstNUM_TASKS ) |
---|
233 |
{ |
---|
234 |
( sCheckVariables[ sCheckVariableToUse ] )++; |
---|
235 |
} |
---|
236 |
} |
---|
237 |
|
---|
238 |
/* If we have a block time then we are running at a priority higher |
---|
239 |
than the idle priority. This task takes a long time to complete |
---|
240 |
a cycle (deliberately so to test the guarding) so will be starving |
---|
241 |
out lower priority tasks. Block for some time to allow give lower |
---|
242 |
priority tasks some processor time. */ |
---|
243 |
vTaskDelay( pxParameters->xBlockTime * semtstDELAY_FACTOR ); |
---|
244 |
} |
---|
245 |
else |
---|
246 |
{ |
---|
247 |
if( pxParameters->xBlockTime == ( portTickType ) 0 ) |
---|
248 |
{ |
---|
249 |
/* We have not got the semaphore yet, so no point using the |
---|
250 |
processor. We are not blocking when attempting to obtain the |
---|
251 |
semaphore. */ |
---|
252 |
taskYIELD(); |
---|
253 |
} |
---|
254 |
} |
---|
255 |
} |
---|
256 |
} |
---|
257 |
/*-----------------------------------------------------------*/ |
---|
258 |
|
---|
259 |
/* This is called to check that all the created tasks are still running. */ |
---|
260 |
portBASE_TYPE xAreSemaphoreTasksStillRunning( void ) |
---|
261 |
{ |
---|
262 |
static portSHORT sLastCheckVariables[ semtstNUM_TASKS ] = { 0 }; |
---|
263 |
portBASE_TYPE xTask, xReturn = pdTRUE; |
---|
264 |
|
---|
265 |
for( xTask = 0; xTask < semtstNUM_TASKS; xTask++ ) |
---|
266 |
{ |
---|
267 |
if( sLastCheckVariables[ xTask ] == sCheckVariables[ xTask ] ) |
---|
268 |
{ |
---|
269 |
xReturn = pdFALSE; |
---|
270 |
} |
---|
271 |
|
---|
272 |
sLastCheckVariables[ xTask ] = sCheckVariables[ xTask ]; |
---|
273 |
} |
---|
274 |
|
---|
275 |
return xReturn; |
---|
276 |
} |
---|
277 |
|
---|
278 |
|
---|