2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
55 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
56 macro to be consistent with the later ports.
\r
60 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
61 its proper value when the scheduler exits.
\r
68 #include "FreeRTOS.h"
\r
70 #include "portasm.h"
\r
72 /*-----------------------------------------------------------
\r
73 * Implementation of functions defined in portable.h for the industrial
\r
75 *----------------------------------------------------------*/
\r
77 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
79 #define portTIMER_INT_NUMBER 0x08
\r
81 /* Setup hardware for required tick interrupt rate. */
\r
82 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz );
\r
84 /* Restore hardware to as it was prior to starting the scheduler. */
\r
85 static void prvExitFunction( void );
\r
87 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
88 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
90 static void prvPortResetPIC( void );
\r
92 /* The ISR used depends on whether the preemptive or cooperative
\r
93 scheduler is being used. */
\r
94 #if( configUSE_PREEMPTION == 1 )
\r
95 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
97 static void __interrupt __far prvPreemptiveTick( void );
\r
99 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
101 static void __interrupt __far prvNonPreemptiveTick( void );
\r
104 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
105 static void __interrupt __far prvYieldProcessor( void );
\r
107 /* Set the tick frequency back so the floppy drive works correctly when the
\r
108 scheduler exits. */
\r
109 static void prvSetTickFrequencyDefault( void );
\r
111 /*lint -e956 File scopes necessary here. */
\r
113 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
114 static portSHORT sDOSTickCounter;
\r
116 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
117 static portBASE_TYPE xSchedulerRunning = pdFALSE;
\r
119 /* 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
120 static void ( __interrupt __far *pxOldSwitchISR )();
\r
122 /* 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
123 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
125 /* Used to restore the original DOS context when the scheduler is ended. */
\r
126 static jmp_buf xJumpBuf;
\r
130 /*-----------------------------------------------------------*/
\r
131 portBASE_TYPE xPortStartScheduler( void )
\r
133 pxISR pxOriginalTickISR;
\r
135 /* This is called with interrupts already disabled. */
\r
137 /* Remember what was on the interrupts we are going to use
\r
138 so we can put them back later if required. */
\r
139 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
140 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
141 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
143 prvSetTickFrequency( configTICK_RATE_HZ );
\r
145 /* Put our manual switch (yield) function on a known
\r
147 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
149 /* Put the old tick on a different interrupt number so we can
\r
150 call it when we want. */
\r
151 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
153 /* The ISR used depends on whether the preemptive or cooperative
\r
154 scheduler is being used. */
\r
155 #if( configUSE_PREEMPTION == 1 )
\r
157 /* Put our tick switch function on the timer interrupt. */
\r
158 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
162 /* We want the timer interrupt to just increment the tick count. */
\r
163 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
167 /* Setup a counter that is used to call the DOS interrupt as close
\r
168 to it's original frequency as can be achieved given our chosen tick
\r
170 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
172 /* Clean up function if we want to return to DOS. */
\r
173 if( setjmp( xJumpBuf ) != 0 )
\r
176 xSchedulerRunning = pdFALSE;
\r
180 xSchedulerRunning = pdTRUE;
\r
182 /* Kick off the scheduler by setting up the context of the first task. */
\r
183 portFIRST_CONTEXT();
\r
186 return xSchedulerRunning;
\r
188 /*-----------------------------------------------------------*/
\r
190 /* The ISR used depends on whether the preemptive or cooperative
\r
191 scheduler is being used. */
\r
192 #if( configUSE_PREEMPTION == 1 )
\r
193 static void __interrupt __far prvPreemptiveTick( void )
\r
195 /* Get the scheduler to update the task states following the tick. */
\r
196 vTaskIncrementTick();
\r
198 /* Switch in the context of the next task to be run. */
\r
199 portSWITCH_CONTEXT();
\r
201 /* Reset the PIC ready for the next time. */
\r
205 static void __interrupt __far prvNonPreemptiveTick( void )
\r
207 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
208 so we don't have to switch in the context of the next task. */
\r
209 vTaskIncrementTick();
\r
213 /*-----------------------------------------------------------*/
\r
215 static void __interrupt __far prvYieldProcessor( void )
\r
217 /* Switch in the context of the next task to be run. */
\r
218 portSWITCH_CONTEXT();
\r
220 /*-----------------------------------------------------------*/
\r
222 static void prvPortResetPIC( void )
\r
224 /* We are going to call the DOS tick interrupt at as close a
\r
225 frequency to the normal DOS tick as possible. */
\r
227 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
229 if( sDOSTickCounter <= 0 )
\r
231 sDOSTickCounter = ( portSHORT ) portTICKS_PER_DOS_TICK;
\r
232 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
236 /* Reset the PIC as the DOS tick is not being called to
\r
245 /*-----------------------------------------------------------*/
\r
247 void vPortEndScheduler( void )
\r
249 /* Jump back to the processor state prior to starting the
\r
250 scheduler. This means we are not going to be using a
\r
251 task stack frame so the task can be deleted. */
\r
252 longjmp( xJumpBuf, 1 );
\r
254 /*-----------------------------------------------------------*/
\r
256 static void prvExitFunction( void )
\r
258 void ( __interrupt __far *pxOriginalTickISR )();
\r
260 /* Interrupts should be disabled here anyway - but no
\r
261 harm in making sure. */
\r
262 portDISABLE_INTERRUPTS();
\r
263 if( xSchedulerRunning == pdTRUE )
\r
265 /* Set the DOS tick back onto the timer ticker. */
\r
266 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
267 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
268 prvSetTickFrequencyDefault();
\r
270 /* Put back the switch interrupt routines that was in place
\r
271 before the scheduler started. */
\r
272 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
273 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
275 /* The tick timer is back how DOS wants it. We can re-enable
\r
276 interrupts without the scheduler being called. */
\r
277 portENABLE_INTERRUPTS();
\r
279 /* This will free up all the memory used by the scheduler.
\r
280 exiting back to dos with INT21 AH=4CH will do this anyway so
\r
281 it is not necessary to call this. */
\r
282 vTaskCleanUpResources();
\r
284 /*-----------------------------------------------------------*/
\r
286 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz )
\r
288 const unsigned portSHORT usPIT_MODE = ( unsigned portSHORT ) 0x43;
\r
289 const unsigned portSHORT usPIT0 = ( unsigned portSHORT ) 0x40;
\r
290 const unsigned portLONG ulPIT_CONST = ( unsigned portLONG ) 1193180UL;
\r
291 const unsigned portSHORT us8254_CTR0_MODE3 = ( unsigned portSHORT ) 0x36;
\r
292 unsigned portLONG ulOutput;
\r
294 /* Setup the 8245 to tick at the wanted frequency. */
\r
295 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
296 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
297 portOUTPUT_BYTE( usPIT0, ( unsigned portSHORT )( ulOutput & ( unsigned portLONG ) 0xff ) );
\r
299 portOUTPUT_BYTE( usPIT0, ( unsigned portSHORT ) ( ulOutput & ( unsigned portLONG ) 0xff ) );
\r
301 /*-----------------------------------------------------------*/
\r
303 static void prvSetTickFrequencyDefault( void )
\r
305 const unsigned portSHORT usPIT_MODE = ( unsigned portSHORT ) 0x43;
\r
306 const unsigned portSHORT usPIT0 = ( unsigned portSHORT ) 0x40;
\r
307 const unsigned portSHORT us8254_CTR0_MODE3 = ( unsigned portSHORT ) 0x36;
\r
309 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
310 portOUTPUT_BYTE( usPIT0,0 );
\r
311 portOUTPUT_BYTE( usPIT0,0 );
\r