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
54 /* Kernel includes. */
\r
55 #include "FreeRTOS.h"
\r
58 /* Demo application includes. */
\r
61 #include "integer.h"
\r
62 #include "blocktim.h"
\r
64 #include "partest.h"
\r
65 #include "semtest.h"
\r
67 #include "GenQTest.h"
\r
69 #include "recmutex.h"
\r
71 /* Constants required to configure the hardware. */
\r
72 #define mainFRQCR_VALUE ( 0x0303 ) /* Input = 12.5MHz, I Clock = 200MHz, B Clock = 50MHz, P Clock = 50MHz */
\r
74 /* Task priorities. */
\r
75 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
76 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
77 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
78 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
79 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
80 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
81 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
82 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
84 /* The LED toggle by the check task. */
\r
85 #define mainCHECK_LED ( 5 )
\r
87 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
89 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_RATE_MS )
\r
91 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
92 by at least one task. */
\r
93 #define mainERROR_CYCLE_TIME ( 200 / portTICK_RATE_MS )
\r
95 void vApplicationMallocFailedHook( void );
\r
96 void vApplicationIdleHook( void );
\r
97 static void prvSetupHardware( void );
\r
98 static void prvCheckTask( void *pvParameters );
\r
100 extern void vRegTest1Task( void *pvParameters );
\r
101 extern void vRegTest2Task( void *pvParameters );
\r
103 volatile unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
105 /*-----------------------------------------------------------*/
\r
109 prvSetupHardware();
\r
111 /* Start the reg test tasks which test the context switching mechanism. */
\r
112 xTaskCreate( vRegTest1Task, "RegTest1", configMINIMAL_STACK_SIZE, ( void * ) 0x12345678UL, tskIDLE_PRIORITY, NULL );
\r
113 xTaskCreate( vRegTest2Task, "RegTest2", configMINIMAL_STACK_SIZE, ( void * ) 0x11223344UL, tskIDLE_PRIORITY, NULL );
\r
115 /* Start the check task as described at the top of this file. */
\r
116 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
118 /* Start the standard demo tasks. These don't perform any particular useful
\r
119 functionality, other than to demonstrate the FreeRTOS API being used. */
\r
120 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
121 vCreateBlockTimeTasks();
\r
122 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
123 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
124 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
125 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
126 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
127 vStartQueuePeekTasks();
\r
128 vStartRecursiveMutexTasks();
\r
130 /* The suicide tasks must be created last as they need to know how many
\r
131 tasks were running prior to their creation in order to ascertain whether
\r
132 or not the correct/expected number of tasks are running at any given time. */
\r
133 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
135 /* Start the tasks running. */
\r
136 vTaskStartScheduler();
\r
138 /* Will only get here if there was insufficient heap memory to create the idle
\r
139 task. Increase the configTOTAL_HEAP_SIZE setting in FreeRTOSConfig.h. */
\r
142 /*-----------------------------------------------------------*/
\r
144 static void prvCheckTask( void *pvParameter )
\r
146 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
147 unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
149 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
150 xNextWakeTime = xTaskGetTickCount();
\r
154 /* Place this task in the blocked state until it is time to run again. */
\r
155 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
157 /* Inspect all the other tasks to esnure none have experienced any errors. */
\r
158 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
160 /* Increase the rate at which this task cycles, which will increase the
\r
161 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
163 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
165 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
167 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
169 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
171 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
173 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
175 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
177 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
179 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
181 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
183 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
185 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
187 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
189 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
191 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
193 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
195 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
198 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
199 their loop counters if they encounter an error. */
\r
200 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
202 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
205 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
207 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
210 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
211 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
213 /* Toggle the check LED to give an indication of the system status. If the
\r
214 LED toggles every 5 seconds then everything is ok. A faster toggle indicates
\r
216 vParTestToggleLED( mainCHECK_LED );
\r
219 /*-----------------------------------------------------------*/
\r
221 void vApplicationMallocFailedHook( void )
\r
223 /* A call to vPortMalloc() failed, probably during the creation of a task,
\r
224 queue or semaphore. Inspect pxCurrentTCB to find which task is currently
\r
228 /*-----------------------------------------------------------*/
\r
230 void vApplicationIdleHook( void )
\r
232 /* Code can be added to the idle task here. This function must *NOT* attempt
\r
233 to block. Also, if the application uses the vTaskDelete() API function then
\r
234 this function must return regularly to ensure the idle task gets a chance to
\r
235 clean up the memory used by deleted tasks. */
\r
237 /*-----------------------------------------------------------*/
\r
239 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
243 /*-----------------------------------------------------------*/
\r
245 static void prvSetupHardware( void )
\r
247 volatile unsigned long ul;
\r
249 /* Set the CPU and peripheral clocks. */
\r
250 CPG.FRQCR.WORD = mainFRQCR_VALUE;
\r
252 /* Wait for the clock to settle. */
\r
253 for( ul = 0; ul < 99; ul++ )
\r
258 /* Initialise the ports used to toggle LEDs. */
\r
259 vParTestInitialise();
\r
261 /*-----------------------------------------------------------*/
\r
263 void vApplicationSetupTimerInterrupt( void )
\r
265 /* The peripheral clock is divided by 32 before feeding the compare match
\r
266 periphersl (CMT). */
\r
267 unsigned long ulCompareMatch = ( configPERIPHERAL_CLOCK_HZ / ( configTICK_RATE_HZ * 32 ) ) + 1;
\r
269 /* Configure a timer to create the RTOS tick interrupt. This example uses
\r
270 the compare match timer, but the multi function timer or possible even the
\r
271 watchdog timer could also be used. Ensure vPortTickInterrupt() is installed
\r
272 as the interrupt handler for whichever peripheral is used. */
\r
274 /* Turn the CMT on. */
\r
275 STB.CR4.BIT._CMT = 0;
\r
277 /* Set the compare match value for the required tick frequency. */
\r
278 CMT0.CMCOR = ( unsigned short ) ulCompareMatch;
\r
280 /* Divide the peripheral clock by 32. */
\r
281 CMT0.CMCSR.BIT.CKS = 0x01;
\r
283 /* Set the CMT interrupt priority - the interrupt priority must be
\r
284 configKERNEL_INTERRUPT_PRIORITY no matter which peripheral is used to generate
\r
285 the tick interrupt. */
\r
286 INTC.IPR08.BIT._CMT0 = configKERNEL_INTERRUPT_PRIORITY;
\r
288 /* Clear the interrupt flag. */
\r
289 CMT0.CMCSR.BIT.CMF = 0;
\r
291 /* Enable the compare match interrupt. */
\r
292 CMT0.CMCSR.BIT.CMIE = 0x01;
\r
294 /* Start the timer. */
\r
295 CMT.CMSTR.BIT.STR0 = 0x01;
\r
297 /*-----------------------------------------------------------*/
\r
299 void vApplicationTickHook( void )
\r
301 CMT0.CMCSR.BIT.CMF = 0;
\r
303 /*-----------------------------------------------------------*/
\r