2 FreeRTOS.org V4.2.1 - 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 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
40 macro to be consistent with the later ports.
\r
44 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
45 its proper value when the scheduler exits.
\r
52 #include "FreeRTOS.h"
\r
54 #include "portasm.h"
\r
56 /*-----------------------------------------------------------
\r
57 * Implementation of functions defined in portable.h for the industrial
\r
59 *----------------------------------------------------------*/
\r
61 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
63 #define portTIMER_INT_NUMBER 0x08
\r
65 /* Setup hardware for required tick interrupt rate. */
\r
66 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz );
\r
68 /* Restore hardware to as it was prior to starting the scheduler. */
\r
69 static void prvExitFunction( void );
\r
71 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
72 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
74 static void prvPortResetPIC( void );
\r
76 /* The ISR used depends on whether the preemptive or cooperative
\r
77 scheduler is being used. */
\r
78 #if( configUSE_PREEMPTION == 1 )
\r
79 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
81 static void __interrupt __far prvPreemptiveTick( void );
\r
83 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
85 static void __interrupt __far prvNonPreemptiveTick( void );
\r
88 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
89 static void __interrupt __far prvYieldProcessor( void );
\r
91 /* Set the tick frequency back so the floppy drive works correctly when the
\r
93 static void prvSetTickFrequencyDefault( void );
\r
95 /*lint -e956 File scopes necessary here. */
\r
97 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
98 static portSHORT sDOSTickCounter;
\r
100 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
101 static portBASE_TYPE xSchedulerRunning = pdFALSE;
\r
103 /* 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
104 static void ( __interrupt __far *pxOldSwitchISR )();
\r
106 /* 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
107 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
109 /* Used to restore the original DOS context when the scheduler is ended. */
\r
110 static jmp_buf xJumpBuf;
\r
114 /*-----------------------------------------------------------*/
\r
115 portBASE_TYPE xPortStartScheduler( void )
\r
117 pxISR pxOriginalTickISR;
\r
119 /* This is called with interrupts already disabled. */
\r
121 /* Remember what was on the interrupts we are going to use
\r
122 so we can put them back later if required. */
\r
123 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
124 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
125 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
127 prvSetTickFrequency( configTICK_RATE_HZ );
\r
129 /* Put our manual switch (yield) function on a known
\r
131 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
133 /* Put the old tick on a different interrupt number so we can
\r
134 call it when we want. */
\r
135 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
137 /* The ISR used depends on whether the preemptive or cooperative
\r
138 scheduler is being used. */
\r
139 #if( configUSE_PREEMPTION == 1 )
\r
141 /* Put our tick switch function on the timer interrupt. */
\r
142 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
146 /* We want the timer interrupt to just increment the tick count. */
\r
147 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
151 /* Setup a counter that is used to call the DOS interrupt as close
\r
152 to it's original frequency as can be achieved given our chosen tick
\r
154 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
156 /* Clean up function if we want to return to DOS. */
\r
157 if( setjmp( xJumpBuf ) != 0 )
\r
160 xSchedulerRunning = pdFALSE;
\r
164 xSchedulerRunning = pdTRUE;
\r
166 /* Kick off the scheduler by setting up the context of the first task. */
\r
167 portFIRST_CONTEXT();
\r
170 return xSchedulerRunning;
\r
172 /*-----------------------------------------------------------*/
\r
174 /* The ISR used depends on whether the preemptive or cooperative
\r
175 scheduler is being used. */
\r
176 #if( configUSE_PREEMPTION == 1 )
\r
177 static void __interrupt __far prvPreemptiveTick( void )
\r
179 /* Get the scheduler to update the task states following the tick. */
\r
180 vTaskIncrementTick();
\r
182 /* Switch in the context of the next task to be run. */
\r
183 portSWITCH_CONTEXT();
\r
185 /* Reset the PIC ready for the next time. */
\r
189 static void __interrupt __far prvNonPreemptiveTick( void )
\r
191 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
192 so we don't have to switch in the context of the next task. */
\r
193 vTaskIncrementTick();
\r
197 /*-----------------------------------------------------------*/
\r
199 static void __interrupt __far prvYieldProcessor( void )
\r
201 /* Switch in the context of the next task to be run. */
\r
202 portSWITCH_CONTEXT();
\r
204 /*-----------------------------------------------------------*/
\r
206 static void prvPortResetPIC( void )
\r
208 /* We are going to call the DOS tick interrupt at as close a
\r
209 frequency to the normal DOS tick as possible. */
\r
211 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
213 if( sDOSTickCounter <= 0 )
\r
215 sDOSTickCounter = ( portSHORT ) portTICKS_PER_DOS_TICK;
\r
216 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
220 /* Reset the PIC as the DOS tick is not being called to
\r
229 /*-----------------------------------------------------------*/
\r
231 void vPortEndScheduler( void )
\r
233 /* Jump back to the processor state prior to starting the
\r
234 scheduler. This means we are not going to be using a
\r
235 task stack frame so the task can be deleted. */
\r
236 longjmp( xJumpBuf, 1 );
\r
238 /*-----------------------------------------------------------*/
\r
240 static void prvExitFunction( void )
\r
242 void ( __interrupt __far *pxOriginalTickISR )();
\r
244 /* Interrupts should be disabled here anyway - but no
\r
245 harm in making sure. */
\r
246 portDISABLE_INTERRUPTS();
\r
247 if( xSchedulerRunning == pdTRUE )
\r
249 /* Set the DOS tick back onto the timer ticker. */
\r
250 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
251 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
252 prvSetTickFrequencyDefault();
\r
254 /* Put back the switch interrupt routines that was in place
\r
255 before the scheduler started. */
\r
256 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
257 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
259 /* The tick timer is back how DOS wants it. We can re-enable
\r
260 interrupts without the scheduler being called. */
\r
261 portENABLE_INTERRUPTS();
\r
263 /* This will free up all the memory used by the scheduler.
\r
264 exiting back to dos with INT21 AH=4CH will do this anyway so
\r
265 it is not necessary to call this. */
\r
266 vTaskCleanUpResources();
\r
268 /*-----------------------------------------------------------*/
\r
270 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz )
\r
272 const unsigned portSHORT usPIT_MODE = ( unsigned portSHORT ) 0x43;
\r
273 const unsigned portSHORT usPIT0 = ( unsigned portSHORT ) 0x40;
\r
274 const unsigned portLONG ulPIT_CONST = ( unsigned portLONG ) 1193180UL;
\r
275 const unsigned portSHORT us8254_CTR0_MODE3 = ( unsigned portSHORT ) 0x36;
\r
276 unsigned portLONG ulOutput;
\r
278 /* Setup the 8245 to tick at the wanted frequency. */
\r
279 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
280 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
281 portOUTPUT_BYTE( usPIT0, ( unsigned portSHORT )( ulOutput & ( unsigned portLONG ) 0xff ) );
\r
283 portOUTPUT_BYTE( usPIT0, ( unsigned portSHORT ) ( ulOutput & ( unsigned portLONG ) 0xff ) );
\r
285 /*-----------------------------------------------------------*/
\r
287 static void prvSetTickFrequencyDefault( void )
\r
289 const unsigned portSHORT usPIT_MODE = ( unsigned portSHORT ) 0x43;
\r
290 const unsigned portSHORT usPIT0 = ( unsigned portSHORT ) 0x40;
\r
291 const unsigned portSHORT us8254_CTR0_MODE3 = ( unsigned portSHORT ) 0x36;
\r
293 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
294 portOUTPUT_BYTE( usPIT0,0 );
\r
295 portOUTPUT_BYTE( usPIT0,0 );
\r