2 FreeRTOS V7.4.2 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
78 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
79 macro to be consistent with the later ports.
\r
83 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
84 its proper value when the scheduler exits.
\r
91 #include "FreeRTOS.h"
\r
93 #include "portasm.h"
\r
95 /*-----------------------------------------------------------
\r
96 * Implementation of functions defined in portable.h for the industrial
\r
98 *----------------------------------------------------------*/
\r
100 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
102 #define portTIMER_INT_NUMBER 0x08
\r
104 /* Setup hardware for required tick interrupt rate. */
\r
105 static void prvSetTickFrequency( unsigned long ulTickRateHz );
\r
107 /* Restore hardware to as it was prior to starting the scheduler. */
\r
108 static void prvExitFunction( void );
\r
110 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
111 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
113 static void prvPortResetPIC( void );
\r
115 /* The ISR used depends on whether the preemptive or cooperative
\r
116 scheduler is being used. */
\r
117 #if( configUSE_PREEMPTION == 1 )
\r
118 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
120 static void __interrupt __far prvPreemptiveTick( void );
\r
122 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
124 static void __interrupt __far prvNonPreemptiveTick( void );
\r
127 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
128 static void __interrupt __far prvYieldProcessor( void );
\r
130 /* Set the tick frequency back so the floppy drive works correctly when the
\r
131 scheduler exits. */
\r
132 static void prvSetTickFrequencyDefault( void );
\r
134 /*lint -e956 File scopes necessary here. */
\r
136 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
137 static short sDOSTickCounter;
\r
139 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
140 static portBASE_TYPE xSchedulerRunning = pdFALSE;
\r
142 /* 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
143 static void ( __interrupt __far *pxOldSwitchISR )();
\r
145 /* 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
146 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
148 /* Used to restore the original DOS context when the scheduler is ended. */
\r
149 static jmp_buf xJumpBuf;
\r
153 /*-----------------------------------------------------------*/
\r
154 portBASE_TYPE xPortStartScheduler( void )
\r
156 pxISR pxOriginalTickISR;
\r
158 /* This is called with interrupts already disabled. */
\r
160 /* Remember what was on the interrupts we are going to use
\r
161 so we can put them back later if required. */
\r
162 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
163 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
164 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
166 prvSetTickFrequency( configTICK_RATE_HZ );
\r
168 /* Put our manual switch (yield) function on a known
\r
170 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
172 /* Put the old tick on a different interrupt number so we can
\r
173 call it when we want. */
\r
174 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
176 /* The ISR used depends on whether the preemptive or cooperative
\r
177 scheduler is being used. */
\r
178 #if( configUSE_PREEMPTION == 1 )
\r
180 /* Put our tick switch function on the timer interrupt. */
\r
181 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
185 /* We want the timer interrupt to just increment the tick count. */
\r
186 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
190 /* Setup a counter that is used to call the DOS interrupt as close
\r
191 to it's original frequency as can be achieved given our chosen tick
\r
193 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
195 /* Clean up function if we want to return to DOS. */
\r
196 if( setjmp( xJumpBuf ) != 0 )
\r
199 xSchedulerRunning = pdFALSE;
\r
203 xSchedulerRunning = pdTRUE;
\r
205 /* Kick off the scheduler by setting up the context of the first task. */
\r
206 portFIRST_CONTEXT();
\r
209 return xSchedulerRunning;
\r
211 /*-----------------------------------------------------------*/
\r
213 /* The ISR used depends on whether the preemptive or cooperative
\r
214 scheduler is being used. */
\r
215 #if( configUSE_PREEMPTION == 1 )
\r
216 static void __interrupt __far prvPreemptiveTick( void )
\r
218 /* Get the scheduler to update the task states following the tick. */
\r
219 if( xTaskIncrementTick() != pdFALSE )
\r
221 /* Switch in the context of the next task to be run. */
\r
222 portSWITCH_CONTEXT();
\r
225 /* Reset the PIC ready for the next time. */
\r
229 static void __interrupt __far prvNonPreemptiveTick( void )
\r
231 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
232 so we don't have to switch in the context of the next task. */
\r
233 xTaskIncrementTick();
\r
237 /*-----------------------------------------------------------*/
\r
239 static void __interrupt __far prvYieldProcessor( void )
\r
241 /* Switch in the context of the next task to be run. */
\r
242 portSWITCH_CONTEXT();
\r
244 /*-----------------------------------------------------------*/
\r
246 static void prvPortResetPIC( void )
\r
248 /* We are going to call the DOS tick interrupt at as close a
\r
249 frequency to the normal DOS tick as possible. */
\r
251 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
253 if( sDOSTickCounter <= 0 )
\r
255 sDOSTickCounter = ( short ) portTICKS_PER_DOS_TICK;
\r
256 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
260 /* Reset the PIC as the DOS tick is not being called to
\r
269 /*-----------------------------------------------------------*/
\r
271 void vPortEndScheduler( void )
\r
273 /* Jump back to the processor state prior to starting the
\r
274 scheduler. This means we are not going to be using a
\r
275 task stack frame so the task can be deleted. */
\r
276 longjmp( xJumpBuf, 1 );
\r
278 /*-----------------------------------------------------------*/
\r
280 static void prvExitFunction( void )
\r
282 void ( __interrupt __far *pxOriginalTickISR )();
\r
284 /* Interrupts should be disabled here anyway - but no
\r
285 harm in making sure. */
\r
286 portDISABLE_INTERRUPTS();
\r
287 if( xSchedulerRunning == pdTRUE )
\r
289 /* Set the DOS tick back onto the timer ticker. */
\r
290 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
291 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
292 prvSetTickFrequencyDefault();
\r
294 /* Put back the switch interrupt routines that was in place
\r
295 before the scheduler started. */
\r
296 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
297 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
299 /* The tick timer is back how DOS wants it. We can re-enable
\r
300 interrupts without the scheduler being called. */
\r
301 portENABLE_INTERRUPTS();
\r
303 /*-----------------------------------------------------------*/
\r
305 static void prvSetTickFrequency( unsigned long ulTickRateHz )
\r
307 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
\r
308 const unsigned short usPIT0 = ( unsigned short ) 0x40;
\r
309 const unsigned long ulPIT_CONST = ( unsigned long ) 1193180UL;
\r
310 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
\r
311 unsigned long ulOutput;
\r
313 /* Setup the 8245 to tick at the wanted frequency. */
\r
314 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
315 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
316 portOUTPUT_BYTE( usPIT0, ( unsigned short )( ulOutput & ( unsigned long ) 0xff ) );
\r
318 portOUTPUT_BYTE( usPIT0, ( unsigned short ) ( ulOutput & ( unsigned long ) 0xff ) );
\r
320 /*-----------------------------------------------------------*/
\r
322 static void prvSetTickFrequencyDefault( void )
\r
324 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
\r
325 const unsigned short usPIT0 = ( unsigned short ) 0x40;
\r
326 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
\r
328 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
329 portOUTPUT_BYTE( usPIT0,0 );
\r
330 portOUTPUT_BYTE( usPIT0,0 );
\r