]> begriffs open source - freertos/blob - Demo/Common/Full/integer.c
Update version numbers to V4.8.0
[freertos] / Demo / Common / Full / integer.c
1 /*\r
2         FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         ***************************************************************************\r
28         *                                                                                                                                                 *\r
29         * SAVE TIME AND MONEY!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 /*\r
50 Changes from V1.2.3\r
51         \r
52         + The created tasks now include calls to tskYIELD(), allowing them to be used\r
53           with the cooperative scheduler.\r
54 */\r
55 \r
56 /**\r
57  * This does the same as flop. c, but uses variables of type long instead of \r
58  * type double.  \r
59  *\r
60  * As with flop. c, the tasks created in this file are a good test of the \r
61  * scheduler context switch mechanism.  The processor has to access 32bit \r
62  * variables in two or four chunks (depending on the processor).  The low \r
63  * priority of these tasks means there is a high probability that a context \r
64  * switch will occur mid calculation.  See the flop. c documentation for \r
65  * more information.\r
66  *\r
67  * \page IntegerC integer.c\r
68  * \ingroup DemoFiles\r
69  * <HR>\r
70  */\r
71 \r
72 /*\r
73 Changes from V1.2.1\r
74 \r
75         + The constants used in the calculations are larger to ensure the\r
76           optimiser does not truncate them to 16 bits.\r
77 */\r
78 \r
79 #include <stdlib.h>\r
80 \r
81 /* Scheduler include files. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 #include "print.h"\r
85 \r
86 /* Demo program include files. */\r
87 #include "integer.h"\r
88 \r
89 #define intgSTACK_SIZE          ( ( unsigned portSHORT ) 256 )\r
90 #define intgNUMBER_OF_TASKS  ( 8 )\r
91 \r
92 /* Four tasks, each of which performs a different calculation on four byte \r
93 variables.  Each of the four is created twice. */\r
94 static void vCompeteingIntMathTask1( void *pvParameters );\r
95 static void vCompeteingIntMathTask2( void *pvParameters );\r
96 static void vCompeteingIntMathTask3( void *pvParameters );\r
97 static void vCompeteingIntMathTask4( void *pvParameters );\r
98 \r
99 /* These variables are used to check that all the tasks are still running.  If a \r
100 task gets a calculation wrong it will stop incrementing its check variable. */\r
101 static volatile unsigned portSHORT usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )\r
105 {\r
106         xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
107         xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
108         xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
109         xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
110         xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
111         xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
112         xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
113         xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 static void vCompeteingIntMathTask1( void *pvParameters )\r
118 {\r
119 portLONG l1, l2, l3, l4;\r
120 portSHORT sError = pdFALSE;\r
121 volatile unsigned portSHORT *pusTaskCheckVariable;\r
122 const portLONG lAnswer = ( ( portLONG ) 74565L + ( portLONG ) 1234567L ) * ( portLONG ) -918L;\r
123 const portCHAR * const pcTaskStartMsg = "Integer math task 1 started.\r\n";\r
124 const portCHAR * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";\r
125 \r
126         /* Queue a message for printing to say the task has started. */\r
127         vPrintDisplayMessage( &pcTaskStartMsg );\r
128 \r
129         /* The variable this task increments to show it is still running is passed in\r
130         as the parameter. */\r
131         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
132 \r
133         /* Keep performing a calculation and checking the result against a constant. */\r
134         for(;;)\r
135         {\r
136                 l1 = ( portLONG ) 74565L;\r
137                 l2 = ( portLONG ) 1234567L;\r
138                 l3 = ( portLONG ) -918L;\r
139 \r
140                 l4 = ( l1 + l2 ) * l3;\r
141 \r
142                 taskYIELD();\r
143 \r
144                 /* If the calculation does not match the expected constant, stop the\r
145                 increment of the check variable. */\r
146                 if( l4 != lAnswer )\r
147                 {\r
148                         vPrintDisplayMessage( &pcTaskFailMsg );\r
149                         sError = pdTRUE;\r
150                 }\r
151 \r
152                 if( sError == pdFALSE )\r
153                 {\r
154                         /* If the calculation has always been correct, increment the check\r
155                         variable so we know     this task is still running okay. */\r
156                         ( *pusTaskCheckVariable )++;\r
157                 }\r
158         }\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 static void vCompeteingIntMathTask2( void *pvParameters )\r
163 {\r
164 portLONG l1, l2, l3, l4;\r
165 portSHORT sError = pdFALSE;\r
166 volatile unsigned portSHORT *pusTaskCheckVariable;\r
167 const portLONG lAnswer = ( ( portLONG ) -389000L / ( portLONG ) 329999L ) * ( portLONG ) -89L;\r
168 const portCHAR * const pcTaskStartMsg = "Integer math task 2 started.\r\n";\r
169 const portCHAR * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";\r
170 \r
171         /* Queue a message for printing to say the task has started. */\r
172         vPrintDisplayMessage( &pcTaskStartMsg );\r
173 \r
174         /* The variable this task increments to show it is still running is passed in\r
175         as the parameter. */\r
176         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
177 \r
178         /* Keep performing a calculation and checking the result against a constant. */\r
179         for( ;; )\r
180         {\r
181                 l1 = -389000L;\r
182                 l2 = 329999L;\r
183                 l3 = -89L;\r
184 \r
185                 l4 = ( l1 / l2 ) * l3;\r
186 \r
187                 taskYIELD();\r
188 \r
189                 /* If the calculation does not match the expected constant, stop the\r
190                 increment of the check variable. */\r
191                 if( l4 != lAnswer )\r
192                 {\r
193                         vPrintDisplayMessage( &pcTaskFailMsg );\r
194                         sError = pdTRUE;\r
195                 }\r
196 \r
197                 if( sError == pdFALSE )\r
198                 {\r
199                         /* If the calculation has always been correct, increment the check\r
200                         variable so we know this task is still running okay. */\r
201                         ( *pusTaskCheckVariable )++;\r
202                 }\r
203         }\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 static void vCompeteingIntMathTask3( void *pvParameters )\r
208 {\r
209 portLONG *plArray, lTotal1, lTotal2;\r
210 portSHORT sError = pdFALSE;\r
211 volatile unsigned portSHORT *pusTaskCheckVariable;\r
212 const unsigned portSHORT usArraySize = ( unsigned portSHORT ) 250;\r
213 unsigned portSHORT usPosition;\r
214 const portCHAR * const pcTaskStartMsg = "Integer math task 3 started.\r\n";\r
215 const portCHAR * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";\r
216 \r
217         /* Queue a message for printing to say the task has started. */\r
218         vPrintDisplayMessage( &pcTaskStartMsg );\r
219 \r
220         /* The variable this task increments to show it is still running is passed in\r
221         as the parameter. */\r
222         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
223 \r
224         /* Create the array we are going to use for our check calculation. */\r
225         plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );\r
226 \r
227         /* Keep filling the array, keeping a running total of the values placed in the\r
228         array.  Then run through the array adding up all the values.  If the two totals\r
229         do not match, stop the check variable from incrementing. */\r
230         for( ;; )\r
231         {\r
232                 lTotal1 = ( portLONG ) 0;\r
233                 lTotal2 = ( portLONG ) 0;\r
234 \r
235                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
236                 {\r
237                         plArray[ usPosition ] = ( portLONG ) usPosition + ( portLONG ) 5;\r
238                         lTotal1 += ( portLONG ) usPosition + ( portLONG ) 5;\r
239                 }\r
240 \r
241                 taskYIELD();\r
242 \r
243                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
244                 {\r
245                         lTotal2 += plArray[ usPosition ];\r
246                 }\r
247 \r
248                 if( lTotal1 != lTotal2 )\r
249                 {\r
250                         vPrintDisplayMessage( &pcTaskFailMsg );\r
251                         sError = pdTRUE;\r
252                 }\r
253 \r
254                 taskYIELD();\r
255 \r
256                 if( sError == pdFALSE )\r
257                 {\r
258                         /* If the calculation has always been correct, increment the check\r
259                         variable so we know     this task is still running okay. */\r
260                         ( *pusTaskCheckVariable )++;\r
261                 }\r
262         }\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 static void vCompeteingIntMathTask4( void *pvParameters )\r
267 {\r
268 portLONG *plArray, lTotal1, lTotal2;\r
269 portSHORT sError = pdFALSE;\r
270 volatile unsigned portSHORT *pusTaskCheckVariable;\r
271 const unsigned portSHORT usArraySize = 250;\r
272 unsigned portSHORT usPosition;\r
273 const portCHAR * const pcTaskStartMsg = "Integer math task 4 started.\r\n";\r
274 const portCHAR * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";\r
275 \r
276         /* Queue a message for printing to say the task has started. */\r
277         vPrintDisplayMessage( &pcTaskStartMsg );\r
278 \r
279         /* The variable this task increments to show it is still running is passed in\r
280         as the parameter. */\r
281         pusTaskCheckVariable = ( unsigned portSHORT * ) pvParameters;\r
282 \r
283         /* Create the array we are going to use for our check calculation. */\r
284         plArray = ( portLONG * ) pvPortMalloc( ( size_t ) 250 * sizeof( portLONG ) );\r
285 \r
286         /* Keep filling the array, keeping a running total of the values placed in the \r
287         array.  Then run through the array adding up all the values.  If the two totals \r
288         do not match, stop the check variable from incrementing. */\r
289         for( ;; )\r
290         {\r
291                 lTotal1 = ( portLONG ) 0;\r
292                 lTotal2 = ( portLONG ) 0;\r
293 \r
294                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
295                 {\r
296                         plArray[ usPosition ] = ( portLONG ) usPosition * ( portLONG ) 12;\r
297                         lTotal1 += ( portLONG ) usPosition * ( portLONG ) 12;   \r
298                 }\r
299 \r
300                 taskYIELD();\r
301         \r
302                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
303                 {\r
304                         lTotal2 += plArray[ usPosition ];\r
305                 }\r
306 \r
307 \r
308                 if( lTotal1 != lTotal2 )\r
309                 {\r
310                         vPrintDisplayMessage( &pcTaskFailMsg );\r
311                         sError = pdTRUE;\r
312                 }\r
313 \r
314                 taskYIELD();\r
315 \r
316                 if( sError == pdFALSE )\r
317                 {\r
318                         /* If the calculation has always been correct, increment the check \r
319                         variable so we know     this task is still running okay. */\r
320                         ( *pusTaskCheckVariable )++;\r
321                 }\r
322         }\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 /* This is called to check that all the created tasks are still running. */\r
327 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )\r
328 {\r
329 /* Keep a history of the check variables so we know if they have been incremented \r
330 since the last call. */\r
331 static unsigned portSHORT usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned portSHORT ) 0 };\r
332 portBASE_TYPE xReturn = pdTRUE, xTask;\r
333 \r
334         /* Check the maths tasks are still running by ensuring their check variables \r
335         are still incrementing. */\r
336         for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )\r
337         {\r
338                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
339                 {\r
340                         /* The check has not incremented so an error exists. */\r
341                         xReturn = pdFALSE;\r
342                 }\r
343 \r
344                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
345         }\r
346 \r
347         return xReturn;\r
348 }\r