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