2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
67 * documentation provides more details of the standard demo application tasks.
\r
68 * In addition to the standard demo tasks, the following tasks and tests are
\r
69 * defined and/or created within this file:
\r
71 * "Check" task - This only executes every three seconds but has a high priority
\r
72 * to ensure it gets processor time. Its main function is to check that all the
\r
73 * standard demo tasks are still operational. If everything is running as
\r
74 * expected then the check task will toggle an LED every 3 seconds. An error
\r
75 * being discovered in any task will cause the toggle rate to increase to 500ms.
\r
77 * "Reg test" tasks - These fill the registers with known values, then check
\r
78 * that each register still contains its expected value. Each task uses
\r
79 * different values. The tasks run with very low priority so get preempted very
\r
80 * frequently. A register containing an unexpected value is indicative of an
\r
81 * error in the context switching mechanism.
\r
84 * Also in addition to the standard demo tasks is a button push task. This is
\r
85 * a very basic task that is included as an example of how to write an interrupt
\r
86 * service routine that interacts with a task. The button on the target board
\r
87 * is used to generate an interrupt that 'gives' a semaphore in order to unblock
\r
88 * a task. In doing so the task is synchronised with the interrupt. Each time
\r
89 * the task unblocks it simply toggles an LED before entering the Blocked state
\r
90 * again to wait for the next button push.
\r
93 /* Standard includes. */
\r
97 /* Scheduler include files. */
\r
98 #include "FreeRTOS.h"
\r
101 /* Standard demo file headers. */
\r
103 #include "semtest.h"
\r
104 #include "GenQTest.h"
\r
105 #include "dynamic.h"
\r
106 #include "blocktim.h"
\r
109 * Priority definitions for most of the tasks in the demo application. Some
\r
110 * tasks just use the idle priority.
\r
112 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
113 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
114 #define mainSEMTEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
115 #define mainBUTTON_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
116 #define mainGEN_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
118 /* The period between executions of the check task. */
\r
119 #define mainNO_ERROR_TOGGLE_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
120 #define mainERROR_TOGGLE_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
122 /* The LED toggled by the check task. */
\r
123 #define mainLED_0 P7_bit.no6
\r
125 /* A value that is passed in as the parameter to the 'check' task. This is done
\r
126 purely to check that the parameter passing mechanism is functioning correctly. */
\r
127 #define mainCHECK_PARAMETER_VALUE ( 0x5678 )
\r
129 /*-----------------------------------------------------------*/
\r
132 * The function that defines the 'check' task as described at the top of this
\r
135 static void vErrorChecks( void *pvParameters );
\r
139 * This function is called from the C startup routine to setup the processor -
\r
140 * in particular the clock source.
\r
142 int __low_level_init(void);
\r
145 * Functions that define the RegTest tasks as described at the top of this file.
\r
147 extern void vRegTest1( void *pvParameters );
\r
148 extern void vRegTest2( void *pvParameters );
\r
151 * Function that defines the button push task as described at the top of this
\r
154 extern void vButtonTask( void *pvParameters );
\r
156 /*-----------------------------------------------------------*/
\r
158 /* If an error is discovered by one of the RegTest tasks then this flag is set
\r
159 to pdFAIL. The 'check' task then inspects this flag to detect errors within
\r
160 the RegTest tasks. */
\r
161 static short sRegTestStatus = pdPASS;
\r
163 /* 78K0R Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface
\r
165 __root __far const unsigned portCHAR OptionByte[] @ 0x00C0 =
\r
167 WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED
\r
170 /* Security byte definition */
\r
171 __root __far const unsigned portCHAR SecuIDCode[] @ 0x00C4 =
\r
173 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
\r
177 /*-----------------------------------------------------------*/
\r
181 /* Creates all the tasks, then starts the scheduler. */
\r
183 /* First create the 'standard demo' tasks. These are used to demonstrate
\r
184 API functions being used and also to test the kernel port. More information
\r
185 is provided on the FreeRTOS.org WEB site. */
\r
186 vStartDynamicPriorityTasks();
\r
188 /* Create the RegTest tasks as described at the top of this file. */
\r
189 xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
190 xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
192 /* Create the button push task as described at the top of this file. */
\r
193 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_PRIORITY, NULL );
\r
195 /* Create the 'check' task as described at the top of this file. */
\r
196 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, ( void* )mainCHECK_PARAMETER_VALUE, mainCHECK_TASK_PRIORITY, NULL );
\r
198 #ifdef __IAR_78K0R_Kx3__
\r
200 /* The Kx3 has enough RAM to create more of the standard demo tasks. */
\r
201 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
202 vStartSemaphoreTasks(mainSEMTEST_PRIORITY);
\r
203 vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );
\r
204 vCreateBlockTimeTasks();
\r
208 /* Finally start the scheduler running. */
\r
209 vTaskStartScheduler();
\r
211 /* If this line is reached then vTaskStartScheduler() returned because there
\r
212 was insufficient heap memory remaining for the idle task to be created. */
\r
215 /*-----------------------------------------------------------*/
\r
217 static void vErrorChecks( void *pvParameters )
\r
219 portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;
\r
221 /* Ensure the parameter was passed in as expected. This is just a test of
\r
222 the kernel port, the parameter is not actually used for anything. The
\r
223 pointer will only actually be either 3 or 2 bytes, depending on the memory
\r
225 if( pvParameters != ( void * ) mainCHECK_PARAMETER_VALUE )
\r
227 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
230 /* Initialise xLastWakeTime before it is used. After this point it is not
\r
231 written to directly. */
\r
232 xLastWakeTime = xTaskGetTickCount();
\r
234 /* Cycle for ever, delaying then checking all the other tasks are still
\r
235 operating without error. */
\r
238 /* Wait until it is time to check all the other tasks again. */
\r
239 vTaskDelayUntil( &xLastWakeTime, xToggleRate );
\r
241 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
243 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
246 if( sRegTestStatus != pdPASS )
\r
248 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
251 #ifdef __IAR_78K0R_Kx3__
\r
253 /* Only the Kx3 runs all the tasks. */
\r
254 if( xArePollingQueuesStillRunning() != pdTRUE)
\r
256 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
259 if( xAreSemaphoreTasksStillRunning() != pdTRUE)
\r
261 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
264 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
266 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
269 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
271 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
276 /* Toggle the LED. The toggle rate will depend on whether or not an
\r
277 error has been found in any tasks. */
\r
278 mainLED_0 = !mainLED_0;
\r
281 /*-----------------------------------------------------------*/
\r
283 int __low_level_init(void)
\r
285 unsigned portCHAR ucResetFlag = RESF;
\r
287 portDISABLE_INTERRUPTS();
\r
289 /* Clock Configuration:
\r
290 In this port, to use the internal high speed clock source of the microcontroller
\r
291 define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h. To use an external
\r
292 clock define configCLOCK_SOURCE as 0. */
\r
293 #if configCLOCK_SOURCE == 1
\r
295 /* Set XT1 and XT2 in Input Port Mode
\r
296 Set X1 and X2 in Input Port Mode
\r
297 High speed oscillator frequency 2MHz <= fMX <= 10MHz */
\r
300 /* X1 external oszillation stopped. */
\r
303 /* Enable internal high speed oszillation. */
\r
307 /* Stop internal subsystem clock. */
\r
310 /* Set clock speed. */
\r
312 CKC &= (unsigned portCHAR)~0x07;
\r
317 /* XT1 and XT2 pin in input port mode
\r
318 X1 and X2 pin in crystal resonator mode
\r
319 High speed oszillation frequency 10MHz < fMX <= 20MHz */
\r
322 /* Set oscillation stabilization time. */
\r
325 /* Set speed mode: fMX > 10MHz for Flash memory high speed operation. */
\r
328 /* Start up X1 oscillator operation
\r
329 Internal high-speed oscillator operating. */
\r
332 /* Check oscillation stabilization time status. */
\r
335 /* Wait until X1 clock stabilization time. */
\r
339 /* Switch CPU clock to X1 oscillator. */
\r
343 /* Wait until CPU and peripherals operate with fX1 clock. */
\r
347 /* Stop the internal high-speed oscillator operation. */
\r
350 /* Stop the XT1 oscillator operation. */
\r
353 /* Operating frequency f = fx
\r
354 Change clock generator setting, if necessary. */
\r
357 /* From here onwards the X1 oscillator is supplied to the CPU. */
\r
361 /* LED port initialization - set port register. */
\r
364 /* Set port mode register. */
\r
367 /* Switch pin initialization - enable pull-up resistor. */
\r
370 /* INTP0 is used by the button on the target board. */
\r
372 /* INTP0 disable. */
\r
375 /* INTP0 IF clear. */
\r
379 /* INTP0 priority low. */
\r
383 /* Enable ext. INTP0 interrupt */
\r
388 /*-----------------------------------------------------------*/
\r
390 void vRegTestError( void )
\r
392 /* Called by the RegTest tasks if an error is found. lRegTestStatus is
\r
393 inspected by the check task. */
\r
394 sRegTestStatus = pdFAIL;
\r
396 /* Do not return from here as the reg test tasks clobber all registers so
\r
397 function calls may not function correctly. */
\r
400 /*-----------------------------------------------------------*/
\r
402 void vApplicationStackOverflowHook( void )
\r
404 /* This will get called if an overflow is detected in the stack of a task.
\r
405 Inspect pxCurrentTCB to see which was the offending task. */
\r