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 an (emulated)
\r
31 * floating 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 priority
\r
35 * means that these tasks will get pre-empted any time another task is ready to run
\r
36 * or a time slice occurs. More often than not the pre-emption will occur mid
\r
37 * calculation, creating a good test of the schedulers context switch mechanism - a
\r
38 * calculation producing an unexpected result could be a symptom of a corruption in
\r
39 * the context of a task.
\r
45 /* Scheduler include files. */
\r
46 #include "FreeRTOS.h"
\r
49 /* Demo program include files. */
\r
52 #define mathSTACK_SIZE configMINIMAL_STACK_SIZE
\r
53 #define mathNUMBER_OF_TASKS ( 8 )
\r
55 /* Four tasks, each of which performs a different floating point calculation.
\r
56 Each of the four is created twice. */
\r
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );
\r
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );
\r
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );
\r
60 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );
\r
62 /* These variables are used to check that all the tasks are still running. If a
\r
63 task gets a calculation wrong it will
\r
64 stop incrementing its check variable. */
\r
65 static volatile unsigned long ulTaskCheck[ mathNUMBER_OF_TASKS ] = { 0 };
\r
67 /* Must be called before any hardware floating point operations are
\r
68 performed to let the RTOS portable layer know that this task requires
\r
69 a floating point context. */
\r
70 #if __TI_VFP_SUPPORT__
\r
71 extern void vPortTaskUsesFPU( void );
\r
74 /*-----------------------------------------------------------*/
\r
76 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )
\r
78 xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );
\r
79 xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );
\r
80 xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );
\r
81 xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );
\r
82 xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );
\r
83 xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );
\r
84 xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );
\r
85 xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), uxPriority, NULL );
\r
87 /*-----------------------------------------------------------*/
\r
89 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )
\r
91 volatile portDOUBLE d1, d2, d3, d4;
\r
92 volatile unsigned long *pulTaskCheckVariable;
\r
93 volatile portDOUBLE dAnswer;
\r
94 short sError = pdFALSE;
\r
97 /* Must be called before any hardware floating point operations are
\r
98 performed to let the RTOS portable layer know that this task requires
\r
99 a floating point context. */
\r
100 #if __TI_VFP_SUPPORT__
\r
101 vPortTaskUsesFPU();
\r
108 dAnswer = ( d1 + d2 ) * d3;
\r
110 /* The variable this task increments to show it is still running is passed in
\r
111 as the parameter. */
\r
112 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
114 /* Keep performing a calculation and checking the result against a constant. */
\r
121 d4 = ( d1 + d2 ) * d3;
\r
123 #if configUSE_PREEMPTION == 0
\r
127 /* If the calculation does not match the expected constant, stop the
\r
128 increment of the check variable. */
\r
129 if( fabs( d4 - dAnswer ) > 0.001 )
\r
134 if( sError == pdFALSE )
\r
136 /* If the calculation has always been correct, increment the check
\r
137 variable so we know this task is still running okay. */
\r
138 ( *pulTaskCheckVariable )++;
\r
141 #if configUSE_PREEMPTION == 0
\r
147 /*-----------------------------------------------------------*/
\r
149 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )
\r
151 volatile portDOUBLE d1, d2, d3, d4;
\r
152 volatile unsigned long *pulTaskCheckVariable;
\r
153 volatile portDOUBLE dAnswer;
\r
154 short sError = pdFALSE;
\r
156 /* Must be called before any hardware floating point operations are
\r
157 performed to let the RTOS portable layer know that this task requires
\r
158 a floating point context. */
\r
159 #if __TI_VFP_SUPPORT__
\r
160 vPortTaskUsesFPU();
\r
167 dAnswer = ( d1 / d2 ) * d3;
\r
170 /* The variable this task increments to show it is still running is passed in
\r
171 as the parameter. */
\r
172 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
174 /* Keep performing a calculation and checking the result against a constant. */
\r
181 d4 = ( d1 / d2 ) * d3;
\r
183 #if configUSE_PREEMPTION == 0
\r
187 /* If the calculation does not match the expected constant, stop the
\r
188 increment of the check variable. */
\r
189 if( fabs( d4 - dAnswer ) > 0.001 )
\r
194 if( sError == pdFALSE )
\r
196 /* If the calculation has always been correct, increment the check
\r
197 variable so we know
\r
198 this task is still running okay. */
\r
199 ( *pulTaskCheckVariable )++;
\r
202 #if configUSE_PREEMPTION == 0
\r
207 /*-----------------------------------------------------------*/
\r
209 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )
\r
211 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
212 volatile unsigned long *pulTaskCheckVariable;
\r
213 const size_t xArraySize = 10;
\r
215 short sError = pdFALSE;
\r
217 /* Must be called before any hardware floating point operations are
\r
218 performed to let the RTOS portable layer know that this task requires
\r
219 a floating point context. */
\r
220 #if __TI_VFP_SUPPORT__
\r
221 vPortTaskUsesFPU();
\r
224 /* The variable this task increments to show it is still running is passed in
\r
225 as the parameter. */
\r
226 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
228 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
230 /* Keep filling an array, keeping a running total of the values placed in the
\r
231 array. Then run through the array adding up all the values. If the two totals
\r
232 do not match, stop the check variable from incrementing. */
\r
238 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
240 pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;
\r
241 dTotal1 += ( portDOUBLE ) xPosition + 5.5;
\r
244 #if configUSE_PREEMPTION == 0
\r
248 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
250 dTotal2 += pdArray[ xPosition ];
\r
253 dDifference = dTotal1 - dTotal2;
\r
254 if( fabs( dDifference ) > 0.001 )
\r
259 #if configUSE_PREEMPTION == 0
\r
263 if( sError == pdFALSE )
\r
265 /* If the calculation has always been correct, increment the check
\r
266 variable so we know this task is still running okay. */
\r
267 ( *pulTaskCheckVariable )++;
\r
271 /*-----------------------------------------------------------*/
\r
273 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )
\r
275 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;
\r
276 volatile unsigned long *pulTaskCheckVariable;
\r
277 const size_t xArraySize = 10;
\r
279 short sError = pdFALSE;
\r
281 /* Must be called before any hardware floating point operations are
\r
282 performed to let the RTOS portable layer know that this task requires
\r
283 a floating point context. */
\r
284 #if __TI_VFP_SUPPORT__
\r
285 vPortTaskUsesFPU();
\r
288 /* The variable this task increments to show it is still running is passed in
\r
289 as the parameter. */
\r
290 pulTaskCheckVariable = ( unsigned long * ) pvParameters;
\r
292 pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );
\r
294 /* Keep filling an array, keeping a running total of the values placed in the
\r
295 array. Then run through the array adding up all the values. If the two totals
\r
296 do not match, stop the check variable from incrementing. */
\r
302 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
304 pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;
\r
305 dTotal1 += ( portDOUBLE ) xPosition * 12.123;
\r
308 #if configUSE_PREEMPTION == 0
\r
312 for( xPosition = 0; xPosition < xArraySize; xPosition++ )
\r
314 dTotal2 += pdArray[ xPosition ];
\r
317 dDifference = dTotal1 - dTotal2;
\r
318 if( fabs( dDifference ) > 0.001 )
\r
323 #if configUSE_PREEMPTION == 0
\r
327 if( sError == pdFALSE )
\r
329 /* If the calculation has always been correct, increment the check
\r
330 variable so we know this task is still running okay. */
\r
331 ( *pulTaskCheckVariable )++;
\r
335 /*-----------------------------------------------------------*/
\r
337 /* This is called to check that all the created tasks are still running. */
\r
338 portBASE_TYPE xAreMathsTaskStillRunning( void )
\r
340 /* Keep a history of the check variables so we know if they have been incremented
\r
341 since the last call. */
\r
342 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };
\r
343 portBASE_TYPE xReturn = pdTRUE, xTask;
\r
345 /* Check the maths tasks are still running by ensuring their check variables
\r
346 are still incrementing. */
\r
347 for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )
\r
349 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )
\r
351 /* The check has not incremented so an error exists. */
\r
355 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];
\r