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