2 FreeRTOS V6.1.1 - Copyright (C) 2011 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 books - available as PDF or paperback *
\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
55 /*-----------------------------------------------------------
\r
56 * Implementation of functions defined in portable.h for the Tern EE 186
\r
58 *----------------------------------------------------------*/
\r
60 /* Library includes. */
\r
61 #include <embedded.h>
\r
64 /* Scheduler includes. */
\r
65 #include "FreeRTOS.h"
\r
67 #include "portasm.h"
\r
69 /* The timer increments every four clocks, hence the divide by 4. */
\r
70 #define portTIMER_COMPARE ( unsigned short ) ( ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / ( unsigned long ) 4 )
\r
72 /* From the RDC data sheet. */
\r
73 #define portENABLE_TIMER_AND_INTERRUPT ( unsigned short ) 0xe001
\r
75 /* Interrupt control. */
\r
76 #define portEIO_REGISTER 0xff22
\r
77 #define portCLEAR_INTERRUPT 0x0008
\r
79 /* Setup the hardware to generate the required tick frequency. */
\r
80 static void prvSetupTimerInterrupt( void );
\r
82 /* The ISR used depends on whether the preemptive or cooperative scheduler
\r
84 #if( configUSE_PREEMPTION == 1 )
\r
85 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
87 static void __interrupt __far prvPreemptiveTick( void );
\r
89 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
91 static void __interrupt __far prvNonPreemptiveTick( void );
\r
94 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
95 static void __interrupt __far prvYieldProcessor( void );
\r
97 /* The timer initialisation functions leave interrupts enabled,
\r
98 which is not what we want. This ISR is installed temporarily in case
\r
99 the timer fires before we get a change to disable interrupts again. */
\r
100 static void __interrupt __far prvDummyISR( void );
\r
102 /*-----------------------------------------------------------*/
\r
103 /* See header file for description. */
\r
104 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
106 portSTACK_TYPE DS_Reg = 0;
\r
108 /* Place a few bytes of known values on the bottom of the stack.
\r
109 This is just useful for debugging. */
\r
111 *pxTopOfStack = 0x1111;
\r
113 *pxTopOfStack = 0x2222;
\r
115 *pxTopOfStack = 0x3333;
\r
118 /* We are going to start the scheduler using a return from interrupt
\r
119 instruction to load the program counter, so first there would be the
\r
120 function call with parameters preamble. */
\r
122 *pxTopOfStack = FP_SEG( pvParameters );
\r
124 *pxTopOfStack = FP_OFF( pvParameters );
\r
126 *pxTopOfStack = FP_SEG( pxCode );
\r
128 *pxTopOfStack = FP_OFF( pxCode );
\r
131 /* Next the status register and interrupt return address. */
\r
132 *pxTopOfStack = portINITIAL_SW;
\r
134 *pxTopOfStack = FP_SEG( pxCode );
\r
136 *pxTopOfStack = FP_OFF( pxCode );
\r
139 /* The remaining registers would be pushed on the stack by our context
\r
140 switch function. These are loaded with values simply to make debugging
\r
142 *pxTopOfStack = ( portSTACK_TYPE ) 0xAAAA; /* AX */
\r
144 *pxTopOfStack = ( portSTACK_TYPE ) 0xBBBB; /* BX */
\r
146 *pxTopOfStack = ( portSTACK_TYPE ) 0xCCCC; /* CX */
\r
148 *pxTopOfStack = ( portSTACK_TYPE ) 0xDDDD; /* DX */
\r
150 *pxTopOfStack = ( portSTACK_TYPE ) 0xEEEE; /* ES */
\r
153 /* We need the true data segment. */
\r
154 __asm{ MOV DS_Reg, DS };
\r
156 *pxTopOfStack = DS_Reg; /* DS */
\r
158 *pxTopOfStack = ( portSTACK_TYPE ) 0x0123; /* SI */
\r
160 *pxTopOfStack = ( portSTACK_TYPE ) 0xDDDD; /* DI */
\r
162 *pxTopOfStack = ( portSTACK_TYPE ) 0xBBBB; /* BP */
\r
164 return pxTopOfStack;
\r
166 /*-----------------------------------------------------------*/
\r
168 portBASE_TYPE xPortStartScheduler( void )
\r
170 /* This is called with interrupts already disabled. */
\r
172 /* Put our manual switch (yield) function on a known
\r
174 setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
176 /* Setup the tick interrupt. */
\r
177 prvSetupTimerInterrupt();
\r
179 /* Kick off the scheduler by setting up the context of the first task. */
\r
180 portFIRST_CONTEXT();
\r
182 /* Should not get here! */
\r
185 /*-----------------------------------------------------------*/
\r
187 static void __interrupt __far prvDummyISR( void )
\r
189 /* The timer initialisation functions leave interrupts enabled,
\r
190 which is not what we want. This ISR is installed temporarily in case
\r
191 the timer fires before we get a change to disable interrupts again. */
\r
192 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
\r
194 /*-----------------------------------------------------------*/
\r
196 /* The ISR used depends on whether the preemptive or cooperative scheduler
\r
198 #if( configUSE_PREEMPTION == 1 )
\r
199 static void __interrupt __far prvPreemptiveTick( void )
\r
201 /* Get the scheduler to update the task states following the tick. */
\r
202 vTaskIncrementTick();
\r
204 /* Switch in the context of the next task to be run. */
\r
205 portSWITCH_CONTEXT();
\r
207 /* Reset interrupt. */
\r
208 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
\r
211 static void __interrupt __far prvNonPreemptiveTick( void )
\r
213 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
214 so we don't have to switch in the context of the next task. */
\r
215 vTaskIncrementTick();
\r
216 /* Reset interrupt. */
\r
217 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
\r
220 /*-----------------------------------------------------------*/
\r
222 static void __interrupt __far prvYieldProcessor( void )
\r
224 /* Switch in the context of the next task to be run. */
\r
225 portSWITCH_CONTEXT();
\r
227 /*-----------------------------------------------------------*/
\r
229 void vPortEndScheduler( void )
\r
231 /* Not implemented. */
\r
233 /*-----------------------------------------------------------*/
\r
235 static void prvSetupTimerInterrupt( void )
\r
237 const unsigned short usTimerACompare = portTIMER_COMPARE, usTimerAMode = portENABLE_TIMER_AND_INTERRUPT;
\r
238 const unsigned short usT2_IRQ = 0x13;
\r
240 /* Configure the timer, the dummy handler is used here as the init
\r
241 function leaves interrupts enabled. */
\r
242 t2_init( usTimerAMode, usTimerACompare, prvDummyISR );
\r
244 /* Disable interrupts again before installing the real handlers. */
\r
245 portDISABLE_INTERRUPTS();
\r
247 #if( configUSE_PREEMPTION == 1 )
\r
248 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
250 setvect( usT2_IRQ, prvPreemptiveTick );
\r
252 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
254 setvect( usT2_IRQ, prvNonPreemptiveTick );
\r