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