]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LPC1768_GCC_RedSuite/src/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_LPC1768_GCC_RedSuite / src / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /*\r
31  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
32  * documentation provides more details of the standard demo application tasks\r
33  * (which just exist to test the kernel port and provide an example of how to use\r
34  * each FreeRTOS API function).\r
35  *\r
36  * In addition to the standard demo tasks, the following tasks and tests are\r
37  * defined and/or created within this file:\r
38  *\r
39  * "Check" hook -  This only executes fully every five seconds from the tick\r
40  * hook.  Its main function is to check that all the standard demo tasks are\r
41  * still operational.  The status can be viewed using on the Task Stats page\r
42  * served by the WEB server.\r
43  *\r
44  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP\r
45  * processing is performed in this task.\r
46  *\r
47  * "USB" task - Enumerates the USB device as a CDC class, then echoes back all\r
48  * received characters with a configurable offset (for example, if the offset\r
49  * is 1 and 'A' is received then 'B' will be sent back).  A dumb terminal such\r
50  * as Hyperterminal can be used to talk to the USB task.\r
51  */\r
52 \r
53 /* Standard includes. */\r
54 #include "stdio.h"\r
55 \r
56 /* Scheduler includes. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 \r
60 /* Demo app includes. */\r
61 #include "BlockQ.h"\r
62 #include "integer.h"\r
63 #include "blocktim.h"\r
64 #include "flash.h"\r
65 #include "partest.h"\r
66 #include "semtest.h"\r
67 #include "PollQ.h"\r
68 #include "GenQTest.h"\r
69 #include "QPeek.h"\r
70 #include "recmutex.h"\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* The time between cycles of the 'check' functionality (defined within the\r
75 tick hook. */\r
76 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
77 \r
78 /* Task priorities. */\r
79 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
80 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
81 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
82 #define mainUIP_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )\r
83 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
84 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
85 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
86 \r
87 /* The WEB server has a larger stack as it utilises stack hungry string\r
88 handling library calls. */\r
89 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 4 )\r
90 \r
91 /* The message displayed by the WEB server when all tasks are executing\r
92 without an error being reported. */\r
93 #define mainPASS_STATUS_MESSAGE                         "All tasks are executing without error."\r
94 \r
95 /* Bit definitions. */\r
96 #define PCONP_PCGPIO    0x00008000\r
97 #define PLLFEED_FEED1   0x000000AA\r
98 #define PLLFEED_FEED2   0x00000055\r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /*\r
102  * Configure the hardware for the demo.\r
103  */\r
104 static void prvSetupHardware( void );\r
105 \r
106 /*\r
107  * The task that handles the uIP stack.  All TCP/IP processing is performed in\r
108  * this task.\r
109  */\r
110 extern void vuIP_Task( void *pvParameters );\r
111 \r
112 /*\r
113  * The task that handles the USB stack.\r
114  */\r
115 extern void vUSBTask( void *pvParameters );\r
116 \r
117 /*\r
118  * Simply returns the current status message for display on served WEB pages.\r
119  */\r
120 char *pcGetTaskStatusMessage( void );\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /* Holds the status message displayed by the WEB server. */\r
125 static char *pcStatusMessage = mainPASS_STATUS_MESSAGE;\r
126 \r
127 /*-----------------------------------------------------------*/\r
128 \r
129 int main( void )\r
130 {\r
131 char cIPAddress[ 16 ]; /* Enough space for "xxx.xxx.xxx.xxx\0". */\r
132 \r
133         /* Configure the hardware for use by this demo. */\r
134         prvSetupHardware();\r
135 \r
136         /* Start the standard demo tasks.  These are just here to exercise the\r
137         kernel port and provide examples of how the FreeRTOS API can be used. */\r
138         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
139     vCreateBlockTimeTasks();\r
140     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
141     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
142     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
143     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
144     vStartQueuePeekTasks();\r
145     vStartRecursiveMutexTasks();\r
146         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
147 \r
148     /* Create the USB task. */\r
149     xTaskCreate( vUSBTask, "USB", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );\r
150 \r
151         /* Display the IP address, then create the uIP task.  The WEB server runs\r
152         in this task.  --- Due to tool changes since this demo was created the LCD\r
153         is no longer used.\r
154         LCDdriver_initialisation();\r
155         LCD_PrintString( 5, 10, "FreeRTOS.org", 14, COLOR_GREEN);\r
156         sprintf( cIPAddress, "%d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
157         LCD_PrintString( 5, 30, cIPAddress, 14, COLOR_RED);\r
158     xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL ); */\r
159 \r
160     /* Start the scheduler. */\r
161         vTaskStartScheduler();\r
162 \r
163     /* Will only get here if there was insufficient memory to create the idle\r
164     task.  The idle task is created within vTaskStartScheduler(). */\r
165         for( ;; );\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 void vApplicationTickHook( void )\r
170 {\r
171 static unsigned long ulTicksSinceLastDisplay = 0;\r
172 \r
173         /* Called from every tick interrupt as described in the comments at the top\r
174         of this file.\r
175 \r
176         Have enough ticks passed to make it     time to perform our health status\r
177         check again? */\r
178         ulTicksSinceLastDisplay++;\r
179         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
180         {\r
181                 /* Reset the counter so these checks run again in mainCHECK_DELAY\r
182                 ticks time. */\r
183                 ulTicksSinceLastDisplay = 0;\r
184 \r
185                 /* Has an error been found in any task? */\r
186                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
187                 {\r
188                         pcStatusMessage = "An error has been detected in the Generic Queue test/demo.";\r
189                 }\r
190                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
191                 {\r
192                         pcStatusMessage = "An error has been detected in the Peek Queue test/demo.";\r
193                 }\r
194                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
195                 {\r
196                         pcStatusMessage = "An error has been detected in the Block Queue test/demo.";\r
197                 }\r
198                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
199                 {\r
200                         pcStatusMessage = "An error has been detected in the Block Time test/demo.";\r
201                 }\r
202             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
203             {\r
204                 pcStatusMessage = "An error has been detected in the Semaphore test/demo.";\r
205             }\r
206             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
207             {\r
208                 pcStatusMessage = "An error has been detected in the Poll Queue test/demo.";\r
209             }\r
210             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
211             {\r
212                 pcStatusMessage = "An error has been detected in the Int Math test/demo.";\r
213             }\r
214             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
215             {\r
216                 pcStatusMessage = "An error has been detected in the Mutex test/demo.";\r
217             }\r
218         }\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 char *pcGetTaskStatusMessage( void )\r
223 {\r
224         /* Not bothered about a critical section here. */\r
225         return pcStatusMessage;\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 void prvSetupHardware( void )\r
230 {\r
231         /* Disable peripherals power. */\r
232         LPC_SC->PCONP = 0;\r
233 \r
234         /* Enable GPIO power. */\r
235         LPC_SC->PCONP = PCONP_PCGPIO;\r
236 \r
237         /* Disable TPIU. */\r
238         LPC_PINCON->PINSEL10 = 0;\r
239 \r
240         if ( LPC_SC->PLL0STAT & ( 1 << 25 ) )\r
241         {\r
242                 /* Enable PLL, disconnected. */\r
243                 LPC_SC->PLL0CON = 1;\r
244                 LPC_SC->PLL0FEED = PLLFEED_FEED1;\r
245                 LPC_SC->PLL0FEED = PLLFEED_FEED2;\r
246         }\r
247 \r
248         /* Disable PLL, disconnected. */\r
249         LPC_SC->PLL0CON = 0;\r
250         LPC_SC->PLL0FEED = PLLFEED_FEED1;\r
251         LPC_SC->PLL0FEED = PLLFEED_FEED2;\r
252 \r
253         /* Enable main OSC. */\r
254         LPC_SC->SCS |= 0x20;\r
255         while( !( LPC_SC->SCS & 0x40 ) );\r
256 \r
257         /* select main OSC, 12MHz, as the PLL clock source. */\r
258         LPC_SC->CLKSRCSEL = 0x1;\r
259 \r
260         LPC_SC->PLL0CFG = 0x20031;\r
261         LPC_SC->PLL0FEED = PLLFEED_FEED1;\r
262         LPC_SC->PLL0FEED = PLLFEED_FEED2;\r
263 \r
264         /* Enable PLL, disconnected. */\r
265         LPC_SC->PLL0CON = 1;\r
266         LPC_SC->PLL0FEED = PLLFEED_FEED1;\r
267         LPC_SC->PLL0FEED = PLLFEED_FEED2;\r
268 \r
269         /* Set clock divider. */\r
270         LPC_SC->CCLKCFG = 0x03;\r
271 \r
272         /* Configure flash accelerator. */\r
273         LPC_SC->FLASHCFG = 0x403a;\r
274 \r
275         /* Check lock bit status. */\r
276         while( ( ( LPC_SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );\r
277 \r
278         /* Enable and connect. */\r
279         LPC_SC->PLL0CON = 3;\r
280         LPC_SC->PLL0FEED = PLLFEED_FEED1;\r
281         LPC_SC->PLL0FEED = PLLFEED_FEED2;\r
282         while( ( ( LPC_SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );\r
283 \r
284 \r
285 \r
286 \r
287         /* Configure the clock for the USB. */\r
288         if( LPC_SC->PLL1STAT & ( 1 << 9 ) )\r
289         {\r
290                 /* Enable PLL, disconnected. */\r
291                 LPC_SC->PLL1CON = 1;\r
292                 LPC_SC->PLL1FEED = PLLFEED_FEED1;\r
293                 LPC_SC->PLL1FEED = PLLFEED_FEED2;\r
294         }\r
295 \r
296         /* Disable PLL, disconnected. */\r
297         LPC_SC->PLL1CON = 0;\r
298         LPC_SC->PLL1FEED = PLLFEED_FEED1;\r
299         LPC_SC->PLL1FEED = PLLFEED_FEED2;\r
300 \r
301         LPC_SC->PLL1CFG = 0x23;\r
302         LPC_SC->PLL1FEED = PLLFEED_FEED1;\r
303         LPC_SC->PLL1FEED = PLLFEED_FEED2;\r
304 \r
305         /* Enable PLL, disconnected. */\r
306         LPC_SC->PLL1CON = 1;\r
307         LPC_SC->PLL1FEED = PLLFEED_FEED1;\r
308         LPC_SC->PLL1FEED = PLLFEED_FEED2;\r
309         while( ( ( LPC_SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );\r
310 \r
311         /* Enable and connect. */\r
312         LPC_SC->PLL1CON = 3;\r
313         LPC_SC->PLL1FEED = PLLFEED_FEED1;\r
314         LPC_SC->PLL1FEED = PLLFEED_FEED2;\r
315         while( ( ( LPC_SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );\r
316 \r
317         /*  Setup the peripheral bus to be the same as the CPU output (100 MHz). */\r
318         LPC_SC->PCLKSEL0 = 0x05555555;\r
319 \r
320         /* Configure the LEDs. */\r
321         vParTestInitialise();\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
326 {\r
327         /* This function will get called if a task overflows its stack. */\r
328 \r
329         ( void ) pxTask;\r
330         ( void ) pcTaskName;\r
331 \r
332         for( ;; );\r
333 }\r
334 /*-----------------------------------------------------------*/\r
335 \r
336 void vConfigureTimerForRunTimeStats( void )\r
337 {\r
338 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;\r
339 \r
340         /* This function configures a timer that is used as the time base when\r
341         collecting run time statistical information - basically the percentage\r
342         of CPU time that each task is utilising.  It is called automatically when\r
343         the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set\r
344         to 1). */\r
345 \r
346         /* Power up and feed the timer. */\r
347         LPC_SC->PCONP |= 0x02UL;\r
348         LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);\r
349 \r
350         /* Reset Timer 0 */\r
351         LPC_TIM0->TCR = TCR_COUNT_RESET;\r
352 \r
353         /* Just count up. */\r
354         LPC_TIM0->CTCR = CTCR_CTM_TIMER;\r
355 \r
356         /* Prescale to a frequency that is good enough to get a decent resolution,\r
357         but not too fast so as to overflow all the time. */\r
358         LPC_TIM0->PR =  ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;\r
359 \r
360         /* Start the counter. */\r
361         LPC_TIM0->TCR = TCR_COUNT_ENABLE;\r
362 }\r
363 /*-----------------------------------------------------------*/\r
364 \r