]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4_RM48_TMS570_CCS5/flop_hercules.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_R4_RM48_TMS570_CCS5 / flop_hercules.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. 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
15  *\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
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * Creates eight tasks, each of which loops continuously performing an (emulated)\r
31  * floating point calculation.\r
32  *\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
40  */\r
41 \r
42 #include <stdlib.h>\r
43 #include <math.h>\r
44 \r
45 /* Scheduler include files. */\r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 \r
49 /* Demo program include files. */\r
50 #include "flop.h"\r
51 \r
52 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
53 #define mathNUMBER_OF_TASKS  ( 8 )\r
54 \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
61 \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
66 \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
72 #endif\r
73 \r
74 /*-----------------------------------------------------------*/\r
75 \r
76 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
77 {\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
86 }\r
87 /*-----------------------------------------------------------*/\r
88 \r
89 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
90 {\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
95 \r
96 \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
102         #endif\r
103 \r
104         d1 = 123.4567;\r
105         d2 = 2345.6789;\r
106         d3 = -918.222;\r
107 \r
108         dAnswer = ( d1 + d2 ) * d3;\r
109 \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
113 \r
114         /* Keep performing a calculation and checking the result against a constant. */\r
115         for(;;)\r
116         {\r
117                 d1 = 123.4567;\r
118                 d2 = 2345.6789;\r
119                 d3 = -918.222;\r
120 \r
121                 d4 = ( d1 + d2 ) * d3;\r
122 \r
123                 #if configUSE_PREEMPTION == 0\r
124                         taskYIELD();\r
125                 #endif\r
126 \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
130                 {\r
131                         sError = pdTRUE;\r
132                 }\r
133 \r
134                 if( sError == pdFALSE )\r
135                 {\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
139                 }\r
140 \r
141                 #if configUSE_PREEMPTION == 0\r
142                         taskYIELD();\r
143                 #endif\r
144 \r
145         }\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
150 {\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
155 \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
161         #endif\r
162 \r
163         d1 = -389.38;\r
164         d2 = 32498.2;\r
165         d3 = -2.0001;\r
166 \r
167         dAnswer = ( d1 / d2 ) * d3;\r
168 \r
169 \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
173 \r
174         /* Keep performing a calculation and checking the result against a constant. */\r
175         for( ;; )\r
176         {\r
177                 d1 = -389.38;\r
178                 d2 = 32498.2;\r
179                 d3 = -2.0001;\r
180 \r
181                 d4 = ( d1 / d2 ) * d3;\r
182 \r
183                 #if configUSE_PREEMPTION == 0\r
184                         taskYIELD();\r
185                 #endif\r
186 \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
190                 {\r
191                         sError = pdTRUE;\r
192                 }\r
193 \r
194                 if( sError == pdFALSE )\r
195                 {\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
200                 }\r
201 \r
202                 #if configUSE_PREEMPTION == 0\r
203                         taskYIELD();\r
204                 #endif\r
205         }\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
210 {\r
211 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
212 volatile unsigned long *pulTaskCheckVariable;\r
213 const size_t xArraySize = 10;\r
214 size_t xPosition;\r
215 short sError = pdFALSE;\r
216 \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
222         #endif\r
223 \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
227 \r
228         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
229 \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
233         for( ;; )\r
234         {\r
235                 dTotal1 = 0.0;\r
236                 dTotal2 = 0.0;\r
237 \r
238                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
239                 {\r
240                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
241                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;\r
242                 }\r
243 \r
244                 #if configUSE_PREEMPTION == 0\r
245                         taskYIELD();\r
246                 #endif\r
247 \r
248                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
249                 {\r
250                         dTotal2 += pdArray[ xPosition ];\r
251                 }\r
252 \r
253                 dDifference = dTotal1 - dTotal2;\r
254                 if( fabs( dDifference ) > 0.001 )\r
255                 {\r
256                         sError = pdTRUE;\r
257                 }\r
258 \r
259                 #if configUSE_PREEMPTION == 0\r
260                         taskYIELD();\r
261                 #endif\r
262 \r
263                 if( sError == pdFALSE )\r
264                 {\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
268                 }\r
269         }\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
274 {\r
275 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
276 volatile unsigned long *pulTaskCheckVariable;\r
277 const size_t xArraySize = 10;\r
278 size_t xPosition;\r
279 short sError = pdFALSE;\r
280 \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
286         #endif\r
287 \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
291 \r
292         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
293 \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
297         for( ;; )\r
298         {\r
299                 dTotal1 = 0.0;\r
300                 dTotal2 = 0.0;\r
301 \r
302                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
303                 {\r
304                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
305                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;\r
306                 }\r
307 \r
308                 #if configUSE_PREEMPTION == 0\r
309                         taskYIELD();\r
310                 #endif\r
311 \r
312                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
313                 {\r
314                         dTotal2 += pdArray[ xPosition ];\r
315                 }\r
316 \r
317                 dDifference = dTotal1 - dTotal2;\r
318                 if( fabs( dDifference ) > 0.001 )\r
319                 {\r
320                         sError = pdTRUE;\r
321                 }\r
322 \r
323                 #if configUSE_PREEMPTION == 0\r
324                         taskYIELD();\r
325                 #endif\r
326 \r
327                 if( sError == pdFALSE )\r
328                 {\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
332                 }\r
333         }\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 /* This is called to check that all the created tasks are still running. */\r
338 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
339 {\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
344 \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
348         {\r
349                 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )\r
350                 {\r
351                         /* The check has not incremented so an error exists. */\r
352                         xReturn = pdFALSE;\r
353                 }\r
354 \r
355                 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];\r
356         }\r
357 \r
358         return xReturn;\r
359 }\r
360 \r
361 \r
362 \r