2 * FreeRTOS Kernel V10.1.0
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\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
35 * In addition to the standard demo tasks, the following tasks and tests are
\r
36 * defined and/or created within this file:
\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
43 * "uIP" task - This is the task that handles the uIP stack. All TCP/IP
\r
44 * processing is performed in this task.
\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
52 /* Standard includes. */
\r
55 /* Scheduler includes. */
\r
56 #include "FreeRTOS.h"
\r
59 /* Demo app includes. */
\r
61 #include "integer.h"
\r
62 #include "blocktim.h"
\r
64 #include "partest.h"
\r
65 #include "semtest.h"
\r
67 #include "GenQTest.h"
\r
69 #include "recmutex.h"
\r
71 /*-----------------------------------------------------------*/
\r
73 /* The time between cycles of the 'check' functionality (defined within the
\r
75 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\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
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
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
94 /* Bit definitions. */
\r
95 #define PCONP_PCGPIO 0x00008000
\r
96 #define PLLFEED_FEED1 0x000000AA
\r
97 #define PLLFEED_FEED2 0x00000055
\r
98 /*-----------------------------------------------------------*/
\r
101 * Configure the hardware for the demo.
\r
103 static void prvSetupHardware( void );
\r
106 * The task that handles the uIP stack. All TCP/IP processing is performed in
\r
109 extern void vuIP_Task( void *pvParameters );
\r
112 * The task that handles the USB stack.
\r
114 extern void vUSBTask( void *pvParameters );
\r
117 * Simply returns the current status message for display on served WEB pages.
\r
119 char *pcGetTaskStatusMessage( void );
\r
121 /*-----------------------------------------------------------*/
\r
123 /* Holds the status message displayed by the WEB server. */
\r
124 static char *pcStatusMessage = mainPASS_STATUS_MESSAGE;
\r
126 /*-----------------------------------------------------------*/
\r
130 char cIPAddress[ 16 ]; /* Enough space for "xxx.xxx.xxx.xxx\0". */
\r
132 /* Configure the hardware for use by this demo. */
\r
133 prvSetupHardware();
\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
147 /* Create the USB task. */
\r
148 xTaskCreate( vUSBTask, "USB", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
\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
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
159 /* Start the scheduler. */
\r
160 vTaskStartScheduler();
\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
166 /*-----------------------------------------------------------*/
\r
168 void vApplicationTickHook( void )
\r
170 static unsigned long ulTicksSinceLastDisplay = 0;
\r
172 /* Called from every tick interrupt as described in the comments at the top
\r
175 Have enough ticks passed to make it time to perform our health status
\r
177 ulTicksSinceLastDisplay++;
\r
178 if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
\r
180 /* Reset the counter so these checks run again in mainCHECK_DELAY
\r
182 ulTicksSinceLastDisplay = 0;
\r
184 /* Has an error been found in any task? */
\r
185 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
187 pcStatusMessage = "An error has been detected in the Generic Queue test/demo.";
\r
189 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
191 pcStatusMessage = "An error has been detected in the Peek Queue test/demo.";
\r
193 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
195 pcStatusMessage = "An error has been detected in the Block Queue test/demo.";
\r
197 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
199 pcStatusMessage = "An error has been detected in the Block Time test/demo.";
\r
201 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
203 pcStatusMessage = "An error has been detected in the Semaphore test/demo.";
\r
205 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
207 pcStatusMessage = "An error has been detected in the Poll Queue test/demo.";
\r
209 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
211 pcStatusMessage = "An error has been detected in the Int Math test/demo.";
\r
213 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
215 pcStatusMessage = "An error has been detected in the Mutex test/demo.";
\r
219 /*-----------------------------------------------------------*/
\r
221 char *pcGetTaskStatusMessage( void )
\r
223 /* Not bothered about a critical section here. */
\r
224 return pcStatusMessage;
\r
226 /*-----------------------------------------------------------*/
\r
228 void prvSetupHardware( void )
\r
230 /* Disable peripherals power. */
\r
233 /* Enable GPIO power. */
\r
234 LPC_SC->PCONP = PCONP_PCGPIO;
\r
236 /* Disable TPIU. */
\r
237 LPC_PINCON->PINSEL10 = 0;
\r
239 if ( LPC_SC->PLL0STAT & ( 1 << 25 ) )
\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
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
252 /* Enable main OSC. */
\r
253 LPC_SC->SCS |= 0x20;
\r
254 while( !( LPC_SC->SCS & 0x40 ) );
\r
256 /* select main OSC, 12MHz, as the PLL clock source. */
\r
257 LPC_SC->CLKSRCSEL = 0x1;
\r
259 LPC_SC->PLL0CFG = 0x20031;
\r
260 LPC_SC->PLL0FEED = PLLFEED_FEED1;
\r
261 LPC_SC->PLL0FEED = PLLFEED_FEED2;
\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
268 /* Set clock divider. */
\r
269 LPC_SC->CCLKCFG = 0x03;
\r
271 /* Configure flash accelerator. */
\r
272 LPC_SC->FLASHCFG = 0x403a;
\r
274 /* Check lock bit status. */
\r
275 while( ( ( LPC_SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
\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
286 /* Configure the clock for the USB. */
\r
287 if( LPC_SC->PLL1STAT & ( 1 << 9 ) )
\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
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
300 LPC_SC->PLL1CFG = 0x23;
\r
301 LPC_SC->PLL1FEED = PLLFEED_FEED1;
\r
302 LPC_SC->PLL1FEED = PLLFEED_FEED2;
\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
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
316 /* Setup the peripheral bus to be the same as the CPU output (100 MHz). */
\r
317 LPC_SC->PCLKSEL0 = 0x05555555;
\r
319 /* Configure the LEDs. */
\r
320 vParTestInitialise();
\r
322 /*-----------------------------------------------------------*/
\r
324 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
326 /* This function will get called if a task overflows its stack. */
\r
329 ( void ) pcTaskName;
\r
333 /*-----------------------------------------------------------*/
\r
335 void vConfigureTimerForRunTimeStats( void )
\r
337 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;
\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
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
349 /* Reset Timer 0 */
\r
350 LPC_TIM0->TCR = TCR_COUNT_RESET;
\r
352 /* Just count up. */
\r
353 LPC_TIM0->CTCR = CTCR_CTM_TIMER;
\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
359 /* Start the counter. */
\r
360 LPC_TIM0->TCR = TCR_COUNT_ENABLE;
\r
362 /*-----------------------------------------------------------*/
\r