2 FreeRTOS V6.0.5 - Copyright (C) 2010 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
55 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
56 * documentation provides more details of the standard demo application tasks.
\r
57 * In addition to the standard demo tasks, the following tasks and tests are
\r
58 * defined and/or created within this file:
\r
60 * "Check" task - This only executes every three seconds but has a high priority
\r
61 * to ensure it gets processor time. Its main function is to check that all the
\r
62 * standard demo tasks are still operational. If everything is running as
\r
63 * expected then the check task will toggle an LED every 3 seconds. An error
\r
64 * being discovered in any task will cause the toggle rate to increase to 500ms.
\r
66 * "Reg test" tasks - These fill the registers with known values, then check
\r
67 * that each register still contains its expected value. Each task uses
\r
68 * different values. The tasks run with very low priority so get preempted very
\r
69 * frequently. A register containing an unexpected value is indicative of an
\r
70 * error in the context switching mechanism.
\r
73 * Also in addition to the standard demo tasks is a button push task. This is
\r
74 * a very basic task that is included as an example of how to write an interrupt
\r
75 * service routine that interacts with a task. The button on the target board
\r
76 * is used to generate an interrupt that 'gives' a semaphore in order to unblock
\r
77 * a task. In doing so the task is synchronised with the interrupt. Each time
\r
78 * the task unblocks it simply toggles an LED before entering the Blocked state
\r
79 * again to wait for the next button push.
\r
82 /* Standard includes. */
\r
86 /* Scheduler include files. */
\r
87 #include "FreeRTOS.h"
\r
90 /* Standard demo file headers. */
\r
92 #include "semtest.h"
\r
93 #include "GenQTest.h"
\r
94 #include "dynamic.h"
\r
95 #include "blocktim.h"
\r
98 * Priority definitions for most of the tasks in the demo application. Some
\r
99 * tasks just use the idle priority.
\r
101 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
102 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
103 #define mainSEMTEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
104 #define mainBUTTON_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
105 #define mainGEN_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
107 /* The period between executions of the check task. */
\r
108 #define mainNO_ERROR_TOGGLE_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
109 #define mainERROR_TOGGLE_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
111 /* The LED toggled by the check task. */
\r
112 #define mainLED_0 P7_bit.no6
\r
114 /* A value that is passed in as the parameter to the 'check' task. This is done
\r
115 purely to check that the parameter passing mechanism is functioning correctly. */
\r
116 #define mainCHECK_PARAMETER_VALUE ( 0x5678 )
\r
118 /*-----------------------------------------------------------*/
\r
121 * The function that defines the 'check' task as described at the top of this
\r
124 static void vErrorChecks( void *pvParameters );
\r
128 * This function is called from the C startup routine to setup the processor -
\r
129 * in particular the clock source.
\r
131 int __low_level_init(void);
\r
134 * Functions that define the RegTest tasks as described at the top of this file.
\r
136 extern void vRegTest1( void *pvParameters );
\r
137 extern void vRegTest2( void *pvParameters );
\r
140 * Function that defines the button push task as described at the top of this
\r
143 extern void vButtonTask( void *pvParameters );
\r
145 /*-----------------------------------------------------------*/
\r
147 /* If an error is discovered by one of the RegTest tasks then this flag is set
\r
148 to pdFAIL. The 'check' task then inspects this flag to detect errors within
\r
149 the RegTest tasks. */
\r
150 static short sRegTestStatus = pdPASS;
\r
152 /* 78K0R Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface
\r
154 __root __far const unsigned portCHAR OptionByte[] @ 0x00C0 =
\r
156 WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED
\r
159 /* Security byte definition */
\r
160 __root __far const unsigned portCHAR SecuIDCode[] @ 0x00C4 =
\r
162 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
\r
166 /*-----------------------------------------------------------*/
\r
170 /* Creates all the tasks, then starts the scheduler. */
\r
172 /* First create the 'standard demo' tasks. These are used to demonstrate
\r
173 API functions being used and also to test the kernel port. More information
\r
174 is provided on the FreeRTOS.org WEB site. */
\r
175 vStartDynamicPriorityTasks();
\r
177 /* Create the RegTest tasks as described at the top of this file. */
\r
178 xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
179 xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
181 /* Create the button push task as described at the top of this file. */
\r
182 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_PRIORITY, NULL );
\r
184 /* Create the 'check' task as described at the top of this file. */
\r
185 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, ( void* )mainCHECK_PARAMETER_VALUE, mainCHECK_TASK_PRIORITY, NULL );
\r
187 #ifdef __IAR_78K0R_Kx3__
\r
189 /* The Kx3 has enough RAM to create more of the standard demo tasks. */
\r
190 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
191 vStartSemaphoreTasks(mainSEMTEST_PRIORITY);
\r
192 vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );
\r
193 vCreateBlockTimeTasks();
\r
197 /* Finally start the scheduler running. */
\r
198 vTaskStartScheduler();
\r
200 /* If this line is reached then vTaskStartScheduler() returned because there
\r
201 was insufficient heap memory remaining for the idle task to be created. */
\r
204 /*-----------------------------------------------------------*/
\r
206 static void vErrorChecks( void *pvParameters )
\r
208 portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;
\r
210 /* Ensure the parameter was passed in as expected. This is just a test of
\r
211 the kernel port, the parameter is not actually used for anything. The
\r
212 pointer will only actually be either 3 or 2 bytes, depending on the memory
\r
214 if( pvParameters != ( void * ) mainCHECK_PARAMETER_VALUE )
\r
216 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
219 /* Initialise xLastWakeTime before it is used. After this point it is not
\r
220 written to directly. */
\r
221 xLastWakeTime = xTaskGetTickCount();
\r
223 /* Cycle for ever, delaying then checking all the other tasks are still
\r
224 operating without error. */
\r
227 /* Wait until it is time to check all the other tasks again. */
\r
228 vTaskDelayUntil( &xLastWakeTime, xToggleRate );
\r
230 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
232 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
235 if( sRegTestStatus != pdPASS )
\r
237 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
240 #ifdef __IAR_78K0R_Kx3__
\r
242 /* Only the Kx3 runs all the tasks. */
\r
243 if( xArePollingQueuesStillRunning() != pdTRUE)
\r
245 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
248 if( xAreSemaphoreTasksStillRunning() != pdTRUE)
\r
250 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
253 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
255 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
258 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
260 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
265 /* Toggle the LED. The toggle rate will depend on whether or not an
\r
266 error has been found in any tasks. */
\r
267 mainLED_0 = !mainLED_0;
\r
270 /*-----------------------------------------------------------*/
\r
272 int __low_level_init(void)
\r
274 unsigned portCHAR ucResetFlag = RESF;
\r
276 portDISABLE_INTERRUPTS();
\r
278 /* Clock Configuration:
\r
279 In this port, to use the internal high speed clock source of the microcontroller
\r
280 define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h. To use an external
\r
281 clock define configCLOCK_SOURCE as 0. */
\r
282 #if configCLOCK_SOURCE == 1
\r
284 /* Set XT1 and XT2 in Input Port Mode
\r
285 Set X1 and X2 in Input Port Mode
\r
286 High speed oscillator frequency 2MHz <= fMX <= 10MHz */
\r
289 /* X1 external oszillation stopped. */
\r
292 /* Enable internal high speed oszillation. */
\r
296 /* Stop internal subsystem clock. */
\r
299 /* Set clock speed. */
\r
301 CKC &= (unsigned portCHAR)~0x07;
\r
306 /* XT1 and XT2 pin in input port mode
\r
307 X1 and X2 pin in crystal resonator mode
\r
308 High speed oszillation frequency 10MHz < fMX <= 20MHz */
\r
311 /* Set oscillation stabilization time. */
\r
314 /* Set speed mode: fMX > 10MHz for Flash memory high speed operation. */
\r
317 /* Start up X1 oscillator operation
\r
318 Internal high-speed oscillator operating. */
\r
321 /* Check oscillation stabilization time status. */
\r
324 /* Wait until X1 clock stabilization time. */
\r
328 /* Switch CPU clock to X1 oscillator. */
\r
332 /* Wait until CPU and peripherals operate with fX1 clock. */
\r
336 /* Stop the internal high-speed oscillator operation. */
\r
339 /* Stop the XT1 oscillator operation. */
\r
342 /* Operating frequency f = fx
\r
343 Change clock generator setting, if necessary. */
\r
346 /* From here onwards the X1 oscillator is supplied to the CPU. */
\r
350 /* LED port initialization - set port register. */
\r
353 /* Set port mode register. */
\r
356 /* Switch pin initialization - enable pull-up resistor. */
\r
359 /* INTP0 is used by the button on the target board. */
\r
361 /* INTP0 disable. */
\r
364 /* INTP0 IF clear. */
\r
368 /* INTP0 priority low. */
\r
372 /* Enable ext. INTP0 interrupt */
\r
377 /*-----------------------------------------------------------*/
\r
379 void vRegTestError( void )
\r
381 /* Called by the RegTest tasks if an error is found. lRegTestStatus is
\r
382 inspected by the check task. */
\r
383 sRegTestStatus = pdFAIL;
\r
385 /* Do not return from here as the reg test tasks clobber all registers so
\r
386 function calls may not function correctly. */
\r
389 /*-----------------------------------------------------------*/
\r
391 void vApplicationStackOverflowHook( void )
\r
393 /* This will get called if an overflow is detected in the stack of a task.
\r
394 Inspect pxCurrentTCB to see which was the offending task. */
\r