]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/flop.c
Improve the error detection in some of the standard demo tasks.
[freertos] / FreeRTOS / Demo / Common / Minimal / flop.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * Creates eight tasks, each of which loops continuously performing a floating \r
77  * point calculation.\r
78  *\r
79  * All the tasks run at the idle priority and never block or yield.  This causes\r
80  * all eight tasks to time slice with the idle task.  Running at the idle \r
81  * priority means that these tasks will get pre-empted any time another task is \r
82  * ready to run or a time slice occurs.  More often than not the pre-emption \r
83  * will occur mid calculation, creating a good test of the schedulers context \r
84  * switch mechanism - a calculation producing an unexpected result could be a \r
85  * symptom of a corruption in the context of a task.\r
86  */\r
87 \r
88 #include <stdlib.h>\r
89 #include <math.h>\r
90 \r
91 /* Scheduler include files. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 \r
95 /* Demo program include files. */\r
96 #include "flop.h"\r
97 \r
98 #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
99 #define mathNUMBER_OF_TASKS  ( 4 )\r
100 \r
101 /* Four tasks, each of which performs a different floating point calculation.\r
102 Each of the four is created twice. */\r
103 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
104 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
105 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
106 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
107 \r
108 /* These variables are used to check that all the tasks are still running.  If a\r
109 task gets a calculation wrong it will stop setting its check variable. */\r
110 static volatile unsigned short usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
115 {\r
116         xTaskCreate( vCompetingMathTask1, ( signed char * ) "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
117         xTaskCreate( vCompetingMathTask2, ( signed char * ) "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
118         xTaskCreate( vCompetingMathTask3, ( signed char * ) "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
119         xTaskCreate( vCompetingMathTask4, ( signed char * ) "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
120 }\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
124 {\r
125 volatile portDOUBLE d1, d2, d3, d4;\r
126 volatile unsigned short *pusTaskCheckVariable;\r
127 volatile portDOUBLE dAnswer;\r
128 short sError = pdFALSE;\r
129 \r
130         /* Some ports require that tasks that use a hardware floating point unit\r
131         tell the kernel that they require a floating point context before any\r
132         floating point instructions are executed. */\r
133         portTASK_USES_FLOATING_POINT();\r
134 \r
135         d1 = 123.4567;\r
136         d2 = 2345.6789;\r
137         d3 = -918.222;\r
138 \r
139         dAnswer = ( d1 + d2 ) * d3;\r
140 \r
141         /* The variable this task increments to show it is still running is passed in\r
142         as the parameter. */\r
143         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
144 \r
145         /* Keep performing a calculation and checking the result against a constant. */\r
146         for(;;)\r
147         {\r
148                 d1 = 123.4567;\r
149                 d2 = 2345.6789;\r
150                 d3 = -918.222;\r
151 \r
152                 d4 = ( d1 + d2 ) * d3;\r
153 \r
154                 #if configUSE_PREEMPTION == 0\r
155                         taskYIELD();\r
156                 #endif\r
157 \r
158                 /* If the calculation does not match the expected constant, stop the\r
159                 increment of the check variable. */\r
160                 if( fabs( d4 - dAnswer ) > 0.001 )\r
161                 {\r
162                         sError = pdTRUE;\r
163                 }\r
164 \r
165                 if( sError == pdFALSE )\r
166                 {\r
167                         /* If the calculation has always been correct then set set the check\r
168                         variable.  The check variable will get set to pdFALSE each time\r
169                         xAreMathsTaskStillRunning() is executed. */\r
170                         ( *pusTaskCheckVariable ) = pdTRUE;\r
171                 }\r
172 \r
173                 #if configUSE_PREEMPTION == 0\r
174                         taskYIELD();\r
175                 #endif\r
176 \r
177         }\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
182 {\r
183 volatile portDOUBLE d1, d2, d3, d4;\r
184 volatile unsigned short *pusTaskCheckVariable;\r
185 volatile portDOUBLE dAnswer;\r
186 short sError = pdFALSE;\r
187 \r
188         /* Some ports require that tasks that use a hardware floating point unit\r
189         tell the kernel that they require a floating point context before any\r
190         floating point instructions are executed. */\r
191         portTASK_USES_FLOATING_POINT();\r
192 \r
193         d1 = -389.38;\r
194         d2 = 32498.2;\r
195         d3 = -2.0001;\r
196 \r
197         dAnswer = ( d1 / d2 ) * d3;\r
198 \r
199 \r
200         /* The variable this task increments to show it is still running is passed in\r
201         as the parameter. */\r
202         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
203 \r
204         /* Keep performing a calculation and checking the result against a constant. */\r
205         for( ;; )\r
206         {\r
207                 d1 = -389.38;\r
208                 d2 = 32498.2;\r
209                 d3 = -2.0001;\r
210 \r
211                 d4 = ( d1 / d2 ) * d3;\r
212 \r
213                 #if configUSE_PREEMPTION == 0\r
214                         taskYIELD();\r
215                 #endif\r
216 \r
217                 /* If the calculation does not match the expected constant, stop the\r
218                 increment of the check variable. */\r
219                 if( fabs( d4 - dAnswer ) > 0.001 )\r
220                 {\r
221                         sError = pdTRUE;\r
222                 }\r
223 \r
224                 if( sError == pdFALSE )\r
225                 {\r
226                         /* If the calculation has always been correct then set set the check\r
227                         variable.  The check variable will get set to pdFALSE each time\r
228                         xAreMathsTaskStillRunning() is executed. */\r
229                         ( *pusTaskCheckVariable ) = pdTRUE;\r
230                 }\r
231 \r
232                 #if configUSE_PREEMPTION == 0\r
233                         taskYIELD();\r
234                 #endif\r
235         }\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
240 {\r
241 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
242 volatile unsigned short *pusTaskCheckVariable;\r
243 const size_t xArraySize = 10;\r
244 size_t xPosition;\r
245 short sError = pdFALSE;\r
246 \r
247         /* Some ports require that tasks that use a hardware floating point unit\r
248         tell the kernel that they require a floating point context before any\r
249         floating point instructions are executed. */\r
250         portTASK_USES_FLOATING_POINT();\r
251 \r
252         /* The variable this task increments to show it is still running is passed in\r
253         as the parameter. */\r
254         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
255 \r
256         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
257 \r
258         /* Keep filling an array, keeping a running total of the values placed in the\r
259         array.  Then run through the array adding up all the values.  If the two totals\r
260         do not match, stop the check variable from incrementing. */\r
261         for( ;; )\r
262         {\r
263                 dTotal1 = 0.0;\r
264                 dTotal2 = 0.0;\r
265 \r
266                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
267                 {\r
268                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
269                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;\r
270                 }\r
271 \r
272                 #if configUSE_PREEMPTION == 0\r
273                         taskYIELD();\r
274                 #endif\r
275 \r
276                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
277                 {\r
278                         dTotal2 += pdArray[ xPosition ];\r
279                 }\r
280 \r
281                 dDifference = dTotal1 - dTotal2;\r
282                 if( fabs( dDifference ) > 0.001 )\r
283                 {\r
284                         sError = pdTRUE;\r
285                 }\r
286 \r
287                 #if configUSE_PREEMPTION == 0\r
288                         taskYIELD();\r
289                 #endif\r
290 \r
291                 if( sError == pdFALSE )\r
292                 {\r
293                         /* If the calculation has always been correct then set set the check\r
294                         variable.  The check variable will get set to pdFALSE each time\r
295                         xAreMathsTaskStillRunning() is executed. */\r
296                         ( *pusTaskCheckVariable ) = pdTRUE;\r
297                 }\r
298         }\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
303 {\r
304 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
305 volatile unsigned short *pusTaskCheckVariable;\r
306 const size_t xArraySize = 10;\r
307 size_t xPosition;\r
308 short sError = pdFALSE;\r
309 \r
310         /* Some ports require that tasks that use a hardware floating point unit\r
311         tell the kernel that they require a floating point context before any\r
312         floating point instructions are executed. */\r
313         portTASK_USES_FLOATING_POINT();\r
314 \r
315         /* The variable this task increments to show it is still running is passed in\r
316         as the parameter. */\r
317         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
318 \r
319         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
320 \r
321         /* Keep filling an array, keeping a running total of the values placed in the\r
322         array.  Then run through the array adding up all the values.  If the two totals\r
323         do not match, stop the check variable from incrementing. */\r
324         for( ;; )\r
325         {\r
326                 dTotal1 = 0.0;\r
327                 dTotal2 = 0.0;\r
328 \r
329                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
330                 {\r
331                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
332                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;\r
333                 }\r
334 \r
335                 #if configUSE_PREEMPTION == 0\r
336                         taskYIELD();\r
337                 #endif\r
338 \r
339                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
340                 {\r
341                         dTotal2 += pdArray[ xPosition ];\r
342                 }\r
343 \r
344                 dDifference = dTotal1 - dTotal2;\r
345                 if( fabs( dDifference ) > 0.001 )\r
346                 {\r
347                         sError = pdTRUE;\r
348                 }\r
349 \r
350                 #if configUSE_PREEMPTION == 0\r
351                         taskYIELD();\r
352                 #endif\r
353 \r
354                 if( sError == pdFALSE )\r
355                 {\r
356                         /* If the calculation has always been correct then set set the check\r
357                         variable.  The check variable will get set to pdFALSE each time\r
358                         xAreMathsTaskStillRunning() is executed. */\r
359                         ( *pusTaskCheckVariable ) = pdTRUE;\r
360                 }\r
361         }\r
362 }\r
363 /*-----------------------------------------------------------*/\r
364 \r
365 /* This is called to check that all the created tasks are still running. */\r
366 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
367 {\r
368 portBASE_TYPE xReturn = pdPASS, xTask;\r
369 \r
370         /* Check the maths tasks are still running by ensuring their check variables\r
371         have been set to pdPASS. */\r
372         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
373         {\r
374                 if( usTaskCheck[ xTask ] != pdTRUE )\r
375                 {\r
376                         /* The check has not been set so the associated task has either\r
377                         stalled or detected an error. */\r
378                         xReturn = pdFAIL;\r
379                 }\r
380                 else\r
381                 {\r
382                         /* Reset the variable so it can be checked again the next time this\r
383                         function is executed. */\r
384                         usTaskCheck[ xTask ] = pdFALSE;\r
385                 }\r
386         }\r
387                 \r
388         return xReturn;\r
389 }\r
390 \r
391 \r
392 \r