]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4_RM48_TMS570_CCS5/flop_hercules.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_R4_RM48_TMS570_CCS5 / flop_hercules.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * Creates eight tasks, each of which loops continuously performing an (emulated)\r
30  * floating point calculation.\r
31  *\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 priority\r
34  * means that these tasks will get pre-empted any time another task is ready to run\r
35  * or a time slice occurs.  More often than not the pre-emption will occur mid\r
36  * calculation, creating a good test of the schedulers context switch mechanism - a\r
37  * calculation producing an unexpected result could be a symptom of a corruption in\r
38  * the context of a task.\r
39  */\r
40 \r
41 #include <stdlib.h>\r
42 #include <math.h>\r
43 \r
44 /* Scheduler include files. */\r
45 #include "FreeRTOS.h"\r
46 #include "task.h"\r
47 \r
48 /* Demo program include files. */\r
49 #include "flop.h"\r
50 \r
51 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
52 #define mathNUMBER_OF_TASKS  ( 8 )\r
53 \r
54 /* Four tasks, each of which performs a different floating point calculation.\r
55 Each of the four is created twice. */\r
56 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
57 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
58 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
59 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
60 \r
61 /* These variables are used to check that all the tasks are still running.  If a\r
62 task gets a calculation wrong it will\r
63 stop incrementing its check variable. */\r
64 static volatile unsigned long ulTaskCheck[ mathNUMBER_OF_TASKS ] = { 0 };\r
65 \r
66 /* Must be called before any hardware floating point operations are\r
67 performed to let the RTOS portable layer know that this task requires\r
68 a floating point context. */\r
69 #if __TI_VFP_SUPPORT__\r
70         extern void vPortTaskUsesFPU( void );\r
71 #endif\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
76 {\r
77         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );\r
78         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );\r
79         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );\r
80         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );\r
81         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );\r
82         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );\r
83         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );\r
84         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), uxPriority, NULL );\r
85 }\r
86 /*-----------------------------------------------------------*/\r
87 \r
88 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
89 {\r
90 volatile portDOUBLE d1, d2, d3, d4;\r
91 volatile unsigned long *pulTaskCheckVariable;\r
92 volatile portDOUBLE dAnswer;\r
93 short sError = pdFALSE;\r
94 \r
95 \r
96         /* Must be called before any hardware floating point operations are\r
97         performed to let the RTOS portable layer know that this task requires\r
98         a floating point context. */\r
99         #if __TI_VFP_SUPPORT__\r
100                 vPortTaskUsesFPU();\r
101         #endif\r
102 \r
103         d1 = 123.4567;\r
104         d2 = 2345.6789;\r
105         d3 = -918.222;\r
106 \r
107         dAnswer = ( d1 + d2 ) * d3;\r
108 \r
109         /* The variable this task increments to show it is still running is passed in\r
110         as the parameter. */\r
111         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
112 \r
113         /* Keep performing a calculation and checking the result against a constant. */\r
114         for(;;)\r
115         {\r
116                 d1 = 123.4567;\r
117                 d2 = 2345.6789;\r
118                 d3 = -918.222;\r
119 \r
120                 d4 = ( d1 + d2 ) * d3;\r
121 \r
122                 #if configUSE_PREEMPTION == 0\r
123                         taskYIELD();\r
124                 #endif\r
125 \r
126                 /* If the calculation does not match the expected constant, stop the\r
127                 increment of the check variable. */\r
128                 if( fabs( d4 - dAnswer ) > 0.001 )\r
129                 {\r
130                         sError = pdTRUE;\r
131                 }\r
132 \r
133                 if( sError == pdFALSE )\r
134                 {\r
135                         /* If the calculation has always been correct, increment the check\r
136                         variable so we know this task is still running okay. */\r
137                         ( *pulTaskCheckVariable )++;\r
138                 }\r
139 \r
140                 #if configUSE_PREEMPTION == 0\r
141                         taskYIELD();\r
142                 #endif\r
143 \r
144         }\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
149 {\r
150 volatile portDOUBLE d1, d2, d3, d4;\r
151 volatile unsigned long *pulTaskCheckVariable;\r
152 volatile portDOUBLE dAnswer;\r
153 short sError = pdFALSE;\r
154 \r
155         /* Must be called before any hardware floating point operations are\r
156         performed to let the RTOS portable layer know that this task requires\r
157         a floating point context. */\r
158         #if __TI_VFP_SUPPORT__\r
159                 vPortTaskUsesFPU();\r
160         #endif\r
161 \r
162         d1 = -389.38;\r
163         d2 = 32498.2;\r
164         d3 = -2.0001;\r
165 \r
166         dAnswer = ( d1 / d2 ) * d3;\r
167 \r
168 \r
169         /* The variable this task increments to show it is still running is passed in\r
170         as the parameter. */\r
171         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
172 \r
173         /* Keep performing a calculation and checking the result against a constant. */\r
174         for( ;; )\r
175         {\r
176                 d1 = -389.38;\r
177                 d2 = 32498.2;\r
178                 d3 = -2.0001;\r
179 \r
180                 d4 = ( d1 / d2 ) * d3;\r
181 \r
182                 #if configUSE_PREEMPTION == 0\r
183                         taskYIELD();\r
184                 #endif\r
185 \r
186                 /* If the calculation does not match the expected constant, stop the\r
187                 increment of the check variable. */\r
188                 if( fabs( d4 - dAnswer ) > 0.001 )\r
189                 {\r
190                         sError = pdTRUE;\r
191                 }\r
192 \r
193                 if( sError == pdFALSE )\r
194                 {\r
195                         /* If the calculation has always been correct, increment the check\r
196                         variable so we know\r
197                         this task is still running okay. */\r
198                         ( *pulTaskCheckVariable )++;\r
199                 }\r
200 \r
201                 #if configUSE_PREEMPTION == 0\r
202                         taskYIELD();\r
203                 #endif\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
209 {\r
210 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
211 volatile unsigned long *pulTaskCheckVariable;\r
212 const size_t xArraySize = 10;\r
213 size_t xPosition;\r
214 short sError = pdFALSE;\r
215 \r
216         /* Must be called before any hardware floating point operations are\r
217         performed to let the RTOS portable layer know that this task requires\r
218         a floating point context. */\r
219         #if __TI_VFP_SUPPORT__\r
220                 vPortTaskUsesFPU();\r
221         #endif\r
222 \r
223         /* The variable this task increments to show it is still running is passed in\r
224         as the parameter. */\r
225         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
226 \r
227         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
228 \r
229         /* Keep filling an array, keeping a running total of the values placed in the\r
230         array.  Then run through the array adding up all the values.  If the two totals\r
231         do not match, stop the check variable from incrementing. */\r
232         for( ;; )\r
233         {\r
234                 dTotal1 = 0.0;\r
235                 dTotal2 = 0.0;\r
236 \r
237                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
238                 {\r
239                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
240                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;\r
241                 }\r
242 \r
243                 #if configUSE_PREEMPTION == 0\r
244                         taskYIELD();\r
245                 #endif\r
246 \r
247                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
248                 {\r
249                         dTotal2 += pdArray[ xPosition ];\r
250                 }\r
251 \r
252                 dDifference = dTotal1 - dTotal2;\r
253                 if( fabs( dDifference ) > 0.001 )\r
254                 {\r
255                         sError = pdTRUE;\r
256                 }\r
257 \r
258                 #if configUSE_PREEMPTION == 0\r
259                         taskYIELD();\r
260                 #endif\r
261 \r
262                 if( sError == pdFALSE )\r
263                 {\r
264                         /* If the calculation has always been correct, increment the check\r
265                         variable so we know     this task is still running okay. */\r
266                         ( *pulTaskCheckVariable )++;\r
267                 }\r
268         }\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
273 {\r
274 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
275 volatile unsigned long *pulTaskCheckVariable;\r
276 const size_t xArraySize = 10;\r
277 size_t xPosition;\r
278 short sError = pdFALSE;\r
279 \r
280         /* Must be called before any hardware floating point operations are\r
281         performed to let the RTOS portable layer know that this task requires\r
282         a floating point context. */\r
283         #if __TI_VFP_SUPPORT__\r
284                 vPortTaskUsesFPU();\r
285         #endif\r
286 \r
287         /* The variable this task increments to show it is still running is passed in\r
288         as the parameter. */\r
289         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
290 \r
291         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
292 \r
293         /* Keep filling an array, keeping a running total of the values placed in the\r
294         array.  Then run through the array adding up all the values.  If the two totals\r
295         do not match, stop the check variable from incrementing. */\r
296         for( ;; )\r
297         {\r
298                 dTotal1 = 0.0;\r
299                 dTotal2 = 0.0;\r
300 \r
301                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
302                 {\r
303                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
304                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;\r
305                 }\r
306 \r
307                 #if configUSE_PREEMPTION == 0\r
308                         taskYIELD();\r
309                 #endif\r
310 \r
311                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
312                 {\r
313                         dTotal2 += pdArray[ xPosition ];\r
314                 }\r
315 \r
316                 dDifference = dTotal1 - dTotal2;\r
317                 if( fabs( dDifference ) > 0.001 )\r
318                 {\r
319                         sError = pdTRUE;\r
320                 }\r
321 \r
322                 #if configUSE_PREEMPTION == 0\r
323                         taskYIELD();\r
324                 #endif\r
325 \r
326                 if( sError == pdFALSE )\r
327                 {\r
328                         /* If the calculation has always been correct, increment the check\r
329                         variable so we know     this task is still running okay. */\r
330                         ( *pulTaskCheckVariable )++;\r
331                 }\r
332         }\r
333 }\r
334 /*-----------------------------------------------------------*/\r
335 \r
336 /* This is called to check that all the created tasks are still running. */\r
337 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
338 {\r
339 /* Keep a history of the check variables so we know if they have been incremented\r
340 since the last call. */\r
341 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
342 portBASE_TYPE xReturn = pdTRUE, xTask;\r
343 \r
344         /* Check the maths tasks are still running by ensuring their check variables\r
345         are still incrementing. */\r
346         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
347         {\r
348                 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )\r
349                 {\r
350                         /* The check has not incremented so an error exists. */\r
351                         xReturn = pdFALSE;\r
352                 }\r
353 \r
354                 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];\r
355         }\r
356 \r
357         return xReturn;\r
358 }\r
359 \r
360 \r
361 \r