2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
73 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
74 * documentation provides more details of the standard demo application tasks,
\r
75 * which provide no particular functionality but do provide a good example of
\r
76 * how to use the FreeRTOS API. In addition to the standard demo tasks, the
\r
77 * following tasks and tests are defined and/or created within this file:
\r
79 * Webserver ("uIP") task - This serves a number of dynamically generated WEB
\r
80 * pages to a standard WEB browser. The IP and MAC addresses are configured by
\r
81 * constants defined at the bottom of FreeRTOSConfig.h. Use either a standard
\r
82 * Ethernet cable to connect through a hug, or a cross over (point to point)
\r
83 * cable to connect directly. Ensure the IP address used is compatible with the
\r
84 * IP address of the machine running the browser - the easiest way to achieve
\r
85 * this is to ensure the first three octets of the IP addresses are the same.
\r
87 * "Reg test" tasks - These fill the registers with known values, then check
\r
88 * that each register still contains its expected value. Each task uses
\r
89 * different values. The tasks run with very low priority so get preempted very
\r
90 * frequently. A register containing an unexpected value is indicative of an
\r
91 * error in the context switching mechanism. Both standard and floating point
\r
92 * registers are checked. The nature of the reg test tasks necessitates that
\r
93 * they are written in assembly code. They are defined in regtest.src.
\r
95 * "math" tasks - These are a set of 8 tasks that perform various double
\r
96 * precision floating point calculations in order to check that the tasks
\r
97 * floating point registers are being correctly saved and restored during
\r
98 * context switches. The math tasks are defined in flop.c.
\r
100 * "Check" task - This only executes every five seconds but has a high priority
\r
101 * to ensure it gets processor time. Its main function is to check that all the
\r
102 * standard demo tasks are still operational. While no errors have been
\r
103 * discovered the check task will toggle an LED every 5 seconds - the toggle
\r
104 * rate increasing to 200ms being a visual indication that at least one task has
\r
105 * reported unexpected behaviour.
\r
107 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
108 * tasks are executing as expected and no errors have been reported in any
\r
109 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
110 * has reported unexpected behaviour.
\r
112 * *NOTE 2* This file and flop.c both demonstrate the use of
\r
113 * xPortUsesFloatingPoint() which informs the kernel that a task should maintain
\r
114 * a floating point context.
\r
116 * *NOTE 3* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
117 * the application set up a timer to generate the tick interrupt. In this
\r
118 * example a compare match timer is used for this purpose.
\r
119 * vApplicationTickHook() is used to clear the timer interrupt and relies on
\r
120 * configUSE_TICK_HOOK being set to 1 in FreeRTOSConfig.h.
\r
122 * *NOTE 4* The traceTASK_SWITCHED_IN and traceTASK_SWITCHED_OUT trace hooks
\r
123 * are used to save and restore the floating point context respectively for
\r
124 * those tasks that require it (those for which xPortUsesFloatingPoint() has
\r
127 * *NOTE 5* Any interrupt that can cause a context switch requires an asm
\r
128 * wrapper and must be assigned an interrupt priority of
\r
129 * portKERNEL_INTERRUPT_PRIORITY.
\r
131 * *NOTE 6* vSetupClockForRunTimeStats() is called by the kernel (via the
\r
132 * portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()) macro to configure an MTU channel
\r
133 * to produce a time base that is used to log how much processor time each task
\r
134 * is consuming. The MTU is used to generate a high(ish) frequency interrupt,
\r
135 * and so also provides an example of how interrupts that don't make use of the
\r
136 * FreeRTOS kernel can be assigned a priority above any priority used by the
\r
140 /* Kernel includes. */
\r
141 #include "FreeRTOS.h"
\r
144 /* Demo application includes. */
\r
145 #include "BlockQ.h"
\r
147 #include "integer.h"
\r
148 #include "blocktim.h"
\r
150 #include "partest.h"
\r
151 #include "semtest.h"
\r
153 #include "GenQTest.h"
\r
155 #include "recmutex.h"
\r
158 /* Constants required to configure the hardware. */
\r
159 #define mainFRQCR_VALUE ( 0x0303 ) /* Input = 12.5MHz, I Clock = 200MHz, B Clock = 50MHz, P Clock = 50MHz */
\r
161 /* Task priorities. */
\r
162 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
163 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
164 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
165 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
166 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
167 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
168 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
169 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
170 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
171 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
173 /* The WEB server uses string handling functions, which in turn use a bit more
\r
174 stack than most of the other tasks. */
\r
175 #define mainuIP_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3 )
\r
177 /* The LED toggled by the check task. */
\r
178 #define mainCHECK_LED ( 5 )
\r
180 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
182 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_PERIOD_MS )
\r
184 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
185 by at least one task. */
\r
186 #define mainERROR_CYCLE_TIME ( 200 / portTICK_PERIOD_MS )
\r
189 * vApplicationMallocFailedHook() will only be called if
\r
190 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
191 * function that will execute if a call to pvPortMalloc() fails.
\r
192 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
193 * semaphore is created. It is also called by various parts of the demo
\r
196 void vApplicationMallocFailedHook( void );
\r
199 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
200 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
201 * of the idle task. It is essential that code added to this hook function
\r
202 * never attempts to block in any way (for example, call xQueueReceive() with
\r
203 * a block time specified). If the application makes use of the vTaskDelete()
\r
204 * API function (as this demo application does) then it is also important that
\r
205 * vApplicationIdleHook() is permitted to return to its calling function because
\r
206 * it is the responsibility of the idle task to clean up memory allocated by the
\r
207 * kernel to any task that has since been deleted.
\r
209 void vApplicationIdleHook( void );
\r
212 * Just sets up clocks, ports, etc. used by the demo application.
\r
214 static void prvSetupHardware( void );
\r
217 * The check task as described at the top of this file.
\r
219 static void prvCheckTask( void *pvParameters );
\r
222 * The reg test tasks as described at the top of this file.
\r
224 extern void vRegTest1Task( void *pvParameters );
\r
225 extern void vRegTest2Task( void *pvParameters );
\r
228 * Contains the implementation of the WEB server.
\r
230 extern void vuIP_Task( void *pvParameters );
\r
233 * The interrupt handler for the MTU - which is used to maintain the time base
\r
234 * used by the run time stats.
\r
236 #pragma interrupt MTU_Match
\r
237 void MTU_Match( void );
\r
239 /*-----------------------------------------------------------*/
\r
241 /* Variables that are incremented on each iteration of the reg test tasks -
\r
242 provided the tasks have not reported any errors. The check task inspects these
\r
243 variables to ensure they are still incrementing as expected. */
\r
244 volatile unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
246 /* The status message that is displayed at the bottom of the "task stats" WEB
\r
247 page, which is served by the uIP task. */
\r
248 const char *pcStatusMessage = "All tasks executing without error.";
\r
250 /* The time use for the run time stats. */
\r
251 unsigned long ulRunTime = 0UL;
\r
253 /*-----------------------------------------------------------*/
\r
256 * Creates the majority of the demo application tasks before starting the
\r
261 TaskHandle_t xCreatedTask;
\r
263 prvSetupHardware();
\r
265 /* Start the reg test tasks which test the context switching mechanism. */
\r
266 xTaskCreate( vRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
267 xPortUsesFloatingPoint( xCreatedTask );
\r
269 xTaskCreate( vRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
270 xPortUsesFloatingPoint( xCreatedTask );
\r
272 xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
\r
274 /* Start the check task as described at the top of this file. */
\r
275 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
277 /* Start the standard demo tasks. These don't perform any particular useful
\r
278 functionality, other than to demonstrate the FreeRTOS API being used. */
\r
279 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
280 vCreateBlockTimeTasks();
\r
281 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
282 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
283 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
284 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
285 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
286 vStartQueuePeekTasks();
\r
287 vStartRecursiveMutexTasks();
\r
289 /* Start the math tasks as described at the top of this file. */
\r
290 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
292 /* The suicide tasks must be created last as they need to know how many
\r
293 tasks were running prior to their creation in order to ascertain whether
\r
294 or not the correct/expected number of tasks are running at any given time. */
\r
295 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
297 /* Start the tasks running. */
\r
298 vTaskStartScheduler();
\r
300 /* Will only get here if there was insufficient heap memory to create the idle
\r
301 task. Increase the configTOTAL_HEAP_SIZE setting in FreeRTOSConfig.h. */
\r
304 /*-----------------------------------------------------------*/
\r
306 static void prvCheckTask( void *pvParameter )
\r
308 TickType_t xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
309 unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
311 /* Just to remove compiler warning. */
\r
312 ( void ) pvParameter;
\r
314 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
315 xNextWakeTime = xTaskGetTickCount();
\r
319 /* Place this task in the blocked state until it is time to run again. */
\r
320 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
322 /* Inspect all the other tasks to ensure none have experienced any errors. */
\r
323 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
325 /* Increase the rate at which this task cycles, which will increase the
\r
326 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
328 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
329 pcStatusMessage = "Error in GenQ test.";
\r
331 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
333 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
334 pcStatusMessage = "Error in Queue Peek test.";
\r
336 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
338 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
339 pcStatusMessage = "Error in Blocking Queue test.";
\r
341 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
343 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
344 pcStatusMessage = "Error in BlockTim test.";
\r
346 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
348 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
349 pcStatusMessage = "Error in Semaphore test.";
\r
351 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
353 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
354 pcStatusMessage = "Error in Polling Queue test.";
\r
356 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
358 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
359 pcStatusMessage = "Error in Create test.";
\r
361 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
363 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
364 pcStatusMessage = "Error in integer Math test.";
\r
366 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
368 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
369 pcStatusMessage = "Error in recursive mutex test.";
\r
371 else if( xAreMathsTaskStillRunning() != pdTRUE )
\r
373 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
374 pcStatusMessage = "Error in floating point Math test.";
\r
377 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
378 their loop counters if they encounter an error. */
\r
379 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
381 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
382 pcStatusMessage = "Error in RegTest.";
\r
385 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
387 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
388 pcStatusMessage = "Error in RegTest.";
\r
391 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
392 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
394 /* Toggle the check LED to give an indication of the system status. If the
\r
395 LED toggles every 5 seconds then everything is ok. A faster toggle indicates
\r
397 vParTestToggleLED( mainCHECK_LED );
\r
400 /*-----------------------------------------------------------*/
\r
402 void vApplicationMallocFailedHook( void )
\r
404 /* A call to vPortMalloc() failed, probably during the creation of a task,
\r
405 queue or semaphore. Inspect pxCurrentTCB to find which task is currently
\r
409 /*-----------------------------------------------------------*/
\r
411 void vApplicationIdleHook( void )
\r
413 /* Code can be added to the idle task here. This function must *NOT* attempt
\r
414 to block. Also, if the application uses the vTaskDelete() API function then
\r
415 this function must return regularly to ensure the idle task gets a chance to
\r
416 clean up the memory used by deleted tasks. */
\r
418 /*-----------------------------------------------------------*/
\r
420 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
422 /* Just to remove compiler warnings. This function will only actually
\r
423 get called if configCHECK_FOR_STACK_OVERFLOW is set to a non zero value.
\r
424 By default this demo does not use the stack overflow checking functionality
\r
425 as the SuperH will normally execute an exception if the stack overflows. */
\r
427 ( void ) pcTaskName;
\r
429 taskDISABLE_INTERRUPTS();
\r
432 /*-----------------------------------------------------------*/
\r
434 static void prvSetupHardware( void )
\r
436 volatile unsigned long ul;
\r
438 /* Set the CPU and peripheral clocks. */
\r
439 CPG.FRQCR.WORD = mainFRQCR_VALUE;
\r
441 /* Wait for the clock to settle. */
\r
442 for( ul = 0; ul < 99; ul++ )
\r
447 /* Initialise the ports used to toggle LEDs. */
\r
448 vParTestInitialise();
\r
450 /*-----------------------------------------------------------*/
\r
452 void vApplicationSetupTimerInterrupt( void )
\r
454 /* The peripheral clock is divided by 32 before feeding the compare match
\r
455 peripheral (CMT). */
\r
456 const unsigned long ulCompareMatch = ( configPERIPHERAL_CLOCK_HZ / ( configTICK_RATE_HZ * 32 ) ) + 1;
\r
458 /* Configure a timer to create the RTOS tick interrupt. This example uses
\r
459 the compare match timer, but the multi function timer or possible even the
\r
460 watchdog timer could also be used. Ensure vPortTickInterrupt() is installed
\r
461 as the interrupt handler for whichever peripheral is used. */
\r
463 /* Turn the CMT on. */
\r
464 STB.CR4.BIT._CMT = 0;
\r
466 /* Set the compare match value for the required tick frequency. */
\r
467 CMT0.CMCOR = ( unsigned short ) ulCompareMatch;
\r
469 /* Divide the peripheral clock by 32. */
\r
470 CMT0.CMCSR.BIT.CKS = 0x01;
\r
472 /* Set the CMT interrupt priority - the interrupt priority must be
\r
473 configKERNEL_INTERRUPT_PRIORITY no matter which peripheral is used to generate
\r
474 the tick interrupt. */
\r
475 INTC.IPR08.BIT._CMT0 = portKERNEL_INTERRUPT_PRIORITY;
\r
477 /* Clear the interrupt flag. */
\r
478 CMT0.CMCSR.BIT.CMF = 0;
\r
480 /* Enable the compare match interrupt. */
\r
481 CMT0.CMCSR.BIT.CMIE = 0x01;
\r
483 /* Start the timer. */
\r
484 CMT.CMSTR.BIT.STR0 = 0x01;
\r
486 /*-----------------------------------------------------------*/
\r
488 void vApplicationTickHook( void )
\r
490 /* Clear the tick inerrupt. This is called from an interrupt context. */
\r
491 CMT0.CMCSR.BIT.CMF = 0;
\r
493 /*-----------------------------------------------------------*/
\r
495 void vSetupClockForRunTimeStats( void )
\r
497 /* Configure an MTU channel to generate a periodic interrupt that is used
\r
498 as the run time stats time base. The run time stats keep a track of how
\r
499 much processing time each task is using. */
\r
501 /* Turn the MTU2 on. */
\r
502 STB.CR3.BIT._MTU2 = 0;
\r
504 /* Clear counter on compare match A. */
\r
505 MTU20.TCR.BIT.CCLR = 0x01;
\r
507 /* Compare match value to give very approximately 10 interrupts per
\r
511 /* Ensure the interrupt is clear. */
\r
512 MTU20.TSR.BIT.TGFA = 0;
\r
514 /* Enable the compare match interrupt. */
\r
515 MTU20.TIER.BIT.TGIEA = 0x01;
\r
517 /* Set the interrupt priority. */
\r
518 INTC.IPR09.BIT._MTU20G = portKERNEL_INTERRUPT_PRIORITY + 1;
\r
520 /* Start the count. */
\r
521 MTU2.TSTR.BIT.CST0 = 1;
\r
523 /*-----------------------------------------------------------*/
\r
525 void MTU_Match( void )
\r
527 volatile unsigned char ucStatus;
\r
529 /* Increment the run time stats time base. */
\r
532 /* Clear the interrupt. */
\r
533 ucStatus = MTU20.TSR.BYTE;
\r
534 MTU20.TSR.BIT.TGFA = 0;
\r
536 /*-----------------------------------------------------------*/
\r
538 char *pcGetTaskStatusMessage( void )
\r
540 /* Not bothered about a critical section here. This just returns a string
\r
541 that is displaed on the "Task Stats" WEB page served by this demo. */
\r
542 return pcStatusMessage;
\r
544 /*-----------------------------------------------------------*/
\r