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