]> git.sur5r.net Git - freertos/blob - Demo/SuperH_SH7216_Renesas/RTOSDemo/main.c
Still a work in progress. Checking in for backup only.
[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 /* Kernel includes. */\r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 \r
58 /* Demo application includes. */\r
59 #include "BlockQ.h"\r
60 #include "death.h"\r
61 #include "integer.h"\r
62 #include "blocktim.h"\r
63 #include "flash.h"\r
64 #include "partest.h"\r
65 #include "semtest.h"\r
66 #include "PollQ.h"\r
67 #include "GenQTest.h"\r
68 #include "QPeek.h"\r
69 #include "recmutex.h"\r
70 \r
71 /* Constants required to configure the hardware. */\r
72 #define mainFRQCR_VALUE                                         ( 0x0303 )      /* Input = 12.5MHz, I Clock = 200MHz, B Clock = 50MHz, P Clock = 50MHz */\r
73 \r
74 /* Task priorities. */\r
75 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 1 )\r
76 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
77 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
78 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
79 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
80 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 1 )\r
81 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
82 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
83 \r
84 /* The LED toggle by the check task. */\r
85 #define mainCHECK_LED                                           ( 5 )\r
86 \r
87 /* The rate at which mainCHECK_LED will toggle when all the tasks are running\r
88 without error. */\r
89 #define mainNO_ERROR_CYCLE_TIME                         ( 5000 / portTICK_RATE_MS )\r
90 \r
91 /* The rate at which mainCHECK_LED will toggle when an error has been reported\r
92 by at least one task. */\r
93 #define mainERROR_CYCLE_TIME                            ( 200 / portTICK_RATE_MS )\r
94 \r
95 void vApplicationMallocFailedHook( void );\r
96 void vApplicationIdleHook( void );\r
97 static void prvSetupHardware( void );\r
98 static void prvCheckTask( void *pvParameters );\r
99 \r
100 extern void vRegTest1Task( void *pvParameters );\r
101 extern void vRegTest2Task( void *pvParameters );\r
102 \r
103 volatile unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 void main(void)\r
108 {\r
109         prvSetupHardware();\r
110 \r
111         /* Start the reg test tasks which test the context switching mechanism. */\r
112         xTaskCreate( vRegTest1Task, "RegTest1", configMINIMAL_STACK_SIZE, ( void * ) 0x12345678UL, tskIDLE_PRIORITY, NULL );\r
113         xTaskCreate( vRegTest2Task, "RegTest2", configMINIMAL_STACK_SIZE, ( void * ) 0x11223344UL, tskIDLE_PRIORITY, NULL );\r
114 \r
115         /* Start the check task as described at the top of this file. */\r
116         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
117 \r
118         /* Start the standard demo tasks.  These don't perform any particular useful\r
119         functionality, other than to demonstrate the FreeRTOS API being used. */\r
120         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
121         vCreateBlockTimeTasks();\r
122     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
123     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
124     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
125     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
126         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
127     vStartQueuePeekTasks();\r
128     vStartRecursiveMutexTasks();\r
129 \r
130         /* The suicide tasks must be created last as they need to know how many\r
131         tasks were running prior to their creation in order to ascertain whether\r
132         or not the correct/expected number of tasks are running at any given time. */\r
133     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
134 \r
135         /* Start the tasks running. */\r
136         vTaskStartScheduler();\r
137 \r
138         /* Will only get here if there was insufficient heap memory to create the idle\r
139     task.  Increase the configTOTAL_HEAP_SIZE setting in FreeRTOSConfig.h. */\r
140         for( ;; );\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 static void prvCheckTask( void *pvParameter )\r
145 {\r
146 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;\r
147 unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;\r
148 \r
149         /* Initialise xNextWakeTime - this only needs to be done once. */\r
150         xNextWakeTime = xTaskGetTickCount();\r
151 \r
152         for( ;; )\r
153         {\r
154                 /* Place this task in the blocked state until it is time to run again. */\r
155                 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
156                 \r
157                 /* Inspect all the other tasks to esnure none have experienced any errors. */\r
158                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
159                 {\r
160                         /* Increase the rate at which this task cycles, which will increase the\r
161                         rate at which mainCHECK_LED flashes to give visual feedback that an error\r
162                         has occurred. */\r
163                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
164                 }\r
165                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
166                 {\r
167                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
168                 }\r
169                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
170                 {\r
171                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
172                 }\r
173                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
174                 {\r
175                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
176                 }\r
177             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
178             {\r
179                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
180             }\r
181             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
182             {\r
183                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
184             }\r
185             else if( xIsCreateTaskStillRunning() != pdTRUE )\r
186             {\r
187                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
188             }\r
189             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
190             {\r
191                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
192             }\r
193             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
194             {\r
195                 xCycleFrequency = mainERROR_CYCLE_TIME;\r
196             }\r
197 \r
198                 /* Check the reg test tasks are still cycling.  They will stop incrementing\r
199                 their loop counters if they encounter an error. */\r
200                 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )\r
201                 {\r
202                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
203                 }\r
204 \r
205                 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )\r
206                 {\r
207                         xCycleFrequency = mainERROR_CYCLE_TIME;\r
208                 }\r
209                 \r
210                 ulLastRegTest1CycleCount = ulRegTest1CycleCount;\r
211                 ulLastRegTest2CycleCount = ulRegTest2CycleCount;\r
212                 \r
213                 /* Toggle the check LED to give an indication of the system status.  If the\r
214                 LED toggles every 5 seconds then everything is ok.  A faster toggle indicates\r
215                 an error. */\r
216                 vParTestToggleLED( mainCHECK_LED );\r
217         }\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 void vApplicationMallocFailedHook( void )\r
222 {\r
223         /* A call to vPortMalloc() failed, probably during the creation of a task,\r
224         queue or semaphore.  Inspect pxCurrentTCB to find which task is currently\r
225         executing. */\r
226         for( ;; );\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 void vApplicationIdleHook( void )\r
231 {\r
232         /* Code can be added to the idle task here.  This function must *NOT* attempt\r
233         to block.  Also, if the application uses the vTaskDelete() API function then\r
234         this function must return regularly to ensure the idle task gets a chance to\r
235         clean up the memory used by deleted tasks. */\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
240 {\r
241         for( ;; );\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static void prvSetupHardware( void )\r
246 {\r
247 volatile unsigned long ul;\r
248 \r
249         /* Set the CPU and peripheral clocks. */\r
250         CPG.FRQCR.WORD = mainFRQCR_VALUE;\r
251         \r
252         /* Wait for the clock to settle. */\r
253         for( ul = 0; ul < 99; ul++ )\r
254         {\r
255                 nop();\r
256         }\r
257         \r
258         /* Initialise the ports used to toggle LEDs. */\r
259         vParTestInitialise();\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 void vApplicationSetupTimerInterrupt( void )\r
264 {\r
265 /* The peripheral clock is divided by 32 before feeding the compare match\r
266 periphersl (CMT). */\r
267 unsigned long ulCompareMatch = ( configPERIPHERAL_CLOCK_HZ / ( configTICK_RATE_HZ * 32 ) ) + 1;\r
268 \r
269         /* Configure a timer to create the RTOS tick interrupt.  This example uses\r
270         the compare match timer, but the multi function timer or possible even the\r
271         watchdog timer could also be used.  Ensure vPortTickInterrupt() is installed\r
272         as the interrupt handler for whichever peripheral is used. */\r
273         \r
274         /* Turn the CMT on. */\r
275         STB.CR4.BIT._CMT = 0;\r
276         \r
277         /* Set the compare match value for the required tick frequency. */\r
278         CMT0.CMCOR = ( unsigned short ) ulCompareMatch;\r
279         \r
280         /* Divide the peripheral clock by 32. */\r
281         CMT0.CMCSR.BIT.CKS = 0x01;\r
282         \r
283         /* Set the CMT interrupt priority - the interrupt priority must be\r
284         configKERNEL_INTERRUPT_PRIORITY no matter which peripheral is used to generate\r
285         the tick interrupt. */\r
286         INTC.IPR08.BIT._CMT0 = configKERNEL_INTERRUPT_PRIORITY;\r
287         \r
288         /* Clear the interrupt flag. */\r
289         CMT0.CMCSR.BIT.CMF = 0;\r
290         \r
291         /* Enable the compare match interrupt. */\r
292         CMT0.CMCSR.BIT.CMIE = 0x01;\r
293         \r
294         /* Start the timer. */\r
295         CMT.CMSTR.BIT.STR0 = 0x01;\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 void vApplicationTickHook( void )\r
300 {\r
301         CMT0.CMCSR.BIT.CMF = 0;\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r