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