2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
54 * documentation provides more details of the standard demo application tasks.
\r
55 * In addition to the standard demo tasks, the following tasks and tests are
\r
56 * defined and/or created within this file:
\r
58 * "Check" task - This only executes every three seconds but has a high priority
\r
59 * to ensure it gets processor time. Its main function is to check that all the
\r
60 * standard demo tasks are still operational. If everything is running as
\r
61 * expected then the check task will toggle an LED every 3 seconds. An error
\r
62 * being discovered in any task will cause the toggle rate to increase to 500ms.
\r
64 * "Reg test" tasks - These fill the registers with known values, then check
\r
65 * that each register still contains its expected value. Each task uses
\r
66 * different values. The tasks run with very low priority so get preempted very
\r
67 * frequently. A register containing an unexpected value is indicative of an
\r
68 * error in the context switching mechanism.
\r
71 * Also in addition to the standard demo tasks is a button push task. This is
\r
72 * a very basic task that is included as an example of how to write an interrupt
\r
73 * service routine that interacts with a task. The button on the target board
\r
74 * is used to generate an interrupt that 'gives' a semaphore in order to unblock
\r
75 * a task. In doing so the task is synchronised with the interrupt. Each time
\r
76 * the task unblocks it simply toggles an LED before entering the Blocked state
\r
77 * again to wait for the next button push.
\r
80 /* Standard includes. */
\r
84 /* Scheduler include files. */
\r
85 #include "FreeRTOS.h"
\r
88 /* Standard demo file headers. */
\r
90 #include "semtest.h"
\r
91 #include "GenQTest.h"
\r
92 #include "dynamic.h"
\r
93 #include "blocktim.h"
\r
96 * Priority definitions for most of the tasks in the demo application. Some
\r
97 * tasks just use the idle priority.
\r
99 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
100 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
101 #define mainSEMTEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
102 #define mainBUTTON_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
103 #define mainGEN_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
105 /* The period between executions of the check task. */
\r
106 #define mainNO_ERROR_TOGGLE_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
107 #define mainERROR_TOGGLE_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
109 /* The LED toggled by the check task. */
\r
110 #define mainLED_0 P7_bit.no6
\r
112 /* A value that is passed in as the parameter to the 'check' task. This is done
\r
113 purely to check that the parameter passing mechanism is functioning correctly. */
\r
114 #define mainCHECK_PARAMETER_VALUE ( 0x5678 )
\r
116 /*-----------------------------------------------------------*/
\r
119 * The function that defines the 'check' task as described at the top of this
\r
122 static void vErrorChecks( void *pvParameters );
\r
126 * This function is called from the C startup routine to setup the processor -
\r
127 * in particular the clock source.
\r
129 int __low_level_init(void);
\r
132 * Functions that define the RegTest tasks as described at the top of this file.
\r
134 extern void vRegTest1( void *pvParameters );
\r
135 extern void vRegTest2( void *pvParameters );
\r
138 * Function that defines the button push task as described at the top of this
\r
141 extern void vButtonTask( void *pvParameters );
\r
143 /*-----------------------------------------------------------*/
\r
145 /* If an error is discovered by one of the RegTest tasks then this flag is set
\r
146 to pdFAIL. The 'check' task then inspects this flag to detect errors within
\r
147 the RegTest tasks. */
\r
148 static short sRegTestStatus = pdPASS;
\r
150 /* 78K0R Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface
\r
152 __root __far const unsigned portCHAR OptionByte[] @ 0x00C0 =
\r
154 WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED
\r
157 /* Security byte definition */
\r
158 __root __far const unsigned portCHAR SecuIDCode[] @ 0x00C4 =
\r
160 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
\r
164 /*-----------------------------------------------------------*/
\r
168 /* Creates all the tasks, then starts the scheduler. */
\r
170 /* First create the 'standard demo' tasks. These are used to demonstrate
\r
171 API functions being used and also to test the kernel port. More information
\r
172 is provided on the FreeRTOS.org WEB site. */
\r
173 vStartDynamicPriorityTasks();
\r
175 /* Create the RegTest tasks as described at the top of this file. */
\r
176 xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
177 xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
179 /* Create the button push task as described at the top of this file. */
\r
180 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_PRIORITY, NULL );
\r
182 /* Create the 'check' task as described at the top of this file. */
\r
183 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, ( void* )mainCHECK_PARAMETER_VALUE, mainCHECK_TASK_PRIORITY, NULL );
\r
185 #ifdef __IAR_78K0R_Kx3__
\r
187 /* The Kx3 has enough RAM to create more of the standard demo tasks. */
\r
188 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
189 vStartSemaphoreTasks(mainSEMTEST_PRIORITY);
\r
190 vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );
\r
191 vCreateBlockTimeTasks();
\r
195 /* Finally start the scheduler running. */
\r
196 vTaskStartScheduler();
\r
198 /* If this line is reached then vTaskStartScheduler() returned because there
\r
199 was insufficient heap memory remaining for the idle task to be created. */
\r
202 /*-----------------------------------------------------------*/
\r
204 static void vErrorChecks( void *pvParameters )
\r
206 portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;
\r
208 /* Ensure the parameter was passed in as expected. This is just a test of
\r
209 the kernel port, the parameter is not actually used for anything. The
\r
210 pointer will only actually be either 3 or 2 bytes, depending on the memory
\r
212 if( pvParameters != ( void * ) mainCHECK_PARAMETER_VALUE )
\r
214 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
217 /* Initialise xLastWakeTime before it is used. After this point it is not
\r
218 written to directly. */
\r
219 xLastWakeTime = xTaskGetTickCount();
\r
221 /* Cycle for ever, delaying then checking all the other tasks are still
\r
222 operating without error. */
\r
225 /* Wait until it is time to check all the other tasks again. */
\r
226 vTaskDelayUntil( &xLastWakeTime, xToggleRate );
\r
228 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
230 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
233 if( sRegTestStatus != pdPASS )
\r
235 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
238 #ifdef __IAR_78K0R_Kx3__
\r
240 /* Only the Kx3 runs all the tasks. */
\r
241 if( xArePollingQueuesStillRunning() != pdTRUE)
\r
243 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
246 if( xAreSemaphoreTasksStillRunning() != pdTRUE)
\r
248 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
251 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
253 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
256 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
258 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
263 /* Toggle the LED. The toggle rate will depend on whether or not an
\r
264 error has been found in any tasks. */
\r
265 mainLED_0 = !mainLED_0;
\r
268 /*-----------------------------------------------------------*/
\r
270 int __low_level_init(void)
\r
272 unsigned portCHAR ucResetFlag = RESF;
\r
274 portDISABLE_INTERRUPTS();
\r
276 /* Clock Configuration:
\r
277 In this port, to use the internal high speed clock source of the microcontroller
\r
278 define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h. To use an external
\r
279 clock define configCLOCK_SOURCE as 0. */
\r
280 #if configCLOCK_SOURCE == 1
\r
282 /* Set XT1 and XT2 in Input Port Mode
\r
283 Set X1 and X2 in Input Port Mode
\r
284 High speed oscillator frequency 2MHz <= fMX <= 10MHz */
\r
287 /* X1 external oszillation stopped. */
\r
290 /* Enable internal high speed oszillation. */
\r
294 /* Stop internal subsystem clock. */
\r
297 /* Set clock speed. */
\r
299 CKC &= (unsigned portCHAR)~0x07;
\r
304 /* XT1 and XT2 pin in input port mode
\r
305 X1 and X2 pin in crystal resonator mode
\r
306 High speed oszillation frequency 10MHz < fMX <= 20MHz */
\r
309 /* Set oscillation stabilization time. */
\r
312 /* Set speed mode: fMX > 10MHz for Flash memory high speed operation. */
\r
315 /* Start up X1 oscillator operation
\r
316 Internal high-speed oscillator operating. */
\r
319 /* Check oscillation stabilization time status. */
\r
322 /* Wait until X1 clock stabilization time. */
\r
326 /* Switch CPU clock to X1 oscillator. */
\r
330 /* Wait until CPU and peripherals operate with fX1 clock. */
\r
334 /* Stop the internal high-speed oscillator operation. */
\r
337 /* Stop the XT1 oscillator operation. */
\r
340 /* Operating frequency f = fx
\r
341 Change clock generator setting, if necessary. */
\r
344 /* From here onwards the X1 oscillator is supplied to the CPU. */
\r
348 /* LED port initialization - set port register. */
\r
351 /* Set port mode register. */
\r
354 /* Switch pin initialization - enable pull-up resistor. */
\r
357 /* INTP0 is used by the button on the target board. */
\r
359 /* INTP0 disable. */
\r
362 /* INTP0 IF clear. */
\r
366 /* INTP0 priority low. */
\r
370 /* Enable ext. INTP0 interrupt */
\r
375 /*-----------------------------------------------------------*/
\r
377 void vRegTestError( void )
\r
379 /* Called by the RegTest tasks if an error is found. lRegTestStatus is
\r
380 inspected by the check task. */
\r
381 sRegTestStatus = pdFAIL;
\r
383 /* Do not return from here as the reg test tasks clobber all registers so
\r
384 function calls may not function correctly. */
\r
387 /*-----------------------------------------------------------*/
\r
389 void vApplicationStackOverflowHook( void )
\r
391 /* This will get called if an overflow is detected in the stack of a task.
\r
392 Inspect pxCurrentTCB to see which was the offending task. */
\r