2 FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 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 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
39 + Call to taskYIELD() from within tick ISR has been replaced by the more
\r
40 efficient portSWITCH_CONTEXT().
\r
41 + ISR function definitions renamed to include the prv prefix.
\r
45 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
46 macro to be consistent with the later ports.
\r
49 /*-----------------------------------------------------------
\r
50 * Implementation of functions defined in portable.h for the Flashlite 186
\r
52 *----------------------------------------------------------*/
\r
58 #include "FreeRTOS.h"
\r
60 #include "portasm.h"
\r
62 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
64 #define portTIMER_EOI_TYPE ( 8 )
\r
65 #define portRESET_PIC() portOUTPUT_WORD( ( unsigned portSHORT ) 0xff22, portTIMER_EOI_TYPE )
\r
66 #define portTIMER_INT_NUMBER 0x12
\r
68 #define portTIMER_1_CONTROL_REGISTER ( ( unsigned portSHORT ) 0xff5e )
\r
69 #define portTIMER_0_CONTROL_REGISTER ( ( unsigned portSHORT ) 0xff56 )
\r
70 #define portTIMER_INTERRUPT_ENABLE ( ( unsigned portSHORT ) 0x2000 )
\r
72 /* Setup the hardware to generate the required tick frequency. */
\r
73 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz );
\r
75 /* Set the hardware back to the state as per before the scheduler started. */
\r
76 static void prvExitFunction( void );
\r
78 /* The ISR used depends on whether the preemptive or cooperative scheduler
\r
80 #if( configUSE_PREEMPTION == 1 )
\r
81 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
83 static void __interrupt __far prvPreemptiveTick( void );
\r
85 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
87 static void __interrupt __far prvNonPreemptiveTick( void );
\r
90 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
91 static void __interrupt __far prvYieldProcessor( void );
\r
93 /*lint -e956 File scopes necessary here. */
\r
95 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
96 static portBASE_TYPE xSchedulerRunning = pdFALSE;
\r
98 /* Points to the original routine installed on the vector we use for manual
\r
99 context switches. This is then used to restore the original routine during
\r
100 prvExitFunction(). */
\r
101 static void ( __interrupt __far *pxOldSwitchISR )();
\r
103 /* Used to restore the original DOS context when the scheduler is ended. */
\r
104 static jmp_buf xJumpBuf;
\r
108 /*-----------------------------------------------------------*/
\r
109 portBASE_TYPE xPortStartScheduler( void )
\r
111 /* This is called with interrupts already disabled. */
\r
113 /* Remember what was on the interrupts we are going to use
\r
114 so we can put them back later if required. */
\r
115 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
117 /* Put our manual switch (yield) function on a known
\r
119 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
121 #if( configUSE_PREEMPTION == 1 )
\r
123 /* Put our tick switch function on the timer interrupt. */
\r
124 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
128 /* We want the timer interrupt to just increment the tick count. */
\r
129 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
133 prvSetTickFrequency( configTICK_RATE_HZ );
\r
135 /* Clean up function if we want to return to DOS. */
\r
136 if( setjmp( xJumpBuf ) != 0 )
\r
139 xSchedulerRunning = pdFALSE;
\r
143 xSchedulerRunning = pdTRUE;
\r
145 /* Kick off the scheduler by setting up the context of the first task. */
\r
146 portFIRST_CONTEXT();
\r
149 return xSchedulerRunning;
\r
151 /*-----------------------------------------------------------*/
\r
153 /* The ISR used depends on whether the preemptive or cooperative scheduler
\r
155 #if( configUSE_PREEMPTION == 1 )
\r
156 static void __interrupt __far prvPreemptiveTick( void )
\r
158 /* Get the scheduler to update the task states following the tick. */
\r
159 vTaskIncrementTick();
\r
161 /* Switch in the context of the next task to be run. */
\r
162 portSWITCH_CONTEXT();
\r
164 /* Reset the PIC ready for the next time. */
\r
168 static void __interrupt __far prvNonPreemptiveTick( void )
\r
170 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
171 so we don't have to switch in the context of the next task. */
\r
172 vTaskIncrementTick();
\r
176 /*-----------------------------------------------------------*/
\r
178 static void __interrupt __far prvYieldProcessor( void )
\r
180 /* Switch in the context of the next task to be run. */
\r
181 portSWITCH_CONTEXT();
\r
183 /*-----------------------------------------------------------*/
\r
185 void vPortEndScheduler( void )
\r
187 /* Jump back to the processor state prior to starting the
\r
188 scheduler. This means we are not going to be using a
\r
189 task stack frame so the task can be deleted. */
\r
190 longjmp( xJumpBuf, 1 );
\r
192 /*-----------------------------------------------------------*/
\r
194 static void prvExitFunction( void )
\r
196 const unsigned portSHORT usTimerDisable = 0x0000;
\r
197 unsigned portSHORT usTimer0Control;
\r
199 /* Interrupts should be disabled here anyway - but no
\r
200 harm in making sure. */
\r
201 portDISABLE_INTERRUPTS();
\r
202 if( xSchedulerRunning == pdTRUE )
\r
204 /* Put back the switch interrupt routines that was in place
\r
205 before the scheduler started. */
\r
206 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
209 /* Disable the timer used for the tick to ensure the scheduler is
\r
210 not called before restoring interrupts. There was previously nothing
\r
211 on this timer so there is no old ISR to restore. */
\r
212 portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );
\r
214 /* Restart the DOS tick. */
\r
215 usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
\r
216 usTimer0Control |= portTIMER_INTERRUPT_ENABLE;
\r
217 portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
\r
220 portENABLE_INTERRUPTS();
\r
222 /* This will free up all the memory used by the scheduler.
\r
223 exiting back to dos with INT21 AH=4CH will do this anyway so
\r
224 it is not necessary to call this. */
\r
225 vTaskCleanUpResources();
\r
227 /*-----------------------------------------------------------*/
\r
229 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz )
\r
231 const unsigned portSHORT usMaxCountRegister = 0xff5a;
\r
232 const unsigned portSHORT usTimerPriorityRegister = 0xff32;
\r
233 const unsigned portSHORT usTimerEnable = 0xC000;
\r
234 const unsigned portSHORT usRetrigger = 0x0001;
\r
235 const unsigned portSHORT usTimerHighPriority = 0x0000;
\r
236 unsigned portSHORT usTimer0Control;
\r
238 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */
\r
240 const unsigned portLONG ulClockFrequency = ( unsigned portLONG ) 0x7f31a0UL;
\r
242 unsigned portLONG ulTimerCount = ulClockFrequency / ulTickRateHz;
\r
244 portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );
\r
245 portOUTPUT_WORD( usMaxCountRegister, ( unsigned portSHORT ) ulTimerCount );
\r
246 portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );
\r
248 /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */
\r
249 usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
\r
250 usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;
\r
251 portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
\r