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! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
53 + Call to taskYIELD() from within tick ISR has been replaced by the more
\r
54 efficient portSWITCH_CONTEXT().
\r
55 + ISR function definitions renamed to include the prv prefix.
\r
57 Changes from V1.2.0:
\r
59 + prvPortResetPIC() is now called last thing before the end of the
\r
60 preemptive tick routine.
\r
64 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
65 macro to be consistent with the later ports.
\r
69 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
70 its proper value when the scheduler exits.
\r
79 #include "FreeRTOS.h"
\r
81 #include "portasm.h"
\r
83 /*-----------------------------------------------------------
\r
84 * Implementation of functions defined in portable.h for the industrial
\r
86 *----------------------------------------------------------*/
\r
88 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
90 #define portTIMER_INT_NUMBER 0x08
\r
92 /* Setup hardware for required tick interrupt rate. */
\r
93 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz );
\r
95 /* Restore hardware to as it was prior to starting the scheduler. */
\r
96 static void prvExitFunction( void );
\r
98 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
99 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
101 static void prvPortResetPIC( void );
\r
103 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
105 #if configUSE_PREEMPTION == 1
\r
106 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
108 static void __interrupt __far prvPreemptiveTick( void );
\r
110 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
112 static void __interrupt __far prvNonPreemptiveTick( void );
\r
114 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
115 static void __interrupt __far prvYieldProcessor( void );
\r
117 /* Set the tick frequency back so the floppy drive works correctly when the
\r
118 scheduler exits. */
\r
119 static void prvSetTickFrequencyDefault( void );
\r
121 /*lint -e956 File scopes necessary here. */
\r
123 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
124 static portSHORT sDOSTickCounter;
\r
126 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
127 static portSHORT sSchedulerRunning = pdFALSE;
\r
129 /* Points to the original routine installed on the vector we use for manual context switches. This is then used to restore the original routine during prvExitFunction(). */
\r
130 static void ( __interrupt __far *pxOldSwitchISR )();
\r
132 /* Points to the original routine installed on the vector we use to chain to the DOS tick. This is then used to restore the original routine during prvExitFunction(). */
\r
133 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
135 /* Used to restore the original DOS context when the scheduler is ended. */
\r
136 static jmp_buf xJumpBuf;
\r
140 /*-----------------------------------------------------------*/
\r
141 portBASE_TYPE xPortStartScheduler( void )
\r
143 pxISR pxOriginalTickISR;
\r
145 /* This is called with interrupts already disabled. */
\r
147 /* Remember what was on the interrupts we are going to use
\r
148 so we can put them back later if required. */
\r
149 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
150 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
151 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
153 prvSetTickFrequency( configTICK_RATE_HZ );
\r
155 /* Put our manual switch (yield) function on a known
\r
157 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
159 /* Put the old tick on a different interrupt number so we can
\r
160 call it when we want. */
\r
161 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
163 #if configUSE_PREEMPTION == 1
\r
165 /* Put our tick switch function on the timer interrupt. */
\r
166 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
170 /* We want the timer interrupt to just increment the tick count. */
\r
171 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
175 /* Setup a counter that is used to call the DOS interrupt as close
\r
176 to it's original frequency as can be achieved given our chosen tick
\r
178 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
180 /* Clean up function if we want to return to DOS. */
\r
181 if( setjmp( xJumpBuf ) != 0 )
\r
184 sSchedulerRunning = pdFALSE;
\r
188 sSchedulerRunning = pdTRUE;
\r
190 /* Kick off the scheduler by setting up the context of the first task. */
\r
191 portFIRST_CONTEXT();
\r
194 return sSchedulerRunning;
\r
196 /*-----------------------------------------------------------*/
\r
198 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
200 #if configUSE_PREEMPTION == 1
\r
201 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
203 static void __interrupt __far prvPreemptiveTick( void )
\r
205 /* Get the scheduler to update the task states following the tick. */
\r
206 vTaskIncrementTick();
\r
208 /* Switch in the context of the next task to be run. */
\r
209 portSWITCH_CONTEXT();
\r
211 /* Reset the PIC ready for the next time. */
\r
215 static void __interrupt __far prvNonPreemptiveTick( void )
\r
217 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
218 so we don't have to switch in the context of the next task. */
\r
219 vTaskIncrementTick();
\r
223 /*-----------------------------------------------------------*/
\r
226 static void __interrupt __far prvYieldProcessor( void )
\r
228 /* Switch in the context of the next task to be run. */
\r
229 portSWITCH_CONTEXT();
\r
231 /*-----------------------------------------------------------*/
\r
233 static void prvPortResetPIC( void )
\r
235 /* We are going to call the DOS tick interrupt at as close a
\r
236 frequency to the normal DOS tick as possible. */
\r
238 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
240 if( sDOSTickCounter <= 0 )
\r
242 sDOSTickCounter = ( portSHORT ) portTICKS_PER_DOS_TICK;
\r
243 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
247 /* Reset the PIC as the DOS tick is not being called to
\r
256 /*-----------------------------------------------------------*/
\r
258 void vPortEndScheduler( void )
\r
260 /* Jump back to the processor state prior to starting the
\r
261 scheduler. This means we are not going to be using a
\r
262 task stack frame so the task can be deleted. */
\r
263 longjmp( xJumpBuf, 1 );
\r
265 /*-----------------------------------------------------------*/
\r
267 static void prvExitFunction( void )
\r
269 void ( __interrupt __far *pxOriginalTickISR )();
\r
271 /* Interrupts should be disabled here anyway - but no
\r
272 harm in making sure. */
\r
273 portDISABLE_INTERRUPTS();
\r
274 if( sSchedulerRunning == pdTRUE )
\r
276 /* Set the DOS tick back onto the timer ticker. */
\r
277 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
278 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
279 prvSetTickFrequencyDefault();
\r
281 /* Put back the switch interrupt routines that was in place
\r
282 before the scheduler started. */
\r
283 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
284 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
286 /* The tick timer is back how DOS wants it. We can re-enable
\r
287 interrupts without the scheduler being called. */
\r
288 portENABLE_INTERRUPTS();
\r
290 /* This will free up all the memory used by the scheduler.
\r
291 exiting back to dos with INT21 AH=4CH will do this anyway so
\r
292 it is not necessary to call this. */
\r
293 vTaskCleanUpResources();
\r
295 /*-----------------------------------------------------------*/
\r
297 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz )
\r
299 const unsigned portSHORT usPIT_MODE = ( unsigned portSHORT ) 0x43;
\r
300 const unsigned portSHORT usPIT0 = ( unsigned portSHORT ) 0x40;
\r
301 const unsigned portLONG ulPIT_CONST = ( unsigned portLONG ) 1193180;
\r
302 const unsigned portSHORT us8254_CTR0_MODE3 = ( unsigned portSHORT ) 0x36;
\r
303 unsigned portLONG ulOutput;
\r
305 /* Setup the 8245 to tick at the wanted frequency. */
\r
306 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
307 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
309 portOUTPUT_BYTE( usPIT0, ( unsigned portSHORT )( ulOutput & ( unsigned portLONG ) 0xff ) );
\r
311 portOUTPUT_BYTE( usPIT0, ( unsigned portSHORT ) ( ulOutput & ( unsigned portLONG ) 0xff ) );
\r
313 /*-----------------------------------------------------------*/
\r
315 static void prvSetTickFrequencyDefault( void )
\r
317 const unsigned portSHORT usPIT_MODE = ( unsigned portSHORT ) 0x43;
\r
318 const unsigned portSHORT usPIT0 = ( unsigned portSHORT ) 0x40;
\r
319 const unsigned portSHORT us8254_CTR0_MODE3 = ( unsigned portSHORT ) 0x36;
\r
321 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
322 portOUTPUT_BYTE( usPIT0,0 );
\r
323 portOUTPUT_BYTE( usPIT0,0 );
\r