2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Creates eight tasks, each of which loops continuously performing a floating
\r
31 * point calculation.
\r
33 * All the tasks run at the idle priority and never block or yield. This causes
\r
34 * all eight tasks to time slice with the idle task. Running at the idle
\r
35 * priority means that these tasks will get pre-empted any time another task is
\r
36 * ready to run or a time slice occurs. More often than not the pre-emption
\r
37 * will occur mid calculation, creating a good test of the schedulers context
\r
38 * switch mechanism - a calculation producing an unexpected result could be a
\r
39 * symptom of a corruption in the context of a task.
\r
42 /* Standard includes. */
\r
46 /* Scheduler include files. */
\r
47 #include "FreeRTOS.h"
\r
50 /* Demo program include files. */
\r
53 #ifndef mathSTACK_SIZE
\r
54 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
57 #define mathNUMBER_OF_TASKS ( 4 )
\r
59 /* Four tasks, each of which performs a different floating point calculation.
\r
60 Each of the four is created twice. */
\r
61 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
62 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
63 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
64 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
66 /* These variables are used to check that all the tasks are still running. If a
\r
67 task gets a calculation wrong it will stop setting its check variable. */
\r
68 static uint16_t usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };
\r
70 /*-----------------------------------------------------------*/
\r
72 void vStartMathTasks( UBaseType_t uxPriority )
\r
74 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );
\r
75 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );
\r
76 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );
\r
77 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );
\r
79 /*-----------------------------------------------------------*/
\r
81 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
83 volatile portDOUBLE d1, d2, d3, d4;
\r
84 volatile uint16_t *pusTaskCheckVariable;
\r
85 volatile portDOUBLE dAnswer;
\r
86 short sError = pdFALSE;
\r
88 /* Some ports require that tasks that use a hardware floating point unit
\r
89 tell the kernel that they require a floating point context before any
\r
90 floating point instructions are executed. */
\r
91 portTASK_USES_FLOATING_POINT();
\r
97 dAnswer = ( d1 + d2 ) * d3;
\r
99 /* The variable this task increments to show it is still running is passed in
\r
100 as the parameter. */
\r
101 pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;
\r
103 /* Keep performing a calculation and checking the result against a constant. */
\r
110 d4 = ( d1 + d2 ) * d3;
\r
112 #if configUSE_PREEMPTION == 0
\r
116 /* If the calculation does not match the expected constant, stop the
\r
117 increment of the check variable. */
\r
118 if( fabs( d4 - dAnswer ) > 0.001 )
\r
123 if( sError == pdFALSE )
\r
125 /* If the calculation has always been correct then set set the check
\r
126 variable. The check variable will get set to pdFALSE each time
\r
127 xAreMathsTaskStillRunning() is executed. */
\r
128 ( *pusTaskCheckVariable ) = pdTRUE;
\r
131 #if configUSE_PREEMPTION == 0
\r
137 /*-----------------------------------------------------------*/
\r
139 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
141 volatile portDOUBLE d1, d2, d3, d4;
\r
142 volatile uint16_t *pusTaskCheckVariable;
\r
143 volatile portDOUBLE dAnswer;
\r
144 short sError = pdFALSE;
\r
146 /* Some ports require that tasks that use a hardware floating point unit
\r
147 tell the kernel that they require a floating point context before any
\r
148 floating point instructions are executed. */
\r
149 portTASK_USES_FLOATING_POINT();
\r
155 dAnswer = ( d1 / d2 ) * d3;
\r
158 /* The variable this task increments to show it is still running is passed in
\r
159 as the parameter. */
\r
160 pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;
\r
162 /* Keep performing a calculation and checking the result against a constant. */
\r
169 d4 = ( d1 / d2 ) * d3;
\r
171 #if configUSE_PREEMPTION == 0
\r
175 /* If the calculation does not match the expected constant, stop the
\r
176 increment of the check variable. */
\r
177 if( fabs( d4 - dAnswer ) > 0.001 )
\r
182 if( sError == pdFALSE )
\r
184 /* If the calculation has always been correct then set set the check
\r
185 variable. The check variable will get set to pdFALSE each time
\r
186 xAreMathsTaskStillRunning() is executed. */
\r
187 ( *pusTaskCheckVariable ) = pdTRUE;
\r
190 #if configUSE_PREEMPTION == 0
\r
195 /*-----------------------------------------------------------*/
\r
197 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
199 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
200 volatile uint16_t *pusTaskCheckVariable;
\r
201 const size_t xArraySize = 10;
\r
203 short sError = pdFALSE;
\r
205 /* Some ports require that tasks that use a hardware floating point unit
\r
206 tell the kernel that they require a floating point context before any
\r
207 floating point instructions are executed. */
\r
208 portTASK_USES_FLOATING_POINT();
\r
210 /* The variable this task increments to show it is still running is passed in
\r
211 as the parameter. */
\r
212 pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;
\r
214 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
216 /* Keep filling an array, keeping a running total of the values placed in the
\r
217 array. Then run through the array adding up all the values. If the two totals
\r
218 do not match, stop the check variable from incrementing. */
\r
224 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
226 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
227 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
230 #if configUSE_PREEMPTION == 0
\r
234 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
236 dTotal2 += pdArray[ xPosition ];
\r
239 dDifference = dTotal1 - dTotal2;
\r
240 if( fabs( dDifference ) > 0.001 )
\r
245 #if configUSE_PREEMPTION == 0
\r
249 if( sError == pdFALSE )
\r
251 /* If the calculation has always been correct then set set the check
\r
252 variable. The check variable will get set to pdFALSE each time
\r
253 xAreMathsTaskStillRunning() is executed. */
\r
254 ( *pusTaskCheckVariable ) = pdTRUE;
\r
258 /*-----------------------------------------------------------*/
\r
260 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
262 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
263 volatile uint16_t *pusTaskCheckVariable;
\r
264 const size_t xArraySize = 10;
\r
266 short sError = pdFALSE;
\r
268 /* Some ports require that tasks that use a hardware floating point unit
\r
269 tell the kernel that they require a floating point context before any
\r
270 floating point instructions are executed. */
\r
271 portTASK_USES_FLOATING_POINT();
\r
273 /* The variable this task increments to show it is still running is passed in
\r
274 as the parameter. */
\r
275 pusTaskCheckVariable = ( volatile uint16_t * ) pvParameters;
\r
277 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
279 /* Keep filling an array, keeping a running total of the values placed in the
\r
280 array. Then run through the array adding up all the values. If the two totals
\r
281 do not match, stop the check variable from incrementing. */
\r
287 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
289 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
290 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
293 #if configUSE_PREEMPTION == 0
\r
297 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
299 dTotal2 += pdArray[ xPosition ];
\r
302 dDifference = dTotal1 - dTotal2;
\r
303 if( fabs( dDifference ) > 0.001 )
\r
308 #if configUSE_PREEMPTION == 0
\r
312 if( sError == pdFALSE )
\r
314 /* If the calculation has always been correct then set set the check
\r
315 variable. The check variable will get set to pdFALSE each time
\r
316 xAreMathsTaskStillRunning() is executed. */
\r
317 ( *pusTaskCheckVariable ) = pdTRUE;
\r
321 /*-----------------------------------------------------------*/
\r
323 /* This is called to check that all the created tasks are still running. */
\r
324 BaseType_t xAreMathsTaskStillRunning( void )
\r
326 BaseType_t xReturn = pdPASS, xTask;
\r
328 /* Check the maths tasks are still running by ensuring their check variables
\r
329 have been set to pdPASS. */
\r
330 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
332 if( usTaskCheck[ xTask ] != pdTRUE )
\r
334 /* The check has not been set so the associated task has either
\r
335 stalled or detected an error. */
\r
340 /* Reset the variable so it can be checked again the next time this
\r
341 function is executed. */
\r
342 usTaskCheck[ xTask ] = pdFALSE;
\r