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