2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. 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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\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
36 * In addition to the standard demo tasks, the following tasks and tests are
\r
37 * defined and/or created within this file:
\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
44 * "uIP" task - This is the task that handles the uIP stack. All TCP/IP
\r
45 * processing is performed in this task.
\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
53 /* Scheduler includes. */
\r
54 #include "FreeRTOS.h"
\r
57 /* Demo app includes. */
\r
59 #include "integer.h"
\r
60 #include "blocktim.h"
\r
62 #include "partest.h"
\r
63 #include "semtest.h"
\r
65 #include "GenQTest.h"
\r
67 #include "recmutex.h"
\r
69 /*-----------------------------------------------------------*/
\r
71 /* The time between cycles of the 'check' functionality (defined within the
\r
73 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
75 /* The toggle rate for the LED. */
\r
76 #define mainLED_TOGGLE_RATE ( ( TickType_t ) 1000 / portTICK_PERIOD_MS )
\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
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
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
95 /*-----------------------------------------------------------*/
\r
98 * Configure the hardware for the demo.
\r
100 static void prvSetupHardware( void );
\r
103 * The task that handles the uIP stack. All TCP/IP processing is performed in
\r
106 extern void vuIP_Task( void *pvParameters );
\r
109 * The task that handles the USB stack.
\r
111 extern void vUSBTask( void *pvParameters );
\r
114 * Very basic task that does nothing but use delays to flash an LED.
\r
116 static void prvFlashTask( void *pvParameters );
\r
119 * Simply returns the current status message for display on served WEB pages.
\r
121 char *pcGetTaskStatusMessage( void );
\r
123 /*-----------------------------------------------------------*/
\r
125 /* Holds the status message displayed by the WEB server. */
\r
126 static char *pcStatusMessage = mainPASS_STATUS_MESSAGE;
\r
128 /*-----------------------------------------------------------*/
\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
146 /* Create the simple LED flash task. */
\r
147 xTaskCreate( prvFlashTask, "Flash", configMINIMAL_STACK_SIZE, ( void * ) NULL, mainFLASH_TASK_PRIORITY, NULL );
\r
149 /* Create the USB task. */
\r
150 xTaskCreate( vUSBTask, "USB", configMINIMAL_STACK_SIZE, ( void * ) NULL, tskIDLE_PRIORITY, NULL );
\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
155 /* Start the scheduler. */
\r
156 vTaskStartScheduler();
\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
162 /*-----------------------------------------------------------*/
\r
164 void vApplicationTickHook( void )
\r
166 static unsigned long ulTicksSinceLastDisplay = 0;
\r
168 /* Called from every tick interrupt as described in the comments at the top
\r
171 Have enough ticks passed to make it time to perform our health status
\r
173 ulTicksSinceLastDisplay++;
\r
174 if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
\r
176 /* Reset the counter so these checks run again in mainCHECK_DELAY
\r
178 ulTicksSinceLastDisplay = 0;
\r
180 /* Has an error been found in any task? */
\r
181 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
183 pcStatusMessage = "An error has been detected in the Generic Queue test/demo.";
\r
185 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
187 pcStatusMessage = "An error has been detected in the Peek Queue test/demo.";
\r
189 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
191 pcStatusMessage = "An error has been detected in the Block Queue test/demo.";
\r
193 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
195 pcStatusMessage = "An error has been detected in the Block Time test/demo.";
\r
197 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
199 pcStatusMessage = "An error has been detected in the Semaphore test/demo.";
\r
201 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
203 pcStatusMessage = "An error has been detected in the Poll Queue test/demo.";
\r
205 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
207 pcStatusMessage = "An error has been detected in the Int Math test/demo.";
\r
209 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
211 pcStatusMessage = "An error has been detected in the Mutex test/demo.";
\r
215 /*-----------------------------------------------------------*/
\r
217 static void prvFlashTask( void *pvParameters )
\r
219 TickType_t xLastFlashTime;
\r
221 /* We need to initialise xLastFlashTime prior to the first call to
\r
222 vTaskDelayUntil(). */
\r
223 xLastFlashTime = xTaskGetTickCount();
\r
227 /* Simply toggle the LED between delays. */
\r
228 vTaskDelayUntil( &xLastFlashTime, mainLED_TOGGLE_RATE );
\r
229 vParTestToggleLED( 0 );
\r
232 /*-----------------------------------------------------------*/
\r
234 char *pcGetTaskStatusMessage( void )
\r
236 /* Not bothered about a critical section here. */
\r
237 return pcStatusMessage;
\r
239 /*-----------------------------------------------------------*/
\r
241 void prvSetupHardware( void )
\r
243 /* Disable peripherals power. */
\r
246 /* Enable GPIO power. */
\r
247 SC->PCONP = PCONP_PCGPIO;
\r
249 /* Disable TPIU. */
\r
250 PINCON->PINSEL10 = 0;
\r
252 if ( SC->PLL0STAT & ( 1 << 25 ) )
\r
254 /* Enable PLL, disconnected. */
\r
256 SC->PLL0FEED = PLLFEED_FEED1;
\r
257 SC->PLL0FEED = PLLFEED_FEED2;
\r
260 /* Disable PLL, disconnected. */
\r
262 SC->PLL0FEED = PLLFEED_FEED1;
\r
263 SC->PLL0FEED = PLLFEED_FEED2;
\r
265 /* Enable main OSC. */
\r
267 while( !( SC->SCS & 0x40 ) );
\r
269 /* select main OSC, 12MHz, as the PLL clock source. */
\r
270 SC->CLKSRCSEL = 0x1;
\r
272 SC->PLL0CFG = 0x20031;
\r
273 SC->PLL0FEED = PLLFEED_FEED1;
\r
274 SC->PLL0FEED = PLLFEED_FEED2;
\r
276 /* Enable PLL, disconnected. */
\r
278 SC->PLL0FEED = PLLFEED_FEED1;
\r
279 SC->PLL0FEED = PLLFEED_FEED2;
\r
281 /* Set clock divider. */
\r
282 SC->CCLKCFG = 0x03;
\r
284 /* Configure flash accelerator. */
\r
285 SC->FLASHCFG = 0x403a;
\r
287 /* Check lock bit status. */
\r
288 while( ( ( SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
\r
290 /* Enable and connect. */
\r
292 SC->PLL0FEED = PLLFEED_FEED1;
\r
293 SC->PLL0FEED = PLLFEED_FEED2;
\r
294 while( ( ( SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );
\r
299 /* Configure the clock for the USB. */
\r
301 if( SC->PLL1STAT & ( 1 << 9 ) )
\r
303 /* Enable PLL, disconnected. */
\r
305 SC->PLL1FEED = PLLFEED_FEED1;
\r
306 SC->PLL1FEED = PLLFEED_FEED2;
\r
309 /* Disable PLL, disconnected. */
\r
311 SC->PLL1FEED = PLLFEED_FEED1;
\r
312 SC->PLL1FEED = PLLFEED_FEED2;
\r
314 SC->PLL1CFG = 0x23;
\r
315 SC->PLL1FEED = PLLFEED_FEED1;
\r
316 SC->PLL1FEED = PLLFEED_FEED2;
\r
318 /* Enable PLL, disconnected. */
\r
320 SC->PLL1FEED = PLLFEED_FEED1;
\r
321 SC->PLL1FEED = PLLFEED_FEED2;
\r
322 while( ( ( SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );
\r
324 /* Enable and connect. */
\r
326 SC->PLL1FEED = PLLFEED_FEED1;
\r
327 SC->PLL1FEED = PLLFEED_FEED2;
\r
328 while( ( ( SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );
\r
330 /* Setup the peripheral bus to be the same as the CPU output (100 MHz). */
\r
331 SC->PCLKSEL0 = 0x05555555;
\r
333 /* Configure the LEDs. */
\r
334 vParTestInitialise();
\r
336 /*-----------------------------------------------------------*/
\r
338 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
340 /* This function will get called if a task overflows its stack. */
\r
343 ( void ) pcTaskName;
\r
347 /*-----------------------------------------------------------*/
\r
349 void vConfigureTimerForRunTimeStats( void )
\r
351 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;
\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
359 /* Power up and feed the timer. */
\r
360 SC->PCONP |= 0x02UL;
\r
361 SC->PCLKSEL0 = (SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);
\r
363 /* Reset Timer 0 */
\r
364 TIM0->TCR = TCR_COUNT_RESET;
\r
366 /* Just count up. */
\r
367 TIM0->CTCR = CTCR_CTM_TIMER;
\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
373 /* Start the counter. */
\r
374 TIM0->TCR = TCR_COUNT_ENABLE;
\r
376 /*-----------------------------------------------------------*/
\r