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