]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MZ_MPLAB/flop_mz.c
1e8ee15e69808007d6ad09c1e57567dc046ffdb7
[freertos] / FreeRTOS / Demo / PIC32MZ_MPLAB / flop_mz.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 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\r
72  * floating 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 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_mz.h"\r
92 \r
93 #define mathSTACK_SIZE          (configMINIMAL_STACK_SIZE + 100)\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 unsigned long ulTaskCheck[ mathNUMBER_OF_TASKS ] = { 0 };\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 void vStartMathTasks( unsigned portBASE_TYPE uxPriority )\r
111 {\r
112         xTaskCreate( vCompetingMathTask1, "Math1", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 0 ] ), uxPriority, NULL );\r
113         xTaskCreate( vCompetingMathTask2, "Math2", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 1 ] ), uxPriority, NULL );\r
114         xTaskCreate( vCompetingMathTask3, "Math3", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 2 ] ), uxPriority, NULL );\r
115         xTaskCreate( vCompetingMathTask4, "Math4", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 3 ] ), uxPriority, NULL );\r
116         xTaskCreate( vCompetingMathTask1, "Math5", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 4 ] ), uxPriority, NULL );\r
117         xTaskCreate( vCompetingMathTask2, "Math6", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 5 ] ), uxPriority, NULL );\r
118         xTaskCreate( vCompetingMathTask3, "Math7", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 6 ] ), uxPriority, NULL );\r
119         xTaskCreate( vCompetingMathTask4, "Math8", mathSTACK_SIZE, ( void * ) &( ulTaskCheck[ 7 ] ), 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 long *pulTaskCheckVariable;\r
127 volatile portDOUBLE dAnswer;\r
128 short sError = pdFALSE;\r
129 \r
130 \r
131         /* Must be called before any hardware floating point operations are\r
132         performed to let the RTOS portable layer know that this task requires\r
133         a floating point context. */\r
134         portTASK_USES_FLOATING_POINT();\r
135 \r
136         d1 = 123.4567;\r
137         d2 = 2345.6789;\r
138         d3 = -918.222;\r
139 \r
140         dAnswer = ( d1 + d2 ) * d3;\r
141 \r
142         /* The variable this task increments to show it is still running is passed in\r
143         as the parameter. */\r
144         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
145     \r
146         /* Keep performing a calculation and checking the result against a constant. */\r
147         for(;;)\r
148         {\r
149                 d1 = 123.4567;\r
150                 d2 = 2345.6789;\r
151                 d3 = -918.222;\r
152 \r
153                 d4 = ( d1 + d2 ) * d3;\r
154 \r
155                 #if configUSE_PREEMPTION == 0\r
156                         taskYIELD();\r
157                 #endif\r
158 \r
159                 /* If the calculation does not match the expected constant, stop the\r
160                 increment of the check variable. */\r
161                 if( fabs( d4 - dAnswer ) > 0.001 )\r
162                 {\r
163                         sError = pdTRUE;\r
164                 }\r
165 \r
166                 if( sError == pdFALSE )\r
167                 {\r
168                         /* If the calculation has always been correct, increment the check\r
169                         variable so we know this task is still running okay. */\r
170                         ( *pulTaskCheckVariable )++;\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 long *pulTaskCheckVariable;\r
185 volatile portDOUBLE dAnswer;\r
186 short sError = pdFALSE;\r
187 \r
188         /* Must be called before any hardware floating point operations are\r
189         performed to let the RTOS portable layer know that this task requires\r
190         a floating point context. */\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         pulTaskCheckVariable = ( unsigned long * ) 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, increment the check\r
227                         variable so we know\r
228                         this task is still running okay. */\r
229                         ( *pulTaskCheckVariable )++;\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 long *pulTaskCheckVariable;\r
243 const size_t xArraySize = 10;\r
244 size_t xPosition;\r
245 short sError = pdFALSE;\r
246 \r
247         /* Must be called before any hardware floating point operations are\r
248         performed to let the RTOS portable layer know that this task requires\r
249         a floating point context. */\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         pulTaskCheckVariable = ( unsigned long * ) 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, increment the check\r
294                         variable so we know     this task is still running okay. */\r
295                         ( *pulTaskCheckVariable )++;\r
296                 }\r
297         }\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 static portTASK_FUNCTION( vCompetingMathTask4, pvParameters )\r
302 {\r
303 volatile portDOUBLE *pdArray, dTotal1, dTotal2, dDifference;\r
304 volatile unsigned long *pulTaskCheckVariable;\r
305 const size_t xArraySize = 10;\r
306 size_t xPosition;\r
307 short sError = pdFALSE;\r
308 \r
309         /* Must be called before any hardware floating point operations are\r
310         performed to let the RTOS portable layer know that this task requires\r
311         a floating point context. */\r
312         portTASK_USES_FLOATING_POINT();\r
313 \r
314         /* The variable this task increments to show it is still running is passed in\r
315         as the parameter. */\r
316         pulTaskCheckVariable = ( unsigned long * ) pvParameters;\r
317 \r
318         pdArray = ( portDOUBLE * ) pvPortMalloc( xArraySize * sizeof( portDOUBLE ) );\r
319 \r
320         /* Keep filling an array, keeping a running total of the values placed in the\r
321         array.  Then run through the array adding up all the values.  If the two totals\r
322         do not match, stop the check variable from incrementing. */\r
323         for( ;; )\r
324         {\r
325                 dTotal1 = 0.0;\r
326                 dTotal2 = 0.0;\r
327 \r
328                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
329                 {\r
330                         pdArray[ xPosition ] = ( portDOUBLE ) xPosition * 12.123;\r
331                         dTotal1 += ( portDOUBLE ) xPosition * 12.123;\r
332                 }\r
333 \r
334                 #if configUSE_PREEMPTION == 0\r
335                         taskYIELD();\r
336                 #endif\r
337 \r
338                 for( xPosition = 0; xPosition < xArraySize; xPosition++ )\r
339                 {\r
340                         dTotal2 += pdArray[ xPosition ];\r
341                 }\r
342 \r
343                 dDifference = dTotal1 - dTotal2;\r
344                 if( fabs( dDifference ) > 0.001 )\r
345                 {\r
346                         sError = pdTRUE;\r
347                 }\r
348 \r
349                 #if configUSE_PREEMPTION == 0\r
350                         taskYIELD();\r
351                 #endif\r
352 \r
353                 if( sError == pdFALSE )\r
354                 {\r
355                         /* If the calculation has always been correct, increment the check\r
356                         variable so we know     this task is still running okay. */\r
357                         ( *pulTaskCheckVariable )++;\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 portBASE_TYPE xAreMathsTaskStillRunning( void )\r
365 {\r
366 /* Keep a history of the check variables so we know if they have been incremented\r
367 since the last call. */\r
368 static unsigned long ulLastTaskCheck[ mathNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
369 portBASE_TYPE xReturn = pdTRUE, xTask;\r
370 \r
371         /* Check the maths tasks are still running by ensuring their check variables\r
372         are still incrementing. */\r
373         for( xTask = 0; xTask < mathNUMBER_OF_TASKS; xTask++ )\r
374         {\r
375                 if( ulTaskCheck[ xTask ] == ulLastTaskCheck[ xTask ] )\r
376                 {\r
377                         /* The check has not incremented so an error exists. */\r
378                         xReturn = pdFALSE;\r
379                 }\r
380 \r
381                 ulLastTaskCheck[ xTask ] = ulTaskCheck[ xTask ];\r
382         }\r
383 \r
384         return xReturn;\r
385 }\r
386 \r
387 \r
388 \r