2 FreeRTOS V6.0.2 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
58 * documentation provides more details of the standard demo application tasks,
\r
59 * which provide no particular functionality but do provide a good example of
\r
60 * how to use the FreeRTOS API. In addition to the standard demo tasks, the
\r
61 * following tasks and tests are defined and/or created within this file:
\r
63 * "Reg test" tasks - These fill the registers with known values, then check
\r
64 * that each register still contains its expected value. Each task uses
\r
65 * different values. The tasks run with very low priority so get preempted very
\r
66 * frequently. A register containing an unexpected value is indicative of an
\r
67 * error in the context switching mechanism. Both standard and floating point
\r
68 * registers are checked. The nature of the reg test tasks necessitates that
\r
69 * they are written in assembly code. They are defined in regtest.src.
\r
71 * "math" tasks - These are a set of 8 tasks that perform various double
\r
72 * precision floating point calculations in order to check that the tasks
\r
73 * floating point registers are being correctly saved and restored during
\r
74 * context switches. The math tasks are defined in flop.c.
\r
76 * "Check" task - This only executes every five seconds but has a high priority
\r
77 * to ensure it gets processor time. Its main function is to check that all the
\r
78 * standard demo tasks are still operational. While no errors have been
\r
79 * discovered the check task will toggle an LED every 5 seconds - the toggle
\r
80 * rate increasing to 500ms being a visual indication that at least one task has
\r
81 * reported unexpected behaviour.
\r
83 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
84 * tasks are executing as expected and no errors have been reported in any
\r
85 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
86 * has reported unexpected behaviour.
\r
88 * *NOTE 2* This file and flop.c both demonstrate the use of
\r
89 * xPortUsesFloatingPoint() which informs the kernel that a task should maintain
\r
90 * a floating point context.
\r
92 * *NOTE 3* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
93 * the application set up a timer to generate the tick interrupt. In this
\r
94 * example a compare match timer is used for this purpose.
\r
95 * vApplicationTickHook() is used to clear the timer interrupt and relies on
\r
96 * configUSE_TICK_HOOK being set to 1 in FreeRTOSConfig.h.
\r
98 * *NOTE 4* The traceTASK_SWITCHED_IN and traceTASK_SWITCHED_OUT trace hooks
\r
99 * are used to save and restore the floating point context respectively for
\r
100 * those tasks that require it (those for which xPortUsesFloatingPoint() has
\r
103 * *NOTE 5* Any task that can cause a context switch requires an asm wrapper
\r
104 * and must be assigned an interrupt priority of portKERNEL_INTERRUPT_PRIORITY.
\r
107 /* Kernel includes. */
\r
108 #include "FreeRTOS.h"
\r
111 /* Demo application includes. */
\r
112 #include "BlockQ.h"
\r
114 #include "integer.h"
\r
115 #include "blocktim.h"
\r
117 #include "partest.h"
\r
118 #include "semtest.h"
\r
120 #include "GenQTest.h"
\r
122 #include "recmutex.h"
\r
125 /* Constants required to configure the hardware. */
\r
126 #define mainFRQCR_VALUE ( 0x0303 ) /* Input = 12.5MHz, I Clock = 200MHz, B Clock = 50MHz, P Clock = 50MHz */
\r
128 /* Task priorities. */
\r
129 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
130 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
131 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
132 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
133 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
134 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
135 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
136 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
137 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
139 /* The LED toggle by the check task. */
\r
140 #define mainCHECK_LED ( 5 )
\r
142 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
144 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_RATE_MS )
\r
146 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
147 by at least one task. */
\r
148 #define mainERROR_CYCLE_TIME ( 200 / portTICK_RATE_MS )
\r
151 * vApplicationMallocFailedHook() will only be called if
\r
152 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
153 * function that will execute if a call to pvPortMalloc() fails.
\r
154 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
155 * semaphore is created. It is also called by various parts of the demo
\r
158 void vApplicationMallocFailedHook( void );
\r
161 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
162 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
163 * of the idle task. It is essential that code added to this hook function
\r
164 * never attempts to block in any way (for example, call xQueueReceive() with
\r
165 * a block time specified). If the application makes use of the vTaskDelete()
\r
166 * API function (as this demo application does) then it is also important that
\r
167 * vApplicationIdleHook() is permitted to return to its calling function because
\r
168 * it is the responsibility of the idle task to clean up memory allocated by the
\r
169 * kernel to any task that has since been deleted.
\r
171 void vApplicationIdleHook( void );
\r
174 * Just sets up clocks, ports, etc. used by the demo application.
\r
176 static void prvSetupHardware( void );
\r
179 * The check task as described at the top of this file.
\r
181 static void prvCheckTask( void *pvParameters );
\r
184 * The reg test tasks as described at the top of this file.
\r
186 extern void vRegTest1Task( void *pvParameters );
\r
187 extern void vRegTest2Task( void *pvParameters );
\r
189 /*-----------------------------------------------------------*/
\r
191 /* Variables that are incremented on each iteration of the reg test tasks -
\r
192 provided the tasks have not reported any errors. The check task inspects these
\r
193 variables to ensure they are still incrementing as expected. */
\r
194 volatile unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
196 /*-----------------------------------------------------------*/
\r
199 * Creates the majority of the demo application tasks before starting the
\r
204 xTaskHandle xCreatedTask;
\r
206 prvSetupHardware();
\r
208 /* Start the reg test tasks which test the context switching mechanism. */
\r
209 xTaskCreate( vRegTest1Task, "RegTest1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
210 xPortUsesFloatingPoint( xCreatedTask );
\r
212 xTaskCreate( vRegTest2Task, "RegTest2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
213 xPortUsesFloatingPoint( xCreatedTask );
\r
215 /* Start the check task as described at the top of this file. */
\r
216 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
218 /* Start the standard demo tasks. These don't perform any particular useful
\r
219 functionality, other than to demonstrate the FreeRTOS API being used. */
\r
220 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
221 vCreateBlockTimeTasks();
\r
222 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
223 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
224 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
225 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
226 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
227 vStartQueuePeekTasks();
\r
228 vStartRecursiveMutexTasks();
\r
230 /* Start the math tasks as described at the top of this file. */
\r
231 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
233 /* The suicide tasks must be created last as they need to know how many
\r
234 tasks were running prior to their creation in order to ascertain whether
\r
235 or not the correct/expected number of tasks are running at any given time. */
\r
236 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
238 /* Start the tasks running. */
\r
239 vTaskStartScheduler();
\r
241 /* Will only get here if there was insufficient heap memory to create the idle
\r
242 task. Increase the configTOTAL_HEAP_SIZE setting in FreeRTOSConfig.h. */
\r
245 /*-----------------------------------------------------------*/
\r
247 static void prvCheckTask( void *pvParameter )
\r
249 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
250 unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
252 /* Just to remove compiler warning. */
\r
253 ( void ) pvParameter;
\r
255 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
256 xNextWakeTime = xTaskGetTickCount();
\r
260 /* Place this task in the blocked state until it is time to run again. */
\r
261 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
263 /* Inspect all the other tasks to ensure none have experienced any errors. */
\r
264 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
266 /* Increase the rate at which this task cycles, which will increase the
\r
267 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
269 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
271 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
273 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
275 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
277 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
279 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
281 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
283 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
285 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
287 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
289 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
291 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
293 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
295 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
297 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
299 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
301 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
303 else if( xAreMathsTaskStillRunning() != pdTRUE )
\r
305 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
308 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
309 their loop counters if they encounter an error. */
\r
310 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
312 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
315 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
317 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
320 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
321 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
323 /* Toggle the check LED to give an indication of the system status. If the
\r
324 LED toggles every 5 seconds then everything is ok. A faster toggle indicates
\r
326 vParTestToggleLED( mainCHECK_LED );
\r
329 /*-----------------------------------------------------------*/
\r
331 void vApplicationMallocFailedHook( void )
\r
333 /* A call to vPortMalloc() failed, probably during the creation of a task,
\r
334 queue or semaphore. Inspect pxCurrentTCB to find which task is currently
\r
338 /*-----------------------------------------------------------*/
\r
340 void vApplicationIdleHook( void )
\r
342 /* Code can be added to the idle task here. This function must *NOT* attempt
\r
343 to block. Also, if the application uses the vTaskDelete() API function then
\r
344 this function must return regularly to ensure the idle task gets a chance to
\r
345 clean up the memory used by deleted tasks. */
\r
347 /*-----------------------------------------------------------*/
\r
349 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
351 /* Just to remove compiler warnings. This function will only actually
\r
352 get called if configCHECK_FOR_STACK_OVERFLOW is set to a non zero value.
\r
353 By default this demo does not use the stack overflow checking functionality
\r
354 as the SuperH will normally execute an exception if the stack overflows. */
\r
356 ( void ) pcTaskName;
\r
360 /*-----------------------------------------------------------*/
\r
362 static void prvSetupHardware( void )
\r
364 volatile unsigned long ul;
\r
366 /* Set the CPU and peripheral clocks. */
\r
367 CPG.FRQCR.WORD = mainFRQCR_VALUE;
\r
369 /* Wait for the clock to settle. */
\r
370 for( ul = 0; ul < 99; ul++ )
\r
375 /* Initialise the ports used to toggle LEDs. */
\r
376 vParTestInitialise();
\r
378 /*-----------------------------------------------------------*/
\r
380 void vApplicationSetupTimerInterrupt( void )
\r
382 /* The peripheral clock is divided by 32 before feeding the compare match
\r
383 peripheral (CMT). */
\r
384 unsigned long ulCompareMatch = ( configPERIPHERAL_CLOCK_HZ / ( configTICK_RATE_HZ * 32 ) ) + 1;
\r
386 /* Configure a timer to create the RTOS tick interrupt. This example uses
\r
387 the compare match timer, but the multi function timer or possible even the
\r
388 watchdog timer could also be used. Ensure vPortTickInterrupt() is installed
\r
389 as the interrupt handler for whichever peripheral is used. */
\r
391 /* Turn the CMT on. */
\r
392 STB.CR4.BIT._CMT = 0;
\r
394 /* Set the compare match value for the required tick frequency. */
\r
395 CMT0.CMCOR = ( unsigned short ) ulCompareMatch;
\r
397 /* Divide the peripheral clock by 32. */
\r
398 CMT0.CMCSR.BIT.CKS = 0x01;
\r
400 /* Set the CMT interrupt priority - the interrupt priority must be
\r
401 configKERNEL_INTERRUPT_PRIORITY no matter which peripheral is used to generate
\r
402 the tick interrupt. */
\r
403 INTC.IPR08.BIT._CMT0 = portKERNEL_INTERRUPT_PRIORITY;
\r
405 /* Clear the interrupt flag. */
\r
406 CMT0.CMCSR.BIT.CMF = 0;
\r
408 /* Enable the compare match interrupt. */
\r
409 CMT0.CMCSR.BIT.CMIE = 0x01;
\r
411 /* Start the timer. */
\r
412 CMT.CMSTR.BIT.STR0 = 0x01;
\r
414 /*-----------------------------------------------------------*/
\r
416 void vApplicationTickHook( void )
\r
418 CMT0.CMCSR.BIT.CMF = 0;
\r
420 /*-----------------------------------------------------------*/
\r