2 FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
69 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
70 * documentation provides more details of the standard demo application tasks,
\r
71 * which provide no particular functionality but do provide a good example of
\r
72 * how to use the FreeRTOS API. In addition to the standard demo tasks, the
\r
73 * following tasks and tests are defined and/or created within this file:
\r
75 * Webserver ("uIP") task - This serves a number of dynamically generated WEB
\r
76 * pages to a standard WEB browser. The IP and MAC addresses are configured by
\r
77 * constants defined at the bottom of FreeRTOSConfig.h. Use either a standard
\r
78 * Ethernet cable to connect through a hug, or a cross over (point to point)
\r
79 * cable to connect directly. Ensure the IP address used is compatible with the
\r
80 * IP address of the machine running the browser - the easiest way to achieve
\r
81 * this is to ensure the first three octets of the IP addresses are the same.
\r
83 * "Reg test" tasks - These fill the registers with known values, then check
\r
84 * that each register still contains its expected value. Each task uses
\r
85 * different values. The tasks run with very low priority so get preempted very
\r
86 * frequently. A register containing an unexpected value is indicative of an
\r
87 * error in the context switching mechanism. Both standard and floating point
\r
88 * registers are checked. The nature of the reg test tasks necessitates that
\r
89 * they are written in assembly code. They are defined in regtest.src.
\r
91 * "math" tasks - These are a set of 8 tasks that perform various double
\r
92 * precision floating point calculations in order to check that the tasks
\r
93 * floating point registers are being correctly saved and restored during
\r
94 * context switches. The math tasks are defined in flop.c.
\r
96 * "Check" task - This only executes every five seconds but has a high priority
\r
97 * to ensure it gets processor time. Its main function is to check that all the
\r
98 * standard demo tasks are still operational. While no errors have been
\r
99 * discovered the check task will toggle an LED every 5 seconds - the toggle
\r
100 * rate increasing to 200ms being a visual indication that at least one task has
\r
101 * reported unexpected behaviour.
\r
103 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
104 * tasks are executing as expected and no errors have been reported in any
\r
105 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
106 * has reported unexpected behaviour.
\r
108 * *NOTE 2* This file and flop.c both demonstrate the use of
\r
109 * xPortUsesFloatingPoint() which informs the kernel that a task should maintain
\r
110 * a floating point context.
\r
112 * *NOTE 3* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
113 * the application set up a timer to generate the tick interrupt. In this
\r
114 * example a compare match timer is used for this purpose.
\r
115 * vApplicationTickHook() is used to clear the timer interrupt and relies on
\r
116 * configUSE_TICK_HOOK being set to 1 in FreeRTOSConfig.h.
\r
118 * *NOTE 4* The traceTASK_SWITCHED_IN and traceTASK_SWITCHED_OUT trace hooks
\r
119 * are used to save and restore the floating point context respectively for
\r
120 * those tasks that require it (those for which xPortUsesFloatingPoint() has
\r
123 * *NOTE 5* Any interrupt that can cause a context switch requires an asm
\r
124 * wrapper and must be assigned an interrupt priority of
\r
125 * portKERNEL_INTERRUPT_PRIORITY.
\r
127 * *NOTE 6* vSetupClockForRunTimeStats() is called by the kernel (via the
\r
128 * portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()) macro to configure an MTU channel
\r
129 * to produce a time base that is used to log how much processor time each task
\r
130 * is consuming. The MTU is used to generate a high(ish) frequency interrupt,
\r
131 * and so also provides an example of how interrupts that don't make use of the
\r
132 * FreeRTOS kernel can be assigned a priority above any priority used by the
\r
136 /* Kernel includes. */
\r
137 #include "FreeRTOS.h"
\r
140 /* Demo application includes. */
\r
141 #include "BlockQ.h"
\r
143 #include "integer.h"
\r
144 #include "blocktim.h"
\r
146 #include "partest.h"
\r
147 #include "semtest.h"
\r
149 #include "GenQTest.h"
\r
151 #include "recmutex.h"
\r
154 /* Constants required to configure the hardware. */
\r
155 #define mainFRQCR_VALUE ( 0x0303 ) /* Input = 12.5MHz, I Clock = 200MHz, B Clock = 50MHz, P Clock = 50MHz */
\r
157 /* Task priorities. */
\r
158 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
159 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
160 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
161 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
162 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
163 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
164 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
165 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
166 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
167 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
169 /* The WEB server uses string handling functions, which in turn use a bit more
\r
170 stack than most of the other tasks. */
\r
171 #define mainuIP_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3 )
\r
173 /* The LED toggled by the check task. */
\r
174 #define mainCHECK_LED ( 5 )
\r
176 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
178 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_PERIOD_MS )
\r
180 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
181 by at least one task. */
\r
182 #define mainERROR_CYCLE_TIME ( 200 / portTICK_PERIOD_MS )
\r
185 * vApplicationMallocFailedHook() will only be called if
\r
186 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
187 * function that will execute if a call to pvPortMalloc() fails.
\r
188 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
189 * semaphore is created. It is also called by various parts of the demo
\r
192 void vApplicationMallocFailedHook( void );
\r
195 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
196 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
197 * of the idle task. It is essential that code added to this hook function
\r
198 * never attempts to block in any way (for example, call xQueueReceive() with
\r
199 * a block time specified). If the application makes use of the vTaskDelete()
\r
200 * API function (as this demo application does) then it is also important that
\r
201 * vApplicationIdleHook() is permitted to return to its calling function because
\r
202 * it is the responsibility of the idle task to clean up memory allocated by the
\r
203 * kernel to any task that has since been deleted.
\r
205 void vApplicationIdleHook( void );
\r
208 * Just sets up clocks, ports, etc. used by the demo application.
\r
210 static void prvSetupHardware( void );
\r
213 * The check task as described at the top of this file.
\r
215 static void prvCheckTask( void *pvParameters );
\r
218 * The reg test tasks as described at the top of this file.
\r
220 extern void vRegTest1Task( void *pvParameters );
\r
221 extern void vRegTest2Task( void *pvParameters );
\r
224 * Contains the implementation of the WEB server.
\r
226 extern void vuIP_Task( void *pvParameters );
\r
229 * The interrupt handler for the MTU - which is used to maintain the time base
\r
230 * used by the run time stats.
\r
232 #pragma interrupt MTU_Match
\r
233 void MTU_Match( void );
\r
235 /*-----------------------------------------------------------*/
\r
237 /* Variables that are incremented on each iteration of the reg test tasks -
\r
238 provided the tasks have not reported any errors. The check task inspects these
\r
239 variables to ensure they are still incrementing as expected. */
\r
240 volatile unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
242 /* The status message that is displayed at the bottom of the "task stats" WEB
\r
243 page, which is served by the uIP task. */
\r
244 const char *pcStatusMessage = "All tasks executing without error.";
\r
246 /* The time use for the run time stats. */
\r
247 unsigned long ulRunTime = 0UL;
\r
249 /*-----------------------------------------------------------*/
\r
252 * Creates the majority of the demo application tasks before starting the
\r
257 TaskHandle_t xCreatedTask;
\r
259 prvSetupHardware();
\r
261 /* Start the reg test tasks which test the context switching mechanism. */
\r
262 xTaskCreate( vRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
263 xPortUsesFloatingPoint( xCreatedTask );
\r
265 xTaskCreate( vRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
266 xPortUsesFloatingPoint( xCreatedTask );
\r
268 xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
\r
270 /* Start the check task as described at the top of this file. */
\r
271 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
273 /* Start the standard demo tasks. These don't perform any particular useful
\r
274 functionality, other than to demonstrate the FreeRTOS API being used. */
\r
275 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
276 vCreateBlockTimeTasks();
\r
277 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
278 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
279 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
280 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
281 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
282 vStartQueuePeekTasks();
\r
283 vStartRecursiveMutexTasks();
\r
285 /* Start the math tasks as described at the top of this file. */
\r
286 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
288 /* The suicide tasks must be created last as they need to know how many
\r
289 tasks were running prior to their creation in order to ascertain whether
\r
290 or not the correct/expected number of tasks are running at any given time. */
\r
291 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
293 /* Start the tasks running. */
\r
294 vTaskStartScheduler();
\r
296 /* Will only get here if there was insufficient heap memory to create the idle
\r
297 task. Increase the configTOTAL_HEAP_SIZE setting in FreeRTOSConfig.h. */
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvCheckTask( void *pvParameter )
\r
304 TickType_t xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
305 unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
307 /* Just to remove compiler warning. */
\r
308 ( void ) pvParameter;
\r
310 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
311 xNextWakeTime = xTaskGetTickCount();
\r
315 /* Place this task in the blocked state until it is time to run again. */
\r
316 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
318 /* Inspect all the other tasks to ensure none have experienced any errors. */
\r
319 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
321 /* Increase the rate at which this task cycles, which will increase the
\r
322 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
324 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
325 pcStatusMessage = "Error in GenQ test.";
\r
327 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
329 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
330 pcStatusMessage = "Error in Queue Peek test.";
\r
332 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
334 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
335 pcStatusMessage = "Error in Blocking Queue test.";
\r
337 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
339 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
340 pcStatusMessage = "Error in BlockTim test.";
\r
342 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
344 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
345 pcStatusMessage = "Error in Semaphore test.";
\r
347 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
349 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
350 pcStatusMessage = "Error in Polling Queue test.";
\r
352 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
354 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
355 pcStatusMessage = "Error in Create test.";
\r
357 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
359 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
360 pcStatusMessage = "Error in integer Math test.";
\r
362 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
364 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
365 pcStatusMessage = "Error in recursive mutex test.";
\r
367 else if( xAreMathsTaskStillRunning() != pdTRUE )
\r
369 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
370 pcStatusMessage = "Error in floating point Math test.";
\r
373 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
374 their loop counters if they encounter an error. */
\r
375 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
377 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
378 pcStatusMessage = "Error in RegTest.";
\r
381 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
383 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
384 pcStatusMessage = "Error in RegTest.";
\r
387 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
388 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
390 /* Toggle the check LED to give an indication of the system status. If the
\r
391 LED toggles every 5 seconds then everything is ok. A faster toggle indicates
\r
393 vParTestToggleLED( mainCHECK_LED );
\r
396 /*-----------------------------------------------------------*/
\r
398 void vApplicationMallocFailedHook( void )
\r
400 /* A call to vPortMalloc() failed, probably during the creation of a task,
\r
401 queue or semaphore. Inspect pxCurrentTCB to find which task is currently
\r
405 /*-----------------------------------------------------------*/
\r
407 void vApplicationIdleHook( void )
\r
409 /* Code can be added to the idle task here. This function must *NOT* attempt
\r
410 to block. Also, if the application uses the vTaskDelete() API function then
\r
411 this function must return regularly to ensure the idle task gets a chance to
\r
412 clean up the memory used by deleted tasks. */
\r
414 /*-----------------------------------------------------------*/
\r
416 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
418 /* Just to remove compiler warnings. This function will only actually
\r
419 get called if configCHECK_FOR_STACK_OVERFLOW is set to a non zero value.
\r
420 By default this demo does not use the stack overflow checking functionality
\r
421 as the SuperH will normally execute an exception if the stack overflows. */
\r
423 ( void ) pcTaskName;
\r
425 taskDISABLE_INTERRUPTS();
\r
428 /*-----------------------------------------------------------*/
\r
430 static void prvSetupHardware( void )
\r
432 volatile unsigned long ul;
\r
434 /* Set the CPU and peripheral clocks. */
\r
435 CPG.FRQCR.WORD = mainFRQCR_VALUE;
\r
437 /* Wait for the clock to settle. */
\r
438 for( ul = 0; ul < 99; ul++ )
\r
443 /* Initialise the ports used to toggle LEDs. */
\r
444 vParTestInitialise();
\r
446 /*-----------------------------------------------------------*/
\r
448 void vApplicationSetupTimerInterrupt( void )
\r
450 /* The peripheral clock is divided by 32 before feeding the compare match
\r
451 peripheral (CMT). */
\r
452 const unsigned long ulCompareMatch = ( configPERIPHERAL_CLOCK_HZ / ( configTICK_RATE_HZ * 32 ) ) + 1;
\r
454 /* Configure a timer to create the RTOS tick interrupt. This example uses
\r
455 the compare match timer, but the multi function timer or possible even the
\r
456 watchdog timer could also be used. Ensure vPortTickInterrupt() is installed
\r
457 as the interrupt handler for whichever peripheral is used. */
\r
459 /* Turn the CMT on. */
\r
460 STB.CR4.BIT._CMT = 0;
\r
462 /* Set the compare match value for the required tick frequency. */
\r
463 CMT0.CMCOR = ( unsigned short ) ulCompareMatch;
\r
465 /* Divide the peripheral clock by 32. */
\r
466 CMT0.CMCSR.BIT.CKS = 0x01;
\r
468 /* Set the CMT interrupt priority - the interrupt priority must be
\r
469 configKERNEL_INTERRUPT_PRIORITY no matter which peripheral is used to generate
\r
470 the tick interrupt. */
\r
471 INTC.IPR08.BIT._CMT0 = portKERNEL_INTERRUPT_PRIORITY;
\r
473 /* Clear the interrupt flag. */
\r
474 CMT0.CMCSR.BIT.CMF = 0;
\r
476 /* Enable the compare match interrupt. */
\r
477 CMT0.CMCSR.BIT.CMIE = 0x01;
\r
479 /* Start the timer. */
\r
480 CMT.CMSTR.BIT.STR0 = 0x01;
\r
482 /*-----------------------------------------------------------*/
\r
484 void vApplicationTickHook( void )
\r
486 /* Clear the tick inerrupt. This is called from an interrupt context. */
\r
487 CMT0.CMCSR.BIT.CMF = 0;
\r
489 /*-----------------------------------------------------------*/
\r
491 void vSetupClockForRunTimeStats( void )
\r
493 /* Configure an MTU channel to generate a periodic interrupt that is used
\r
494 as the run time stats time base. The run time stats keep a track of how
\r
495 much processing time each task is using. */
\r
497 /* Turn the MTU2 on. */
\r
498 STB.CR3.BIT._MTU2 = 0;
\r
500 /* Clear counter on compare match A. */
\r
501 MTU20.TCR.BIT.CCLR = 0x01;
\r
503 /* Compare match value to give very approximately 10 interrupts per
\r
507 /* Ensure the interrupt is clear. */
\r
508 MTU20.TSR.BIT.TGFA = 0;
\r
510 /* Enable the compare match interrupt. */
\r
511 MTU20.TIER.BIT.TGIEA = 0x01;
\r
513 /* Set the interrupt priority. */
\r
514 INTC.IPR09.BIT._MTU20G = portKERNEL_INTERRUPT_PRIORITY + 1;
\r
516 /* Start the count. */
\r
517 MTU2.TSTR.BIT.CST0 = 1;
\r
519 /*-----------------------------------------------------------*/
\r
521 void MTU_Match( void )
\r
523 volatile unsigned char ucStatus;
\r
525 /* Increment the run time stats time base. */
\r
528 /* Clear the interrupt. */
\r
529 ucStatus = MTU20.TSR.BYTE;
\r
530 MTU20.TSR.BIT.TGFA = 0;
\r
532 /*-----------------------------------------------------------*/
\r
534 char *pcGetTaskStatusMessage( void )
\r
536 /* Not bothered about a critical section here. This just returns a string
\r
537 that is displaed on the "Task Stats" WEB page served by this demo. */
\r
538 return pcStatusMessage;
\r
540 /*-----------------------------------------------------------*/
\r