]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/integer.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / Common / Full / integer.c
1 /*\r
2     FreeRTOS V8.1.2 - 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67 Changes from V1.2.3\r
68         \r
69         + The created tasks now include calls to tskYIELD(), allowing them to be used\r
70           with the cooperative scheduler.\r
71 */\r
72 \r
73 /**\r
74  * This does the same as flop. c, but uses variables of type long instead of \r
75  * type double.  \r
76  *\r
77  * As with flop. c, the tasks created in this file are a good test of the \r
78  * scheduler context switch mechanism.  The processor has to access 32bit \r
79  * variables in two or four chunks (depending on the processor).  The low \r
80  * priority of these tasks means there is a high probability that a context \r
81  * switch will occur mid calculation.  See the flop. c documentation for \r
82  * more information.\r
83  *\r
84  * \page IntegerC integer.c\r
85  * \ingroup DemoFiles\r
86  * <HR>\r
87  */\r
88 \r
89 /*\r
90 Changes from V1.2.1\r
91 \r
92         + The constants used in the calculations are larger to ensure the\r
93           optimiser does not truncate them to 16 bits.\r
94 */\r
95 \r
96 #include <stdlib.h>\r
97 \r
98 /* Scheduler include files. */\r
99 #include "FreeRTOS.h"\r
100 #include "task.h"\r
101 #include "print.h"\r
102 \r
103 /* Demo program include files. */\r
104 #include "integer.h"\r
105 \r
106 #define intgSTACK_SIZE          ( ( unsigned short ) 256 )\r
107 #define intgNUMBER_OF_TASKS  ( 8 )\r
108 \r
109 /* Four tasks, each of which performs a different calculation on four byte \r
110 variables.  Each of the four is created twice. */\r
111 static void vCompeteingIntMathTask1( void *pvParameters );\r
112 static void vCompeteingIntMathTask2( void *pvParameters );\r
113 static void vCompeteingIntMathTask3( void *pvParameters );\r
114 static void vCompeteingIntMathTask4( void *pvParameters );\r
115 \r
116 /* These variables are used to check that all the tasks are still running.  If a \r
117 task gets a calculation wrong it will stop incrementing its check variable. */\r
118 static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
119 /*-----------------------------------------------------------*/\r
120 \r
121 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )\r
122 {\r
123         xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
124         xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
125         xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
126         xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
127         xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
128         xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
129         xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
130         xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 static void vCompeteingIntMathTask1( void *pvParameters )\r
135 {\r
136 long l1, l2, l3, l4;\r
137 short sError = pdFALSE;\r
138 volatile unsigned short *pusTaskCheckVariable;\r
139 const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;\r
140 const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";\r
141 const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";\r
142 \r
143         /* Queue a message for printing to say the task has started. */\r
144         vPrintDisplayMessage( &pcTaskStartMsg );\r
145 \r
146         /* The variable this task increments to show it is still running is passed in\r
147         as the parameter. */\r
148         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
149 \r
150         /* Keep performing a calculation and checking the result against a constant. */\r
151         for(;;)\r
152         {\r
153                 l1 = ( long ) 74565L;\r
154                 l2 = ( long ) 1234567L;\r
155                 l3 = ( long ) -918L;\r
156 \r
157                 l4 = ( l1 + l2 ) * l3;\r
158 \r
159                 taskYIELD();\r
160 \r
161                 /* If the calculation does not match the expected constant, stop the\r
162                 increment of the check variable. */\r
163                 if( l4 != lAnswer )\r
164                 {\r
165                         vPrintDisplayMessage( &pcTaskFailMsg );\r
166                         sError = pdTRUE;\r
167                 }\r
168 \r
169                 if( sError == pdFALSE )\r
170                 {\r
171                         /* If the calculation has always been correct, increment the check\r
172                         variable so we know     this task is still running okay. */\r
173                         ( *pusTaskCheckVariable )++;\r
174                 }\r
175         }\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static void vCompeteingIntMathTask2( void *pvParameters )\r
180 {\r
181 long l1, l2, l3, l4;\r
182 short sError = pdFALSE;\r
183 volatile unsigned short *pusTaskCheckVariable;\r
184 const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;\r
185 const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";\r
186 const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";\r
187 \r
188         /* Queue a message for printing to say the task has started. */\r
189         vPrintDisplayMessage( &pcTaskStartMsg );\r
190 \r
191         /* The variable this task increments to show it is still running is passed in\r
192         as the parameter. */\r
193         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
194 \r
195         /* Keep performing a calculation and checking the result against a constant. */\r
196         for( ;; )\r
197         {\r
198                 l1 = -389000L;\r
199                 l2 = 329999L;\r
200                 l3 = -89L;\r
201 \r
202                 l4 = ( l1 / l2 ) * l3;\r
203 \r
204                 taskYIELD();\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( l4 != lAnswer )\r
209                 {\r
210                         vPrintDisplayMessage( &pcTaskFailMsg );\r
211                         sError = pdTRUE;\r
212                 }\r
213 \r
214                 if( sError == pdFALSE )\r
215                 {\r
216                         /* If the calculation has always been correct, increment the check\r
217                         variable so we know this task is still running okay. */\r
218                         ( *pusTaskCheckVariable )++;\r
219                 }\r
220         }\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void vCompeteingIntMathTask3( void *pvParameters )\r
225 {\r
226 long *plArray, lTotal1, lTotal2;\r
227 short sError = pdFALSE;\r
228 volatile unsigned short *pusTaskCheckVariable;\r
229 const unsigned short usArraySize = ( unsigned short ) 250;\r
230 unsigned short usPosition;\r
231 const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";\r
232 const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";\r
233 \r
234         /* Queue a message for printing to say the task has started. */\r
235         vPrintDisplayMessage( &pcTaskStartMsg );\r
236 \r
237         /* The variable this task increments to show it is still running is passed in\r
238         as the parameter. */\r
239         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
240 \r
241         /* Create the array we are going to use for our check calculation. */\r
242         plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );\r
243 \r
244         /* Keep filling the array, keeping a running total of the values placed in the\r
245         array.  Then run through the array adding up all the values.  If the two totals\r
246         do not match, stop the check variable from incrementing. */\r
247         for( ;; )\r
248         {\r
249                 lTotal1 = ( long ) 0;\r
250                 lTotal2 = ( long ) 0;\r
251 \r
252                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
253                 {\r
254                         plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;\r
255                         lTotal1 += ( long ) usPosition + ( long ) 5;\r
256                 }\r
257 \r
258                 taskYIELD();\r
259 \r
260                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
261                 {\r
262                         lTotal2 += plArray[ usPosition ];\r
263                 }\r
264 \r
265                 if( lTotal1 != lTotal2 )\r
266                 {\r
267                         vPrintDisplayMessage( &pcTaskFailMsg );\r
268                         sError = pdTRUE;\r
269                 }\r
270 \r
271                 taskYIELD();\r
272 \r
273                 if( sError == pdFALSE )\r
274                 {\r
275                         /* If the calculation has always been correct, increment the check\r
276                         variable so we know     this task is still running okay. */\r
277                         ( *pusTaskCheckVariable )++;\r
278                 }\r
279         }\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 static void vCompeteingIntMathTask4( void *pvParameters )\r
284 {\r
285 long *plArray, lTotal1, lTotal2;\r
286 short sError = pdFALSE;\r
287 volatile unsigned short *pusTaskCheckVariable;\r
288 const unsigned short usArraySize = 250;\r
289 unsigned short usPosition;\r
290 const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";\r
291 const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";\r
292 \r
293         /* Queue a message for printing to say the task has started. */\r
294         vPrintDisplayMessage( &pcTaskStartMsg );\r
295 \r
296         /* The variable this task increments to show it is still running is passed in\r
297         as the parameter. */\r
298         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
299 \r
300         /* Create the array we are going to use for our check calculation. */\r
301         plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );\r
302 \r
303         /* Keep filling the array, keeping a running total of the values placed in the \r
304         array.  Then run through the array adding up all the values.  If the two totals \r
305         do not match, stop the check variable from incrementing. */\r
306         for( ;; )\r
307         {\r
308                 lTotal1 = ( long ) 0;\r
309                 lTotal2 = ( long ) 0;\r
310 \r
311                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
312                 {\r
313                         plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;\r
314                         lTotal1 += ( long ) usPosition * ( long ) 12;   \r
315                 }\r
316 \r
317                 taskYIELD();\r
318         \r
319                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
320                 {\r
321                         lTotal2 += plArray[ usPosition ];\r
322                 }\r
323 \r
324 \r
325                 if( lTotal1 != lTotal2 )\r
326                 {\r
327                         vPrintDisplayMessage( &pcTaskFailMsg );\r
328                         sError = pdTRUE;\r
329                 }\r
330 \r
331                 taskYIELD();\r
332 \r
333                 if( sError == pdFALSE )\r
334                 {\r
335                         /* If the calculation has always been correct, increment the check \r
336                         variable so we know     this task is still running okay. */\r
337                         ( *pusTaskCheckVariable )++;\r
338                 }\r
339         }\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 /* This is called to check that all the created tasks are still running. */\r
344 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )\r
345 {\r
346 /* Keep a history of the check variables so we know if they have been incremented \r
347 since the last call. */\r
348 static unsigned short usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
349 portBASE_TYPE xReturn = pdTRUE, xTask;\r
350 \r
351         /* Check the maths tasks are still running by ensuring their check variables \r
352         are still incrementing. */\r
353         for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )\r
354         {\r
355                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
356                 {\r
357                         /* The check has not incremented so an error exists. */\r
358                         xReturn = pdFALSE;\r
359                 }\r
360 \r
361                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
362         }\r
363 \r
364         return xReturn;\r
365 }\r