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