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