2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
78 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
79 * documentation provides more details of the standard demo application tasks,
\r
80 * which provide no particular functionality but do provide a good example of
\r
81 * how to use the FreeRTOS API. In addition to the standard demo tasks, the
\r
82 * following tasks and tests are defined and/or created within this file:
\r
84 * Webserver ("uIP") task - This serves a number of dynamically generated WEB
\r
85 * pages to a standard WEB browser. The IP and MAC addresses are configured by
\r
86 * constants defined at the bottom of FreeRTOSConfig.h. Use either a standard
\r
87 * Ethernet cable to connect through a hug, or a cross over (point to point)
\r
88 * cable to connect directly. Ensure the IP address used is compatible with the
\r
89 * IP address of the machine running the browser - the easiest way to achieve
\r
90 * this is to ensure the first three octets of the IP addresses are the same.
\r
92 * "Reg test" tasks - These fill the registers with known values, then check
\r
93 * that each register still contains its expected value. Each task uses
\r
94 * different values. The tasks run with very low priority so get preempted very
\r
95 * frequently. A register containing an unexpected value is indicative of an
\r
96 * error in the context switching mechanism. Both standard and floating point
\r
97 * registers are checked. The nature of the reg test tasks necessitates that
\r
98 * they are written in assembly code. They are defined in regtest.src.
\r
100 * "math" tasks - These are a set of 8 tasks that perform various double
\r
101 * precision floating point calculations in order to check that the tasks
\r
102 * floating point registers are being correctly saved and restored during
\r
103 * context switches. The math tasks are defined in flop.c.
\r
105 * "Check" task - This only executes every five seconds but has a high priority
\r
106 * to ensure it gets processor time. Its main function is to check that all the
\r
107 * standard demo tasks are still operational. While no errors have been
\r
108 * discovered the check task will toggle an LED every 5 seconds - the toggle
\r
109 * rate increasing to 200ms being a visual indication that at least one task has
\r
110 * reported unexpected behaviour.
\r
112 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
113 * tasks are executing as expected and no errors have been reported in any
\r
114 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
115 * has reported unexpected behaviour.
\r
117 * *NOTE 2* This file and flop.c both demonstrate the use of
\r
118 * xPortUsesFloatingPoint() which informs the kernel that a task should maintain
\r
119 * a floating point context.
\r
121 * *NOTE 3* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
122 * the application set up a timer to generate the tick interrupt. In this
\r
123 * example a compare match timer is used for this purpose.
\r
124 * vApplicationTickHook() is used to clear the timer interrupt and relies on
\r
125 * configUSE_TICK_HOOK being set to 1 in FreeRTOSConfig.h.
\r
127 * *NOTE 4* The traceTASK_SWITCHED_IN and traceTASK_SWITCHED_OUT trace hooks
\r
128 * are used to save and restore the floating point context respectively for
\r
129 * those tasks that require it (those for which xPortUsesFloatingPoint() has
\r
132 * *NOTE 5* Any interrupt that can cause a context switch requires an asm
\r
133 * wrapper and must be assigned an interrupt priority of
\r
134 * portKERNEL_INTERRUPT_PRIORITY.
\r
136 * *NOTE 6* vSetupClockForRunTimeStats() is called by the kernel (via the
\r
137 * portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()) macro to configure an MTU channel
\r
138 * to produce a time base that is used to log how much processor time each task
\r
139 * is consuming. The MTU is used to generate a high(ish) frequency interrupt,
\r
140 * and so also provides an example of how interrupts that don't make use of the
\r
141 * FreeRTOS kernel can be assigned a priority above any priority used by the
\r
145 /* Kernel includes. */
\r
146 #include "FreeRTOS.h"
\r
149 /* Demo application includes. */
\r
150 #include "BlockQ.h"
\r
152 #include "integer.h"
\r
153 #include "blocktim.h"
\r
155 #include "partest.h"
\r
156 #include "semtest.h"
\r
158 #include "GenQTest.h"
\r
160 #include "recmutex.h"
\r
163 /* Constants required to configure the hardware. */
\r
164 #define mainFRQCR_VALUE ( 0x0303 ) /* Input = 12.5MHz, I Clock = 200MHz, B Clock = 50MHz, P Clock = 50MHz */
\r
166 /* Task priorities. */
\r
167 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
168 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
169 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
170 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
171 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
172 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
173 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
174 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
175 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
176 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
178 /* The WEB server uses string handling functions, which in turn use a bit more
\r
179 stack than most of the other tasks. */
\r
180 #define mainuIP_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3 )
\r
182 /* The LED toggled by the check task. */
\r
183 #define mainCHECK_LED ( 5 )
\r
185 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
187 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_RATE_MS )
\r
189 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
190 by at least one task. */
\r
191 #define mainERROR_CYCLE_TIME ( 200 / portTICK_RATE_MS )
\r
194 * vApplicationMallocFailedHook() will only be called if
\r
195 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
196 * function that will execute if a call to pvPortMalloc() fails.
\r
197 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
198 * semaphore is created. It is also called by various parts of the demo
\r
201 void vApplicationMallocFailedHook( void );
\r
204 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
205 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
206 * of the idle task. It is essential that code added to this hook function
\r
207 * never attempts to block in any way (for example, call xQueueReceive() with
\r
208 * a block time specified). If the application makes use of the vTaskDelete()
\r
209 * API function (as this demo application does) then it is also important that
\r
210 * vApplicationIdleHook() is permitted to return to its calling function because
\r
211 * it is the responsibility of the idle task to clean up memory allocated by the
\r
212 * kernel to any task that has since been deleted.
\r
214 void vApplicationIdleHook( void );
\r
217 * Just sets up clocks, ports, etc. used by the demo application.
\r
219 static void prvSetupHardware( void );
\r
222 * The check task as described at the top of this file.
\r
224 static void prvCheckTask( void *pvParameters );
\r
227 * The reg test tasks as described at the top of this file.
\r
229 extern void vRegTest1Task( void *pvParameters );
\r
230 extern void vRegTest2Task( void *pvParameters );
\r
233 * Contains the implementation of the WEB server.
\r
235 extern void vuIP_Task( void *pvParameters );
\r
238 * The interrupt handler for the MTU - which is used to maintain the time base
\r
239 * used by the run time stats.
\r
241 #pragma interrupt MTU_Match
\r
242 void MTU_Match( void );
\r
244 /*-----------------------------------------------------------*/
\r
246 /* Variables that are incremented on each iteration of the reg test tasks -
\r
247 provided the tasks have not reported any errors. The check task inspects these
\r
248 variables to ensure they are still incrementing as expected. */
\r
249 volatile unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
251 /* The status message that is displayed at the bottom of the "task stats" WEB
\r
252 page, which is served by the uIP task. */
\r
253 const char *pcStatusMessage = "All tasks executing without error.";
\r
255 /* The time use for the run time stats. */
\r
256 unsigned long ulRunTime = 0UL;
\r
258 /*-----------------------------------------------------------*/
\r
261 * Creates the majority of the demo application tasks before starting the
\r
266 xTaskHandle xCreatedTask;
\r
268 prvSetupHardware();
\r
270 /* Start the reg test tasks which test the context switching mechanism. */
\r
271 xTaskCreate( vRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
272 xPortUsesFloatingPoint( xCreatedTask );
\r
274 xTaskCreate( vRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
275 xPortUsesFloatingPoint( xCreatedTask );
\r
277 xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
\r
279 /* Start the check task as described at the top of this file. */
\r
280 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
282 /* Start the standard demo tasks. These don't perform any particular useful
\r
283 functionality, other than to demonstrate the FreeRTOS API being used. */
\r
284 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
285 vCreateBlockTimeTasks();
\r
286 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
287 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
288 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
289 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
290 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
291 vStartQueuePeekTasks();
\r
292 vStartRecursiveMutexTasks();
\r
294 /* Start the math tasks as described at the top of this file. */
\r
295 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
297 /* The suicide tasks must be created last as they need to know how many
\r
298 tasks were running prior to their creation in order to ascertain whether
\r
299 or not the correct/expected number of tasks are running at any given time. */
\r
300 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
302 /* Start the tasks running. */
\r
303 vTaskStartScheduler();
\r
305 /* Will only get here if there was insufficient heap memory to create the idle
\r
306 task. Increase the configTOTAL_HEAP_SIZE setting in FreeRTOSConfig.h. */
\r
309 /*-----------------------------------------------------------*/
\r
311 static void prvCheckTask( void *pvParameter )
\r
313 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
314 unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
316 /* Just to remove compiler warning. */
\r
317 ( void ) pvParameter;
\r
319 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
320 xNextWakeTime = xTaskGetTickCount();
\r
324 /* Place this task in the blocked state until it is time to run again. */
\r
325 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
327 /* Inspect all the other tasks to ensure none have experienced any errors. */
\r
328 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
330 /* Increase the rate at which this task cycles, which will increase the
\r
331 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
333 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
334 pcStatusMessage = "Error in GenQ test.";
\r
336 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
338 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
339 pcStatusMessage = "Error in Queue Peek test.";
\r
341 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
343 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
344 pcStatusMessage = "Error in Blocking Queue test.";
\r
346 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
348 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
349 pcStatusMessage = "Error in BlockTim test.";
\r
351 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
353 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
354 pcStatusMessage = "Error in Semaphore test.";
\r
356 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
358 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
359 pcStatusMessage = "Error in Polling Queue test.";
\r
361 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
363 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
364 pcStatusMessage = "Error in Create test.";
\r
366 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
368 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
369 pcStatusMessage = "Error in integer Math test.";
\r
371 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
373 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
374 pcStatusMessage = "Error in recursive mutex test.";
\r
376 else if( xAreMathsTaskStillRunning() != pdTRUE )
\r
378 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
379 pcStatusMessage = "Error in floating point Math test.";
\r
382 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
383 their loop counters if they encounter an error. */
\r
384 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
386 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
387 pcStatusMessage = "Error in RegTest.";
\r
390 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
392 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
393 pcStatusMessage = "Error in RegTest.";
\r
396 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
397 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
399 /* Toggle the check LED to give an indication of the system status. If the
\r
400 LED toggles every 5 seconds then everything is ok. A faster toggle indicates
\r
402 vParTestToggleLED( mainCHECK_LED );
\r
405 /*-----------------------------------------------------------*/
\r
407 void vApplicationMallocFailedHook( void )
\r
409 /* A call to vPortMalloc() failed, probably during the creation of a task,
\r
410 queue or semaphore. Inspect pxCurrentTCB to find which task is currently
\r
414 /*-----------------------------------------------------------*/
\r
416 void vApplicationIdleHook( void )
\r
418 /* Code can be added to the idle task here. This function must *NOT* attempt
\r
419 to block. Also, if the application uses the vTaskDelete() API function then
\r
420 this function must return regularly to ensure the idle task gets a chance to
\r
421 clean up the memory used by deleted tasks. */
\r
423 /*-----------------------------------------------------------*/
\r
425 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
427 /* Just to remove compiler warnings. This function will only actually
\r
428 get called if configCHECK_FOR_STACK_OVERFLOW is set to a non zero value.
\r
429 By default this demo does not use the stack overflow checking functionality
\r
430 as the SuperH will normally execute an exception if the stack overflows. */
\r
432 ( void ) pcTaskName;
\r
434 taskDISABLE_INTERRUPTS();
\r
437 /*-----------------------------------------------------------*/
\r
439 static void prvSetupHardware( void )
\r
441 volatile unsigned long ul;
\r
443 /* Set the CPU and peripheral clocks. */
\r
444 CPG.FRQCR.WORD = mainFRQCR_VALUE;
\r
446 /* Wait for the clock to settle. */
\r
447 for( ul = 0; ul < 99; ul++ )
\r
452 /* Initialise the ports used to toggle LEDs. */
\r
453 vParTestInitialise();
\r
455 /*-----------------------------------------------------------*/
\r
457 void vApplicationSetupTimerInterrupt( void )
\r
459 /* The peripheral clock is divided by 32 before feeding the compare match
\r
460 peripheral (CMT). */
\r
461 const unsigned long ulCompareMatch = ( configPERIPHERAL_CLOCK_HZ / ( configTICK_RATE_HZ * 32 ) ) + 1;
\r
463 /* Configure a timer to create the RTOS tick interrupt. This example uses
\r
464 the compare match timer, but the multi function timer or possible even the
\r
465 watchdog timer could also be used. Ensure vPortTickInterrupt() is installed
\r
466 as the interrupt handler for whichever peripheral is used. */
\r
468 /* Turn the CMT on. */
\r
469 STB.CR4.BIT._CMT = 0;
\r
471 /* Set the compare match value for the required tick frequency. */
\r
472 CMT0.CMCOR = ( unsigned short ) ulCompareMatch;
\r
474 /* Divide the peripheral clock by 32. */
\r
475 CMT0.CMCSR.BIT.CKS = 0x01;
\r
477 /* Set the CMT interrupt priority - the interrupt priority must be
\r
478 configKERNEL_INTERRUPT_PRIORITY no matter which peripheral is used to generate
\r
479 the tick interrupt. */
\r
480 INTC.IPR08.BIT._CMT0 = portKERNEL_INTERRUPT_PRIORITY;
\r
482 /* Clear the interrupt flag. */
\r
483 CMT0.CMCSR.BIT.CMF = 0;
\r
485 /* Enable the compare match interrupt. */
\r
486 CMT0.CMCSR.BIT.CMIE = 0x01;
\r
488 /* Start the timer. */
\r
489 CMT.CMSTR.BIT.STR0 = 0x01;
\r
491 /*-----------------------------------------------------------*/
\r
493 void vApplicationTickHook( void )
\r
495 /* Clear the tick inerrupt. This is called from an interrupt context. */
\r
496 CMT0.CMCSR.BIT.CMF = 0;
\r
498 /*-----------------------------------------------------------*/
\r
500 void vSetupClockForRunTimeStats( void )
\r
502 /* Configure an MTU channel to generate a periodic interrupt that is used
\r
503 as the run time stats time base. The run time stats keep a track of how
\r
504 much processing time each task is using. */
\r
506 /* Turn the MTU2 on. */
\r
507 STB.CR3.BIT._MTU2 = 0;
\r
509 /* Clear counter on compare match A. */
\r
510 MTU20.TCR.BIT.CCLR = 0x01;
\r
512 /* Compare match value to give very approximately 10 interrupts per
\r
516 /* Ensure the interrupt is clear. */
\r
517 MTU20.TSR.BIT.TGFA = 0;
\r
519 /* Enable the compare match interrupt. */
\r
520 MTU20.TIER.BIT.TGIEA = 0x01;
\r
522 /* Set the interrupt priority. */
\r
523 INTC.IPR09.BIT._MTU20G = portKERNEL_INTERRUPT_PRIORITY + 1;
\r
525 /* Start the count. */
\r
526 MTU2.TSTR.BIT.CST0 = 1;
\r
528 /*-----------------------------------------------------------*/
\r
530 void MTU_Match( void )
\r
532 volatile unsigned char ucStatus;
\r
534 /* Increment the run time stats time base. */
\r
537 /* Clear the interrupt. */
\r
538 ucStatus = MTU20.TSR.BYTE;
\r
539 MTU20.TSR.BIT.TGFA = 0;
\r
541 /*-----------------------------------------------------------*/
\r
543 char *pcGetTaskStatusMessage( void )
\r
545 /* Not bothered about a critical section here. This just returns a string
\r
546 that is displaed on the "Task Stats" WEB page served by this demo. */
\r
547 return pcStatusMessage;
\r
549 /*-----------------------------------------------------------*/
\r