]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LPC1768_IAR/main.c
c4d4dd9d4656b74d4d3a54f86b21981cbee10955
[freertos] / FreeRTOS / Demo / CORTEX_LPC1768_IAR / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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 /* Scheduler includes. */\r
53 #include "FreeRTOS.h"\r
54 #include "task.h"\r
55 \r
56 /* Demo app includes. */\r
57 #include "BlockQ.h"\r
58 #include "integer.h"\r
59 #include "blocktim.h"\r
60 #include "flash.h"\r
61 #include "partest.h"\r
62 #include "semtest.h"\r
63 #include "PollQ.h"\r
64 #include "GenQTest.h"\r
65 #include "QPeek.h"\r
66 #include "recmutex.h"\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /* The time between cycles of the 'check' functionality (defined within the\r
71 tick hook). */\r
72 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
73 \r
74 /* The toggle rate for the LED. */\r
75 #define mainLED_TOGGLE_RATE                                     ( ( TickType_t ) 1000 / 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 mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
83 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
84 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\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 /*-----------------------------------------------------------*/\r
95 \r
96 /*\r
97  * Configure the hardware for the demo.\r
98  */\r
99 static void prvSetupHardware( void );\r
100 \r
101 /*\r
102  * The task that handles the uIP stack.  All TCP/IP processing is performed in\r
103  * this task.\r
104  */\r
105 extern void vuIP_Task( void *pvParameters );\r
106 \r
107 /*\r
108  * The task that handles the USB stack.\r
109  */\r
110 extern void vUSBTask( void *pvParameters );\r
111 \r
112 /*\r
113  * Very basic task that does nothing but use delays to flash an LED.\r
114  */\r
115 static void prvFlashTask( 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         /* Configure the hardware for use by this demo. */\r
132         prvSetupHardware();\r
133 \r
134         /* Start the standard demo tasks.  These are just here to exercise the\r
135         kernel port and provide examples of how the FreeRTOS API can be used. */\r
136         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
137     vCreateBlockTimeTasks();\r
138     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
139     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
140     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
141     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
142     vStartQueuePeekTasks();\r
143     vStartRecursiveMutexTasks();\r
144 \r
145         /* Create the simple LED flash task. */\r
146         xTaskCreate( prvFlashTask, "Flash", configMINIMAL_STACK_SIZE, ( void * ) NULL, mainFLASH_TASK_PRIORITY, NULL );\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         /* Create the uIP task.  The WEB server runs in this task. */\r
152     xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );\r
153 \r
154     /* Start the scheduler. */\r
155         vTaskStartScheduler();\r
156 \r
157     /* Will only get here if there was insufficient memory to create the idle\r
158     task.  The idle task is created within vTaskStartScheduler(). */\r
159         for( ;; );\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 void vApplicationTickHook( void )\r
164 {\r
165 static unsigned long ulTicksSinceLastDisplay = 0;\r
166 \r
167         /* Called from every tick interrupt as described in the comments at the top\r
168         of this file.\r
169 \r
170         Have enough ticks passed to make it     time to perform our health status\r
171         check again? */\r
172         ulTicksSinceLastDisplay++;\r
173         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
174         {\r
175                 /* Reset the counter so these checks run again in mainCHECK_DELAY\r
176                 ticks time. */\r
177                 ulTicksSinceLastDisplay = 0;\r
178 \r
179                 /* Has an error been found in any task? */\r
180                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
181                 {\r
182                         pcStatusMessage = "An error has been detected in the Generic Queue test/demo.";\r
183                 }\r
184                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
185                 {\r
186                         pcStatusMessage = "An error has been detected in the Peek Queue test/demo.";\r
187                 }\r
188                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
189                 {\r
190                         pcStatusMessage = "An error has been detected in the Block Queue test/demo.";\r
191                 }\r
192                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
193                 {\r
194                         pcStatusMessage = "An error has been detected in the Block Time test/demo.";\r
195                 }\r
196             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
197             {\r
198                 pcStatusMessage = "An error has been detected in the Semaphore test/demo.";\r
199             }\r
200             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
201             {\r
202                 pcStatusMessage = "An error has been detected in the Poll Queue test/demo.";\r
203             }\r
204             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
205             {\r
206                 pcStatusMessage = "An error has been detected in the Int Math test/demo.";\r
207             }\r
208             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
209             {\r
210                 pcStatusMessage = "An error has been detected in the Mutex test/demo.";\r
211             }\r
212         }\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static void prvFlashTask( void *pvParameters )\r
217 {\r
218 TickType_t xLastFlashTime;\r
219 \r
220         /* We need to initialise xLastFlashTime prior to the first call to\r
221         vTaskDelayUntil(). */\r
222         xLastFlashTime = xTaskGetTickCount();\r
223 \r
224         for(;;)\r
225         {\r
226                 /* Simply toggle the LED between delays. */\r
227                 vTaskDelayUntil( &xLastFlashTime, mainLED_TOGGLE_RATE );\r
228                 vParTestToggleLED( 0 );\r
229         }\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 char *pcGetTaskStatusMessage( void )\r
234 {\r
235         /* Not bothered about a critical section here. */\r
236         return pcStatusMessage;\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 void prvSetupHardware( void )\r
241 {\r
242         /* Disable peripherals power. */\r
243         SC->PCONP = 0;\r
244 \r
245         /* Enable GPIO power. */\r
246         SC->PCONP = PCONP_PCGPIO;\r
247 \r
248         /* Disable TPIU. */\r
249         PINCON->PINSEL10 = 0;\r
250 \r
251         if ( SC->PLL0STAT & ( 1 << 25 ) )\r
252         {\r
253                 /* Enable PLL, disconnected. */\r
254                 SC->PLL0CON = 1;\r
255                 SC->PLL0FEED = PLLFEED_FEED1;\r
256                 SC->PLL0FEED = PLLFEED_FEED2;\r
257         }\r
258 \r
259         /* Disable PLL, disconnected. */\r
260         SC->PLL0CON = 0;\r
261         SC->PLL0FEED = PLLFEED_FEED1;\r
262         SC->PLL0FEED = PLLFEED_FEED2;\r
263 \r
264         /* Enable main OSC. */\r
265         SC->SCS |= 0x20;\r
266         while( !( SC->SCS & 0x40 ) );\r
267 \r
268         /* select main OSC, 12MHz, as the PLL clock source. */\r
269         SC->CLKSRCSEL = 0x1;\r
270 \r
271         SC->PLL0CFG = 0x20031;\r
272         SC->PLL0FEED = PLLFEED_FEED1;\r
273         SC->PLL0FEED = PLLFEED_FEED2;\r
274 \r
275         /* Enable PLL, disconnected. */\r
276         SC->PLL0CON = 1;\r
277         SC->PLL0FEED = PLLFEED_FEED1;\r
278         SC->PLL0FEED = PLLFEED_FEED2;\r
279 \r
280         /* Set clock divider. */\r
281         SC->CCLKCFG = 0x03;\r
282 \r
283         /* Configure flash accelerator. */\r
284         SC->FLASHCFG = 0x403a;\r
285 \r
286         /* Check lock bit status. */\r
287         while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );\r
288 \r
289         /* Enable and connect. */\r
290         SC->PLL0CON = 3;\r
291         SC->PLL0FEED = PLLFEED_FEED1;\r
292         SC->PLL0FEED = PLLFEED_FEED2;\r
293         while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );\r
294 \r
295 \r
296 \r
297 \r
298         /* Configure the clock for the USB. */\r
299 \r
300         if( SC->PLL1STAT & ( 1 << 9 ) )\r
301         {\r
302                 /* Enable PLL, disconnected. */\r
303                 SC->PLL1CON = 1;\r
304                 SC->PLL1FEED = PLLFEED_FEED1;\r
305                 SC->PLL1FEED = PLLFEED_FEED2;\r
306         }\r
307 \r
308         /* Disable PLL, disconnected. */\r
309         SC->PLL1CON = 0;\r
310         SC->PLL1FEED = PLLFEED_FEED1;\r
311         SC->PLL1FEED = PLLFEED_FEED2;\r
312 \r
313         SC->PLL1CFG = 0x23;\r
314         SC->PLL1FEED = PLLFEED_FEED1;\r
315         SC->PLL1FEED = PLLFEED_FEED2;\r
316 \r
317         /* Enable PLL, disconnected. */\r
318         SC->PLL1CON = 1;\r
319         SC->PLL1FEED = PLLFEED_FEED1;\r
320         SC->PLL1FEED = PLLFEED_FEED2;\r
321         while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );\r
322 \r
323         /* Enable and connect. */\r
324         SC->PLL1CON = 3;\r
325         SC->PLL1FEED = PLLFEED_FEED1;\r
326         SC->PLL1FEED = PLLFEED_FEED2;\r
327         while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );\r
328 \r
329         /*  Setup the peripheral bus to be the same as the CPU output (100 MHz). */\r
330         SC->PCLKSEL0 = 0x05555555;\r
331 \r
332         /* Configure the LEDs. */\r
333         vParTestInitialise();\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
338 {\r
339         /* This function will get called if a task overflows its stack. */\r
340 \r
341         ( void ) pxTask;\r
342         ( void ) pcTaskName;\r
343 \r
344         for( ;; );\r
345 }\r
346 /*-----------------------------------------------------------*/\r
347 \r
348 void vConfigureTimerForRunTimeStats( void )\r
349 {\r
350 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;\r
351 \r
352         /* This function configures a timer that is used as the time base when\r
353         collecting run time statistical information - basically the percentage\r
354         of CPU time that each task is utilising.  It is called automatically when\r
355         the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set\r
356         to 1). */\r
357 \r
358         /* Power up and feed the timer. */\r
359         SC->PCONP |= 0x02UL;\r
360         SC->PCLKSEL0 = (SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);\r
361 \r
362         /* Reset Timer 0 */\r
363         TIM0->TCR = TCR_COUNT_RESET;\r
364 \r
365         /* Just count up. */\r
366         TIM0->CTCR = CTCR_CTM_TIMER;\r
367 \r
368         /* Prescale to a frequency that is good enough to get a decent resolution,\r
369         but not too fast so as to overflow all the time. */\r
370         TIM0->PR =  ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;\r
371 \r
372         /* Start the counter. */\r
373         TIM0->TCR = TCR_COUNT_ENABLE;\r
374 }\r
375 /*-----------------------------------------------------------*/\r
376 \r