1 |
572 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/*
|
55 |
|
|
Changes from V1.00:
|
56 |
|
|
|
57 |
|
|
+ Call to taskYIELD() from within tick ISR has been replaced by the more
|
58 |
|
|
efficient portSWITCH_CONTEXT().
|
59 |
|
|
+ ISR function definitions renamed to include the prv prefix.
|
60 |
|
|
|
61 |
|
|
Changes from V1.2.0:
|
62 |
|
|
|
63 |
|
|
+ prvPortResetPIC() is now called last thing before the end of the
|
64 |
|
|
preemptive tick routine.
|
65 |
|
|
|
66 |
|
|
Changes from V2.6.1
|
67 |
|
|
|
68 |
|
|
+ Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
|
69 |
|
|
macro to be consistent with the later ports.
|
70 |
|
|
|
71 |
|
|
Changes from V4.0.1
|
72 |
|
|
|
73 |
|
|
+ Add function prvSetTickFrequencyDefault() to set the DOS tick back to
|
74 |
|
|
its proper value when the scheduler exits.
|
75 |
|
|
*/
|
76 |
|
|
|
77 |
|
|
#include <stdlib.h>
|
78 |
|
|
#include <stdio.h>
|
79 |
|
|
#include <i86.h>
|
80 |
|
|
#include <dos.h>
|
81 |
|
|
#include <setjmp.h>
|
82 |
|
|
|
83 |
|
|
#include "FreeRTOS.h"
|
84 |
|
|
#include "task.h"
|
85 |
|
|
#include "portasm.h"
|
86 |
|
|
|
87 |
|
|
/*-----------------------------------------------------------
|
88 |
|
|
* Implementation of functions defined in portable.h for the industrial
|
89 |
|
|
* PC port.
|
90 |
|
|
*----------------------------------------------------------*/
|
91 |
|
|
|
92 |
|
|
/*lint -e950 Non ANSI reserved words okay in this file only. */
|
93 |
|
|
|
94 |
|
|
#define portTIMER_INT_NUMBER 0x08
|
95 |
|
|
|
96 |
|
|
/* Setup hardware for required tick interrupt rate. */
|
97 |
|
|
static void prvSetTickFrequency( unsigned long ulTickRateHz );
|
98 |
|
|
|
99 |
|
|
/* Restore hardware to as it was prior to starting the scheduler. */
|
100 |
|
|
static void prvExitFunction( void );
|
101 |
|
|
|
102 |
|
|
/* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
|
103 |
|
|
directly. We chain to the DOS tick as close as possible to the standard DOS
|
104 |
|
|
tick rate. */
|
105 |
|
|
static void prvPortResetPIC( void );
|
106 |
|
|
|
107 |
|
|
/* The tick ISR used depends on whether the preemptive or cooperative scheduler
|
108 |
|
|
is being used. */
|
109 |
|
|
#if configUSE_PREEMPTION == 1
|
110 |
|
|
/* Tick service routine used by the scheduler when preemptive scheduling is
|
111 |
|
|
being used. */
|
112 |
|
|
static void __interrupt __far prvPreemptiveTick( void );
|
113 |
|
|
#else
|
114 |
|
|
/* Tick service routine used by the scheduler when cooperative scheduling is
|
115 |
|
|
being used. */
|
116 |
|
|
static void __interrupt __far prvNonPreemptiveTick( void );
|
117 |
|
|
#endif
|
118 |
|
|
/* Trap routine used by taskYIELD() to manually cause a context switch. */
|
119 |
|
|
static void __interrupt __far prvYieldProcessor( void );
|
120 |
|
|
|
121 |
|
|
/* Set the tick frequency back so the floppy drive works correctly when the
|
122 |
|
|
scheduler exits. */
|
123 |
|
|
static void prvSetTickFrequencyDefault( void );
|
124 |
|
|
|
125 |
|
|
/*lint -e956 File scopes necessary here. */
|
126 |
|
|
|
127 |
|
|
/* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
|
128 |
|
|
static short sDOSTickCounter;
|
129 |
|
|
|
130 |
|
|
/* Set true when the vectors are set so the scheduler will service the tick. */
|
131 |
|
|
static short sSchedulerRunning = pdFALSE;
|
132 |
|
|
|
133 |
|
|
/* 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(). */
|
134 |
|
|
static void ( __interrupt __far *pxOldSwitchISR )();
|
135 |
|
|
|
136 |
|
|
/* 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(). */
|
137 |
|
|
static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
|
138 |
|
|
|
139 |
|
|
/* Used to restore the original DOS context when the scheduler is ended. */
|
140 |
|
|
static jmp_buf xJumpBuf;
|
141 |
|
|
|
142 |
|
|
/*lint +e956 */
|
143 |
|
|
|
144 |
|
|
/*-----------------------------------------------------------*/
|
145 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
146 |
|
|
{
|
147 |
|
|
pxISR pxOriginalTickISR;
|
148 |
|
|
|
149 |
|
|
/* This is called with interrupts already disabled. */
|
150 |
|
|
|
151 |
|
|
/* Remember what was on the interrupts we are going to use
|
152 |
|
|
so we can put them back later if required. */
|
153 |
|
|
pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
|
154 |
|
|
pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
|
155 |
|
|
pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
|
156 |
|
|
|
157 |
|
|
prvSetTickFrequency( configTICK_RATE_HZ );
|
158 |
|
|
|
159 |
|
|
/* Put our manual switch (yield) function on a known
|
160 |
|
|
vector. */
|
161 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
|
162 |
|
|
|
163 |
|
|
/* Put the old tick on a different interrupt number so we can
|
164 |
|
|
call it when we want. */
|
165 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
|
166 |
|
|
|
167 |
|
|
#if configUSE_PREEMPTION == 1
|
168 |
|
|
{
|
169 |
|
|
/* Put our tick switch function on the timer interrupt. */
|
170 |
|
|
_dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
|
171 |
|
|
}
|
172 |
|
|
#else
|
173 |
|
|
{
|
174 |
|
|
/* We want the timer interrupt to just increment the tick count. */
|
175 |
|
|
_dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
|
176 |
|
|
}
|
177 |
|
|
#endif
|
178 |
|
|
|
179 |
|
|
/* Setup a counter that is used to call the DOS interrupt as close
|
180 |
|
|
to it's original frequency as can be achieved given our chosen tick
|
181 |
|
|
frequency. */
|
182 |
|
|
sDOSTickCounter = portTICKS_PER_DOS_TICK;
|
183 |
|
|
|
184 |
|
|
/* Clean up function if we want to return to DOS. */
|
185 |
|
|
if( setjmp( xJumpBuf ) != 0 )
|
186 |
|
|
{
|
187 |
|
|
prvExitFunction();
|
188 |
|
|
sSchedulerRunning = pdFALSE;
|
189 |
|
|
}
|
190 |
|
|
else
|
191 |
|
|
{
|
192 |
|
|
sSchedulerRunning = pdTRUE;
|
193 |
|
|
|
194 |
|
|
/* Kick off the scheduler by setting up the context of the first task. */
|
195 |
|
|
portFIRST_CONTEXT();
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
return sSchedulerRunning;
|
199 |
|
|
}
|
200 |
|
|
/*-----------------------------------------------------------*/
|
201 |
|
|
|
202 |
|
|
/* The tick ISR used depends on whether the preemptive or cooperative scheduler
|
203 |
|
|
is being used. */
|
204 |
|
|
#if configUSE_PREEMPTION == 1
|
205 |
|
|
/* Tick service routine used by the scheduler when preemptive scheduling is
|
206 |
|
|
being used. */
|
207 |
|
|
static void __interrupt __far prvPreemptiveTick( void )
|
208 |
|
|
{
|
209 |
|
|
/* Get the scheduler to update the task states following the tick. */
|
210 |
|
|
vTaskIncrementTick();
|
211 |
|
|
|
212 |
|
|
/* Switch in the context of the next task to be run. */
|
213 |
|
|
portSWITCH_CONTEXT();
|
214 |
|
|
|
215 |
|
|
/* Reset the PIC ready for the next time. */
|
216 |
|
|
prvPortResetPIC();
|
217 |
|
|
}
|
218 |
|
|
#else
|
219 |
|
|
static void __interrupt __far prvNonPreemptiveTick( void )
|
220 |
|
|
{
|
221 |
|
|
/* Same as preemptive tick, but the cooperative scheduler is being used
|
222 |
|
|
so we don't have to switch in the context of the next task. */
|
223 |
|
|
vTaskIncrementTick();
|
224 |
|
|
prvPortResetPIC();
|
225 |
|
|
}
|
226 |
|
|
#endif
|
227 |
|
|
/*-----------------------------------------------------------*/
|
228 |
|
|
|
229 |
|
|
|
230 |
|
|
static void __interrupt __far prvYieldProcessor( void )
|
231 |
|
|
{
|
232 |
|
|
/* Switch in the context of the next task to be run. */
|
233 |
|
|
portSWITCH_CONTEXT();
|
234 |
|
|
}
|
235 |
|
|
/*-----------------------------------------------------------*/
|
236 |
|
|
|
237 |
|
|
static void prvPortResetPIC( void )
|
238 |
|
|
{
|
239 |
|
|
/* We are going to call the DOS tick interrupt at as close a
|
240 |
|
|
frequency to the normal DOS tick as possible. */
|
241 |
|
|
|
242 |
|
|
/* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
|
243 |
|
|
--sDOSTickCounter;
|
244 |
|
|
if( sDOSTickCounter <= 0 )
|
245 |
|
|
{
|
246 |
|
|
sDOSTickCounter = ( short ) portTICKS_PER_DOS_TICK;
|
247 |
|
|
__asm{ int portSWITCH_INT_NUMBER + 1 };
|
248 |
|
|
}
|
249 |
|
|
else
|
250 |
|
|
{
|
251 |
|
|
/* Reset the PIC as the DOS tick is not being called to
|
252 |
|
|
do it. */
|
253 |
|
|
__asm
|
254 |
|
|
{
|
255 |
|
|
mov al, 20H
|
256 |
|
|
out 20H, al
|
257 |
|
|
};
|
258 |
|
|
}
|
259 |
|
|
}
|
260 |
|
|
/*-----------------------------------------------------------*/
|
261 |
|
|
|
262 |
|
|
void vPortEndScheduler( void )
|
263 |
|
|
{
|
264 |
|
|
/* Jump back to the processor state prior to starting the
|
265 |
|
|
scheduler. This means we are not going to be using a
|
266 |
|
|
task stack frame so the task can be deleted. */
|
267 |
|
|
longjmp( xJumpBuf, 1 );
|
268 |
|
|
}
|
269 |
|
|
/*-----------------------------------------------------------*/
|
270 |
|
|
|
271 |
|
|
static void prvExitFunction( void )
|
272 |
|
|
{
|
273 |
|
|
void ( __interrupt __far *pxOriginalTickISR )();
|
274 |
|
|
|
275 |
|
|
/* Interrupts should be disabled here anyway - but no
|
276 |
|
|
harm in making sure. */
|
277 |
|
|
portDISABLE_INTERRUPTS();
|
278 |
|
|
if( sSchedulerRunning == pdTRUE )
|
279 |
|
|
{
|
280 |
|
|
/* Set the DOS tick back onto the timer ticker. */
|
281 |
|
|
pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
|
282 |
|
|
_dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
|
283 |
|
|
prvSetTickFrequencyDefault();
|
284 |
|
|
|
285 |
|
|
/* Put back the switch interrupt routines that was in place
|
286 |
|
|
before the scheduler started. */
|
287 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
|
288 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
|
289 |
|
|
}
|
290 |
|
|
/* The tick timer is back how DOS wants it. We can re-enable
|
291 |
|
|
interrupts without the scheduler being called. */
|
292 |
|
|
portENABLE_INTERRUPTS();
|
293 |
|
|
|
294 |
|
|
/* This will free up all the memory used by the scheduler.
|
295 |
|
|
exiting back to dos with INT21 AH=4CH will do this anyway so
|
296 |
|
|
it is not necessary to call this. */
|
297 |
|
|
vTaskCleanUpResources();
|
298 |
|
|
}
|
299 |
|
|
/*-----------------------------------------------------------*/
|
300 |
|
|
|
301 |
|
|
static void prvSetTickFrequency( unsigned long ulTickRateHz )
|
302 |
|
|
{
|
303 |
|
|
const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
|
304 |
|
|
const unsigned short usPIT0 = ( unsigned short ) 0x40;
|
305 |
|
|
const unsigned long ulPIT_CONST = ( unsigned long ) 1193180;
|
306 |
|
|
const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
|
307 |
|
|
unsigned long ulOutput;
|
308 |
|
|
|
309 |
|
|
/* Setup the 8245 to tick at the wanted frequency. */
|
310 |
|
|
portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
|
311 |
|
|
ulOutput = ulPIT_CONST / ulTickRateHz;
|
312 |
|
|
|
313 |
|
|
portOUTPUT_BYTE( usPIT0, ( unsigned short )( ulOutput & ( unsigned long ) 0xff ) );
|
314 |
|
|
ulOutput >>= 8;
|
315 |
|
|
portOUTPUT_BYTE( usPIT0, ( unsigned short ) ( ulOutput & ( unsigned long ) 0xff ) );
|
316 |
|
|
}
|
317 |
|
|
/*-----------------------------------------------------------*/
|
318 |
|
|
|
319 |
|
|
static void prvSetTickFrequencyDefault( void )
|
320 |
|
|
{
|
321 |
|
|
const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
|
322 |
|
|
const unsigned short usPIT0 = ( unsigned short ) 0x40;
|
323 |
|
|
const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
|
324 |
|
|
|
325 |
|
|
portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
|
326 |
|
|
portOUTPUT_BYTE( usPIT0,0 );
|
327 |
|
|
portOUTPUT_BYTE( usPIT0,0 );
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
/*lint +e950 */
|
332 |
|
|
|