2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
69 * documentation provides more details of the standard demo application tasks.
\r
70 * In addition to the standard demo tasks, the following tasks and tests are
\r
71 * defined and/or created within this file:
\r
73 * "Check" task - This only executes every three seconds but has a high priority
\r
74 * to ensure it gets processor time. Its main function is to check that all the
\r
75 * standard demo tasks are still operational. If everything is running as
\r
76 * expected then the check task will toggle an LED every 3 seconds. An error
\r
77 * being discovered in any task will cause the toggle rate to increase to 500ms.
\r
79 * "Reg test" tasks - These fill the registers with known values, then check
\r
80 * that each register still contains its expected value. Each task uses
\r
81 * different values. The tasks run with very low priority so get preempted very
\r
82 * frequently. A register containing an unexpected value is indicative of an
\r
83 * error in the context switching mechanism.
\r
86 * Also in addition to the standard demo tasks is a button push task. This is
\r
87 * a very basic task that is included as an example of how to write an interrupt
\r
88 * service routine that interacts with a task. The button on the target board
\r
89 * is used to generate an interrupt that 'gives' a semaphore in order to unblock
\r
90 * a task. In doing so the task is synchronised with the interrupt. Each time
\r
91 * the task unblocks it simply toggles an LED before entering the Blocked state
\r
92 * again to wait for the next button push.
\r
95 /* Standard includes. */
\r
99 /* Scheduler include files. */
\r
100 #include "FreeRTOS.h"
\r
103 /* Standard demo file headers. */
\r
105 #include "semtest.h"
\r
106 #include "GenQTest.h"
\r
107 #include "dynamic.h"
\r
108 #include "blocktim.h"
\r
111 * Priority definitions for most of the tasks in the demo application. Some
\r
112 * tasks just use the idle priority.
\r
114 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
115 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
116 #define mainSEMTEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
117 #define mainBUTTON_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
118 #define mainGEN_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
120 /* The period between executions of the check task. */
\r
121 #define mainNO_ERROR_TOGGLE_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
122 #define mainERROR_TOGGLE_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
124 /* The LED toggled by the check task. */
\r
125 #define mainLED_0 P7_bit.no6
\r
127 /* A value that is passed in as the parameter to the 'check' task. This is done
\r
128 purely to check that the parameter passing mechanism is functioning correctly. */
\r
129 #define mainCHECK_PARAMETER_VALUE ( 0x5678 )
\r
131 /*-----------------------------------------------------------*/
\r
134 * The function that defines the 'check' task as described at the top of this
\r
137 static void vErrorChecks( void *pvParameters );
\r
141 * This function is called from the C startup routine to setup the processor -
\r
142 * in particular the clock source.
\r
144 int __low_level_init(void);
\r
147 * Functions that define the RegTest tasks as described at the top of this file.
\r
149 extern void vRegTest1( void *pvParameters );
\r
150 extern void vRegTest2( void *pvParameters );
\r
153 * Function that defines the button push task as described at the top of this
\r
156 extern void vButtonTask( void *pvParameters );
\r
158 /*-----------------------------------------------------------*/
\r
160 /* If an error is discovered by one of the RegTest tasks then this flag is set
\r
161 to pdFAIL. The 'check' task then inspects this flag to detect errors within
\r
162 the RegTest tasks. */
\r
163 static short sRegTestStatus = pdPASS;
\r
165 /* 78K0R Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface
\r
167 __root __far const unsigned portCHAR OptionByte[] @ 0x00C0 =
\r
169 WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED
\r
172 /* Security byte definition */
\r
173 __root __far const unsigned portCHAR SecuIDCode[] @ 0x00C4 =
\r
175 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
\r
179 /*-----------------------------------------------------------*/
\r
183 /* Creates all the tasks, then starts the scheduler. */
\r
185 /* First create the 'standard demo' tasks. These are used to demonstrate
\r
186 API functions being used and also to test the kernel port. More information
\r
187 is provided on the FreeRTOS.org WEB site. */
\r
188 vStartDynamicPriorityTasks();
\r
190 /* Create the RegTest tasks as described at the top of this file. */
\r
191 xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
192 xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
194 /* Create the button push task as described at the top of this file. */
\r
195 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_PRIORITY, NULL );
\r
197 /* Create the 'check' task as described at the top of this file. */
\r
198 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, ( void* )mainCHECK_PARAMETER_VALUE, mainCHECK_TASK_PRIORITY, NULL );
\r
200 #ifdef __IAR_78K0R_Kx3__
\r
202 /* The Kx3 has enough RAM to create more of the standard demo tasks. */
\r
203 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
204 vStartSemaphoreTasks(mainSEMTEST_PRIORITY);
\r
205 vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );
\r
206 vCreateBlockTimeTasks();
\r
210 /* Finally start the scheduler running. */
\r
211 vTaskStartScheduler();
\r
213 /* If this line is reached then vTaskStartScheduler() returned because there
\r
214 was insufficient heap memory remaining for the idle task to be created. */
\r
217 /*-----------------------------------------------------------*/
\r
219 static void vErrorChecks( void *pvParameters )
\r
221 portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;
\r
223 /* Ensure the parameter was passed in as expected. This is just a test of
\r
224 the kernel port, the parameter is not actually used for anything. The
\r
225 pointer will only actually be either 3 or 2 bytes, depending on the memory
\r
227 if( pvParameters != ( void * ) mainCHECK_PARAMETER_VALUE )
\r
229 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
232 /* Initialise xLastWakeTime before it is used. After this point it is not
\r
233 written to directly. */
\r
234 xLastWakeTime = xTaskGetTickCount();
\r
236 /* Cycle for ever, delaying then checking all the other tasks are still
\r
237 operating without error. */
\r
240 /* Wait until it is time to check all the other tasks again. */
\r
241 vTaskDelayUntil( &xLastWakeTime, xToggleRate );
\r
243 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
245 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
248 if( sRegTestStatus != pdPASS )
\r
250 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
253 #ifdef __IAR_78K0R_Kx3__
\r
255 /* Only the Kx3 runs all the tasks. */
\r
256 if( xArePollingQueuesStillRunning() != pdTRUE)
\r
258 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
261 if( xAreSemaphoreTasksStillRunning() != pdTRUE)
\r
263 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
266 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
268 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
271 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
273 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
278 /* Toggle the LED. The toggle rate will depend on whether or not an
\r
279 error has been found in any tasks. */
\r
280 mainLED_0 = !mainLED_0;
\r
283 /*-----------------------------------------------------------*/
\r
285 int __low_level_init(void)
\r
287 unsigned portCHAR ucResetFlag = RESF;
\r
289 portDISABLE_INTERRUPTS();
\r
291 /* Clock Configuration:
\r
292 In this port, to use the internal high speed clock source of the microcontroller
\r
293 define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h. To use an external
\r
294 clock define configCLOCK_SOURCE as 0. */
\r
295 #if configCLOCK_SOURCE == 1
\r
297 /* Set XT1 and XT2 in Input Port Mode
\r
298 Set X1 and X2 in Input Port Mode
\r
299 High speed oscillator frequency 2MHz <= fMX <= 10MHz */
\r
302 /* X1 external oszillation stopped. */
\r
305 /* Enable internal high speed oszillation. */
\r
309 /* Stop internal subsystem clock. */
\r
312 /* Set clock speed. */
\r
314 CKC &= (unsigned portCHAR)~0x07;
\r
319 /* XT1 and XT2 pin in input port mode
\r
320 X1 and X2 pin in crystal resonator mode
\r
321 High speed oszillation frequency 10MHz < fMX <= 20MHz */
\r
324 /* Set oscillation stabilization time. */
\r
327 /* Set speed mode: fMX > 10MHz for Flash memory high speed operation. */
\r
330 /* Start up X1 oscillator operation
\r
331 Internal high-speed oscillator operating. */
\r
334 /* Check oscillation stabilization time status. */
\r
337 /* Wait until X1 clock stabilization time. */
\r
341 /* Switch CPU clock to X1 oscillator. */
\r
345 /* Wait until CPU and peripherals operate with fX1 clock. */
\r
349 /* Stop the internal high-speed oscillator operation. */
\r
352 /* Stop the XT1 oscillator operation. */
\r
355 /* Operating frequency f = fx
\r
356 Change clock generator setting, if necessary. */
\r
359 /* From here onwards the X1 oscillator is supplied to the CPU. */
\r
363 /* LED port initialization - set port register. */
\r
366 /* Set port mode register. */
\r
369 /* Switch pin initialization - enable pull-up resistor. */
\r
372 /* INTP0 is used by the button on the target board. */
\r
374 /* INTP0 disable. */
\r
377 /* INTP0 IF clear. */
\r
381 /* INTP0 priority low. */
\r
385 /* Enable ext. INTP0 interrupt */
\r
390 /*-----------------------------------------------------------*/
\r
392 void vRegTestError( void )
\r
394 /* Called by the RegTest tasks if an error is found. lRegTestStatus is
\r
395 inspected by the check task. */
\r
396 sRegTestStatus = pdFAIL;
\r
398 /* Do not return from here as the reg test tasks clobber all registers so
\r
399 function calls may not function correctly. */
\r
402 /*-----------------------------------------------------------*/
\r
404 void vApplicationStackOverflowHook( void )
\r
406 /* This will get called if an overflow is detected in the stack of a task.
\r
407 Inspect pxCurrentTCB to see which was the offending task. */
\r