2 FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* Standard includes. */
\r
57 /* Scheduler includes. */
\r
58 #include "FreeRTOS.h"
\r
61 /* The critical nesting value is initialised to a non zero value to ensure
\r
62 interrupts don't accidentally become enabled before the scheduler is started. */
\r
63 #define portINITIAL_CRITICAL_NESTING (( unsigned short ) 10)
\r
65 /* Initial PSW value allocated to a newly created task.
\r
67 * ||||||||-------------- Fill byte
\r
68 * |||||||--------------- Carry Flag cleared
\r
69 * |||||----------------- In-service priority Flags set to low level
\r
70 * ||||------------------ Register bank Select 0 Flag cleared
\r
71 * |||------------------- Auxiliary Carry Flag cleared
\r
72 * ||-------------------- Register bank Select 1 Flag cleared
\r
73 * |--------------------- Zero Flag set
\r
74 * ---------------------- Global Interrupt Flag set (enabled)
\r
76 #define portPSW (0xc6UL)
\r
78 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
79 any details of its type. */
\r
80 typedef void tskTCB;
\r
81 extern volatile tskTCB * volatile pxCurrentTCB;
\r
83 /* Most ports implement critical sections by placing the interrupt flags on
\r
84 the stack before disabling interrupts. Exiting the critical section is then
\r
85 simply a case of popping the flags from the stack. As 78K0 IAR does not use
\r
86 a frame pointer this cannot be done as modifying the stack will clobber all
\r
87 the stack variables. Instead each task maintains a count of the critical
\r
88 section nesting depth. Each time a critical section is entered the count is
\r
89 incremented. Each time a critical section is left the count is decremented -
\r
90 with interrupts only being re-enabled if the count is zero.
\r
92 usCriticalNesting will get set to zero when the scheduler starts, but must
\r
93 not be initialised to zero as this will cause problems during the startup
\r
95 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
96 /*-----------------------------------------------------------*/
\r
99 * Sets up the periodic ISR used for the RTOS tick.
\r
101 static void prvSetupTimerInterrupt( void );
\r
102 /*-----------------------------------------------------------*/
\r
105 * Initialise the stack of a task to look exactly as if a call to
\r
106 * portSAVE_CONTEXT had been called.
\r
108 * See the header file portable.h.
\r
110 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
112 unsigned long *pulLocal;
\r
114 #if configMEMORY_MODE == 1
\r
116 /* Parameters are passed in on the stack, and written using a 32bit value
\r
117 hence a space is left for the second two bytes. */
\r
120 /* Write in the parameter value. */
\r
121 pulLocal = ( unsigned long * ) pxTopOfStack;
\r
122 *pulLocal = ( unsigned long ) pvParameters;
\r
125 /* These values are just spacers. The return address of the function
\r
126 would normally be written here. */
\r
127 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;
\r
129 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;
\r
132 /* The start address / PSW value is also written in as a 32bit value,
\r
133 so leave a space for the second two bytes. */
\r
136 /* Task function start address combined with the PSW. */
\r
137 pulLocal = ( unsigned long * ) pxTopOfStack;
\r
138 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );
\r
141 /* An initial value for the AX register. */
\r
142 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;
\r
147 /* Task function address is written to the stack first. As it is
\r
148 written as a 32bit value a space is left on the stack for the second
\r
152 /* Task function start address combined with the PSW. */
\r
153 pulLocal = ( unsigned long * ) pxTopOfStack;
\r
154 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );
\r
157 /* The parameter is passed in AX. */
\r
158 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
163 /* An initial value for the HL register. */
\r
164 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;
\r
167 /* CS and ES registers. */
\r
168 *pxTopOfStack = ( portSTACK_TYPE ) 0x0F00;
\r
171 /* Finally the remaining general purpose registers DE and BC */
\r
172 *pxTopOfStack = ( portSTACK_TYPE ) 0xDEDE;
\r
174 *pxTopOfStack = ( portSTACK_TYPE ) 0xBCBC;
\r
177 /* Finally the critical section nesting count is set to zero when the task
\r
179 *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;
\r
181 /* Return a pointer to the top of the stack we have generated so this can
\r
182 be stored in the task control block for the task. */
\r
183 return pxTopOfStack;
\r
185 /*-----------------------------------------------------------*/
\r
187 portBASE_TYPE xPortStartScheduler( void )
\r
189 /* Setup the hardware to generate the tick. Interrupts are disabled when
\r
190 this function is called. */
\r
191 prvSetupTimerInterrupt();
\r
193 /* Restore the context of the first task that is going to run. */
\r
196 /* Should not get here as the tasks are now running! */
\r
199 /*-----------------------------------------------------------*/
\r
201 void vPortEndScheduler( void )
\r
203 /* It is unlikely that the 78K0R port will get stopped. If required simply
\r
204 disable the tick interrupt here. */
\r
206 /*-----------------------------------------------------------*/
\r
208 static void prvSetupTimerInterrupt( void )
\r
210 /* Setup channel 5 of the TAU to generate the tick interrupt. */
\r
212 /* First the Timer Array Unit has to be enabled. */
\r
215 /* To configure the Timer Array Unit all Channels have to first be stopped. */
\r
218 /* Interrupt of Timer Array Unit Channel 5 is disabled to set the interrupt
\r
222 /* Clear Timer Array Unit Channel 5 interrupt flag. */
\r
225 /* Set Timer Array Unit Channel 5 interrupt priority */
\r
229 /* Set Timer Array Unit Channel 5 Mode as interval timer. */
\r
232 /* Set the compare match value according to the tick rate we want. */
\r
233 TDR05 = ( portTickType ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
\r
235 /* Set Timer Array Unit Channel 5 output mode */
\r
238 /* Set Timer Array Unit Channel 5 output level */
\r
241 /* Set Timer Array Unit Channel 5 output enable */
\r
244 /* Interrupt of Timer Array Unit Channel 5 enabled */
\r
247 /* Start Timer Array Unit Channel 5.*/
\r
250 /*-----------------------------------------------------------*/
\r