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