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
105 /* Kernel includes. */
\r
106 #include "FreeRTOS.h"
\r
109 /* Demo application includes. */
\r
110 #include "BlockQ.h"
\r
112 #include "integer.h"
\r
113 #include "blocktim.h"
\r
115 #include "partest.h"
\r
116 #include "semtest.h"
\r
118 #include "GenQTest.h"
\r
120 #include "recmutex.h"
\r
123 /* Constants required to configure the hardware. */
\r
124 #define mainFRQCR_VALUE ( 0x0303 ) /* Input = 12.5MHz, I Clock = 200MHz, B Clock = 50MHz, P Clock = 50MHz */
\r
126 /* Task priorities. */
\r
127 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
128 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
129 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
130 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
131 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
132 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
133 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
134 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
135 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
137 /* The LED toggle by the check task. */
\r
138 #define mainCHECK_LED ( 5 )
\r
140 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
142 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_RATE_MS )
\r
144 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
145 by at least one task. */
\r
146 #define mainERROR_CYCLE_TIME ( 200 / portTICK_RATE_MS )
\r
149 * vApplicationMallocFailedHook() will only be called if
\r
150 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
151 * function that will execute if a call to pvPortMalloc() fails.
\r
152 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
153 * semaphore is created. It is also called by various parts of the demo
\r
156 void vApplicationMallocFailedHook( void );
\r
159 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
160 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
161 * of the idle task. It is essential that code added to this hook function
\r
162 * never attempts to block in any way (for example, call xQueueReceive() with
\r
163 * a block time specified). If the application makes use of the vTaskDelete()
\r
164 * API function (as this demo application does) then it is also important that
\r
165 * vApplicationIdleHook() is permitted to return to its calling function because
\r
166 * it is the responsibility of the idle task to clean up memory allocated by the
\r
167 * kernel to any task that has since been deleted.
\r
169 void vApplicationIdleHook( void );
\r
172 * Just sets up clocks, ports, etc. used by the demo application.
\r
174 static void prvSetupHardware( void );
\r
177 * The check task as described at the top of this file.
\r
179 static void prvCheckTask( void *pvParameters );
\r
182 * The reg test tasks as described at the top of this file.
\r
184 extern void vRegTest1Task( void *pvParameters );
\r
185 extern void vRegTest2Task( void *pvParameters );
\r
187 /*-----------------------------------------------------------*/
\r
189 /* Variables that are incremented on each iteration of the reg test tasks -
\r
190 provided the tasks have not reported any errors. The check task inspects these
\r
191 variables to ensure they are still incrementing as expected. */
\r
192 volatile unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
194 /*-----------------------------------------------------------*/
\r
197 * Creates the majority of the demo application tasks before starting the
\r
202 xTaskHandle xCreatedTask;
\r
204 prvSetupHardware();
\r
206 /* Start the reg test tasks which test the context switching mechanism. */
\r
207 xTaskCreate( vRegTest1Task, "RegTest1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
208 xPortUsesFloatingPoint( xCreatedTask );
\r
210 xTaskCreate( vRegTest2Task, "RegTest2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xCreatedTask );
\r
211 xPortUsesFloatingPoint( xCreatedTask );
\r
213 /* Start the check task as described at the top of this file. */
\r
214 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
216 /* Start the standard demo tasks. These don't perform any particular useful
\r
217 functionality, other than to demonstrate the FreeRTOS API being used. */
\r
218 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
219 vCreateBlockTimeTasks();
\r
220 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
221 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
222 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
223 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
224 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
225 vStartQueuePeekTasks();
\r
226 vStartRecursiveMutexTasks();
\r
228 /* Start the math tasks as described at the top of this file. */
\r
229 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
231 /* The suicide tasks must be created last as they need to know how many
\r
232 tasks were running prior to their creation in order to ascertain whether
\r
233 or not the correct/expected number of tasks are running at any given time. */
\r
234 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
236 /* Start the tasks running. */
\r
237 vTaskStartScheduler();
\r
239 /* Will only get here if there was insufficient heap memory to create the idle
\r
240 task. Increase the configTOTAL_HEAP_SIZE setting in FreeRTOSConfig.h. */
\r
243 /*-----------------------------------------------------------*/
\r
245 static void prvCheckTask( void *pvParameter )
\r
247 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
248 unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
250 /* Just to remove compiler warning. */
\r
251 ( void ) pvParameter;
\r
253 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
254 xNextWakeTime = xTaskGetTickCount();
\r
258 /* Place this task in the blocked state until it is time to run again. */
\r
259 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
261 /* Inspect all the other tasks to ensure none have experienced any errors. */
\r
262 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
264 /* Increase the rate at which this task cycles, which will increase the
\r
265 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
267 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
269 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
271 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
273 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
275 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
277 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
279 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
281 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
283 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
285 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
287 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
289 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
291 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
293 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
295 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
297 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
299 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
301 else if( xAreMathsTaskStillRunning() != pdTRUE )
\r
303 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
306 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
307 their loop counters if they encounter an error. */
\r
308 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
310 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
313 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
315 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
318 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
319 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
321 /* Toggle the check LED to give an indication of the system status. If the
\r
322 LED toggles every 5 seconds then everything is ok. A faster toggle indicates
\r
324 vParTestToggleLED( mainCHECK_LED );
\r
327 /*-----------------------------------------------------------*/
\r
329 void vApplicationMallocFailedHook( void )
\r
331 /* A call to vPortMalloc() failed, probably during the creation of a task,
\r
332 queue or semaphore. Inspect pxCurrentTCB to find which task is currently
\r
336 /*-----------------------------------------------------------*/
\r
338 void vApplicationIdleHook( void )
\r
340 /* Code can be added to the idle task here. This function must *NOT* attempt
\r
341 to block. Also, if the application uses the vTaskDelete() API function then
\r
342 this function must return regularly to ensure the idle task gets a chance to
\r
343 clean up the memory used by deleted tasks. */
\r
345 /*-----------------------------------------------------------*/
\r
347 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
349 /* Just to remove compiler warnings. This function will only actually
\r
350 get called if configCHECK_FOR_STACK_OVERFLOW is set to a non zero value.
\r
351 By default this demo does not use the stack overflow checking functionality
\r
352 as the SuperH will normally execute an exception if the stack overflows. */
\r
354 ( void ) pcTaskName;
\r
358 /*-----------------------------------------------------------*/
\r
360 static void prvSetupHardware( void )
\r
362 volatile unsigned long ul;
\r
364 /* Set the CPU and peripheral clocks. */
\r
365 CPG.FRQCR.WORD = mainFRQCR_VALUE;
\r
367 /* Wait for the clock to settle. */
\r
368 for( ul = 0; ul < 99; ul++ )
\r
373 /* Initialise the ports used to toggle LEDs. */
\r
374 vParTestInitialise();
\r
376 /*-----------------------------------------------------------*/
\r
378 void vApplicationSetupTimerInterrupt( void )
\r
380 /* The peripheral clock is divided by 32 before feeding the compare match
\r
381 peripheral (CMT). */
\r
382 unsigned long ulCompareMatch = ( configPERIPHERAL_CLOCK_HZ / ( configTICK_RATE_HZ * 32 ) ) + 1;
\r
384 /* Configure a timer to create the RTOS tick interrupt. This example uses
\r
385 the compare match timer, but the multi function timer or possible even the
\r
386 watchdog timer could also be used. Ensure vPortTickInterrupt() is installed
\r
387 as the interrupt handler for whichever peripheral is used. */
\r
389 /* Turn the CMT on. */
\r
390 STB.CR4.BIT._CMT = 0;
\r
392 /* Set the compare match value for the required tick frequency. */
\r
393 CMT0.CMCOR = ( unsigned short ) ulCompareMatch;
\r
395 /* Divide the peripheral clock by 32. */
\r
396 CMT0.CMCSR.BIT.CKS = 0x01;
\r
398 /* Set the CMT interrupt priority - the interrupt priority must be
\r
399 configKERNEL_INTERRUPT_PRIORITY no matter which peripheral is used to generate
\r
400 the tick interrupt. */
\r
401 INTC.IPR08.BIT._CMT0 = portKERNEL_INTERRUPT_PRIORITY;
\r
403 /* Clear the interrupt flag. */
\r
404 CMT0.CMCSR.BIT.CMF = 0;
\r
406 /* Enable the compare match interrupt. */
\r
407 CMT0.CMCSR.BIT.CMIE = 0x01;
\r
409 /* Start the timer. */
\r
410 CMT.CMSTR.BIT.STR0 = 0x01;
\r
412 /*-----------------------------------------------------------*/
\r
414 void vApplicationTickHook( void )
\r
416 CMT0.CMCSR.BIT.CMF = 0;
\r
418 /*-----------------------------------------------------------*/
\r