2 FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! Why not get us to quote to get FreeRTOS.org *
\r
30 * running on your hardware - or even write all or part of your application*
\r
31 * for you? See http://www.OpenRTOS.com for details. *
\r
33 ***************************************************************************
\r
34 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
52 + Call to taskYIELD() from within tick ISR has been replaced by the more
\r
53 efficient portSWITCH_CONTEXT().
\r
54 + ISR function definitions renamed to include the prv prefix.
\r
58 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
59 macro to be consistent with the later ports.
\r
62 /*-----------------------------------------------------------
\r
63 * Implementation of functions defined in portable.h for the Flashlite 186
\r
65 *----------------------------------------------------------*/
\r
71 #include "FreeRTOS.h"
\r
73 #include "portasm.h"
\r
75 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
77 #define portTIMER_EOI_TYPE ( 8 )
\r
78 #define portRESET_PIC() portOUTPUT_WORD( ( unsigned portSHORT ) 0xff22, portTIMER_EOI_TYPE )
\r
79 #define portTIMER_INT_NUMBER 0x12
\r
81 #define portTIMER_1_CONTROL_REGISTER ( ( unsigned portSHORT ) 0xff5e )
\r
82 #define portTIMER_0_CONTROL_REGISTER ( ( unsigned portSHORT ) 0xff56 )
\r
83 #define portTIMER_INTERRUPT_ENABLE ( ( unsigned portSHORT ) 0x2000 )
\r
85 /* Setup the hardware to generate the required tick frequency. */
\r
86 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz );
\r
88 /* Set the hardware back to the state as per before the scheduler started. */
\r
89 static void prvExitFunction( void );
\r
91 /* The ISR used depends on whether the preemptive or cooperative scheduler
\r
93 #if( configUSE_PREEMPTION == 1 )
\r
94 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
96 static void __interrupt __far prvPreemptiveTick( void );
\r
98 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
100 static void __interrupt __far prvNonPreemptiveTick( void );
\r
103 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
104 static void __interrupt __far prvYieldProcessor( void );
\r
106 /*lint -e956 File scopes necessary here. */
\r
108 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
109 static portBASE_TYPE xSchedulerRunning = pdFALSE;
\r
111 /* Points to the original routine installed on the vector we use for manual
\r
112 context switches. This is then used to restore the original routine during
\r
113 prvExitFunction(). */
\r
114 static void ( __interrupt __far *pxOldSwitchISR )();
\r
116 /* Used to restore the original DOS context when the scheduler is ended. */
\r
117 static jmp_buf xJumpBuf;
\r
121 /*-----------------------------------------------------------*/
\r
122 portBASE_TYPE xPortStartScheduler( void )
\r
124 /* This is called with interrupts already disabled. */
\r
126 /* Remember what was on the interrupts we are going to use
\r
127 so we can put them back later if required. */
\r
128 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
130 /* Put our manual switch (yield) function on a known
\r
132 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
134 #if( configUSE_PREEMPTION == 1 )
\r
136 /* Put our tick switch function on the timer interrupt. */
\r
137 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
141 /* We want the timer interrupt to just increment the tick count. */
\r
142 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
146 prvSetTickFrequency( configTICK_RATE_HZ );
\r
148 /* Clean up function if we want to return to DOS. */
\r
149 if( setjmp( xJumpBuf ) != 0 )
\r
152 xSchedulerRunning = pdFALSE;
\r
156 xSchedulerRunning = pdTRUE;
\r
158 /* Kick off the scheduler by setting up the context of the first task. */
\r
159 portFIRST_CONTEXT();
\r
162 return xSchedulerRunning;
\r
164 /*-----------------------------------------------------------*/
\r
166 /* The ISR used depends on whether the preemptive or cooperative scheduler
\r
168 #if( configUSE_PREEMPTION == 1 )
\r
169 static void __interrupt __far prvPreemptiveTick( void )
\r
171 /* Get the scheduler to update the task states following the tick. */
\r
172 vTaskIncrementTick();
\r
174 /* Switch in the context of the next task to be run. */
\r
175 portSWITCH_CONTEXT();
\r
177 /* Reset the PIC ready for the next time. */
\r
181 static void __interrupt __far prvNonPreemptiveTick( void )
\r
183 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
184 so we don't have to switch in the context of the next task. */
\r
185 vTaskIncrementTick();
\r
189 /*-----------------------------------------------------------*/
\r
191 static void __interrupt __far prvYieldProcessor( void )
\r
193 /* Switch in the context of the next task to be run. */
\r
194 portSWITCH_CONTEXT();
\r
196 /*-----------------------------------------------------------*/
\r
198 void vPortEndScheduler( void )
\r
200 /* Jump back to the processor state prior to starting the
\r
201 scheduler. This means we are not going to be using a
\r
202 task stack frame so the task can be deleted. */
\r
203 longjmp( xJumpBuf, 1 );
\r
205 /*-----------------------------------------------------------*/
\r
207 static void prvExitFunction( void )
\r
209 const unsigned portSHORT usTimerDisable = 0x0000;
\r
210 unsigned portSHORT usTimer0Control;
\r
212 /* Interrupts should be disabled here anyway - but no
\r
213 harm in making sure. */
\r
214 portDISABLE_INTERRUPTS();
\r
215 if( xSchedulerRunning == pdTRUE )
\r
217 /* Put back the switch interrupt routines that was in place
\r
218 before the scheduler started. */
\r
219 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
222 /* Disable the timer used for the tick to ensure the scheduler is
\r
223 not called before restoring interrupts. There was previously nothing
\r
224 on this timer so there is no old ISR to restore. */
\r
225 portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );
\r
227 /* Restart the DOS tick. */
\r
228 usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
\r
229 usTimer0Control |= portTIMER_INTERRUPT_ENABLE;
\r
230 portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
\r
233 portENABLE_INTERRUPTS();
\r
235 /* This will free up all the memory used by the scheduler.
\r
236 exiting back to dos with INT21 AH=4CH will do this anyway so
\r
237 it is not necessary to call this. */
\r
238 vTaskCleanUpResources();
\r
240 /*-----------------------------------------------------------*/
\r
242 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz )
\r
244 const unsigned portSHORT usMaxCountRegister = 0xff5a;
\r
245 const unsigned portSHORT usTimerPriorityRegister = 0xff32;
\r
246 const unsigned portSHORT usTimerEnable = 0xC000;
\r
247 const unsigned portSHORT usRetrigger = 0x0001;
\r
248 const unsigned portSHORT usTimerHighPriority = 0x0000;
\r
249 unsigned portSHORT usTimer0Control;
\r
251 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */
\r
253 const unsigned portLONG ulClockFrequency = ( unsigned portLONG ) 0x7f31a0UL;
\r
255 unsigned portLONG ulTimerCount = ulClockFrequency / ulTickRateHz;
\r
257 portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );
\r
258 portOUTPUT_WORD( usMaxCountRegister, ( unsigned portSHORT ) ulTimerCount );
\r
259 portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );
\r
261 /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */
\r
262 usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
\r
263 usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;
\r
264 portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
\r