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