2 FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
55 * documentation provides more details of the standard demo application tasks
\r
56 * (which just exist to test the kernel port and provide an example of how to use
\r
57 * each FreeRTOS API function).
\r
59 * In addition to the standard demo tasks, the following tasks and tests are
\r
60 * defined and/or created within this file:
\r
62 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
63 * is permitted to access the display directly. Other tasks wishing to write a
\r
64 * message to the LCD send the message on a queue to the LCD task instead of
\r
65 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
66 * for messages - waking and displaying the messages as they arrive. The use
\r
67 * of a gatekeeper in this manner permits both tasks and interrupts to write to
\r
68 * the LCD without worrying about mutual exclusion. This is demonstrated by the
\r
69 * check hook (see below) which sends messages to the display even though it
\r
70 * executes from an interrupt context.
\r
72 * "Check" hook - This only executes fully every five seconds from the tick
\r
73 * hook. Its main function is to check that all the standard demo tasks are
\r
74 * still operational. Should any unexpected behaviour be discovered within a
\r
75 * demo task then the tick hook will write an error to the LCD (via the LCD task).
\r
76 * If all the demo tasks are executing with their expected behaviour then the
\r
77 * check hook writes PASS to the LCD (again via the LCD task), as described above.
\r
78 * The check hook also toggles LED 4 each time it executes.
\r
80 * LED tasks - These just demonstrate how multiple instances of a single task
\r
81 * definition can be created. Each LED task simply toggles an LED. The task
\r
82 * parameter is used to pass the number of the LED to be toggled into the task.
\r
84 * "uIP" task - This is the task that handles the uIP stack. All TCP/IP
\r
85 * processing is performed in this task.
\r
88 /* Standard includes. */
\r
91 /* Scheduler includes. */
\r
92 #include "FreeRTOS.h"
\r
97 /* Demo app includes. */
\r
99 #include "integer.h"
\r
100 #include "blocktim.h"
\r
102 #include "partest.h"
\r
103 #include "semtest.h"
\r
105 #include "GenQTest.h"
\r
107 #include "recmutex.h"
\r
109 /*-----------------------------------------------------------*/
\r
111 /* The number of LED tasks that will be created. */
\r
112 #define mainNUM_LED_TASKS ( 6 )
\r
114 /* The time between cycles of the 'check' functionality (defined within the
\r
116 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
118 /* Task priorities. */
\r
119 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
120 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
121 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
122 #define mainUIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
123 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
124 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
125 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
126 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
128 /* The WEB server has a larger stack as it utilises stack hungry string
\r
129 handling library calls. */
\r
130 #define mainBASIC_WEB_STACK_SIZE ( configMINIMAL_STACK_SIZE * 4 )
\r
132 /* The length of the queue used to send messages to the LCD task. */
\r
133 #define mainQUEUE_SIZE ( 3 )
\r
135 /* The task that is toggled by the check task. */
\r
136 #define mainCHECK_TASK_LED ( 4 )
\r
137 /*-----------------------------------------------------------*/
\r
140 * Configure the hardware for the demo.
\r
142 static void prvSetupHardware( void );
\r
145 * Very simple task that toggles an LED.
\r
147 static void vLEDTask( void *pvParameters );
\r
150 * The task that handles the uIP stack. All TCP/IP processing is performed in
\r
153 extern void vuIP_Task( void *pvParameters );
\r
156 * The LCD gatekeeper task as described in the comments at the top of this file.
\r
158 static void vLCDTask( void *pvParameters );
\r
160 /*-----------------------------------------------------------*/
\r
162 /* The queue used to send messages to the LCD task. */
\r
163 xQueueHandle xLCDQueue;
\r
165 /*-----------------------------------------------------------*/
\r
169 /* Configure the hardware for use by this demo. */
\r
170 prvSetupHardware();
\r
172 /* Start the standard demo tasks. These are just here to exercise the
\r
173 kernel port and provide examples of how the FreeRTOS API can be used. */
\r
174 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
175 vCreateBlockTimeTasks();
\r
176 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
177 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
178 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
179 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
180 vStartQueuePeekTasks();
\r
181 vStartRecursiveMutexTasks();
\r
182 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
184 /* Create the uIP task. The WEB server runs in this task. */
\r
185 xTaskCreate( vuIP_Task, ( signed char * ) "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );
\r
187 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
188 are received via this queue. */
\r
189 xLCDQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( xLCDMessage ) );
\r
191 /* Start the LCD gatekeeper task - as described in the comments at the top
\r
193 xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );
\r
195 /* Start the scheduler. */
\r
196 vTaskStartScheduler();
\r
198 /* Will only get here if there was insufficient memory to create the idle
\r
199 task. The idle task is created within vTaskStartScheduler(). */
\r
202 /*-----------------------------------------------------------*/
\r
204 void vLCDTask( void *pvParameters )
\r
206 xLCDMessage xMessage;
\r
207 unsigned long ulRow = 0;
\r
208 char cIPAddr[ 17 ]; /* To fit max IP address length of xxx.xxx.xxx.xxx\0 */
\r
210 ( void ) pvParameters;
\r
212 /* The LCD gatekeeper task as described in the comments at the top of this
\r
215 /* Initialise the LCD and display a startup message that includes the
\r
216 configured IP address. */
\r
217 sprintf( cIPAddr, "%d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
221 /* Wait for a message to arrive to be displayed. */
\r
222 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
226 /*-----------------------------------------------------------*/
\r
228 void vApplicationTickHook( void )
\r
230 static xLCDMessage xMessage = { "PASS" };
\r
231 static unsigned portLONG ulTicksSinceLastDisplay = 0;
\r
232 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
234 /* Called from every tick interrupt as described in the comments at the top
\r
237 Have enough ticks passed to make it time to perform our health status
\r
239 ulTicksSinceLastDisplay++;
\r
240 if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
\r
242 /* Reset the counter so these checks run again in mainCHECK_DELAY
\r
244 ulTicksSinceLastDisplay = 0;
\r
246 /* Has an error been found in any task? */
\r
247 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
249 xMessage.pcMessage = "ERROR: GEN Q";
\r
251 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
253 xMessage.pcMessage = "ERROR: PEEK Q";
\r
255 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
257 xMessage.pcMessage = "ERROR: BLOCK Q";
\r
259 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
261 xMessage.pcMessage = "ERROR: BLOCK TIME";
\r
263 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
265 xMessage.pcMessage = "ERROR: SEMAPHR";
\r
267 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
269 xMessage.pcMessage = "ERROR: POLL Q";
\r
271 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
273 xMessage.pcMessage = "ERROR: INT MATH";
\r
275 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
277 xMessage.pcMessage = "ERROR: REC MUTEX";
\r
280 /* Send the message to the OLED gatekeeper for display. The
\r
281 xHigherPriorityTaskWoken parameter is not actually used here
\r
282 as this function is running in the tick interrupt anyway - but
\r
283 it must still be supplied. */
\r
284 xHigherPriorityTaskWoken = pdFALSE;
\r
285 xQueueSendFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );
\r
287 /* Also toggle and LED. This can be done from here because in this port
\r
288 the LED toggling functions don't use critical sections. */
\r
289 vParTestToggleLED( mainCHECK_TASK_LED );
\r
292 /*-----------------------------------------------------------*/
\r
294 void prvSetupHardware( void )
\r
296 /* Disable peripherals power. */
\r
299 /* Enable GPIO power. */
\r
300 SC->PCONP = PCONP_PCGPIO;
\r
302 /* Disable TPIU. */
\r
303 PINCON->PINSEL10 = 0;
\r
305 /* Disconnect the main PLL. */
\r
306 SC->PLL0CON &= ~PLLCON_PLLC;
\r
307 SC->PLL0FEED = PLLFEED_FEED1;
\r
308 SC->PLL0FEED = PLLFEED_FEED2;
\r
309 while ((SC->PLL0STAT & PLLSTAT_PLLC) != 0);
\r
311 /* Turn off the main PLL. */
\r
312 SC->PLL0CON &= ~PLLCON_PLLE;
\r
313 SC->PLL0FEED = PLLFEED_FEED1;
\r
314 SC->PLL0FEED = PLLFEED_FEED2;
\r
315 while ((SC->PLL0STAT & PLLSTAT_PLLE) != 0);
\r
317 /* No CPU clock divider. */
\r
322 while ((SC->SCS & 0x40) == 0);
\r
324 /* Use main oscillator. */
\r
326 SC->PLL0CFG = (PLLCFG_MUL16 | PLLCFG_DIV1);
\r
328 SC->PLL0FEED = PLLFEED_FEED1;
\r
329 SC->PLL0FEED = PLLFEED_FEED2;
\r
331 /* Activate the PLL by turning it on then feeding the correct
\r
332 sequence of bytes. */
\r
333 SC->PLL0CON = PLLCON_PLLE;
\r
334 SC->PLL0FEED = PLLFEED_FEED1;
\r
335 SC->PLL0FEED = PLLFEED_FEED2;
\r
337 /* 6x CPU clock divider (64 MHz) */
\r
340 /* Wait for the PLL to lock. */
\r
341 while ((SC->PLL0STAT & PLLSTAT_PLOCK) == 0);
\r
343 /* Connect the PLL. */
\r
344 SC->PLL0CON = PLLCON_PLLC | PLLCON_PLLE;
\r
345 SC->PLL0FEED = PLLFEED_FEED1;
\r
346 SC->PLL0FEED = PLLFEED_FEED2;
\r
348 /* Setup the peripheral bus to be the same as the PLL output (64 MHz). */
\r
349 SC->PCLKSEL0 = 0x05555555;
\r
351 /* Configure the LEDs. */
\r
352 vParTestInitialise();
\r
354 /*-----------------------------------------------------------*/
\r
356 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName )
\r
358 /* This function will get called if a task overflows its stack. */
\r
361 ( void ) pcTaskName;
\r
365 /*-----------------------------------------------------------*/
\r
367 void vConfigureTimerForRunTimeStats( void )
\r
369 const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;
\r
371 /* This function configures a timer that is used as the time base when
\r
372 collecting run time statistical information - basically the percentage
\r
373 of CPU time that each task is utilising. It is called automatically when
\r
374 the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set
\r
377 /* Power up and feed the timer. */
\r
378 SC->PCONP |= 0x02UL;
\r
379 SC->PCLKSEL0 = (SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);
\r
381 /* Reset Timer 0 */
\r
382 TIM0->TCR = TCR_COUNT_RESET;
\r
384 /* Just count up. */
\r
385 TIM0->CTCR = CTCR_CTM_TIMER;
\r
387 /* Prescale to a frequency that is good enough to get a decent resolution,
\r
388 but not too fast so as to overflow all the time. */
\r
389 TIM0->PR = ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;
\r
391 /* Start the counter. */
\r
392 TIM0->TCR = TCR_COUNT_ENABLE;
\r
394 /*-----------------------------------------------------------*/
\r