]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LPC1768_GCC_RedSuite/src/main.c
Add simple USB CDC task.
[freertos] / Demo / CORTEX_LPC1768_GCC_RedSuite / src / main.c
1 /*\r
2         FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 \r
49 #error The batch file Demo\CORTEX_LPC1768_GCC_RedSuite\CreateProjectDirectoryStructure.bat must be executed before the first build.  After executing the batch file hit F5 to refrech the Eclipse project, then delete this line.\r
50 \r
51 \r
52 \r
53 /*\r
54  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
55  * documentation provides more details of the standard demo application tasks\r
56  * (which just exist to test the kernel port and provide an example of how to use\r
57  * each FreeRTOS API function).\r
58  *\r
59  * In addition to the standard demo tasks, the following tasks and tests are\r
60  * defined and/or created within this file:\r
61  *\r
62  * "Check" hook -  This only executes fully every five seconds from the tick\r
63  * hook.  Its main function is to check that all the standard demo tasks are\r
64  * still operational.  The status can be viewed using on the Task Stats page\r
65  * served by the WEB server.\r
66  *\r
67  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP\r
68  * processing is performed in this task.\r
69  * \r
70  * "USB" task - Enumerates the USB device as a CDC class, then echoes back all\r
71  * received characters with a configurable offset (for example, if the offset\r
72  * is 1 and 'A' is received then 'B' will be sent back).  A dumb terminal such\r
73  * as Hyperterminal can be used to talk to the USB task.\r
74  */\r
75 \r
76 /* Standard includes. */\r
77 #include "stdio.h"\r
78 \r
79 /* Scheduler includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 \r
83 /* Demo app includes. */\r
84 #include "BlockQ.h"\r
85 #include "integer.h"\r
86 #include "blocktim.h"\r
87 #include "flash.h"\r
88 #include "partest.h"\r
89 #include "semtest.h"\r
90 #include "PollQ.h"\r
91 #include "GenQTest.h"\r
92 #include "QPeek.h"\r
93 #include "recmutex.h"\r
94 \r
95 /* Red Suite includes. */\r
96 #include "lcd_driver.h"\r
97 #include "lcd.h"\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* The time between cycles of the 'check' functionality (defined within the\r
102 tick hook. */\r
103 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
104 \r
105 /* Task priorities. */\r
106 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
107 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
108 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
109 #define mainUIP_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )\r
110 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
111 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
112 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
113 \r
114 /* The WEB server has a larger stack as it utilises stack hungry string\r
115 handling library calls. */\r
116 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 4 )\r
117 \r
118 /* The message displayed by the WEB server when all tasks are executing\r
119 without an error being reported. */\r
120 #define mainPASS_STATUS_MESSAGE                         "All tasks are executing without error."\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * Configure the hardware for the demo.\r
126  */\r
127 static void prvSetupHardware( void );\r
128 \r
129 /*\r
130  * The task that handles the uIP stack.  All TCP/IP processing is performed in\r
131  * this task.\r
132  */\r
133 extern void vuIP_Task( void *pvParameters );\r
134 \r
135 /*\r
136  * The task that handles the USB stack.\r
137  */\r
138 extern void vUSBTask( void *pvParameters );\r
139 \r
140 /*\r
141  * Simply returns the current status message for display on served WEB pages.\r
142  */\r
143 char *pcGetTaskStatusMessage( void );\r
144 \r
145 /*-----------------------------------------------------------*/\r
146 \r
147 /* Holds the status message displayed by the WEB server. */\r
148 static char *pcStatusMessage = mainPASS_STATUS_MESSAGE;\r
149 \r
150 /*-----------------------------------------------------------*/\r
151 \r
152 int main( void )\r
153 {\r
154 char cIPAddress[ 16 ]; /* Enough space for "xxx.xxx.xxx.xxx\0". */\r
155 \r
156         /* Configure the hardware for use by this demo. */\r
157         prvSetupHardware();\r
158 \r
159         /* Start the standard demo tasks.  These are just here to exercise the\r
160         kernel port and provide examples of how the FreeRTOS API can be used. */\r
161         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
162     vCreateBlockTimeTasks();\r
163     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
164     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
165     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
166     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
167     vStartQueuePeekTasks();\r
168     vStartRecursiveMutexTasks();\r
169         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
170 \r
171     /* Create the USB task. */\r
172     xTaskCreate( vUSBTask, ( signed char * ) "USB", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
173         \r
174         /* Display the IP address, then create the uIP task.  The WEB server runs \r
175         in this task. */\r
176         LCDdriver_initialisation();\r
177         LCD_PrintString( 5, 10, "FreeRTOS.org", 14, COLOR_GREEN);\r
178         sprintf( cIPAddress, "%d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
179         LCD_PrintString( 5, 30, cIPAddress, 14, COLOR_RED);\r
180     xTaskCreate( vuIP_Task, ( signed char * ) "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );\r
181 \r
182     /* Start the scheduler. */\r
183         vTaskStartScheduler();\r
184 \r
185     /* Will only get here if there was insufficient memory to create the idle\r
186     task.  The idle task is created within vTaskStartScheduler(). */\r
187         for( ;; );\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 void vApplicationTickHook( void )\r
192 {\r
193 static unsigned portLONG ulTicksSinceLastDisplay = 0;\r
194 \r
195         /* Called from every tick interrupt as described in the comments at the top\r
196         of this file.\r
197 \r
198         Have enough ticks passed to make it     time to perform our health status\r
199         check again? */\r
200         ulTicksSinceLastDisplay++;\r
201         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
202         {\r
203                 /* Reset the counter so these checks run again in mainCHECK_DELAY\r
204                 ticks time. */\r
205                 ulTicksSinceLastDisplay = 0;\r
206 \r
207                 /* Has an error been found in any task? */\r
208                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
209                 {\r
210                         pcStatusMessage = "An error has been detected in the Generic Queue test/demo.";\r
211                 }\r
212                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
213                 {\r
214                         pcStatusMessage = "An error has been detected in the Peek Queue test/demo.";\r
215                 }\r
216                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
217                 {\r
218                         pcStatusMessage = "An error has been detected in the Block Queue test/demo.";\r
219                 }\r
220                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
221                 {\r
222                         pcStatusMessage = "An error has been detected in the Block Time test/demo.";\r
223                 }\r
224             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
225             {\r
226                 pcStatusMessage = "An error has been detected in the Semaphore test/demo.";\r
227             }\r
228             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
229             {\r
230                 pcStatusMessage = "An error has been detected in the Poll Queue test/demo.";\r
231             }\r
232             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
233             {\r
234                 pcStatusMessage = "An error has been detected in the Int Math test/demo.";\r
235             }\r
236             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
237             {\r
238                 pcStatusMessage = "An error has been detected in the Mutex test/demo.";\r
239             }\r
240         }\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 char *pcGetTaskStatusMessage( void )\r
245 {\r
246         /* Not bothered about a critical section here. */\r
247         return pcStatusMessage;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 void prvSetupHardware( void )\r
252 {\r
253         /* Disable peripherals power. */\r
254         SC->PCONP = 0;\r
255 \r
256         /* Enable GPIO power. */\r
257         SC->PCONP = PCONP_PCGPIO;\r
258 \r
259         /* Disable TPIU. */\r
260         PINCON->PINSEL10 = 0;\r
261 \r
262         if ( SC->PLL0STAT & ( 1 << 25 ) )\r
263         {\r
264                 /* Enable PLL, disconnected. */\r
265                 SC->PLL0CON = 1;                        \r
266                 SC->PLL0FEED = PLLFEED_FEED1;\r
267                 SC->PLL0FEED = PLLFEED_FEED2;\r
268         }\r
269         \r
270         /* Disable PLL, disconnected. */\r
271         SC->PLL0CON = 0;                                \r
272         SC->PLL0FEED = PLLFEED_FEED1;\r
273         SC->PLL0FEED = PLLFEED_FEED2;\r
274             \r
275         /* Enable main OSC. */\r
276         SC->SCS |= 0x20;                        \r
277         while( !( SC->SCS & 0x40 ) );\r
278         \r
279         /* select main OSC, 12MHz, as the PLL clock source. */\r
280         SC->CLKSRCSEL = 0x1;            \r
281         \r
282         SC->PLL0CFG = 0x0b;\r
283         SC->PLL0FEED = PLLFEED_FEED1;\r
284         SC->PLL0FEED = PLLFEED_FEED2;\r
285               \r
286         /* Enable PLL, disconnected. */\r
287         SC->PLL0CON = 1;                                \r
288         SC->PLL0FEED = PLLFEED_FEED1;\r
289         SC->PLL0FEED = PLLFEED_FEED2;\r
290         \r
291         /* Set clock divider. */\r
292         SC->CCLKCFG = 0x03;\r
293         \r
294         /* Configure flash accelerator. */\r
295         SC->FLASHCFG = 0x303a;\r
296         \r
297         /* Check lock bit status. */\r
298         while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );       \r
299             \r
300         /* Enable and connect. */\r
301         SC->PLL0CON = 3;                                \r
302         SC->PLL0FEED = PLLFEED_FEED1;\r
303         SC->PLL0FEED = PLLFEED_FEED2;\r
304         while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );       \r
305 \r
306         \r
307         \r
308         \r
309         /* Configure the clock for the USB. */\r
310           \r
311         if( SC->PLL1STAT & ( 1 << 9 ) )\r
312         {\r
313                 /* Enable PLL, disconnected. */\r
314                 SC->PLL1CON = 1;                        \r
315                 SC->PLL1FEED = PLLFEED_FEED1;\r
316                 SC->PLL1FEED = PLLFEED_FEED2;\r
317         }\r
318         \r
319         /* Disable PLL, disconnected. */\r
320         SC->PLL1CON = 0;                                \r
321         SC->PLL1FEED = PLLFEED_FEED1;\r
322         SC->PLL1FEED = PLLFEED_FEED2;\r
323         \r
324         SC->PLL1CFG = 0x23;\r
325         SC->PLL1FEED = PLLFEED_FEED1;\r
326         SC->PLL1FEED = PLLFEED_FEED2;\r
327               \r
328         /* Enable PLL, disconnected. */\r
329         SC->PLL1CON = 1;                                \r
330         SC->PLL1FEED = PLLFEED_FEED1;\r
331         SC->PLL1FEED = PLLFEED_FEED2;\r
332         while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );\r
333         \r
334         /* Enable and connect. */\r
335         SC->PLL1CON = 3;                                \r
336         SC->PLL1FEED = PLLFEED_FEED1;\r
337         SC->PLL1FEED = PLLFEED_FEED2;\r
338         while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );\r
339 \r
340         /*  Setup the peripheral bus to be the same as the PLL output (64 MHz). */\r
341         SC->PCLKSEL0 = 0x05555555;\r
342 \r
343         /* Configure the LEDs. */\r
344         vParTestInitialise();\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName )\r
349 {\r
350         /* This function will get called if a task overflows its stack. */\r
351 \r
352         ( void ) pxTask;\r
353         ( void ) pcTaskName;\r
354 \r
355         for( ;; );\r
356 }\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 void vConfigureTimerForRunTimeStats( void )\r
360 {\r
361 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;\r
362 \r
363         /* This function configures a timer that is used as the time base when\r
364         collecting run time statistical information - basically the percentage\r
365         of CPU time that each task is utilising.  It is called automatically when\r
366         the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set\r
367         to 1). */\r
368 \r
369         /* Power up and feed the timer. */\r
370         SC->PCONP |= 0x02UL;\r
371         SC->PCLKSEL0 = (SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);\r
372 \r
373         /* Reset Timer 0 */\r
374         TIM0->TCR = TCR_COUNT_RESET;\r
375 \r
376         /* Just count up. */\r
377         TIM0->CTCR = CTCR_CTM_TIMER;\r
378 \r
379         /* Prescale to a frequency that is good enough to get a decent resolution,\r
380         but not too fast so as to overflow all the time. */\r
381         TIM0->PR =  ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;\r
382 \r
383         /* Start the counter. */\r
384         TIM0->TCR = TCR_COUNT_ENABLE;\r
385 }\r
386 /*-----------------------------------------------------------*/\r
387 \r