2 FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
77 * documentation provides more details of the standard demo application tasks.
\r
78 * In addition to the standard demo tasks, the following tasks and tests are
\r
79 * defined and/or created within this file:
\r
81 * "Check" task - This only executes every three seconds but has a high priority
\r
82 * to ensure it gets processor time. Its main function is to check that all the
\r
83 * standard demo tasks are still operational. If everything is running as
\r
84 * expected then the check task will toggle an LED every 3 seconds. An error
\r
85 * being discovered in any task will cause the toggle rate to increase to 500ms.
\r
87 * "Reg test" tasks - These fill the registers with known values, then check
\r
88 * that each register still contains its expected value. Each task uses
\r
89 * different values. The tasks run with very low priority so get preempted very
\r
90 * frequently. A register containing an unexpected value is indicative of an
\r
91 * error in the context switching mechanism.
\r
94 * Also in addition to the standard demo tasks is a button push task. This is
\r
95 * a very basic task that is included as an example of how to write an interrupt
\r
96 * service routine that interacts with a task. The button on the target board
\r
97 * is used to generate an interrupt that 'gives' a semaphore in order to unblock
\r
98 * a task. In doing so the task is synchronised with the interrupt. Each time
\r
99 * the task unblocks it simply toggles an LED before entering the Blocked state
\r
100 * again to wait for the next button push.
\r
103 /* Standard includes. */
\r
104 #include <stdlib.h>
\r
105 #include <string.h>
\r
107 /* Scheduler include files. */
\r
108 #include "FreeRTOS.h"
\r
111 /* Standard demo file headers. */
\r
113 #include "semtest.h"
\r
114 #include "GenQTest.h"
\r
115 #include "dynamic.h"
\r
116 #include "blocktim.h"
\r
119 * Priority definitions for most of the tasks in the demo application. Some
\r
120 * tasks just use the idle priority.
\r
122 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
123 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
124 #define mainSEMTEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
125 #define mainBUTTON_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
126 #define mainGEN_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
128 /* The period between executions of the check task. */
\r
129 #define mainNO_ERROR_TOGGLE_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
130 #define mainERROR_TOGGLE_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
132 /* The LED toggled by the check task. */
\r
133 #define mainLED_0 P7_bit.no6
\r
135 /* A value that is passed in as the parameter to the 'check' task. This is done
\r
136 purely to check that the parameter passing mechanism is functioning correctly. */
\r
137 #define mainCHECK_PARAMETER_VALUE ( 0x5678 )
\r
139 /*-----------------------------------------------------------*/
\r
142 * The function that defines the 'check' task as described at the top of this
\r
145 static void vErrorChecks( void *pvParameters );
\r
149 * This function is called from the C startup routine to setup the processor -
\r
150 * in particular the clock source.
\r
152 int __low_level_init(void);
\r
155 * Functions that define the RegTest tasks as described at the top of this file.
\r
157 extern void vRegTest1( void *pvParameters );
\r
158 extern void vRegTest2( void *pvParameters );
\r
161 * Function that defines the button push task as described at the top of this
\r
164 extern void vButtonTask( void *pvParameters );
\r
166 /*-----------------------------------------------------------*/
\r
168 /* If an error is discovered by one of the RegTest tasks then this flag is set
\r
169 to pdFAIL. The 'check' task then inspects this flag to detect errors within
\r
170 the RegTest tasks. */
\r
171 static short sRegTestStatus = pdPASS;
\r
173 /* 78K0R Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface
\r
175 __root __far const unsigned portCHAR OptionByte[] @ 0x00C0 =
\r
177 WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED
\r
180 /* Security byte definition */
\r
181 __root __far const unsigned portCHAR SecuIDCode[] @ 0x00C4 =
\r
183 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
\r
187 /*-----------------------------------------------------------*/
\r
191 /* Creates all the tasks, then starts the scheduler. */
\r
193 /* First create the 'standard demo' tasks. These are used to demonstrate
\r
194 API functions being used and also to test the kernel port. More information
\r
195 is provided on the FreeRTOS.org WEB site. */
\r
196 vStartDynamicPriorityTasks();
\r
198 /* Create the RegTest tasks as described at the top of this file. */
\r
199 xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
200 xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
202 /* Create the button push task as described at the top of this file. */
\r
203 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_PRIORITY, NULL );
\r
205 /* Create the 'check' task as described at the top of this file. */
\r
206 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, ( void* )mainCHECK_PARAMETER_VALUE, mainCHECK_TASK_PRIORITY, NULL );
\r
208 #ifdef __IAR_78K0R_Kx3__
\r
210 /* The Kx3 has enough RAM to create more of the standard demo tasks. */
\r
211 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
212 vStartSemaphoreTasks(mainSEMTEST_PRIORITY);
\r
213 vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );
\r
214 vCreateBlockTimeTasks();
\r
218 /* Finally start the scheduler running. */
\r
219 vTaskStartScheduler();
\r
221 /* If this line is reached then vTaskStartScheduler() returned because there
\r
222 was insufficient heap memory remaining for the idle task to be created. */
\r
225 /*-----------------------------------------------------------*/
\r
227 static void vErrorChecks( void *pvParameters )
\r
229 portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;
\r
231 /* Ensure the parameter was passed in as expected. This is just a test of
\r
232 the kernel port, the parameter is not actually used for anything. The
\r
233 pointer will only actually be either 3 or 2 bytes, depending on the memory
\r
235 if( pvParameters != ( void * ) mainCHECK_PARAMETER_VALUE )
\r
237 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
240 /* Initialise xLastWakeTime before it is used. After this point it is not
\r
241 written to directly. */
\r
242 xLastWakeTime = xTaskGetTickCount();
\r
244 /* Cycle for ever, delaying then checking all the other tasks are still
\r
245 operating without error. */
\r
248 /* Wait until it is time to check all the other tasks again. */
\r
249 vTaskDelayUntil( &xLastWakeTime, xToggleRate );
\r
251 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
253 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
256 if( sRegTestStatus != pdPASS )
\r
258 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
261 #ifdef __IAR_78K0R_Kx3__
\r
263 /* Only the Kx3 runs all the tasks. */
\r
264 if( xArePollingQueuesStillRunning() != pdTRUE)
\r
266 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
269 if( xAreSemaphoreTasksStillRunning() != pdTRUE)
\r
271 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
274 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
276 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
279 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
281 xToggleRate = mainERROR_TOGGLE_PERIOD;
\r
286 /* Toggle the LED. The toggle rate will depend on whether or not an
\r
287 error has been found in any tasks. */
\r
288 mainLED_0 = !mainLED_0;
\r
291 /*-----------------------------------------------------------*/
\r
293 int __low_level_init(void)
\r
295 unsigned portCHAR ucResetFlag = RESF;
\r
297 portDISABLE_INTERRUPTS();
\r
299 /* Clock Configuration:
\r
300 In this port, to use the internal high speed clock source of the microcontroller
\r
301 define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h. To use an external
\r
302 clock define configCLOCK_SOURCE as 0. */
\r
303 #if configCLOCK_SOURCE == 1
\r
305 /* Set XT1 and XT2 in Input Port Mode
\r
306 Set X1 and X2 in Input Port Mode
\r
307 High speed oscillator frequency 2MHz <= fMX <= 10MHz */
\r
310 /* X1 external oszillation stopped. */
\r
313 /* Enable internal high speed oszillation. */
\r
317 /* Stop internal subsystem clock. */
\r
320 /* Set clock speed. */
\r
322 CKC &= (unsigned portCHAR)~0x07;
\r
327 /* XT1 and XT2 pin in input port mode
\r
328 X1 and X2 pin in crystal resonator mode
\r
329 High speed oszillation frequency 10MHz < fMX <= 20MHz */
\r
332 /* Set oscillation stabilization time. */
\r
335 /* Set speed mode: fMX > 10MHz for Flash memory high speed operation. */
\r
338 /* Start up X1 oscillator operation
\r
339 Internal high-speed oscillator operating. */
\r
342 /* Check oscillation stabilization time status. */
\r
345 /* Wait until X1 clock stabilization time. */
\r
349 /* Switch CPU clock to X1 oscillator. */
\r
353 /* Wait until CPU and peripherals operate with fX1 clock. */
\r
357 /* Stop the internal high-speed oscillator operation. */
\r
360 /* Stop the XT1 oscillator operation. */
\r
363 /* Operating frequency f = fx
\r
364 Change clock generator setting, if necessary. */
\r
367 /* From here onwards the X1 oscillator is supplied to the CPU. */
\r
371 /* LED port initialization - set port register. */
\r
374 /* Set port mode register. */
\r
377 /* Switch pin initialization - enable pull-up resistor. */
\r
380 /* INTP0 is used by the button on the target board. */
\r
382 /* INTP0 disable. */
\r
385 /* INTP0 IF clear. */
\r
389 /* INTP0 priority low. */
\r
393 /* Enable ext. INTP0 interrupt */
\r
398 /*-----------------------------------------------------------*/
\r
400 void vRegTestError( void )
\r
402 /* Called by the RegTest tasks if an error is found. lRegTestStatus is
\r
403 inspected by the check task. */
\r
404 sRegTestStatus = pdFAIL;
\r
406 /* Do not return from here as the reg test tasks clobber all registers so
\r
407 function calls may not function correctly. */
\r
410 /*-----------------------------------------------------------*/
\r
412 void vApplicationStackOverflowHook( void )
\r
414 /* This will get called if an overflow is detected in the stack of a task.
\r
415 Inspect pxCurrentTCB to see which was the offending task. */
\r