2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
68 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
69 macro to be consistent with the later ports.
\r
73 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
74 its proper value when the scheduler exits.
\r
81 #include "FreeRTOS.h"
\r
83 #include "portasm.h"
\r
85 /*-----------------------------------------------------------
\r
86 * Implementation of functions defined in portable.h for the industrial
\r
88 *----------------------------------------------------------*/
\r
90 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
92 #define portTIMER_INT_NUMBER 0x08
\r
94 /* Setup hardware for required tick interrupt rate. */
\r
95 static void prvSetTickFrequency( unsigned long ulTickRateHz );
\r
97 /* Restore hardware to as it was prior to starting the scheduler. */
\r
98 static void prvExitFunction( void );
\r
100 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
101 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
103 static void prvPortResetPIC( void );
\r
105 /* The ISR used depends on whether the preemptive or cooperative
\r
106 scheduler is being used. */
\r
107 #if( configUSE_PREEMPTION == 1 )
\r
108 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
110 static void __interrupt __far prvPreemptiveTick( void );
\r
112 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
114 static void __interrupt __far prvNonPreemptiveTick( void );
\r
117 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
118 static void __interrupt __far prvYieldProcessor( void );
\r
120 /* Set the tick frequency back so the floppy drive works correctly when the
\r
121 scheduler exits. */
\r
122 static void prvSetTickFrequencyDefault( void );
\r
124 /*lint -e956 File scopes necessary here. */
\r
126 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
127 static short sDOSTickCounter;
\r
129 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
130 static portBASE_TYPE xSchedulerRunning = pdFALSE;
\r
132 /* 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
133 static void ( __interrupt __far *pxOldSwitchISR )();
\r
135 /* 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
136 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
138 /* Used to restore the original DOS context when the scheduler is ended. */
\r
139 static jmp_buf xJumpBuf;
\r
143 /*-----------------------------------------------------------*/
\r
144 portBASE_TYPE xPortStartScheduler( void )
\r
146 pxISR pxOriginalTickISR;
\r
148 /* This is called with interrupts already disabled. */
\r
150 /* Remember what was on the interrupts we are going to use
\r
151 so we can put them back later if required. */
\r
152 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
153 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
154 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
156 prvSetTickFrequency( configTICK_RATE_HZ );
\r
158 /* Put our manual switch (yield) function on a known
\r
160 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
162 /* Put the old tick on a different interrupt number so we can
\r
163 call it when we want. */
\r
164 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
166 /* The ISR used depends on whether the preemptive or cooperative
\r
167 scheduler is being used. */
\r
168 #if( configUSE_PREEMPTION == 1 )
\r
170 /* Put our tick switch function on the timer interrupt. */
\r
171 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
175 /* We want the timer interrupt to just increment the tick count. */
\r
176 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
180 /* Setup a counter that is used to call the DOS interrupt as close
\r
181 to it's original frequency as can be achieved given our chosen tick
\r
183 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
185 /* Clean up function if we want to return to DOS. */
\r
186 if( setjmp( xJumpBuf ) != 0 )
\r
189 xSchedulerRunning = pdFALSE;
\r
193 xSchedulerRunning = pdTRUE;
\r
195 /* Kick off the scheduler by setting up the context of the first task. */
\r
196 portFIRST_CONTEXT();
\r
199 return xSchedulerRunning;
\r
201 /*-----------------------------------------------------------*/
\r
203 /* The ISR used depends on whether the preemptive or cooperative
\r
204 scheduler is being used. */
\r
205 #if( configUSE_PREEMPTION == 1 )
\r
206 static void __interrupt __far prvPreemptiveTick( void )
\r
208 /* Get the scheduler to update the task states following the tick. */
\r
209 if( xTaskIncrementTick() != pdFALSE )
\r
211 /* Switch in the context of the next task to be run. */
\r
212 portSWITCH_CONTEXT();
\r
215 /* Reset the PIC ready for the next time. */
\r
219 static void __interrupt __far prvNonPreemptiveTick( void )
\r
221 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
222 so we don't have to switch in the context of the next task. */
\r
223 xTaskIncrementTick();
\r
227 /*-----------------------------------------------------------*/
\r
229 static void __interrupt __far prvYieldProcessor( void )
\r
231 /* Switch in the context of the next task to be run. */
\r
232 portSWITCH_CONTEXT();
\r
234 /*-----------------------------------------------------------*/
\r
236 static void prvPortResetPIC( void )
\r
238 /* We are going to call the DOS tick interrupt at as close a
\r
239 frequency to the normal DOS tick as possible. */
\r
241 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
243 if( sDOSTickCounter <= 0 )
\r
245 sDOSTickCounter = ( short ) portTICKS_PER_DOS_TICK;
\r
246 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
250 /* Reset the PIC as the DOS tick is not being called to
\r
259 /*-----------------------------------------------------------*/
\r
261 void vPortEndScheduler( void )
\r
263 /* Jump back to the processor state prior to starting the
\r
264 scheduler. This means we are not going to be using a
\r
265 task stack frame so the task can be deleted. */
\r
266 longjmp( xJumpBuf, 1 );
\r
268 /*-----------------------------------------------------------*/
\r
270 static void prvExitFunction( void )
\r
272 void ( __interrupt __far *pxOriginalTickISR )();
\r
274 /* Interrupts should be disabled here anyway - but no
\r
275 harm in making sure. */
\r
276 portDISABLE_INTERRUPTS();
\r
277 if( xSchedulerRunning == pdTRUE )
\r
279 /* Set the DOS tick back onto the timer ticker. */
\r
280 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
281 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
282 prvSetTickFrequencyDefault();
\r
284 /* Put back the switch interrupt routines that was in place
\r
285 before the scheduler started. */
\r
286 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
287 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
289 /* The tick timer is back how DOS wants it. We can re-enable
\r
290 interrupts without the scheduler being called. */
\r
291 portENABLE_INTERRUPTS();
\r
293 /*-----------------------------------------------------------*/
\r
295 static void prvSetTickFrequency( unsigned long ulTickRateHz )
\r
297 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
\r
298 const unsigned short usPIT0 = ( unsigned short ) 0x40;
\r
299 const unsigned long ulPIT_CONST = ( unsigned long ) 1193180UL;
\r
300 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
\r
301 unsigned long ulOutput;
\r
303 /* Setup the 8245 to tick at the wanted frequency. */
\r
304 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
305 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
306 portOUTPUT_BYTE( usPIT0, ( unsigned short )( ulOutput & ( unsigned long ) 0xff ) );
\r
308 portOUTPUT_BYTE( usPIT0, ( unsigned short ) ( ulOutput & ( unsigned long ) 0xff ) );
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvSetTickFrequencyDefault( void )
\r
314 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
\r
315 const unsigned short usPIT0 = ( unsigned short ) 0x40;
\r
316 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
\r
318 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
319 portOUTPUT_BYTE( usPIT0,0 );
\r
320 portOUTPUT_BYTE( usPIT0,0 );
\r