]> git.sur5r.net Git - freertos/blob - Demo/SuperH_SH7216_Renesas/RTOSDemo/main.c
Update SuperH port to include WEB server.
[freertos] / Demo / SuperH_SH7216_Renesas / RTOSDemo / main.c
1 /*\r
2     FreeRTOS V6.0.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 \r
55 \r
56 /*\r
57  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
58  * documentation provides more details of the standard demo application tasks,\r
59  * which provide no particular functionality but do provide a good example of\r
60  * how to use the FreeRTOS API.  In addition to the standard demo tasks, the \r
61  * following tasks and tests are defined and/or created within this file:\r
62  *\r
63  * "Reg test" tasks - These fill the registers with known values, then check\r
64  * that each register still contains its expected value.  Each task uses\r
65  * different values.  The tasks run with very low priority so get preempted very\r
66  * frequently.  A register containing an unexpected value is indicative of an\r
67  * error in the context switching mechanism.  Both standard and floating point\r
68  * registers are checked.  The nature of the reg test tasks necessitates that\r
69  * they are written in assembly code.  They are defined in regtest.src.\r
70  *\r
71  * "math" tasks - These are a set of 8 tasks that perform various double\r
72  * precision floating point calculations in order to check that the tasks \r
73  * floating point registers are being correctly saved and restored during\r
74  * context switches.  The math tasks are defined in flop.c.\r
75  *\r
76  * "Check" task - This only executes every five seconds but has a high priority\r
77  * to ensure it gets processor time.  Its main function is to check that all the\r
78  * standard demo tasks are still operational.  While no errors have been\r
79  * discovered the check task will toggle an LED every 5 seconds - the toggle\r
80  * rate increasing to 500ms being a visual indication that at least one task has\r
81  * reported unexpected behaviour.\r
82  *\r
83  * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application\r
84  * tasks are executing as expected and no errors have been reported in any \r
85  * tasks.  The toggle rate increasing to 200ms indicates that at least one task\r
86  * has reported unexpected behaviour.\r
87  * \r
88  * *NOTE 2* This file and flop.c both demonstrate the use of \r
89  * xPortUsesFloatingPoint() which informs the kernel that a task should maintain\r
90  * a floating point context.\r
91  *\r
92  * *NOTE 3* vApplicationSetupTimerInterrupt() is called by the kernel to let\r
93  * the application set up a timer to generate the tick interrupt.  In this\r
94  * example a compare match timer is used for this purpose.  \r
95  * vApplicationTickHook() is used to clear the timer interrupt and relies on\r
96  * configUSE_TICK_HOOK being set to 1 in FreeRTOSConfig.h.\r
97  *\r
98  * *NOTE 4* The traceTASK_SWITCHED_IN and traceTASK_SWITCHED_OUT trace hooks\r
99  * are used to save and restore the floating point context respectively for\r
100  * those tasks that require it (those for which xPortUsesFloatingPoint() has\r
101  * been called).\r
102  * \r
103  * *NOTE 5* Any task that can cause a context switch requires an asm wrapper\r
104  * and must be assigned an interrupt priority of portKERNEL_INTERRUPT_PRIORITY.\r
105  */\r
106 \r
107 /* Kernel includes. */\r
108 #include "FreeRTOS.h"\r
109 #include "task.h"\r
110 \r
111 /* Demo application includes. */\r
112 #include "BlockQ.h"\r
113 #include "death.h"\r
114 #include "integer.h"\r
115 #include "blocktim.h"\r
116 #include "flash.h"\r
117 #include "partest.h"\r
118 #include "semtest.h"\r
119 #include "PollQ.h"\r
120 #include "GenQTest.h"\r
121 #include "QPeek.h"\r
122 #include "recmutex.h"\r
123 #include "flop.h"\r
124 \r
125 /* Constants required to configure the hardware. */\r
126 #define mainFRQCR_VALUE                                         ( 0x0303 )      /* Input = 12.5MHz, I Clock = 200MHz, B Clock = 50MHz, P Clock = 50MHz */\r
127 \r
128 /* Task priorities. */\r
129 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 1 )\r
130 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
131 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
132 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
133 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
134 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1 )\r
135 #define mainuIP_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
136 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
137 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
138 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
139 \r
140 #define mainuIP_STACK_SIZE                                      ( configMINIMAL_STACK_SIZE * 3 )\r
141 \r
142 /* The LED toggle by the check task. */\r
143 #define mainCHECK_LED                                           ( 5 )\r
144 \r
145 /* The rate at which mainCHECK_LED will toggle when all the tasks are running\r
146 without error. */\r
147 #define mainNO_ERROR_CYCLE_TIME                         ( 5000 / portTICK_RATE_MS )\r
148 \r
149 /* The rate at which mainCHECK_LED will toggle when an error has been reported\r
150 by at least one task. */\r
151 #define mainERROR_CYCLE_TIME                            ( 200 / portTICK_RATE_MS )\r
152 \r
153 /*\r
154  * vApplicationMallocFailedHook() will only be called if\r
155  * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
156  * function that will execute if a call to pvPortMalloc() fails.\r
157  * pvPortMalloc() is called internally by the kernel whenever a task, queue or\r
158  * semaphore is created.  It is also called by various parts of the demo\r
159  * application.  \r
160  */\r
161 void vApplicationMallocFailedHook( void );\r
162 \r
163 /*\r
164  * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1\r
165  * in FreeRTOSConfig.h.  It is a hook function that is called on each iteration\r
166  * of the idle task.  It is essential that code added to this hook function\r
167  * never attempts to block in any way (for example, call xQueueReceive() with\r
168  * a block time specified).  If the application makes use of the vTaskDelete()\r
169  * API function (as this demo application does) then it is also important that\r
170  * vApplicationIdleHook() is permitted to return to its calling function because\r
171  * it is the responsibility of the idle task to clean up memory allocated by the\r
172  * kernel to any task that has since been deleted.\r
173  */\r
174 void vApplicationIdleHook( void );\r
175 \r
176 /*\r
177  * Just sets up clocks, ports, etc. used by the demo application.\r
178  */\r
179 static void prvSetupHardware( void );\r
180 \r
181 /*\r
182  * The check task as described at the top of this file.\r
183  */\r
184 static void prvCheckTask( void *pvParameters );\r
185 \r
186 /*\r
187  * The reg test tasks as described at the top of this file.\r
188  */\r
189 extern void vRegTest1Task( void *pvParameters );\r
190 extern void vRegTest2Task( void *pvParameters );\r
191 \r
192 extern void vuIP_Task( void *pvParameters );\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /* Variables that are incremented on each iteration of the reg test tasks - \r
197 provided the tasks have not reported any errors.  The check task inspects these\r
198 variables to ensure they are still incrementing as expected. */\r
199 volatile unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;\r
200 \r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /*\r
204  * Creates the majority of the demo application tasks before starting the\r
205  * scheduler.\r
206  */\r
207 void main(void)\r
208 {\r
209 xTaskHandle xCreatedTask;\r
210 \r
211         prvSetupHardware();\r
212 \r
213         /* Start the reg test tasks which test the context switching mechanism. */\r
214         xTaskCreate( vRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );\r
215         xPortUsesFloatingPoint( xCreatedTask );\r
216         \r
217         xTaskCreate( vRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );\r
218         xPortUsesFloatingPoint( xCreatedTask );\r
219 \r
220         xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );\r
221 \r
222         /* Start the check task as described at the top of this file. */\r
223         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
224 \r
225         /* Start the standard demo tasks.  These don't perform any particular useful\r
226         functionality, other than to demonstrate the FreeRTOS API being used. */\r
227         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
228         vCreateBlockTimeTasks();\r
229     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
230     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
231     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
232     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
233         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
234     vStartQueuePeekTasks();\r
235         vStartRecursiveMutexTasks();\r
236         \r
237         /* Start the math tasks as described at the top of this file. */\r
238         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
239 \r
240         /* The suicide tasks must be created last as they need to know how many\r
241         tasks were running prior to their creation in order to ascertain whether\r
242         or not the correct/expected number of tasks are running at any given time. */\r
243     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
244 \r
245         /* Start the tasks running. */\r
246         vTaskStartScheduler();\r
247 \r
248         /* Will only get here if there was insufficient heap memory to create the idle\r
249     task.  Increase the configTOTAL_HEAP_SIZE setting in FreeRTOSConfig.h. */\r
250         for( ;; );\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 static void prvCheckTask( void *pvParameter )\r
255 {\r
256 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;\r
257 unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;\r
258 \r
259         /* Just to remove compiler warning. */\r
260         ( void ) pvParameter;\r
261 \r
262         /* Initialise xNextWakeTime - this only needs to be done once. */\r
263         xNextWakeTime = xTaskGetTickCount();\r
264 \r
265         for( ;; )\r
266         {\r
267                 /* Place this task in the blocked state until it is time to run again. */\r
268                 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
269                 \r
270                 /* Inspect all the other tasks to ensure none have experienced any errors. */\r
271                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
272                 {\r
273                         /* Increase the rate at which this task cycles, which will increase the\r
274                         rate at which mainCHECK_LED flashes to give visual feedback that an error\r
275                         has occurred. */\r
276                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
277                 }\r
278                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
279                 {\r
280                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
281                 }\r
282                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
283                 {\r
284                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
285                 }\r
286                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
287                 {\r
288                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
289                 }\r
290             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
291             {\r
292                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
293             }\r
294             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
295             {\r
296                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
297             }\r
298             else if( xIsCreateTaskStillRunning() != pdTRUE )\r
299             {\r
300                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
301             }\r
302             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
303             {\r
304                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
305             }\r
306             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
307             {\r
308                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
309             }\r
310                 else if( xAreMathsTaskStillRunning() != pdTRUE )\r
311                 {\r
312                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
313                 }\r
314 \r
315                 /* Check the reg test tasks are still cycling.  They will stop incrementing\r
316                 their loop counters if they encounter an error. */\r
317                 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )\r
318                 {\r
319                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
320                 }\r
321 \r
322                 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )\r
323                 {\r
324                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
325                 }\r
326                 \r
327                 ulLastRegTest1CycleCount = ulRegTest1CycleCount;\r
328                 ulLastRegTest2CycleCount = ulRegTest2CycleCount;\r
329                 \r
330                 /* Toggle the check LED to give an indication of the system status.  If the\r
331                 LED toggles every 5 seconds then everything is ok.  A faster toggle indicates\r
332                 an error. */\r
333                 vParTestToggleLED( mainCHECK_LED );\r
334         }\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 void vApplicationMallocFailedHook( void )\r
339 {\r
340         /* A call to vPortMalloc() failed, probably during the creation of a task,\r
341         queue or semaphore.  Inspect pxCurrentTCB to find which task is currently\r
342         executing. */\r
343         for( ;; );\r
344 }\r
345 /*-----------------------------------------------------------*/\r
346 \r
347 void vApplicationIdleHook( void )\r
348 {\r
349         /* Code can be added to the idle task here.  This function must *NOT* attempt\r
350         to block.  Also, if the application uses the vTaskDelete() API function then\r
351         this function must return regularly to ensure the idle task gets a chance to\r
352         clean up the memory used by deleted tasks. */\r
353 }\r
354 /*-----------------------------------------------------------*/\r
355 \r
356 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
357 {\r
358         /* Just to remove compiler warnings.  This function will only actually\r
359         get called if configCHECK_FOR_STACK_OVERFLOW is set to a non zero value.\r
360         By default this demo does not use the stack overflow checking functionality\r
361         as the SuperH will normally execute an exception if the stack overflows. */\r
362         ( void ) pxTask;\r
363         ( void ) pcTaskName;\r
364         \r
365         taskDISABLE_INTERRUPTS();\r
366         for( ;; );\r
367 }\r
368 /*-----------------------------------------------------------*/\r
369 \r
370 static void prvSetupHardware( void )\r
371 {\r
372 volatile unsigned long ul;\r
373 \r
374         /* Set the CPU and peripheral clocks. */\r
375         CPG.FRQCR.WORD = mainFRQCR_VALUE;\r
376         \r
377         /* Wait for the clock to settle. */\r
378         for( ul = 0; ul < 99; ul++ )\r
379         {\r
380                 nop();\r
381         }\r
382 \r
383         /* Initialise the ports used to toggle LEDs. */\r
384         vParTestInitialise();   \r
385 }\r
386 /*-----------------------------------------------------------*/\r
387 \r
388 void vApplicationSetupTimerInterrupt( void )\r
389 {\r
390 /* The peripheral clock is divided by 32 before feeding the compare match\r
391 peripheral (CMT). */\r
392 unsigned long ulCompareMatch = ( configPERIPHERAL_CLOCK_HZ / ( configTICK_RATE_HZ * 32 ) ) + 1;\r
393 \r
394         /* Configure a timer to create the RTOS tick interrupt.  This example uses\r
395         the compare match timer, but the multi function timer or possible even the\r
396         watchdog timer could also be used.  Ensure vPortTickInterrupt() is installed\r
397         as the interrupt handler for whichever peripheral is used. */\r
398         \r
399         /* Turn the CMT on. */\r
400         STB.CR4.BIT._CMT = 0;\r
401         \r
402         /* Set the compare match value for the required tick frequency. */\r
403         CMT0.CMCOR = ( unsigned short ) ulCompareMatch;\r
404         \r
405         /* Divide the peripheral clock by 32. */\r
406         CMT0.CMCSR.BIT.CKS = 0x01;\r
407         \r
408         /* Set the CMT interrupt priority - the interrupt priority must be\r
409         configKERNEL_INTERRUPT_PRIORITY no matter which peripheral is used to generate\r
410         the tick interrupt. */\r
411         INTC.IPR08.BIT._CMT0 = portKERNEL_INTERRUPT_PRIORITY;\r
412         \r
413         /* Clear the interrupt flag. */\r
414         CMT0.CMCSR.BIT.CMF = 0;\r
415         \r
416         /* Enable the compare match interrupt. */\r
417         CMT0.CMCSR.BIT.CMIE = 0x01;\r
418         \r
419         /* Start the timer. */\r
420         CMT.CMSTR.BIT.STR0 = 0x01;\r
421 }\r
422 /*-----------------------------------------------------------*/\r
423 \r
424 void vApplicationTickHook( void )\r
425 {\r
426         CMT0.CMCSR.BIT.CMF = 0;\r
427 }\r
428 /*-----------------------------------------------------------*/\r
429 \r
430 char *pcGetTaskStatusMessage( void )\r
431 {\r
432         /* Not bothered about a critical section here. */\r
433         return "Need to implement status message!";\r
434 }\r
435 /*-----------------------------------------------------------*/\r