2 * FreeRTOS Kernel V10.1.1
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * Creates eight tasks, each of which loops continuously performing a floating
\r
30 * point calculation.
\r
32 * All the tasks run at the idle priority and never block or yield. This causes
\r
33 * all eight tasks to time slice with the idle task. Running at the idle
\r
34 * priority means that these tasks will get pre-empted any time another task is
\r
35 * ready to run or a time slice occurs. More often than not the pre-emption
\r
36 * will occur mid calculation, creating a good test of the schedulers context
\r
37 * switch mechanism - a calculation producing an unexpected result could be a
\r
38 * symptom of a corruption in the context of a task.
\r
41 /* Standard includes. */
\r
45 /* Scheduler include files. */
\r
46 #include "FreeRTOS.h"
\r
49 /* Demo program include files. */
\r
52 #ifndef mathSTACK_SIZE
\r
53 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
56 #define mathNUMBER_OF_TASKS ( 4 )
\r
58 /* Four tasks, each of which performs a different floating point calculation.
\r
59 Each of the four is created twice. */
\r
60 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
61 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
62 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
63 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
65 /* These variables are used to check that all the tasks are still running. If a
\r
66 task gets a calculation wrong it will stop setting its check variable. */
\r
67 static uint16_t usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };
\r
69 /*-----------------------------------------------------------*/
\r
71 void vStartMathTasks( UBaseType_t uxPriority )
\r
73 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
74 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
75 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
76 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
78 /*-----------------------------------------------------------*/
\r
80 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
82 volatile portDOUBLE d1, d2, d3, d4;
\r
83 volatile uint16_t *pusTaskCheckVariable;
\r
84 volatile portDOUBLE dAnswer;
\r
85 short sError = pdFALSE;
\r
87 /* Some ports require that tasks that use a hardware floating point unit
\r
88 tell the kernel that they require a floating point context before any
\r
89 floating point instructions are executed. */
\r
90 portTASK_USES_FLOATING_POINT();
\r
96 dAnswer = ( d1 + d2 ) * d3;
\r
98 /* The variable this task increments to show it is still running is passed in
\r
99 as the parameter. */
\r
100 pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;
\r
102 /* Keep performing a calculation and checking the result against a constant. */
\r
109 d4 = ( d1 + d2 ) * d3;
\r
111 #if configUSE_PREEMPTION == 0
\r
115 /* If the calculation does not match the expected constant, stop the
\r
116 increment of the check variable. */
\r
117 if( fabs( d4 - dAnswer ) > 0.001 )
\r
122 if( sError == pdFALSE )
\r
124 /* If the calculation has always been correct then set set the check
\r
125 variable. The check variable will get set to pdFALSE each time
\r
126 xAreMathsTaskStillRunning() is executed. */
\r
127 ( *pusTaskCheckVariable ) = pdTRUE;
\r
130 #if configUSE_PREEMPTION == 0
\r
136 /*-----------------------------------------------------------*/
\r
138 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
140 volatile portDOUBLE d1, d2, d3, d4;
\r
141 volatile uint16_t *pusTaskCheckVariable;
\r
142 volatile portDOUBLE dAnswer;
\r
143 short sError = pdFALSE;
\r
145 /* Some ports require that tasks that use a hardware floating point unit
\r
146 tell the kernel that they require a floating point context before any
\r
147 floating point instructions are executed. */
\r
148 portTASK_USES_FLOATING_POINT();
\r
154 dAnswer = ( d1 / d2 ) * d3;
\r
157 /* The variable this task increments to show it is still running is passed in
\r
158 as the parameter. */
\r
159 pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;
\r
161 /* Keep performing a calculation and checking the result against a constant. */
\r
168 d4 = ( d1 / d2 ) * d3;
\r
170 #if configUSE_PREEMPTION == 0
\r
174 /* If the calculation does not match the expected constant, stop the
\r
175 increment of the check variable. */
\r
176 if( fabs( d4 - dAnswer ) > 0.001 )
\r
181 if( sError == pdFALSE )
\r
183 /* If the calculation has always been correct then set set the check
\r
184 variable. The check variable will get set to pdFALSE each time
\r
185 xAreMathsTaskStillRunning() is executed. */
\r
186 ( *pusTaskCheckVariable ) = pdTRUE;
\r
189 #if configUSE_PREEMPTION == 0
\r
194 /*-----------------------------------------------------------*/
\r
196 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
198 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
199 volatile uint16_t *pusTaskCheckVariable;
\r
200 const size_t xArraySize = 10;
\r
202 short sError = pdFALSE;
\r
204 /* Some ports require that tasks that use a hardware floating point unit
\r
205 tell the kernel that they require a floating point context before any
\r
206 floating point instructions are executed. */
\r
207 portTASK_USES_FLOATING_POINT();
\r
209 /* The variable this task increments to show it is still running is passed in
\r
210 as the parameter. */
\r
211 pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;
\r
213 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
215 /* Keep filling an array, keeping a running total of the values placed in the
\r
216 array. Then run through the array adding up all the values. If the two totals
\r
217 do not match, stop the check variable from incrementing. */
\r
223 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
225 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
226 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
229 #if configUSE_PREEMPTION == 0
\r
233 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
235 dTotal2 += pdArray[ xPosition ];
\r
238 dDifference = dTotal1 - dTotal2;
\r
239 if( fabs( dDifference ) > 0.001 )
\r
244 #if configUSE_PREEMPTION == 0
\r
248 if( sError == pdFALSE )
\r
250 /* If the calculation has always been correct then set set the check
\r
251 variable. The check variable will get set to pdFALSE each time
\r
252 xAreMathsTaskStillRunning() is executed. */
\r
253 ( *pusTaskCheckVariable ) = pdTRUE;
\r
257 /*-----------------------------------------------------------*/
\r
259 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
261 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
262 volatile uint16_t *pusTaskCheckVariable;
\r
263 const size_t xArraySize = 10;
\r
265 short sError = pdFALSE;
\r
267 /* Some ports require that tasks that use a hardware floating point unit
\r
268 tell the kernel that they require a floating point context before any
\r
269 floating point instructions are executed. */
\r
270 portTASK_USES_FLOATING_POINT();
\r
272 /* The variable this task increments to show it is still running is passed in
\r
273 as the parameter. */
\r
274 pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;
\r
276 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
278 /* Keep filling an array, keeping a running total of the values placed in the
\r
279 array. Then run through the array adding up all the values. If the two totals
\r
280 do not match, stop the check variable from incrementing. */
\r
286 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
288 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
289 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
292 #if configUSE_PREEMPTION == 0
\r
296 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
298 dTotal2 += pdArray[ xPosition ];
\r
301 dDifference = dTotal1 - dTotal2;
\r
302 if( fabs( dDifference ) > 0.001 )
\r
307 #if configUSE_PREEMPTION == 0
\r
311 if( sError == pdFALSE )
\r
313 /* If the calculation has always been correct then set set the check
\r
314 variable. The check variable will get set to pdFALSE each time
\r
315 xAreMathsTaskStillRunning() is executed. */
\r
316 ( *pusTaskCheckVariable ) = pdTRUE;
\r
320 /*-----------------------------------------------------------*/
\r
322 /* This is called to check that all the created tasks are still running. */
\r
323 BaseType_t xAreMathsTaskStillRunning( void )
\r
325 BaseType_t xReturn = pdPASS, xTask;
\r
327 /* Check the maths tasks are still running by ensuring their check variables
\r
328 have been set to pdPASS. */
\r
329 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
331 if( usTaskCheck[ xTask ] != pdTRUE )
\r
333 /* The check has not been set so the associated task has either
\r
334 stalled or detected an error. */
\r
339 /* Reset the variable so it can be checked again the next time this
\r
340 function is executed. */
\r
341 usTaskCheck[ xTask ] = pdFALSE;
\r