]> git.sur5r.net Git - freertos/blob - Demo/MB91460_Softune/SRC/main.c
Continue to work on Fujitsu 32bit port.
[freertos] / Demo / MB91460_Softune / SRC / main.c
1 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */\r
2 /* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */\r
3 /* ELIGIBILITY FOR ANY PURPOSES.                                                                                         */\r
4 /*                               (C) Fujitsu Microelectronics Europe GmbH                                 */\r
5 /*------------------------------------------------------------------------\r
6   MAIN.C\r
7   - description\r
8   - See README.TXT for project description and disclaimer.\r
9 -------------------------------------------------------------------------*/\r
10 \r
11 \r
12 /*\r
13  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
14  * documentation provides more details of the demo application tasks.\r
15  * \r
16  * Main.c also creates a task called "Check".  This only executes every three \r
17  * seconds but has the highest priority so is guaranteed to get processor time.  \r
18  * Its main function is to check that all the other tasks are still operational.\r
19  * Each task (other than the "flash" tasks) maintains a unique count that is \r
20  * incremented each time the task successfully completes its function.  Should \r
21  * any error occur within such a task the count is permanently halted.  The \r
22  * check task inspects the count of each task to ensure it has changed since\r
23  * the last time the check task executed.  If all the count variables have \r
24  * changed all the tasks are still executing error free, and the check task\r
25  * toggles the onboard LED.  Should any task contain an error at any time \r
26  * the LED toggle rate will change from 3 seconds to 500ms.\r
27  *\r
28  */\r
29 \r
30 \r
31 /* Hardware specific includes. */\r
32 #include "mb91467d.h"\r
33 #include "vectors.h"\r
34 #include "watchdog.h"\r
35 \r
36 /* Scheduler includes. */\r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 \r
40 /* Demo app includes. */\r
41 #include "flash.h"\r
42 #include "integer.h"\r
43 #include "comtest2.h"\r
44 #include "PollQ.h"\r
45 #include "semtest.h"\r
46 #include "BlockQ.h"\r
47 #include "dynamic.h"\r
48 #include "flop.h"\r
49 #include "GenQTest.h"\r
50 #include "QPeek.h"\r
51 #include "BlockTim.h"\r
52 #include "death.h"\r
53 #include "taskutility.h"\r
54 #include "partest.h"\r
55         \r
56 /* Demo task priorities. */\r
57 #define mainWATCHDOG_TASK_PRIORITY              ( tskIDLE_PRIORITY + 5 )\r
58 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 4 )\r
59 #define mainUTILITY_TASK_PRIORITY               ( tskIDLE_PRIORITY + 3 )\r
60 #define mainSEM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 3 )\r
61 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
62 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
63 #define mainQUEUE_BLOCK_PRIORITY                ( tskIDLE_PRIORITY + 2 )\r
64 #define mainDEATH_PRIORITY                              ( tskIDLE_PRIORITY + 1 )\r
65 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
66 #define mainGENERIC_QUEUE_PRIORITY              ( tskIDLE_PRIORITY )\r
67 \r
68 /* Baud rate used by the COM test tasks. */\r
69 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned portLONG ) 19200 )\r
70 \r
71 /* The frequency at which the 'Check' tasks executes.  See the comments at the \r
72 top of the page.  When the system is operating error free the 'Check' task\r
73 toggles an LED every three seconds.  If an error is discovered in any task the\r
74 rate is increased to 500 milliseconds.  [in this case the '*' characters on the \r
75 LCD represent LED's]*/\r
76 #define mainNO_ERROR_CHECK_DELAY                ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
77 #define mainERROR_CHECK_DELAY                   ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
78 \r
79 /* The total number of LEDs available. */\r
80 #define mainNO_CO_ROUTINE_LEDs  ( 8 )\r
81 \r
82 /* The first LED used by the comtest tasks. */\r
83 #define mainCOM_TEST_LED                ( 0x05 )\r
84 \r
85 /* The LED used by the check task. */\r
86 #define mainCHECK_TEST_LED              ( 0x07 )\r
87 \r
88 /* The number of interrupt levels to use. */\r
89 #define mainINTERRUPT_LEVELS    ( 31 )\r
90 \r
91 /*---------------------------------------------------------------------------*/\r
92 \r
93 /* \r
94  * The function that implements the Check task.  See the comments at the head\r
95  * of the page for implementation details.\r
96  */ \r
97 static void vErrorChecks( void *pvParameters );\r
98 \r
99 /*\r
100  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
101  * to be operating without error - otherwise returns pdFAIL.\r
102  */\r
103 static portSHORT prvCheckOtherTasksAreStillRunning( void );\r
104 \r
105 /* \r
106  * Setup the microcontroller as used by this demo. \r
107  */\r
108 static void prvSetupHardware( void );\r
109 \r
110 /*\r
111  * Tasks that test the context switch mechanism by filling the CPU registers\r
112  * with known values then checking that each register contains the value\r
113  * expected.  Each of the two tasks use different values, and as low priority\r
114  * tasks, get swapped in and out regularly.\r
115  */\r
116 static void vFirstRegisterTestTask( void *pvParameters );\r
117 static void vSecondRegisterTestTask( void *pvParameters );\r
118 \r
119 /*---------------------------------------------------------------------------*/\r
120 \r
121 /* The variable that is set to true should an error be found in one of the \r
122 register test tasks. */\r
123 unsigned portLONG ulRegTestError = pdFALSE;\r
124 \r
125 /* Variables used to ensure the register check tasks are still executing. */\r
126 static volatile unsigned portLONG ulRegTest1Counter = 0UL, ulRegTest2Counter = 0UL;\r
127 \r
128 /*---------------------------------------------------------------------------*/\r
129 \r
130 /* Start all the demo application tasks, then start the scheduler. */\r
131 void main(void)\r
132 {\r
133         /* Initialise the hardware ready for the demo. */       \r
134         prvSetupHardware();\r
135 \r
136         /* Start the standard demo application tasks. */\r
137         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );   \r
138         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
139         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
140         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
141         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
142         vStartBlockingQueueTasks ( mainQUEUE_BLOCK_PRIORITY );  \r
143         vStartDynamicPriorityTasks();   \r
144         vStartMathTasks( tskIDLE_PRIORITY );    \r
145         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
146         vStartQueuePeekTasks();\r
147         vCreateBlockTimeTasks();\r
148 \r
149         /* Start the 'Check' task which is defined in this file. */\r
150         xTaskCreate( vErrorChecks, ( signed portCHAR * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );      \r
151 \r
152         xTaskCreate( vFirstRegisterTestTask, ( signed portCHAR * ) "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
153         xTaskCreate( vSecondRegisterTestTask, ( signed portCHAR * ) "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
154 \r
155         /* Start the task that write trace information to the UART. */  \r
156         vUtilityStartTraceTask( mainUTILITY_TASK_PRIORITY );\r
157 \r
158         /* If we are going to service the watchdog from within a task, then create\r
159         the task here. */       \r
160         #if WATCHDOG == WTC_IN_TASK     \r
161                 vStartWatchdogTask( mainWATCHDOG_TASK_PRIORITY );\r
162         #endif          \r
163         \r
164         /* The suicide tasks must be started last as they record the number of other\r
165         tasks that exist within the system.  The value is then used to ensure at run\r
166         time the number of tasks that exists is within expected bounds. */\r
167         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
168 \r
169         /* Now start the scheduler.  Following this call the created tasks should\r
170         be executing. */        \r
171         vTaskStartScheduler( );\r
172         \r
173         /* vTaskStartScheduler() will only return if an error occurs while the \r
174         idle task is being created. */\r
175         for( ;; );\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static void vErrorChecks( void *pvParameters )\r
180 {\r
181 portTickType xDelayPeriod = mainNO_ERROR_CHECK_DELAY, xLastExecutionTime;\r
182 \r
183         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
184         works correctly. */\r
185         xLastExecutionTime = xTaskGetTickCount();\r
186 \r
187         /* Cycle for ever, delaying then checking all the other tasks are still\r
188         operating without error. */\r
189         for( ;; )\r
190         {\r
191                 /* Wait until it is time to check again.  The time we wait here depends\r
192                 on whether an error has been detected or not.  When an error is \r
193                 detected the time is shortened resulting in a faster LED flash rate. */\r
194                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
195                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
196 \r
197                 /* See if the other tasks are all ok. */\r
198                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
199                 {\r
200                         /* An error occurred in one of the tasks so shorten the delay \r
201                         period - which has the effect of increasing the frequency of the\r
202                         LED toggle. */\r
203                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
204                 }\r
205 \r
206                 /* Flash! */\r
207                 vParTestToggleLED( mainCHECK_TEST_LED );\r
208         }\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static portSHORT prvCheckOtherTasksAreStillRunning( void )\r
213 {\r
214 portBASE_TYPE lReturn = pdPASS;\r
215 static unsigned portLONG ulLastRegTest1Counter = 0UL, ulLastRegTest2Counter = 0UL;\r
216 \r
217         /* The demo tasks maintain a count that increments every cycle of the task\r
218         provided that the task has never encountered an error.  This function \r
219         checks the counts maintained by the tasks to ensure they are still being\r
220         incremented.  A count remaining at the same value between calls therefore\r
221         indicates that an error has been detected. */\r
222 \r
223         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
224         {\r
225                 lReturn = pdFAIL;\r
226         }\r
227 \r
228         if( xArePollingQueuesStillRunning() != pdTRUE )\r
229         {\r
230                 lReturn = pdFAIL;\r
231         }\r
232 \r
233         if( xAreComTestTasksStillRunning() != pdTRUE )\r
234         {\r
235                 lReturn = pdFAIL;\r
236         }\r
237         \r
238         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
239         {\r
240                 lReturn = pdFAIL;\r
241         }\r
242         \r
243         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
244         {\r
245                 lReturn = pdFAIL;\r
246         }\r
247         \r
248         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
249         {\r
250                 lReturn = pdFAIL;\r
251         }\r
252         \r
253         if( xAreMathsTaskStillRunning() != pdTRUE )\r
254         {\r
255                 lReturn = pdFAIL;\r
256         }\r
257         \r
258         if( xIsCreateTaskStillRunning() != pdTRUE )\r
259         {\r
260                 lReturn = pdFAIL;\r
261         }\r
262         \r
263         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
264         {\r
265                 lReturn = pdFAIL;\r
266         }\r
267         \r
268         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
269         {\r
270                 lReturn = pdFAIL;\r
271         }\r
272         \r
273         if ( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
274         {\r
275                 lReturn = pdFAIL;\r
276         }\r
277 \r
278         /* Have the register test tasks found any errors? */\r
279         if( ulRegTestError != pdFALSE )\r
280         {\r
281                 lReturn = pdFAIL;\r
282         }\r
283 \r
284         /* Are the register test tasks still running? */\r
285         if( ulLastRegTest1Counter == ulRegTest1Counter )\r
286         {\r
287                 lReturn = pdFAIL;\r
288         }\r
289         \r
290         if( ulLastRegTest2Counter == ulRegTest2Counter )\r
291         {\r
292                 lReturn = pdFAIL;\r
293         }\r
294 \r
295         ulLastRegTest1Counter = ulRegTest1Counter;\r
296         ulLastRegTest2Counter = ulRegTest2Counter;\r
297 \r
298         return lReturn;\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 static void prvSetupHardware( void )\r
303 {\r
304         /* Allow all interrupt levels. */\r
305         __set_il( mainINTERRUPT_LEVELS );\r
306 \r
307         /* Initialise interrupts. */\r
308         InitIrqLevels();\r
309 \r
310         /* Initialise the ports used by the LEDs. */\r
311         vParTestInitialise();\r
312 \r
313         /* If we are going to use the watchdog, then initialise it now. */\r
314         #if WATCHDOG != WTC_NONE        \r
315                 InitWatchdog();\r
316         #endif\r
317 }\r
318 /*-----------------------------------------------------------*/\r
319 \r
320 /* The below callback function is called from Delayed ISR if configUSE_IDLE_HOOK \r
321 is configured as 1. */  \r
322 #if configUSE_IDLE_HOOK == 1\r
323         void vApplicationIdleHook( void )\r
324         {\r
325                 /* Are we using the idle task to kick the watchdog? */\r
326                 #if WATCHDOG == WTC_IN_IDLE\r
327                         Kick_Watchdog();\r
328                 #endif\r
329 \r
330                 #if configUSE_CO_ROUTINES == 1          \r
331                         vCoRoutineSchedule();\r
332                 #endif\r
333         }\r
334 #endif\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 /*\r
338 The below callback function is called from Tick ISR if configUSE_TICK_HOOK \r
339 is configured as 1. */  \r
340 #if configUSE_TICK_HOOK == 1\r
341         void vApplicationTickHook( void )\r
342         {\r
343                 #if WATCHDOG == WTC_IN_TICK\r
344                         Kick_Watchdog();\r
345                 #endif\r
346         }\r
347 #endif\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 static void vFirstRegisterTestTask( void *pvParameters )\r
351 {\r
352 extern volatile unsigned portLONG ulCriticalNesting;\r
353 \r
354         /* Fills the registers with known values (different to the values\r
355         used in vSecondRegisterTestTask()), then checks that the registers still\r
356         all contain the expected value.  This is done to test the context save\r
357         and restore mechanism as this task is swapped onto and off of the CPU.\r
358 \r
359         The critical nesting depth is also saved as part of the context so also\r
360         check this maintains an expected value. */\r
361         ulCriticalNesting = 0x12345678;\r
362 \r
363         for( ;; )\r
364         {\r
365                 #pragma asm\r
366                         ;Load known values into each register.\r
367                         LDI     #0x11111111, R0\r
368                         LDI     #0x22222222, R1\r
369                         LDI     #0x33333333, R2\r
370                         LDI #0x44444444, R3\r
371                         LDI     #0x55555555, R4\r
372                         LDI     #0x66666666, R5\r
373                         LDI     #0x77777777, R6\r
374                         LDI     #0x88888888, R7\r
375                         LDI     #0x99999999, R8\r
376                         LDI     #0xaaaaaaaa, R9\r
377                         LDI     #0xbbbbbbbb, R10\r
378                         LDI     #0xcccccccc, R11\r
379                         LDI     #0xdddddddd, R12\r
380                         \r
381                         ;Check each register still contains the expected value.\r
382                         LDI #0x11111111, R13\r
383                         CMP R13, R0\r
384                         BNE First_Set_Error\r
385                         NOP\r
386 \r
387                         LDI #0x22222222, R13\r
388                         CMP R13, R1\r
389                         BNE First_Set_Error\r
390                         NOP\r
391 \r
392                         LDI #0x33333333, R13\r
393                         CMP R13, R2\r
394                         BNE First_Set_Error\r
395                         NOP\r
396 \r
397                         LDI #0x44444444, R13\r
398                         CMP R13, R3\r
399                         BNE First_Set_Error\r
400                         NOP\r
401 \r
402                         LDI #0x55555555, R13\r
403                         CMP R13, R4\r
404                         BNE First_Set_Error\r
405                         NOP\r
406 \r
407                         LDI #0x66666666, R13\r
408                         CMP R13, R5\r
409                         BNE First_Set_Error\r
410                         NOP\r
411 \r
412                         LDI #0x77777777, R13\r
413                         CMP R13, R6\r
414                         BNE First_Set_Error\r
415                         NOP\r
416 \r
417                         LDI #0x88888888, R13\r
418                         CMP R13, R7\r
419                         BNE First_Set_Error\r
420                         NOP\r
421 \r
422                         LDI #0x99999999, R13\r
423                         CMP R13, R8\r
424                         BNE First_Set_Error\r
425                         NOP\r
426 \r
427                         LDI #0xaaaaaaaa, R13\r
428                         CMP R13, R9\r
429                         BNE First_Set_Error\r
430                         NOP\r
431 \r
432                         LDI #0xbbbbbbbb, R13\r
433                         CMP R13, R10\r
434                         BNE First_Set_Error\r
435                         NOP\r
436 \r
437                         LDI #0xcccccccc, R13\r
438                         CMP R13, R11\r
439                         BNE First_Set_Error\r
440                         NOP\r
441 \r
442                         LDI #0xdddddddd, R13\r
443                         CMP R13, R12\r
444                         BNE First_Set_Error\r
445                         NOP\r
446 \r
447                         BRA First_Start_Next_Loop\r
448                         NOP\r
449 \r
450                 First_Set_Error:\r
451 \r
452                         ; Latch that an error has occurred.\r
453                         LDI #_ulRegTestError, R0                        \r
454                         LDI #0x00000001, R1\r
455                         ST R1, @R0\r
456 \r
457 \r
458                 First_Start_Next_Loop:\r
459 \r
460 \r
461                 #pragma endasm\r
462 \r
463                 ulRegTest1Counter++;\r
464 \r
465                 if( ulCriticalNesting != 0x12345678 )\r
466                 {\r
467                         ulRegTestError = pdTRUE;\r
468                 }\r
469         }\r
470 }\r
471 /*-----------------------------------------------------------*/\r
472 \r
473 static void vSecondRegisterTestTask( void *pvParameters )\r
474 {\r
475 extern volatile unsigned portLONG ulCriticalNesting;\r
476 \r
477         /* Fills the registers with known values (different to the values\r
478         used in vFirstRegisterTestTask()), then checks that the registers still\r
479         all contain the expected value.  This is done to test the context save\r
480         and restore mechanism as this task is swapped onto and off of the CPU.\r
481 \r
482         The critical nesting depth is also saved as part of the context so also\r
483         check this maintains an expected value. */\r
484         ulCriticalNesting = 0x87654321;\r
485 \r
486         for( ;; )\r
487         {\r
488                 #pragma asm\r
489                         ;Load known values into each register.\r
490                         LDI     #0x11111111, R1\r
491                         LDI     #0x22222222, R2\r
492                         LDI     #0x33333333, R3\r
493                         LDI #0x44444444, R4\r
494                         LDI     #0x55555555, R5\r
495                         LDI     #0x66666666, R6\r
496                         LDI     #0x77777777, R7\r
497                         LDI     #0x88888888, R8\r
498                         LDI     #0x99999999, R9\r
499                         LDI     #0xaaaaaaaa, R10\r
500                         LDI     #0xbbbbbbbb, R11\r
501                         LDI     #0xcccccccc, R12\r
502                         LDI     #0xdddddddd, R0\r
503                         \r
504                         ;Check each register still contains the expected value.\r
505                         LDI #0x11111111, R13\r
506                         CMP R13, R1\r
507                         BNE Second_Set_Error\r
508                         NOP\r
509 \r
510                         LDI #0x22222222, R13\r
511                         CMP R13, R2\r
512                         BNE Second_Set_Error\r
513                         NOP\r
514 \r
515                         LDI #0x33333333, R13\r
516                         CMP R13, R3\r
517                         BNE Second_Set_Error\r
518                         NOP\r
519 \r
520                         LDI #0x44444444, R13\r
521                         CMP R13, R4\r
522                         BNE Second_Set_Error\r
523                         NOP\r
524 \r
525                         LDI #0x55555555, R13\r
526                         CMP R13, R5\r
527                         BNE Second_Set_Error\r
528                         NOP\r
529 \r
530                         LDI #0x66666666, R13\r
531                         CMP R13, R6\r
532                         BNE Second_Set_Error\r
533                         NOP\r
534 \r
535                         LDI #0x77777777, R13\r
536                         CMP R13, R7\r
537                         BNE Second_Set_Error\r
538                         NOP\r
539 \r
540                         LDI #0x88888888, R13\r
541                         CMP R13, R8\r
542                         BNE Second_Set_Error\r
543                         NOP\r
544 \r
545                         LDI #0x99999999, R13\r
546                         CMP R13, R9\r
547                         BNE Second_Set_Error\r
548                         NOP\r
549 \r
550                         LDI #0xaaaaaaaa, R13\r
551                         CMP R13, R10\r
552                         BNE Second_Set_Error\r
553                         NOP\r
554 \r
555                         LDI #0xbbbbbbbb, R13\r
556                         CMP R13, R11\r
557                         BNE Second_Set_Error\r
558                         NOP\r
559 \r
560                         LDI #0xcccccccc, R13\r
561                         CMP R13, R12\r
562                         BNE Second_Set_Error\r
563                         NOP\r
564 \r
565                         LDI #0xdddddddd, R13\r
566                         CMP R13, R0\r
567                         BNE Second_Set_Error\r
568                         NOP\r
569 \r
570                         BRA Second_Start_Next_Loop\r
571                         NOP\r
572 \r
573                 Second_Set_Error:\r
574 \r
575                         ; Latch that an error has occurred.\r
576                         LDI #_ulRegTestError, R0                        \r
577                         LDI #0x00000001, R1\r
578                         ST R1, @R0\r
579 \r
580 \r
581                 Second_Start_Next_Loop:\r
582 \r
583 \r
584                 #pragma endasm\r
585 \r
586                 ulRegTest2Counter++;\r
587 \r
588                 if( ulCriticalNesting != 0x87654321 )\r
589                 {\r
590                         ulRegTestError = pdTRUE;\r
591                 }\r
592         }\r
593 }\r
594 /*-----------------------------------------------------------*/\r
595 \r
596 \r