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