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