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
67 /* Standard includes. */
\r
70 /* Scheduler includes. */
\r
71 #include "FreeRTOS.h"
\r
74 /* The critical nesting value is initialised to a non zero value to ensure
\r
75 interrupts don't accidentally become enabled before the scheduler is started. */
\r
76 #define portINITIAL_CRITICAL_NESTING (( unsigned short ) 10)
\r
78 /* Initial PSW value allocated to a newly created task.
\r
80 * ||||||||-------------- Fill byte
\r
81 * |||||||--------------- Carry Flag cleared
\r
82 * |||||----------------- In-service priority Flags set to low level
\r
83 * ||||------------------ Register bank Select 0 Flag cleared
\r
84 * |||------------------- Auxiliary Carry Flag cleared
\r
85 * ||-------------------- Register bank Select 1 Flag cleared
\r
86 * |--------------------- Zero Flag set
\r
87 * ---------------------- Global Interrupt Flag set (enabled)
\r
89 #define portPSW (0xc6UL)
\r
91 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
92 any details of its type. */
\r
93 typedef void tskTCB;
\r
94 extern volatile tskTCB * volatile pxCurrentTCB;
\r
96 /* Most ports implement critical sections by placing the interrupt flags on
\r
97 the stack before disabling interrupts. Exiting the critical section is then
\r
98 simply a case of popping the flags from the stack. As 78K0 IAR does not use
\r
99 a frame pointer this cannot be done as modifying the stack will clobber all
\r
100 the stack variables. Instead each task maintains a count of the critical
\r
101 section nesting depth. Each time a critical section is entered the count is
\r
102 incremented. Each time a critical section is left the count is decremented -
\r
103 with interrupts only being re-enabled if the count is zero.
\r
105 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
106 not be initialised to zero as this will cause problems during the startup
\r
108 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
109 /*-----------------------------------------------------------*/
\r
112 * Sets up the periodic ISR used for the RTOS tick.
\r
114 static void prvSetupTimerInterrupt( void );
\r
115 /*-----------------------------------------------------------*/
\r
118 * Initialise the stack of a task to look exactly as if a call to
\r
119 * portSAVE_CONTEXT had been called.
\r
121 * See the header file portable.h.
\r
123 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
125 unsigned long *pulLocal;
\r
127 #if configMEMORY_MODE == 1
\r
129 /* Parameters are passed in on the stack, and written using a 32bit value
\r
130 hence a space is left for the second two bytes. */
\r
133 /* Write in the parameter value. */
\r
134 pulLocal = ( unsigned long * ) pxTopOfStack;
\r
135 *pulLocal = ( unsigned long ) pvParameters;
\r
138 /* These values are just spacers. The return address of the function
\r
139 would normally be written here. */
\r
140 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;
\r
142 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;
\r
145 /* The start address / PSW value is also written in as a 32bit value,
\r
146 so leave a space for the second two bytes. */
\r
149 /* Task function start address combined with the PSW. */
\r
150 pulLocal = ( unsigned long * ) pxTopOfStack;
\r
151 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );
\r
154 /* An initial value for the AX register. */
\r
155 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;
\r
160 /* Task function address is written to the stack first. As it is
\r
161 written as a 32bit value a space is left on the stack for the second
\r
165 /* Task function start address combined with the PSW. */
\r
166 pulLocal = ( unsigned long * ) pxTopOfStack;
\r
167 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );
\r
170 /* The parameter is passed in AX. */
\r
171 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
176 /* An initial value for the HL register. */
\r
177 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;
\r
180 /* CS and ES registers. */
\r
181 *pxTopOfStack = ( portSTACK_TYPE ) 0x0F00;
\r
184 /* Finally the remaining general purpose registers DE and BC */
\r
185 *pxTopOfStack = ( portSTACK_TYPE ) 0xDEDE;
\r
187 *pxTopOfStack = ( portSTACK_TYPE ) 0xBCBC;
\r
190 /* Finally the critical section nesting count is set to zero when the task
\r
192 *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;
\r
194 /* Return a pointer to the top of the stack we have generated so this can
\r
195 be stored in the task control block for the task. */
\r
196 return pxTopOfStack;
\r
198 /*-----------------------------------------------------------*/
\r
200 portBASE_TYPE xPortStartScheduler( void )
\r
202 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
203 this function is called. */
\r
204 prvSetupTimerInterrupt();
\r
206 /* Restore the context of the first task that is going to run. */
\r
209 /* Should not get here as the tasks are now running! */
\r
212 /*-----------------------------------------------------------*/
\r
214 void vPortEndScheduler( void )
\r
216 /* It is unlikely that the 78K0R port will get stopped. If required simply
\r
217 disable the tick interrupt here. */
\r
219 /*-----------------------------------------------------------*/
\r
221 static void prvSetupTimerInterrupt( void )
\r
223 /* Setup channel 5 of the TAU to generate the tick interrupt. */
\r
225 /* First the Timer Array Unit has to be enabled. */
\r
228 /* To configure the Timer Array Unit all Channels have to first be stopped. */
\r
231 /* Interrupt of Timer Array Unit Channel 5 is disabled to set the interrupt
\r
235 /* Clear Timer Array Unit Channel 5 interrupt flag. */
\r
238 /* Set Timer Array Unit Channel 5 interrupt priority */
\r
242 /* Set Timer Array Unit Channel 5 Mode as interval timer. */
\r
245 /* Set the compare match value according to the tick rate we want. */
\r
246 TDR05 = ( portTickType ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
\r
248 /* Set Timer Array Unit Channel 5 output mode */
\r
251 /* Set Timer Array Unit Channel 5 output level */
\r
254 /* Set Timer Array Unit Channel 5 output enable */
\r
257 /* Interrupt of Timer Array Unit Channel 5 enabled */
\r
260 /* Start Timer Array Unit Channel 5.*/
\r
263 /*-----------------------------------------------------------*/
\r