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