]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/integer.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / Common / Full / integer.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30 Changes from V1.2.3\r
31         \r
32         + The created tasks now include calls to tskYIELD(), allowing them to be used\r
33           with the cooperative scheduler.\r
34 */\r
35 \r
36 /**\r
37  * This does the same as flop. c, but uses variables of type long instead of \r
38  * type double.  \r
39  *\r
40  * As with flop. c, the tasks created in this file are a good test of the \r
41  * scheduler context switch mechanism.  The processor has to access 32bit \r
42  * variables in two or four chunks (depending on the processor).  The low \r
43  * priority of these tasks means there is a high probability that a context \r
44  * switch will occur mid calculation.  See the flop. c documentation for \r
45  * more information.\r
46  *\r
47  * \page IntegerC integer.c\r
48  * \ingroup DemoFiles\r
49  * <HR>\r
50  */\r
51 \r
52 /*\r
53 Changes from V1.2.1\r
54 \r
55         + The constants used in the calculations are larger to ensure the\r
56           optimiser does not truncate them to 16 bits.\r
57 */\r
58 \r
59 #include <stdlib.h>\r
60 \r
61 /* Scheduler include files. */\r
62 #include "FreeRTOS.h"\r
63 #include "task.h"\r
64 #include "print.h"\r
65 \r
66 /* Demo program include files. */\r
67 #include "integer.h"\r
68 \r
69 #define intgSTACK_SIZE          ( ( unsigned short ) 256 )\r
70 #define intgNUMBER_OF_TASKS  ( 8 )\r
71 \r
72 /* Four tasks, each of which performs a different calculation on four byte \r
73 variables.  Each of the four is created twice. */\r
74 static void vCompeteingIntMathTask1( void *pvParameters );\r
75 static void vCompeteingIntMathTask2( void *pvParameters );\r
76 static void vCompeteingIntMathTask3( void *pvParameters );\r
77 static void vCompeteingIntMathTask4( void *pvParameters );\r
78 \r
79 /* These variables are used to check that all the tasks are still running.  If a \r
80 task gets a calculation wrong it will stop incrementing its check variable. */\r
81 static volatile unsigned short usTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
82 /*-----------------------------------------------------------*/\r
83 \r
84 void vStartIntegerMathTasks( unsigned portBASE_TYPE uxPriority )\r
85 {\r
86         xTaskCreate( vCompeteingIntMathTask1, "IntMath1", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 0 ] ), uxPriority, NULL );\r
87         xTaskCreate( vCompeteingIntMathTask2, "IntMath2", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 1 ] ), uxPriority, NULL );\r
88         xTaskCreate( vCompeteingIntMathTask3, "IntMath3", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 2 ] ), uxPriority, NULL );\r
89         xTaskCreate( vCompeteingIntMathTask4, "IntMath4", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 3 ] ), uxPriority, NULL );\r
90         xTaskCreate( vCompeteingIntMathTask1, "IntMath5", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 4 ] ), uxPriority, NULL );\r
91         xTaskCreate( vCompeteingIntMathTask2, "IntMath6", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 5 ] ), uxPriority, NULL );\r
92         xTaskCreate( vCompeteingIntMathTask3, "IntMath7", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 6 ] ), uxPriority, NULL );\r
93         xTaskCreate( vCompeteingIntMathTask4, "IntMath8", intgSTACK_SIZE, ( void * ) &( usTaskCheck[ 7 ] ), uxPriority, NULL );\r
94 }\r
95 /*-----------------------------------------------------------*/\r
96 \r
97 static void vCompeteingIntMathTask1( void *pvParameters )\r
98 {\r
99 long l1, l2, l3, l4;\r
100 short sError = pdFALSE;\r
101 volatile unsigned short *pusTaskCheckVariable;\r
102 const long lAnswer = ( ( long ) 74565L + ( long ) 1234567L ) * ( long ) -918L;\r
103 const char * const pcTaskStartMsg = "Integer math task 1 started.\r\n";\r
104 const char * const pcTaskFailMsg = "Integer math task 1 failed.\r\n";\r
105 \r
106         /* Queue a message for printing to say the task has started. */\r
107         vPrintDisplayMessage( &pcTaskStartMsg );\r
108 \r
109         /* The variable this task increments to show it is still running is passed in\r
110         as the parameter. */\r
111         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
112 \r
113         /* Keep performing a calculation and checking the result against a constant. */\r
114         for(;;)\r
115         {\r
116                 l1 = ( long ) 74565L;\r
117                 l2 = ( long ) 1234567L;\r
118                 l3 = ( long ) -918L;\r
119 \r
120                 l4 = ( l1 + l2 ) * l3;\r
121 \r
122                 taskYIELD();\r
123 \r
124                 /* If the calculation does not match the expected constant, stop the\r
125                 increment of the check variable. */\r
126                 if( l4 != lAnswer )\r
127                 {\r
128                         vPrintDisplayMessage( &pcTaskFailMsg );\r
129                         sError = pdTRUE;\r
130                 }\r
131 \r
132                 if( sError == pdFALSE )\r
133                 {\r
134                         /* If the calculation has always been correct, increment the check\r
135                         variable so we know     this task is still running okay. */\r
136                         ( *pusTaskCheckVariable )++;\r
137                 }\r
138         }\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 static void vCompeteingIntMathTask2( void *pvParameters )\r
143 {\r
144 long l1, l2, l3, l4;\r
145 short sError = pdFALSE;\r
146 volatile unsigned short *pusTaskCheckVariable;\r
147 const long lAnswer = ( ( long ) -389000L / ( long ) 329999L ) * ( long ) -89L;\r
148 const char * const pcTaskStartMsg = "Integer math task 2 started.\r\n";\r
149 const char * const pcTaskFailMsg = "Integer math task 2 failed.\r\n";\r
150 \r
151         /* Queue a message for printing to say the task has started. */\r
152         vPrintDisplayMessage( &pcTaskStartMsg );\r
153 \r
154         /* The variable this task increments to show it is still running is passed in\r
155         as the parameter. */\r
156         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
157 \r
158         /* Keep performing a calculation and checking the result against a constant. */\r
159         for( ;; )\r
160         {\r
161                 l1 = -389000L;\r
162                 l2 = 329999L;\r
163                 l3 = -89L;\r
164 \r
165                 l4 = ( l1 / l2 ) * l3;\r
166 \r
167                 taskYIELD();\r
168 \r
169                 /* If the calculation does not match the expected constant, stop the\r
170                 increment of the check variable. */\r
171                 if( l4 != lAnswer )\r
172                 {\r
173                         vPrintDisplayMessage( &pcTaskFailMsg );\r
174                         sError = pdTRUE;\r
175                 }\r
176 \r
177                 if( sError == pdFALSE )\r
178                 {\r
179                         /* If the calculation has always been correct, increment the check\r
180                         variable so we know this task is still running okay. */\r
181                         ( *pusTaskCheckVariable )++;\r
182                 }\r
183         }\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static void vCompeteingIntMathTask3( void *pvParameters )\r
188 {\r
189 long *plArray, lTotal1, lTotal2;\r
190 short sError = pdFALSE;\r
191 volatile unsigned short *pusTaskCheckVariable;\r
192 const unsigned short usArraySize = ( unsigned short ) 250;\r
193 unsigned short usPosition;\r
194 const char * const pcTaskStartMsg = "Integer math task 3 started.\r\n";\r
195 const char * const pcTaskFailMsg = "Integer math task 3 failed.\r\n";\r
196 \r
197         /* Queue a message for printing to say the task has started. */\r
198         vPrintDisplayMessage( &pcTaskStartMsg );\r
199 \r
200         /* The variable this task increments to show it is still running is passed in\r
201         as the parameter. */\r
202         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
203 \r
204         /* Create the array we are going to use for our check calculation. */\r
205         plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );\r
206 \r
207         /* Keep filling the array, keeping a running total of the values placed in the\r
208         array.  Then run through the array adding up all the values.  If the two totals\r
209         do not match, stop the check variable from incrementing. */\r
210         for( ;; )\r
211         {\r
212                 lTotal1 = ( long ) 0;\r
213                 lTotal2 = ( long ) 0;\r
214 \r
215                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
216                 {\r
217                         plArray[ usPosition ] = ( long ) usPosition + ( long ) 5;\r
218                         lTotal1 += ( long ) usPosition + ( long ) 5;\r
219                 }\r
220 \r
221                 taskYIELD();\r
222 \r
223                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
224                 {\r
225                         lTotal2 += plArray[ usPosition ];\r
226                 }\r
227 \r
228                 if( lTotal1 != lTotal2 )\r
229                 {\r
230                         vPrintDisplayMessage( &pcTaskFailMsg );\r
231                         sError = pdTRUE;\r
232                 }\r
233 \r
234                 taskYIELD();\r
235 \r
236                 if( sError == pdFALSE )\r
237                 {\r
238                         /* If the calculation has always been correct, increment the check\r
239                         variable so we know     this task is still running okay. */\r
240                         ( *pusTaskCheckVariable )++;\r
241                 }\r
242         }\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 static void vCompeteingIntMathTask4( void *pvParameters )\r
247 {\r
248 long *plArray, lTotal1, lTotal2;\r
249 short sError = pdFALSE;\r
250 volatile unsigned short *pusTaskCheckVariable;\r
251 const unsigned short usArraySize = 250;\r
252 unsigned short usPosition;\r
253 const char * const pcTaskStartMsg = "Integer math task 4 started.\r\n";\r
254 const char * const pcTaskFailMsg = "Integer math task 4 failed.\r\n";\r
255 \r
256         /* Queue a message for printing to say the task has started. */\r
257         vPrintDisplayMessage( &pcTaskStartMsg );\r
258 \r
259         /* The variable this task increments to show it is still running is passed in\r
260         as the parameter. */\r
261         pusTaskCheckVariable = ( unsigned short * ) pvParameters;\r
262 \r
263         /* Create the array we are going to use for our check calculation. */\r
264         plArray = ( long * ) pvPortMalloc( ( size_t ) 250 * sizeof( long ) );\r
265 \r
266         /* Keep filling the array, keeping a running total of the values placed in the \r
267         array.  Then run through the array adding up all the values.  If the two totals \r
268         do not match, stop the check variable from incrementing. */\r
269         for( ;; )\r
270         {\r
271                 lTotal1 = ( long ) 0;\r
272                 lTotal2 = ( long ) 0;\r
273 \r
274                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
275                 {\r
276                         plArray[ usPosition ] = ( long ) usPosition * ( long ) 12;\r
277                         lTotal1 += ( long ) usPosition * ( long ) 12;   \r
278                 }\r
279 \r
280                 taskYIELD();\r
281         \r
282                 for( usPosition = 0; usPosition < usArraySize; usPosition++ )\r
283                 {\r
284                         lTotal2 += plArray[ usPosition ];\r
285                 }\r
286 \r
287 \r
288                 if( lTotal1 != lTotal2 )\r
289                 {\r
290                         vPrintDisplayMessage( &pcTaskFailMsg );\r
291                         sError = pdTRUE;\r
292                 }\r
293 \r
294                 taskYIELD();\r
295 \r
296                 if( sError == pdFALSE )\r
297                 {\r
298                         /* If the calculation has always been correct, increment the check \r
299                         variable so we know     this task is still running okay. */\r
300                         ( *pusTaskCheckVariable )++;\r
301                 }\r
302         }\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 /* This is called to check that all the created tasks are still running. */\r
307 portBASE_TYPE xAreIntegerMathsTaskStillRunning( void )\r
308 {\r
309 /* Keep a history of the check variables so we know if they have been incremented \r
310 since the last call. */\r
311 static unsigned short usLastTaskCheck[ intgNUMBER_OF_TASKS ] = { ( unsigned short ) 0 };\r
312 portBASE_TYPE xReturn = pdTRUE, xTask;\r
313 \r
314         /* Check the maths tasks are still running by ensuring their check variables \r
315         are still incrementing. */\r
316         for( xTask = 0; xTask < intgNUMBER_OF_TASKS; xTask++ )\r
317         {\r
318                 if( usTaskCheck[ xTask ] == usLastTaskCheck[ xTask ] )\r
319                 {\r
320                         /* The check has not incremented so an error exists. */\r
321                         xReturn = pdFALSE;\r
322                 }\r
323 \r
324                 usLastTaskCheck[ xTask ] = usTaskCheck[ xTask ];\r
325         }\r
326 \r
327         return xReturn;\r
328 }\r