2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
72 + Call to taskYIELD() from within tick ISR has been replaced by the more
\r
73 efficient portSWITCH_CONTEXT().
\r
74 + ISR function definitions renamed to include the prv prefix.
\r
76 Changes from V1.2.0:
\r
78 + prvPortResetPIC() is now called last thing before the end of the
\r
79 preemptive tick routine.
\r
83 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
84 macro to be consistent with the later ports.
\r
88 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
89 its proper value when the scheduler exits.
\r
98 #include "FreeRTOS.h"
\r
100 #include "portasm.h"
\r
102 /*-----------------------------------------------------------
\r
103 * Implementation of functions defined in portable.h for the industrial
\r
105 *----------------------------------------------------------*/
\r
107 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
109 #define portTIMER_INT_NUMBER 0x08
\r
111 /* Setup hardware for required tick interrupt rate. */
\r
112 static void prvSetTickFrequency( unsigned long ulTickRateHz );
\r
114 /* Restore hardware to as it was prior to starting the scheduler. */
\r
115 static void prvExitFunction( void );
\r
117 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
118 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
120 static void prvPortResetPIC( void );
\r
122 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
124 #if configUSE_PREEMPTION == 1
\r
125 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
127 static void __interrupt __far prvPreemptiveTick( void );
\r
129 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
131 static void __interrupt __far prvNonPreemptiveTick( void );
\r
133 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
134 static void __interrupt __far prvYieldProcessor( void );
\r
136 /* Set the tick frequency back so the floppy drive works correctly when the
\r
137 scheduler exits. */
\r
138 static void prvSetTickFrequencyDefault( void );
\r
140 /*lint -e956 File scopes necessary here. */
\r
142 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
143 static short sDOSTickCounter;
\r
145 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
146 static short sSchedulerRunning = pdFALSE;
\r
148 /* 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
149 static void ( __interrupt __far *pxOldSwitchISR )();
\r
151 /* 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
152 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
154 /* Used to restore the original DOS context when the scheduler is ended. */
\r
155 static jmp_buf xJumpBuf;
\r
159 /*-----------------------------------------------------------*/
\r
160 portBASE_TYPE xPortStartScheduler( void )
\r
162 pxISR pxOriginalTickISR;
\r
164 /* This is called with interrupts already disabled. */
\r
166 /* Remember what was on the interrupts we are going to use
\r
167 so we can put them back later if required. */
\r
168 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
169 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
170 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
172 prvSetTickFrequency( configTICK_RATE_HZ );
\r
174 /* Put our manual switch (yield) function on a known
\r
176 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
178 /* Put the old tick on a different interrupt number so we can
\r
179 call it when we want. */
\r
180 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
182 #if configUSE_PREEMPTION == 1
\r
184 /* Put our tick switch function on the timer interrupt. */
\r
185 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
189 /* We want the timer interrupt to just increment the tick count. */
\r
190 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
194 /* Setup a counter that is used to call the DOS interrupt as close
\r
195 to it's original frequency as can be achieved given our chosen tick
\r
197 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
199 /* Clean up function if we want to return to DOS. */
\r
200 if( setjmp( xJumpBuf ) != 0 )
\r
203 sSchedulerRunning = pdFALSE;
\r
207 sSchedulerRunning = pdTRUE;
\r
209 /* Kick off the scheduler by setting up the context of the first task. */
\r
210 portFIRST_CONTEXT();
\r
213 return sSchedulerRunning;
\r
215 /*-----------------------------------------------------------*/
\r
217 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
219 #if configUSE_PREEMPTION == 1
\r
220 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
222 static void __interrupt __far prvPreemptiveTick( void )
\r
224 /* Get the scheduler to update the task states following the tick. */
\r
225 vTaskIncrementTick();
\r
227 /* Switch in the context of the next task to be run. */
\r
228 portSWITCH_CONTEXT();
\r
230 /* Reset the PIC ready for the next time. */
\r
234 static void __interrupt __far prvNonPreemptiveTick( void )
\r
236 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
237 so we don't have to switch in the context of the next task. */
\r
238 vTaskIncrementTick();
\r
242 /*-----------------------------------------------------------*/
\r
245 static void __interrupt __far prvYieldProcessor( void )
\r
247 /* Switch in the context of the next task to be run. */
\r
248 portSWITCH_CONTEXT();
\r
250 /*-----------------------------------------------------------*/
\r
252 static void prvPortResetPIC( void )
\r
254 /* We are going to call the DOS tick interrupt at as close a
\r
255 frequency to the normal DOS tick as possible. */
\r
257 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
259 if( sDOSTickCounter <= 0 )
\r
261 sDOSTickCounter = ( short ) portTICKS_PER_DOS_TICK;
\r
262 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
266 /* Reset the PIC as the DOS tick is not being called to
\r
275 /*-----------------------------------------------------------*/
\r
277 void vPortEndScheduler( void )
\r
279 /* Jump back to the processor state prior to starting the
\r
280 scheduler. This means we are not going to be using a
\r
281 task stack frame so the task can be deleted. */
\r
282 longjmp( xJumpBuf, 1 );
\r
284 /*-----------------------------------------------------------*/
\r
286 static void prvExitFunction( void )
\r
288 void ( __interrupt __far *pxOriginalTickISR )();
\r
290 /* Interrupts should be disabled here anyway - but no
\r
291 harm in making sure. */
\r
292 portDISABLE_INTERRUPTS();
\r
293 if( sSchedulerRunning == pdTRUE )
\r
295 /* Set the DOS tick back onto the timer ticker. */
\r
296 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
297 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
298 prvSetTickFrequencyDefault();
\r
300 /* Put back the switch interrupt routines that was in place
\r
301 before the scheduler started. */
\r
302 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
303 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
305 /* The tick timer is back how DOS wants it. We can re-enable
\r
306 interrupts without the scheduler being called. */
\r
307 portENABLE_INTERRUPTS();
\r
309 /*-----------------------------------------------------------*/
\r
311 static void prvSetTickFrequency( unsigned long ulTickRateHz )
\r
313 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
\r
314 const unsigned short usPIT0 = ( unsigned short ) 0x40;
\r
315 const unsigned long ulPIT_CONST = ( unsigned long ) 1193180;
\r
316 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
\r
317 unsigned long ulOutput;
\r
319 /* Setup the 8245 to tick at the wanted frequency. */
\r
320 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
321 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
323 portOUTPUT_BYTE( usPIT0, ( unsigned short )( ulOutput & ( unsigned long ) 0xff ) );
\r
325 portOUTPUT_BYTE( usPIT0, ( unsigned short ) ( ulOutput & ( unsigned long ) 0xff ) );
\r
327 /*-----------------------------------------------------------*/
\r
329 static void prvSetTickFrequencyDefault( void )
\r
331 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
\r
332 const unsigned short usPIT0 = ( unsigned short ) 0x40;
\r
333 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
\r
335 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
336 portOUTPUT_BYTE( usPIT0,0 );
\r
337 portOUTPUT_BYTE( usPIT0,0 );
\r