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