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