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