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