2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
72 * Creates all the demo application tasks, then starts the scheduler. The WEB
73 * documentation provides more details of the standard demo application tasks.
74 * In addition to the standard demo tasks, the following tasks and tests are
75 * defined and/or created within this file:
77 * "Web server" - Very basic demonstration of the uIP stack. The WEB server
78 * simply generates a page that shows the current state of all the tasks within
79 * the system, including the high water mark of each task stack. The high water
80 * mark is displayed as the amount of stack that has never been used, so the
81 * closer the value is to zero the closer the task has come to overflowing its
82 * stack. The IP address and net mask are set within FreeRTOSConfig.h. Sub
83 * pages display some TCP/IP status information and permit LED3 to be turned on
84 * and off using a check box.
86 * Tick hook function that implements a "Check" function - This is executed
87 * every 5 seconds from the tick hook function. It checks to ensure that all
88 * the standard demo tasks are still operational and running without error.
89 * The system status (pass/fail) is then displayed underneith the task table on
90 * the served WEB pages.
92 * "Reg test" tasks - These fill the registers with known values, then check
93 * that each register still contains its expected value. Each task uses
94 * different values. The tasks run with very low priority so get preempted very
95 * frequently. A register containing an unexpected value is indicative of an
96 * error in the context switching mechanism.
100 /* Standard includes. */
103 /* Scheduler includes. */
104 #include "FreeRTOS.h"
109 /* Demo app includes. */
114 #include "GenQTest.h"
116 #include "recmutex.h"
118 /*-----------------------------------------------------------*/
120 /* ComTest constants - there is no free LED for the comtest tasks. */
121 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 19200 )
122 #define mainCOM_TEST_LED ( 5 )
124 /* Task priorities. */
125 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
126 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
127 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
128 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
129 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
130 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
131 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
132 #define mainWEB_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
134 /* WEB server requires enough stack for the string handling functions. */
135 #define mainBASIC_WEB_STACK_SIZE ( configMINIMAL_STACK_SIZE * 2 )
138 * Configure the hardware for the demo.
140 static void prvSetupHardware( void );
143 * Implements the 'check' function as described at the top of this file.
145 static void prvCheckFunction( void );
148 * Implement the 'Reg test' functionality as described at the top of this file.
150 static void vRegTest1Task( void *pvParameters );
151 static void vRegTest2Task( void *pvParameters );
154 * The task that handles the uIP stack. All TCP/IP processing is performed in
157 extern void vuIP_Task( void *pvParameters );
159 /*-----------------------------------------------------------*/
161 /* Counters used to detect errors within the reg test tasks. */
162 static volatile unsigned long ulRegTest1Counter = 0x11111111, ulRegTest2Counter = 0x22222222;
164 /* Flag that latches any errors detected in the system. */
165 unsigned long ulCheckErrors = 0;
167 /*-----------------------------------------------------------*/
171 extern void vBasicWEBServer( void *pv );
173 /* Setup the hardware ready for this demo. */
176 xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );
178 /* Start the standard demo tasks. */
179 vStartLEDFlashTasks( tskIDLE_PRIORITY );
180 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
181 vStartQueuePeekTasks();
182 vStartRecursiveMutexTasks();
183 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
185 /* Start the reg test tasks - defined in this file. */
186 xTaskCreate( vRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, ( void * ) &ulRegTest1Counter, tskIDLE_PRIORITY, NULL );
187 xTaskCreate( vRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, ( void * ) &ulRegTest2Counter, tskIDLE_PRIORITY, NULL );
189 /* Start the scheduler. */
190 vTaskStartScheduler();
192 /* Will only get here if there was insufficient memory to create the idle
198 /*-----------------------------------------------------------*/
200 void vApplicationTickHook( void )
202 static unsigned long ulExecutionCount = 0, ulLastRegTest1Count = 0, ulLastRegTest2Count = 0;
203 const unsigned long ulExecutionRate = 5000 / portTICK_PERIOD_MS;
205 /* Increment the count of how many times the tick hook has been called. */
208 /* Is it time to perform the check again? */
209 if( ulExecutionCount >= ulExecutionRate )
211 /* Reset the execution count so this function is called again in 5
213 ulExecutionCount = 0;
215 /* Has an error been found in any task? */
216 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
218 ulCheckErrors |= 0x01UL;
221 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
223 ulCheckErrors |= 0x02UL;
226 if( xAreBlockingQueuesStillRunning() != pdTRUE )
228 ulCheckErrors |= 0x04UL;
231 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
233 ulCheckErrors |= 0x200UL;
236 if( ulLastRegTest1Count == ulRegTest1Counter )
238 ulCheckErrors |= 0x1000UL;
241 if( ulLastRegTest2Count == ulRegTest2Counter )
243 ulCheckErrors |= 0x1000UL;
246 ulLastRegTest1Count = ulRegTest1Counter;
247 ulLastRegTest2Count = ulRegTest2Counter;
250 /*-----------------------------------------------------------*/
252 static void prvSetupHardware( void )
254 /* Disable the watchdog, STOP and WAIT modes. */
257 /* --- Setup clock to use external 25MHz source. --- */
259 /* Extal and xtal pin ON. */
263 /* Switch from FEI to FBE (FLL bypassed external)
264 enable external clock source */
265 MCGC2 = MCGC2_ERCLKEN_MASK /* Activate external reference clock */
266 | MCGC2_EREFS_MASK /* Because crystal is being used */
267 | MCGC2_RANGE_MASK; /* High range */
269 /* Select clock mode and clear IREFs. */
270 MCGC1 = (0x02 << 6 ) /* CLKS = 10 -> external reference clock. */
271 | (0x04 << 3 ) /* RDIV = 2^4 -> 25MHz/16 = 1.5625 MHz */
272 | MCGC1_IRCLKEN_MASK; /* IRCLK to RTC enabled */
274 /* Wait for Reference and Clock status bits to update. */
275 while( MCGSC_IREFST | ( MCGSC_CLKST != 0x02 ) )
277 /* Nothing to do here. */
280 /* Switch from FBE to PBE (PLL bypassed internal) mode. */
281 MCGC3 = 0x08 /* Set PLL multi 50MHz. */
282 | MCGC3_PLLS_MASK; /* Select PLL. */
284 /* Wait for PLL status and lock bits to update. */
285 while( !MCGSC_PLLST | !MCGSC_LOCK )
287 /* Nothing to do here. */
291 /* Now in PBE Mode, finally switch from PBE to PEE (PLL enabled external
293 MCGC1_CLKS = 0b00; /* PLL clock to system (MCGOUT) */
295 /* Wait for the clock status bits to update. */
296 while( MCGSC_CLKST != 0x03 )
298 /* Nothing to do here. */
301 /* Setup the IO for the LED outputs. */
302 vParTestInitialise();
304 /*-----------------------------------------------------------*/
306 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
308 /* This will get called if a stack overflow is detected during the context
309 switch. Set configCHECK_FOR_STACK_OVERFLOWS to 2 to also check for stack
310 problems within nested interrupts, but only do this for debug purposes as
311 it will increase the context switch time. */
320 /*-----------------------------------------------------------*/
322 static void vRegTest1Task( void *pvParameters )
324 /* Just to remove compiler warnings. */
325 ( void ) pvParameters;
327 /* Set all the registers to known values, then check that each retains its
328 expected value - as described at the top of this file. If an error is
329 found then the loop counter will no longer be incremented allowing the check
330 task to recognise the error. */
331 asm volatile ( "reg_test_1_start: \n\t"
347 " cmpi.l #1, d0 \n\t"
348 " bne reg_test_1_error \n\t"
349 " cmpi.l #2, d1 \n\t"
350 " bne reg_test_1_error \n\t"
351 " cmpi.l #3, d2 \n\t"
352 " bne reg_test_1_error \n\t"
353 " cmpi.l #4, d3 \n\t"
354 " bne reg_test_1_error \n\t"
355 " cmpi.l #5, d4 \n\t"
356 " bne reg_test_1_error \n\t"
357 " cmpi.l #6, d5 \n\t"
358 " bne reg_test_1_error \n\t"
359 " cmpi.l #7, d6 \n\t"
360 " bne reg_test_1_error \n\t"
361 " cmpi.l #8, d7 \n\t"
362 " bne reg_test_1_error \n\t"
364 " cmpi.l #9, d0 \n\t"
365 " bne reg_test_1_error \n\t"
367 " cmpi.l #10, d0 \n\t"
368 " bne reg_test_1_error \n\t"
370 " cmpi.l #11, d0 \n\t"
371 " bne reg_test_1_error \n\t"
373 " cmpi.l #12, d0 \n\t"
374 " bne reg_test_1_error \n\t"
376 " cmpi.l #13, d0 \n\t"
377 " bne reg_test_1_error \n\t"
379 " cmpi.l #15, d0 \n\t"
380 " bne reg_test_1_error \n\t"
381 " move ulRegTest1Counter, d0 \n\t"
383 " move d0, ulRegTest1Counter \n\t"
384 " bra reg_test_1_start \n\t"
385 "reg_test_1_error: \n\t"
386 " bra reg_test_1_error \n\t"
389 /*-----------------------------------------------------------*/
391 static void vRegTest2Task( void *pvParameters )
393 /* Just to remove compiler warnings. */
394 ( void ) pvParameters;
396 /* Set all the registers to known values, then check that each retains its
397 expected value - as described at the top of this file. If an error is
398 found then the loop counter will no longer be incremented allowing the check
399 task to recognise the error. */
400 asm volatile ( "reg_test_2_start: \n\t"
401 " moveq #10, d0 \n\t"
402 " moveq #20, d1 \n\t"
403 " moveq #30, d2 \n\t"
404 " moveq #40, d3 \n\t"
405 " moveq #50, d4 \n\t"
406 " moveq #60, d5 \n\t"
407 " moveq #70, d6 \n\t"
408 " moveq #80, d7 \n\t"
410 " move #100, a1 \n\t"
411 " move #110, a2 \n\t"
412 " move #120, a3 \n\t"
413 " move #130, a4 \n\t"
414 " move #150, a6 \n\t"
416 " cmpi.l #10, d0 \n\t"
417 " bne reg_test_2_error \n\t"
418 " cmpi.l #20, d1 \n\t"
419 " bne reg_test_2_error \n\t"
420 " cmpi.l #30, d2 \n\t"
421 " bne reg_test_2_error \n\t"
422 " cmpi.l #40, d3 \n\t"
423 " bne reg_test_2_error \n\t"
424 " cmpi.l #50, d4 \n\t"
425 " bne reg_test_2_error \n\t"
426 " cmpi.l #60, d5 \n\t"
427 " bne reg_test_2_error \n\t"
428 " cmpi.l #70, d6 \n\t"
429 " bne reg_test_2_error \n\t"
430 " cmpi.l #80, d7 \n\t"
431 " bne reg_test_2_error \n\t"
433 " cmpi.l #90, d0 \n\t"
434 " bne reg_test_2_error \n\t"
436 " cmpi.l #100, d0 \n\t"
437 " bne reg_test_2_error \n\t"
439 " cmpi.l #110, d0 \n\t"
440 " bne reg_test_2_error \n\t"
442 " cmpi.l #120, d0 \n\t"
443 " bne reg_test_2_error \n\t"
445 " cmpi.l #130, d0 \n\t"
446 " bne reg_test_2_error \n\t"
448 " cmpi.l #150, d0 \n\t"
449 " bne reg_test_2_error \n\t"
450 " move ulRegTest1Counter, d0 \n\t"
452 " move d0, ulRegTest2Counter \n\t"
453 " bra reg_test_2_start \n\t"
454 "reg_test_2_error: \n\t"
455 " bra reg_test_2_error \n\t"
458 /*-----------------------------------------------------------*/