]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/flop.c
Update version numbers in preparation for new release.
[freertos] / FreeRTOS / Demo / Common / Minimal / flop.c
1 /*\r
2     FreeRTOS V8.2.2 - 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 #ifndef mathSTACK_SIZE\r
94         #define mathSTACK_SIZE          configMINIMAL_STACK_SIZE\r
95 #endif\r
96 \r
97 #define mathNUMBER_OF_TASKS  ( 4 )\r
98 \r
99 /* Four tasks, each of which performs a different floating point calculation.\r
100 Each of the four is created twice. */\r
101 static portTASK_FUNCTION_PROTO( vCompetingMathTask1, pvParameters );\r
102 static portTASK_FUNCTION_PROTO( vCompetingMathTask2, pvParameters );\r
103 static portTASK_FUNCTION_PROTO( vCompetingMathTask3, pvParameters );\r
104 static portTASK_FUNCTION_PROTO( vCompetingMathTask4, pvParameters );\r
105 \r
106 /* These variables are used to check that all the tasks are still running.  If a\r
107 task gets a calculation wrong it will stop setting its check variable. */\r
108 static volatile uint16_t usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 void vStartMathTasks( UBaseType_t uxPriority )\r
113 {\r
114         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
115         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
116         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
117         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
118 }\r
119 /*-----------------------------------------------------------*/\r
120 \r
121 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
122 {\r
123 volatile portDOUBLE d1, d2, d3, d4;\r
124 volatile uint16_t *pusTaskCheckVariable;\r
125 volatile portDOUBLE dAnswer;\r
126 short sError = pdFALSE;\r
127 \r
128         /* Some ports require that tasks that use a hardware floating point unit\r
129         tell the kernel that they require a floating point context before any\r
130         floating point instructions are executed. */\r
131         portTASK_USES_FLOATING_POINT();\r
132 \r
133         d1 = 123.4567;\r
134         d2 = 2345.6789;\r
135         d3 = -918.222;\r
136 \r
137         dAnswer = ( d1 + d2 ) * d3;\r
138 \r
139         /* The variable this task increments to show it is still running is passed in\r
140         as the parameter. */\r
141         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
142 \r
143         /* Keep performing a calculation and checking the result against a constant. */\r
144         for(;;)\r
145         {\r
146                 d1 = 123.4567;\r
147                 d2 = 2345.6789;\r
148                 d3 = -918.222;\r
149 \r
150                 d4 = ( d1 + d2 ) * d3;\r
151 \r
152                 #if configUSE_PREEMPTION == 0\r
153                         taskYIELD();\r
154                 #endif\r
155 \r
156                 /* If the calculation does not match the expected constant, stop the\r
157                 increment of the check variable. */\r
158                 if( fabs( d4 - dAnswer ) > 0.001 )\r
159                 {\r
160                         sError = pdTRUE;\r
161                 }\r
162 \r
163                 if( sError == pdFALSE )\r
164                 {\r
165                         /* If the calculation has always been correct then set set the check\r
166                         variable.  The check variable will get set to pdFALSE each time\r
167                         xAreMathsTaskStillRunning() is executed. */\r
168                         ( *pusTaskCheckVariable ) = pdTRUE;\r
169                 }\r
170 \r
171                 #if configUSE_PREEMPTION == 0\r
172                         taskYIELD();\r
173                 #endif\r
174 \r
175         }\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
180 {\r
181 volatile portDOUBLE d1, d2, d3, d4;\r
182 volatile uint16_t *pusTaskCheckVariable;\r
183 volatile portDOUBLE dAnswer;\r
184 short sError = pdFALSE;\r
185 \r
186         /* Some ports require that tasks that use a hardware floating point unit\r
187         tell the kernel that they require a floating point context before any\r
188         floating point instructions are executed. */\r
189         portTASK_USES_FLOATING_POINT();\r
190 \r
191         d1 = -389.38;\r
192         d2 = 32498.2;\r
193         d3 = -2.0001;\r
194 \r
195         dAnswer = ( d1 / d2 ) * d3;\r
196 \r
197 \r
198         /* The variable this task increments to show it is still running is passed in\r
199         as the parameter. */\r
200         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
201 \r
202         /* Keep performing a calculation and checking the result against a constant. */\r
203         for( ;; )\r
204         {\r
205                 d1 = -389.38;\r
206                 d2 = 32498.2;\r
207                 d3 = -2.0001;\r
208 \r
209                 d4 = ( d1 / d2 ) * d3;\r
210 \r
211                 #if configUSE_PREEMPTION == 0\r
212                         taskYIELD();\r
213                 #endif\r
214 \r
215                 /* If the calculation does not match the expected constant, stop the\r
216                 increment of the check variable. */\r
217                 if( fabs( d4 - dAnswer ) > 0.001 )\r
218                 {\r
219                         sError = pdTRUE;\r
220                 }\r
221 \r
222                 if( sError == pdFALSE )\r
223                 {\r
224                         /* If the calculation has always been correct then set set the check\r
225                         variable.  The check variable will get set to pdFALSE each time\r
226                         xAreMathsTaskStillRunning() is executed. */\r
227                         ( *pusTaskCheckVariable ) = pdTRUE;\r
228                 }\r
229 \r
230                 #if configUSE_PREEMPTION == 0\r
231                         taskYIELD();\r
232                 #endif\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
238 {\r
239 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
240 volatile uint16_t *pusTaskCheckVariable;\r
241 const size_t xArraySize = 10;\r
242 size_t xPosition;\r
243 short sError = pdFALSE;\r
244 \r
245         /* Some ports require that tasks that use a hardware floating point unit\r
246         tell the kernel that they require a floating point context before any\r
247         floating point instructions are executed. */\r
248         portTASK_USES_FLOATING_POINT();\r
249 \r
250         /* The variable this task increments to show it is still running is passed in\r
251         as the parameter. */\r
252         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
253 \r
254         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
255 \r
256         /* Keep filling an array, keeping a running total of the values placed in the\r
257         array.  Then run through the array adding up all the values.  If the two totals\r
258         do not match, stop the check variable from incrementing. */\r
259         for( ;; )\r
260         {\r
261                 dTotal1 = 0.0;\r
262                 dTotal2 = 0.0;\r
263 \r
264                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
265                 {\r
266                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition + 5.5;\r
267                         dTotal1 += ( portDOUBLE ) xPosition + 5.5;\r
268                 }\r
269 \r
270                 #if configUSE_PREEMPTION == 0\r
271                         taskYIELD();\r
272                 #endif\r
273 \r
274                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
275                 {\r
276                         dTotal2 += pdArray[ xPosition ];\r
277                 }\r
278 \r
279                 dDifference = dTotal1 - dTotal2;\r
280                 if( fabs( dDifference ) > 0.001 )\r
281                 {\r
282                         sError = pdTRUE;\r
283                 }\r
284 \r
285                 #if configUSE_PREEMPTION == 0\r
286                         taskYIELD();\r
287                 #endif\r
288 \r
289                 if( sError == pdFALSE )\r
290                 {\r
291                         /* If the calculation has always been correct then set set the check\r
292                         variable.  The check variable will get set to pdFALSE each time\r
293                         xAreMathsTaskStillRunning() is executed. */\r
294                         ( *pusTaskCheckVariable ) = pdTRUE;\r
295                 }\r
296         }\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
301 {\r
302 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
303 volatile uint16_t *pusTaskCheckVariable;\r
304 const size_t xArraySize = 10;\r
305 size_t xPosition;\r
306 short sError = pdFALSE;\r
307 \r
308         /* Some ports require that tasks that use a hardware floating point unit\r
309         tell the kernel that they require a floating point context before any\r
310         floating point instructions are executed. */\r
311         portTASK_USES_FLOATING_POINT();\r
312 \r
313         /* The variable this task increments to show it is still running is passed in\r
314         as the parameter. */\r
315         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
316 \r
317         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
318 \r
319         /* Keep filling an array, keeping a running total of the values placed in the\r
320         array.  Then run through the array adding up all the values.  If the two totals\r
321         do not match, stop the check variable from incrementing. */\r
322         for( ;; )\r
323         {\r
324                 dTotal1 = 0.0;\r
325                 dTotal2 = 0.0;\r
326 \r
327                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
328                 {\r
329                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
330                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;\r
331                 }\r
332 \r
333                 #if configUSE_PREEMPTION == 0\r
334                         taskYIELD();\r
335                 #endif\r
336 \r
337                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
338                 {\r
339                         dTotal2 += pdArray[ xPosition ];\r
340                 }\r
341 \r
342                 dDifference = dTotal1 - dTotal2;\r
343                 if( fabs( dDifference ) > 0.001 )\r
344                 {\r
345                         sError = pdTRUE;\r
346                 }\r
347 \r
348                 #if configUSE_PREEMPTION == 0\r
349                         taskYIELD();\r
350                 #endif\r
351 \r
352                 if( sError == pdFALSE )\r
353                 {\r
354                         /* If the calculation has always been correct then set set the check\r
355                         variable.  The check variable will get set to pdFALSE each time\r
356                         xAreMathsTaskStillRunning() is executed. */\r
357                         ( *pusTaskCheckVariable ) = pdTRUE;\r
358                 }\r
359         }\r
360 }\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 /* This is called to check that all the created tasks are still running. */\r
364 BaseType_t xAreMathsTaskStillRunning( void )\r
365 {\r
366 BaseType_t xReturn = pdPASS, xTask;\r
367 \r
368         /* Check the maths tasks are still running by ensuring their check variables\r
369         have been set to pdPASS. */\r
370         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
371         {\r
372                 if( usTaskCheck[ xTask ] != pdTRUE )\r
373                 {\r
374                         /* The check has not been set so the associated task has either\r
375                         stalled or detected an error. */\r
376                         xReturn = pdFAIL;\r
377                 }\r
378                 else\r
379                 {\r
380                         /* Reset the variable so it can be checked again the next time this\r
381                         function is executed. */\r
382                         usTaskCheck[ xTask ] = pdFALSE;\r
383                 }\r
384         }\r
385 \r
386         return xReturn;\r
387 }\r
388 \r
389 \r
390 \r