]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/sp_flop.c
Update version number in preparation for maintenance release.
[freertos] / FreeRTOS / Demo / Common / Minimal / sp_flop.c
1 /*\r
2     FreeRTOS V9.0.1 - Copyright (C) 2017 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 - using single precision variables.\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 priority \r
76  * means that these tasks will get pre-empted any time another task is ready to run\r
77  * or a time slice occurs.  More often than not the pre-emption will occur mid \r
78  * calculation, creating a good test of the schedulers context switch mechanism - a \r
79  * calculation producing an unexpected result could be a symptom of a corruption in \r
80  * 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  ( 8 )\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\r
105 stop incrementing its check variable. */\r
106 static volatile uint16_t usTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 void vStartMathTasks( UBaseType_t uxPriority )\r
111 {\r
112         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
113         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
114         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
115         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
116         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
117         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
118         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
119         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
120 }\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 static portTASK_FUNCTION( vCompetingMathTask1, pvParameters )\r
124 {\r
125 volatile float f1, f2, f3, f4;\r
126 volatile uint16_t *pusTaskCheckVariable;\r
127 volatile float fAnswer;\r
128 short sError = pdFALSE;\r
129 \r
130         f1 = 123.4567F;\r
131         f2 = 2345.6789F;\r
132         f3 = -918.222F;\r
133 \r
134         fAnswer = ( f1 + f2 ) * f3;\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                 f1 = 123.4567F;\r
144                 f2 = 2345.6789F;\r
145                 f3 = -918.222F;\r
146 \r
147                 f4 = ( f1 + f2 ) * f3;\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( f4 - fAnswer ) > 0.001F )\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, increment the check \r
163                         variable so we know this task is still running okay. */\r
164                         ( *pusTaskCheckVariable )++;\r
165                 }\r
166 \r
167                 #if configUSE_PREEMPTION == 0\r
168                         taskYIELD();\r
169                 #endif\r
170 \r
171         }\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 static portTASK_FUNCTION( vCompetingMathTask2, pvParameters )\r
176 {\r
177 volatile float f1, f2, f3, f4;\r
178 volatile uint16_t *pusTaskCheckVariable;\r
179 volatile float fAnswer;\r
180 short sError = pdFALSE;\r
181 \r
182         f1 = -389.38F;\r
183         f2 = 32498.2F;\r
184         f3 = -2.0001F;\r
185 \r
186         fAnswer = ( f1 / f2 ) * f3;\r
187 \r
188 \r
189         /* The variable this task increments to show it is still running is passed in \r
190         as the parameter. */\r
191         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
192 \r
193         /* Keep performing a calculation and checking the result against a constant. */\r
194         for( ;; )\r
195         {\r
196                 f1 = -389.38F;\r
197                 f2 = 32498.2F;\r
198                 f3 = -2.0001F;\r
199 \r
200                 f4 = ( f1 / f2 ) * f3;\r
201 \r
202                 #if configUSE_PREEMPTION == 0\r
203                         taskYIELD();\r
204                 #endif\r
205                 \r
206                 /* If the calculation does not match the expected constant, stop the \r
207                 increment of the check variable. */\r
208                 if( fabs( f4 - fAnswer ) > 0.001F )\r
209                 {\r
210                         sError = pdTRUE;\r
211                 }\r
212 \r
213                 if( sError == pdFALSE )\r
214                 {\r
215                         /* If the calculation has always been correct, increment the check \r
216                         variable so we know\r
217                         this task is still running okay. */\r
218                         ( *pusTaskCheckVariable )++;\r
219                 }\r
220 \r
221                 #if configUSE_PREEMPTION == 0\r
222                         taskYIELD();\r
223                 #endif\r
224         }\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 static portTASK_FUNCTION( vCompetingMathTask3, pvParameters )\r
229 {\r
230 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;\r
231 volatile uint16_t *pusTaskCheckVariable;\r
232 const size_t xArraySize = 10;\r
233 size_t xPosition;\r
234 short sError = pdFALSE;\r
235 \r
236         /* The variable this task increments to show it is still running is passed in \r
237         as the parameter. */\r
238         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
239 \r
240         pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );\r
241 \r
242         /* Keep filling an array, keeping a running total of the values placed in the \r
243         array.  Then run through the array adding up all the values.  If the two totals \r
244         do not match, stop the check variable from incrementing. */\r
245         for( ;; )\r
246         {\r
247                 fTotal1 = 0.0F;\r
248                 fTotal2 = 0.0F;\r
249                 fPosition = 0.0F;\r
250                 \r
251                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
252                 {\r
253                         pfArray[ xPosition ] = fPosition + 5.5F;\r
254                         fTotal1 += fPosition + 5.5F;    \r
255                 }\r
256 \r
257                 #if configUSE_PREEMPTION == 0\r
258                         taskYIELD();\r
259                 #endif\r
260 \r
261                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
262                 {\r
263                         fTotal2 += pfArray[ xPosition ];\r
264                 }\r
265 \r
266                 fDifference = fTotal1 - fTotal2;\r
267                 if( fabs( fDifference ) > 0.001F )\r
268                 {\r
269                         sError = pdTRUE;\r
270                 }\r
271 \r
272                 #if configUSE_PREEMPTION == 0\r
273                         taskYIELD();\r
274                 #endif\r
275 \r
276                 if( sError == pdFALSE )\r
277                 {\r
278                         /* If the calculation has always been correct, increment the check \r
279                         variable so we know     this task is still running okay. */\r
280                         ( *pusTaskCheckVariable )++;\r
281                 }\r
282         }\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
287 {\r
288 volatile float *pfArray, fTotal1, fTotal2, fDifference, fPosition;\r
289 volatile uint16_t *pusTaskCheckVariable;\r
290 const size_t xArraySize = 10;\r
291 size_t xPosition;\r
292 short sError = pdFALSE;\r
293 \r
294         /* The variable this task increments to show it is still running is passed in \r
295         as the parameter. */\r
296         pusTaskCheckVariable = ( uint16_t * ) pvParameters;\r
297 \r
298         pfArray = ( float * ) pvPortMalloc( xArraySize * sizeof( float ) );\r
299 \r
300         /* Keep filling an array, keeping a running total of the values placed in the \r
301         array.  Then run through the array adding up all the values.  If the two totals \r
302         do not match, stop the check variable from incrementing. */\r
303         for( ;; )\r
304         {\r
305                 fTotal1 = 0.0F;\r
306                 fTotal2 = 0.0F;\r
307                 fPosition = 0.0F;\r
308 \r
309                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
310                 {\r
311                         pfArray[ xPosition ] = fPosition * 12.123F;\r
312                         fTotal1 += fPosition * 12.123F; \r
313                 }\r
314 \r
315                 #if configUSE_PREEMPTION == 0\r
316                         taskYIELD();\r
317                 #endif\r
318 \r
319                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
320                 {\r
321                         fTotal2 += pfArray[ xPosition ];\r
322                 }\r
323 \r
324                 fDifference = fTotal1 - fTotal2;\r
325                 if( fabs( fDifference ) > 0.001F )\r
326                 {\r
327                         sError = pdTRUE;\r
328                 }\r
329 \r
330                 #if configUSE_PREEMPTION == 0\r
331                         taskYIELD();\r
332                 #endif\r
333 \r
334                 if( sError == pdFALSE )\r
335                 {\r
336                         /* If the calculation has always been correct, increment the check \r
337                         variable so we know     this task is still running okay. */\r
338                         ( *pusTaskCheckVariable )++;\r
339                 }\r
340         }\r
341 }                                \r
342 /*-----------------------------------------------------------*/\r
343 \r
344 /* This is called to check that all the created tasks are still running. */\r
345 BaseType_t xAreMathsTaskStillRunning( void )\r
346 {\r
347 /* Keep a history of the check variables so we know if they have been incremented \r
348 since the last call. */\r
349 static uint16_t usLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( uint16_t ) 0 };\r
350 BaseType_t xReturn = pdTRUE, xTask;\r
351 \r
352         /* Check the maths tasks are still running by ensuring their check variables \r
353         are still incrementing. */\r
354         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
355         {\r
356                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
357                 {\r
358                         /* The check has not incremented so an error exists. */\r
359                         xReturn = pdFALSE;\r
360                 }\r
361 \r
362                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
363         }\r
364 \r
365         return xReturn;\r
366 }\r
367 \r
368 \r
369 \r