]> git.sur5r.net Git - freertos/blob - Demo/MicroBlaze/main.c
Update to V4.1.0.
[freertos] / Demo / MicroBlaze / main.c
1 /*\r
2         FreeRTOS.org V4.1.0 - Copyright (C) 2003-2006 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         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 /*\r
34  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
35  * documentation provides more details of the standard demo application tasks.\r
36  *\r
37  * In addition to the standard tasks, main() creates two "Register Check" \r
38  * tasks.  These tasks write known values into every general purpose register,\r
39  * then check each register to ensure it still contains the expected (written)\r
40  * value.  The register check tasks operate at the idle priority so will get\r
41  * repeatedly preempted.  A register being found to contain an incorrect value\r
42  * following such a preemption would be indicative of an error in the context\r
43  * switch mechanism.\r
44  * \r
45  * Main.c also creates a task called "Check".  This only executes every three \r
46  * seconds but has the highest priority so is guaranteed to get processor time.  \r
47  * Its main function is to check that all the other tasks are still operational.\r
48  * Each task (other than the "flash" tasks) maintains a unique count that is \r
49  * incremented each time the task successfully completes its function.  Should \r
50  * any error occur within such a task the count is permanently halted.  The \r
51  * check task inspects the count of each task to ensure it has changed since\r
52  * the last time the check task executed.  If all the count variables have \r
53  * changed all the tasks are still executing error free, and the check task\r
54  * toggles the onboard LED.  Should any task contain an error at any time \r
55  * the LED toggle rate will change from 3 seconds to 500ms.\r
56  *\r
57  */\r
58 \r
59 /* Scheduler includes. */\r
60 #include "FreeRTOS.h"\r
61 #include "task.h"\r
62 \r
63 /* Demo application includes. */
64 #include "ParTest.h"\r
65 #include "flash.h"\r
66 #include "comtest2.h"\r
67 #include "integer.h"\r
68 #include "semtest.h"\r
69 #include "BlockQ.h"\r
70 #include "dynamic.h"\r
71 #include "PollQ.h"\r
72 \r
73 /* Hardware library includes. */\r
74 #include <xintc.h>\r
75 \r
76 /* The rate at which the 'check' LED will flash when no errors have been\r
77 detected. */\r
78 #define mainNO_ERROR_CHECK_PERIOD       3000\r
79 \r
80 /* The rate at which the 'check' LED will flash when an error has been\r
81 detected in one of the demo tasks. */\r
82 #define mainERROR_CHECK_PERIOD          500\r
83 \r
84 /* Demo application task priorities. */\r
85 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
86 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
87 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
88 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
89 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
90 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
91 \r
92 /* Software cannot influence the BAUD rate used by the simple UART \r
93 implementation. */\r
94 #define mainBAUD_RATE                           0\r
95 \r
96 /* The LED flashed by the 'check' task to indicate the system status. */\r
97 #define mainCHECK_TASK_LED                      3\r
98 \r
99 /* The first LED flashed by the COM port test tasks.  LED mainCOM_TEST_LED + 1\r
100 will also be used. */\r
101 #define mainCOM_TEST_LED                        4\r
102 \r
103 /* The register test task does not make any function calls so does not require\r
104 much stack at all. */\r
105 #define mainTINY_STACK                          70\r
106 \r
107 /*\r
108  * The task that executes at the highest priority and calls \r
109  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
110  * of the file.\r
111  */\r
112 static void vErrorChecks( void *pvParameters );\r
113 \r
114 /*\r
115  * Checks that all the demo application tasks are still executing without error\r
116  * - as described at the top of the file.\r
117  */\r
118 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );\r
119 \r
120 /*\r
121  * The register test task as described at the top of this file.\r
122  */\r
123 static void vRegisterTest( void *pvParameters );\r
124 \r
125 /*\r
126  * Perform any necessary hardware configuration.\r
127  */\r
128 static void prvSetupHardware( void );\r
129 \r
130 /* Set to pdFAIL should an error be discovered in the register test tasks. */\r
131 static unsigned portLONG ulRegisterTestStatus = pdPASS;\r
132 const unsigned portLONG *pulStatusAddr = &ulRegisterTestStatus;\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /*\r
137  * Create all the demo tasks - then start the scheduler.\r
138  */\r
139 int main (void) \r
140 {\r
141         /* When re-starting a debug session (rather than cold booting) we want\r
142         to ensure the installed interrupt handlers do not execute until after the\r
143         scheduler has been started. */\r
144         portDISABLE_INTERRUPTS();\r
145 \r
146         prvSetupHardware();\r
147 \r
148         /* Start the standard demo application tasks. */\r
149         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
150         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainBAUD_RATE, mainCOM_TEST_LED );\r
151         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
152         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
153         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
154         vStartDynamicPriorityTasks();\r
155         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
156         \r
157         /* Create two register check tasks - using a different parameter for each.\r
158         The parameter is used to generate the known values written to the registers. */\r
159         #if configUSE_PREEMPTION == 1\r
160                 xTaskCreate( vRegisterTest, "Reg1", mainTINY_STACK, ( void * ) 10, tskIDLE_PRIORITY, NULL );\r
161                 xTaskCreate( vRegisterTest, "Reg2", mainTINY_STACK, ( void * ) 20, tskIDLE_PRIORITY, NULL );\r
162         #endif\r
163 \r
164         /* Create the 'check' task that is defined in this file. */\r
165         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
166 \r
167         /* Finally start the scheduler. */\r
168         vTaskStartScheduler();\r
169 \r
170         /* Should not get here as the processor is now under control of the \r
171         scheduler! */\r
172 \r
173         return 0;
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 static void vErrorChecks( void *pvParameters )\r
178 {\r
179 portTickType xDelayPeriod = mainNO_ERROR_CHECK_PERIOD;\r
180 \r
181         /* The parameters are not used. */\r
182         ( void ) pvParameters;\r
183 \r
184         /* Cycle for ever, delaying then checking all the other tasks are still\r
185         operating without error.  The delay period used will depend on whether\r
186         or not an error has been discovered in one of the demo tasks. */\r
187         for( ;; )\r
188         {\r
189                 vTaskDelay( xDelayPeriod );\r
190                 if( !prvCheckOtherTasksAreStillRunning() )\r
191                 {\r
192                         /* An error has been found.  Shorten the delay period to make\r
193                         the LED flash faster. */\r
194                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
195                 }\r
196 \r
197                 vParTestToggleLED( mainCHECK_TASK_LED );\r
198         }\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )\r
203 {\r
204 static portBASE_TYPE xAllTestsPass = pdTRUE;\r
205 \r
206         /* Return pdFALSE if any demo application task set has encountered\r
207         an error. */\r
208 \r
209         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
210         {\r
211                 xAllTestsPass = pdFALSE;\r
212         }\r
213 \r
214         if( xAreComTestTasksStillRunning() != pdTRUE )\r
215         {\r
216                 xAllTestsPass = pdFALSE;\r
217         }\r
218 \r
219         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
220         {\r
221                 xAllTestsPass = pdFALSE;\r
222         }\r
223 \r
224         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
225         {\r
226                 xAllTestsPass = pdFAIL;\r
227         }\r
228 \r
229         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
230         {\r
231                 xAllTestsPass = ( portLONG ) pdFAIL;\r
232         }\r
233 \r
234         if( xArePollingQueuesStillRunning() != pdTRUE )\r
235         {\r
236                 xAllTestsPass = ( portLONG ) pdFAIL;\r
237         }\r
238 \r
239         /* Mutual exclusion on this variable is not necessary as we only read it. */\r
240         if( ulRegisterTestStatus != pdPASS )\r
241         {\r
242                 xAllTestsPass = pdFALSE;\r
243         }\r
244 \r
245         return xAllTestsPass;\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 static void prvSetupHardware( void )\r
250 {\r
251         /* Ensure the interrupt controller is enabled in order that subsequent \r
252         code can successfully configure the peripherals. */\r
253         XIntc_mMasterEnable( XPAR_OPB_INTC_0_BASEADDR );\r
254 \r
255         /* Initialise the GPIO used for the LED's. */\r
256         vParTestInitialise();\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 static void vRegisterTest( void *pvParameters )\r
261 {\r
262         for( ;; )\r
263         {\r
264                 /* Fill the registers with their register number plus the offset \r
265                 (added) value.  The added value is passed in as a parameter so\r
266                 is contained in r5. */\r
267                 asm volatile (  "addi r3, r5, 3         \n\t" \\r
268                                                 "addi r4, r5, 4         \n\t" \\r
269                                                 "addi r6, r5, 6         \n\t" \\r
270                                                 "addi r7, r5, 7         \n\t" \\r
271                                                 "addi r8, r5, 8         \n\t" \\r
272                                                 "addi r9, r5, 9         \n\t" \\r
273                                                 "addi r10, r5, 10       \n\t" \\r
274                                                 "addi r11, r5, 11       \n\t" \\r
275                                                 "addi r12, r5, 12       \n\t" \\r
276                                                 "addi r16, r5, 16       \n\t" \\r
277                                                 "addi r17, r5, 17       \n\t" \\r
278                                                 "addi r18, r5, 18       \n\t" \\r
279                                                 "addi r19, r5, 19       \n\t" \\r
280                                                 "addi r20, r5, 20       \n\t" \\r
281                                                 "addi r21, r5, 21       \n\t" \\r
282                                                 "addi r22, r5, 22       \n\t" \\r
283                                                 "addi r23, r5, 23       \n\t" \\r
284                                                 "addi r24, r5, 24       \n\t" \\r
285                                                 "addi r25, r5, 25       \n\t" \\r
286                                                 "addi r26, r5, 26       \n\t" \\r
287                                                 "addi r27, r5, 27       \n\t" \\r
288                                                 "addi r28, r5, 28       \n\t" \\r
289                                                 "addi r29, r5, 29       \n\t" \\r
290                                                 "addi r30, r5, 30       \n\t" \\r
291                                                 "addi r31, r5, 31       \n\t"\r
292                                         );\r
293 \r
294                 /* Now read back the register values to ensure they are as we expect. \r
295                 This task will get preempted frequently so other tasks are likely to\r
296                 have executed since the register values were written. */\r
297 \r
298                 /* r3 should contain r5 + 3.  Subtract 3 to leave r3 equal to r5. */\r
299                 asm volatile (  "addi r3, r3, -3 " );\r
300 \r
301                 /* Compare r3 and r5.  If they are not equal then either r3 or r5\r
302                 contains the wrong value and *pulStatusAddr is to pdFAIL. */\r
303                 asm volatile (  "cmp r3, r3, r5                         \n\t" \\r
304                                                 "beqi r3, 12                            \n\t" \\r
305                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
306                                                 "sw     r0, r0, r3                              \n\t" \r
307                                          );\r
308 \r
309                 /* Repeat for all the other registers. */\r
310                 asm volatile (  "addi r4, r4, -4                        \n\t" \\r
311                                                 "cmp r4, r4, r5                         \n\t" \\r
312                                                 "beqi r4, 12                            \n\t" \\r
313                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
314                                                 "sw     r0, r0, r3                              \n\t" \\r
315                                                 "addi r6, r6, -6                        \n\t" \\r
316                                                 "cmp r6, r6, r5                         \n\t" \\r
317                                                 "beqi r6, 12                            \n\t" \\r
318                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
319                                                 "sw     r0, r0, r3                              \n\t" \\r
320                                                 "addi r7, r7, -7                        \n\t" \\r
321                                                 "cmp r7, r7, r5                         \n\t" \\r
322                                                 "beqi r7, 12                            \n\t" \\r
323                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
324                                                 "sw     r0, r0, r3                              \n\t" \\r
325                                                 "addi r8, r8, -8                        \n\t" \\r
326                                                 "cmp r8, r8, r5                         \n\t" \\r
327                                                 "beqi r8, 12                            \n\t" \\r
328                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
329                                                 "sw     r0, r0, r3                              \n\t" \\r
330                                                 "addi r9, r9, -9                        \n\t" \\r
331                                                 "cmp r9, r9, r5                         \n\t" \\r
332                                                 "beqi r9, 12                            \n\t" \\r
333                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
334                                                 "sw     r0, r0, r3                              \n\t" \\r
335                                                 "addi r10, r10, -10                     \n\t" \\r
336                                                 "cmp r10, r10, r5                       \n\t" \\r
337                                                 "beqi r10, 12                           \n\t" \\r
338                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
339                                                 "sw     r0, r0, r3                              \n\t" \\r
340                                                 "addi r11, r11, -11                     \n\t" \\r
341                                                 "cmp r11, r11, r5                       \n\t" \\r
342                                                 "beqi r11, 12                           \n\t" \\r
343                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
344                                                 "sw     r0, r0, r3                              \n\t" \\r
345                                                 "addi r12, r12, -12                     \n\t" \\r
346                                                 "cmp r12, r12, r5                       \n\t" \\r
347                                                 "beqi r12, 12                           \n\t" \\r
348                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
349                                                 "sw     r0, r0, r3                              \n\t" \\r
350                                                 "sw     r0, r0, r3                              \n\t" \\r
351                                                 "addi r16, r16, -16                     \n\t" \\r
352                                                 "cmp r16, r16, r5                       \n\t" \\r
353                                                 "beqi r16, 12                           \n\t" \\r
354                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
355                                                 "sw     r0, r0, r3                              \n\t" \\r
356                                                 "addi r17, r17, -17                     \n\t" \\r
357                                                 "cmp r17, r17, r5                       \n\t" \\r
358                                                 "beqi r17, 12                           \n\t" \\r
359                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
360                                                 "sw     r0, r0, r3                              \n\t" \\r
361                                                 "addi r18, r18, -18                     \n\t" \\r
362                                                 "cmp r18, r18, r5                       \n\t" \\r
363                                                 "beqi r18, 12                           \n\t" \\r
364                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
365                                                 "sw     r0, r0, r3                              \n\t" \\r
366                                                 "addi r19, r19, -19                     \n\t" \\r
367                                                 "cmp r19, r19, r5                       \n\t" \\r
368                                                 "beqi r19, 12                           \n\t" \\r
369                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
370                                                 "sw     r0, r0, r3                              \n\t" \\r
371                                                 "addi r20, r20, -20                     \n\t" \\r
372                                                 "cmp r20, r20, r5                       \n\t" \\r
373                                                 "beqi r20, 12                           \n\t" \\r
374                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
375                                                 "sw     r0, r0, r3                              \n\t" \\r
376                                                 "addi r21, r21, -21                     \n\t" \\r
377                                                 "cmp r21, r21, r5                       \n\t" \\r
378                                                 "beqi r21, 12                           \n\t" \\r
379                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
380                                                 "sw     r0, r0, r3                              \n\t" \\r
381                                                 "addi r22, r22, -22                     \n\t" \\r
382                                                 "cmp r22, r22, r5                       \n\t" \\r
383                                                 "beqi r22, 12                           \n\t" \\r
384                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
385                                                 "sw     r0, r0, r3                              \n\t" \\r
386                                                 "addi r23, r23, -23                     \n\t" \\r
387                                                 "cmp r23, r23, r5                       \n\t" \\r
388                                                 "beqi r23, 12                           \n\t" \\r
389                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
390                                                 "sw     r0, r0, r3                              \n\t" \\r
391                                                 "addi r24, r24, -24                     \n\t" \\r
392                                                 "cmp r24, r24, r5                       \n\t" \\r
393                                                 "beqi r24, 12                           \n\t" \\r
394                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
395                                                 "sw     r0, r0, r3                              \n\t" \\r
396                                                 "addi r25, r25, -25                     \n\t" \\r
397                                                 "cmp r25, r25, r5                       \n\t" \\r
398                                                 "beqi r25, 12                           \n\t" \\r
399                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
400                                                 "sw     r0, r0, r3                              \n\t" \\r
401                                                 "addi r26, r26, -26                     \n\t" \\r
402                                                 "cmp r26, r26, r5                       \n\t" \\r
403                                                 "beqi r26, 12                           \n\t" \\r
404                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
405                                                 "sw     r0, r0, r3                              \n\t" \\r
406                                                 "addi r27, r27, -27                     \n\t" \\r
407                                                 "cmp r27, r27, r5                       \n\t" \\r
408                                                 "beqi r27, 12                           \n\t" \\r
409                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
410                                                 "sw     r0, r0, r3                              \n\t" \\r
411                                                 "addi r28, r28, -28                     \n\t" \\r
412                                                 "cmp r28, r28, r5                       \n\t" \\r
413                                                 "beqi r28, 12                           \n\t" \\r
414                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
415                                                 "sw     r0, r0, r3                              \n\t" \\r
416                                                 "addi r29, r29, -29                     \n\t" \\r
417                                                 "cmp r29, r29, r5                       \n\t" \\r
418                                                 "beqi r29, 12                           \n\t" \\r
419                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
420                                                 "sw     r0, r0, r3                              \n\t" \\r
421                                                 "addi r30, r30, -30                     \n\t" \\r
422                                                 "cmp r30, r30, r5                       \n\t" \\r
423                                                 "beqi r30, 12                           \n\t" \\r
424                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
425                                                 "sw     r0, r0, r3                              \n\t" \\r
426                                                 "addi r31, r31, -31                     \n\t" \\r
427                                                 "cmp r31, r31, r5                       \n\t" \\r
428                                                 "beqi r31, 12                           \n\t" \\r
429                                                 "lwi r3, r0, pulStatusAddr      \n\t" \\r
430                                                 "sw     r0, r0, r3                              \n\t"\r
431                                         );\r
432         }\r
433 }\r
434 \r
435 \r
436 \r