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 V2.6.1
|
56 |
|
|
|
57 |
|
|
+ Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
|
58 |
|
|
macro to be consistent with the later ports.
|
59 |
|
|
|
60 |
|
|
Changes from V4.0.1
|
61 |
|
|
|
62 |
|
|
+ Add function prvSetTickFrequencyDefault() to set the DOS tick back to
|
63 |
|
|
its proper value when the scheduler exits.
|
64 |
|
|
*/
|
65 |
|
|
|
66 |
|
|
#include <stdlib.h>
|
67 |
|
|
#include <dos.h>
|
68 |
|
|
#include <setjmp.h>
|
69 |
|
|
|
70 |
|
|
#include "FreeRTOS.h"
|
71 |
|
|
#include "task.h"
|
72 |
|
|
#include "portasm.h"
|
73 |
|
|
|
74 |
|
|
/*-----------------------------------------------------------
|
75 |
|
|
* Implementation of functions defined in portable.h for the industrial
|
76 |
|
|
* PC port.
|
77 |
|
|
*----------------------------------------------------------*/
|
78 |
|
|
|
79 |
|
|
/*lint -e950 Non ANSI reserved words okay in this file only. */
|
80 |
|
|
|
81 |
|
|
#define portTIMER_INT_NUMBER 0x08
|
82 |
|
|
|
83 |
|
|
/* Setup hardware for required tick interrupt rate. */
|
84 |
|
|
static void prvSetTickFrequency( unsigned long ulTickRateHz );
|
85 |
|
|
|
86 |
|
|
/* Restore hardware to as it was prior to starting the scheduler. */
|
87 |
|
|
static void prvExitFunction( void );
|
88 |
|
|
|
89 |
|
|
/* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
|
90 |
|
|
directly. We chain to the DOS tick as close as possible to the standard DOS
|
91 |
|
|
tick rate. */
|
92 |
|
|
static void prvPortResetPIC( void );
|
93 |
|
|
|
94 |
|
|
/* The ISR used depends on whether the preemptive or cooperative
|
95 |
|
|
scheduler is being used. */
|
96 |
|
|
#if( configUSE_PREEMPTION == 1 )
|
97 |
|
|
/* Tick service routine used by the scheduler when preemptive scheduling is
|
98 |
|
|
being used. */
|
99 |
|
|
static void __interrupt __far prvPreemptiveTick( void );
|
100 |
|
|
#else
|
101 |
|
|
/* Tick service routine used by the scheduler when cooperative scheduling is
|
102 |
|
|
being used. */
|
103 |
|
|
static void __interrupt __far prvNonPreemptiveTick( void );
|
104 |
|
|
#endif
|
105 |
|
|
|
106 |
|
|
/* Trap routine used by taskYIELD() to manually cause a context switch. */
|
107 |
|
|
static void __interrupt __far prvYieldProcessor( void );
|
108 |
|
|
|
109 |
|
|
/* Set the tick frequency back so the floppy drive works correctly when the
|
110 |
|
|
scheduler exits. */
|
111 |
|
|
static void prvSetTickFrequencyDefault( void );
|
112 |
|
|
|
113 |
|
|
/*lint -e956 File scopes necessary here. */
|
114 |
|
|
|
115 |
|
|
/* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
|
116 |
|
|
static short sDOSTickCounter;
|
117 |
|
|
|
118 |
|
|
/* Set true when the vectors are set so the scheduler will service the tick. */
|
119 |
|
|
static portBASE_TYPE xSchedulerRunning = pdFALSE;
|
120 |
|
|
|
121 |
|
|
/* 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(). */
|
122 |
|
|
static void ( __interrupt __far *pxOldSwitchISR )();
|
123 |
|
|
|
124 |
|
|
/* 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(). */
|
125 |
|
|
static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
|
126 |
|
|
|
127 |
|
|
/* Used to restore the original DOS context when the scheduler is ended. */
|
128 |
|
|
static jmp_buf xJumpBuf;
|
129 |
|
|
|
130 |
|
|
/*lint +e956 */
|
131 |
|
|
|
132 |
|
|
/*-----------------------------------------------------------*/
|
133 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
134 |
|
|
{
|
135 |
|
|
pxISR pxOriginalTickISR;
|
136 |
|
|
|
137 |
|
|
/* This is called with interrupts already disabled. */
|
138 |
|
|
|
139 |
|
|
/* Remember what was on the interrupts we are going to use
|
140 |
|
|
so we can put them back later if required. */
|
141 |
|
|
pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
|
142 |
|
|
pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
|
143 |
|
|
pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
|
144 |
|
|
|
145 |
|
|
prvSetTickFrequency( configTICK_RATE_HZ );
|
146 |
|
|
|
147 |
|
|
/* Put our manual switch (yield) function on a known
|
148 |
|
|
vector. */
|
149 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
|
150 |
|
|
|
151 |
|
|
/* Put the old tick on a different interrupt number so we can
|
152 |
|
|
call it when we want. */
|
153 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
|
154 |
|
|
|
155 |
|
|
/* The ISR used depends on whether the preemptive or cooperative
|
156 |
|
|
scheduler is being used. */
|
157 |
|
|
#if( configUSE_PREEMPTION == 1 )
|
158 |
|
|
{
|
159 |
|
|
/* Put our tick switch function on the timer interrupt. */
|
160 |
|
|
_dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
|
161 |
|
|
}
|
162 |
|
|
#else
|
163 |
|
|
{
|
164 |
|
|
/* We want the timer interrupt to just increment the tick count. */
|
165 |
|
|
_dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
|
166 |
|
|
}
|
167 |
|
|
#endif
|
168 |
|
|
|
169 |
|
|
/* Setup a counter that is used to call the DOS interrupt as close
|
170 |
|
|
to it's original frequency as can be achieved given our chosen tick
|
171 |
|
|
frequency. */
|
172 |
|
|
sDOSTickCounter = portTICKS_PER_DOS_TICK;
|
173 |
|
|
|
174 |
|
|
/* Clean up function if we want to return to DOS. */
|
175 |
|
|
if( setjmp( xJumpBuf ) != 0 )
|
176 |
|
|
{
|
177 |
|
|
prvExitFunction();
|
178 |
|
|
xSchedulerRunning = pdFALSE;
|
179 |
|
|
}
|
180 |
|
|
else
|
181 |
|
|
{
|
182 |
|
|
xSchedulerRunning = pdTRUE;
|
183 |
|
|
|
184 |
|
|
/* Kick off the scheduler by setting up the context of the first task. */
|
185 |
|
|
portFIRST_CONTEXT();
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
return xSchedulerRunning;
|
189 |
|
|
}
|
190 |
|
|
/*-----------------------------------------------------------*/
|
191 |
|
|
|
192 |
|
|
/* The ISR used depends on whether the preemptive or cooperative
|
193 |
|
|
scheduler is being used. */
|
194 |
|
|
#if( configUSE_PREEMPTION == 1 )
|
195 |
|
|
static void __interrupt __far prvPreemptiveTick( void )
|
196 |
|
|
{
|
197 |
|
|
/* Get the scheduler to update the task states following the tick. */
|
198 |
|
|
vTaskIncrementTick();
|
199 |
|
|
|
200 |
|
|
/* Switch in the context of the next task to be run. */
|
201 |
|
|
portSWITCH_CONTEXT();
|
202 |
|
|
|
203 |
|
|
/* Reset the PIC ready for the next time. */
|
204 |
|
|
prvPortResetPIC();
|
205 |
|
|
}
|
206 |
|
|
#else
|
207 |
|
|
static void __interrupt __far prvNonPreemptiveTick( void )
|
208 |
|
|
{
|
209 |
|
|
/* Same as preemptive tick, but the cooperative scheduler is being used
|
210 |
|
|
so we don't have to switch in the context of the next task. */
|
211 |
|
|
vTaskIncrementTick();
|
212 |
|
|
prvPortResetPIC();
|
213 |
|
|
}
|
214 |
|
|
#endif
|
215 |
|
|
/*-----------------------------------------------------------*/
|
216 |
|
|
|
217 |
|
|
static void __interrupt __far prvYieldProcessor( void )
|
218 |
|
|
{
|
219 |
|
|
/* Switch in the context of the next task to be run. */
|
220 |
|
|
portSWITCH_CONTEXT();
|
221 |
|
|
}
|
222 |
|
|
/*-----------------------------------------------------------*/
|
223 |
|
|
|
224 |
|
|
static void prvPortResetPIC( void )
|
225 |
|
|
{
|
226 |
|
|
/* We are going to call the DOS tick interrupt at as close a
|
227 |
|
|
frequency to the normal DOS tick as possible. */
|
228 |
|
|
|
229 |
|
|
/* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
|
230 |
|
|
--sDOSTickCounter;
|
231 |
|
|
if( sDOSTickCounter <= 0 )
|
232 |
|
|
{
|
233 |
|
|
sDOSTickCounter = ( short ) portTICKS_PER_DOS_TICK;
|
234 |
|
|
__asm{ int portSWITCH_INT_NUMBER + 1 };
|
235 |
|
|
}
|
236 |
|
|
else
|
237 |
|
|
{
|
238 |
|
|
/* Reset the PIC as the DOS tick is not being called to
|
239 |
|
|
do it. */
|
240 |
|
|
__asm
|
241 |
|
|
{
|
242 |
|
|
mov al, 20H
|
243 |
|
|
out 20H, al
|
244 |
|
|
};
|
245 |
|
|
}
|
246 |
|
|
}
|
247 |
|
|
/*-----------------------------------------------------------*/
|
248 |
|
|
|
249 |
|
|
void vPortEndScheduler( void )
|
250 |
|
|
{
|
251 |
|
|
/* Jump back to the processor state prior to starting the
|
252 |
|
|
scheduler. This means we are not going to be using a
|
253 |
|
|
task stack frame so the task can be deleted. */
|
254 |
|
|
longjmp( xJumpBuf, 1 );
|
255 |
|
|
}
|
256 |
|
|
/*-----------------------------------------------------------*/
|
257 |
|
|
|
258 |
|
|
static void prvExitFunction( void )
|
259 |
|
|
{
|
260 |
|
|
void ( __interrupt __far *pxOriginalTickISR )();
|
261 |
|
|
|
262 |
|
|
/* Interrupts should be disabled here anyway - but no
|
263 |
|
|
harm in making sure. */
|
264 |
|
|
portDISABLE_INTERRUPTS();
|
265 |
|
|
if( xSchedulerRunning == pdTRUE )
|
266 |
|
|
{
|
267 |
|
|
/* Set the DOS tick back onto the timer ticker. */
|
268 |
|
|
pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
|
269 |
|
|
_dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
|
270 |
|
|
prvSetTickFrequencyDefault();
|
271 |
|
|
|
272 |
|
|
/* Put back the switch interrupt routines that was in place
|
273 |
|
|
before the scheduler started. */
|
274 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
|
275 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
|
276 |
|
|
}
|
277 |
|
|
/* The tick timer is back how DOS wants it. We can re-enable
|
278 |
|
|
interrupts without the scheduler being called. */
|
279 |
|
|
portENABLE_INTERRUPTS();
|
280 |
|
|
|
281 |
|
|
/* This will free up all the memory used by the scheduler.
|
282 |
|
|
exiting back to dos with INT21 AH=4CH will do this anyway so
|
283 |
|
|
it is not necessary to call this. */
|
284 |
|
|
vTaskCleanUpResources();
|
285 |
|
|
}
|
286 |
|
|
/*-----------------------------------------------------------*/
|
287 |
|
|
|
288 |
|
|
static void prvSetTickFrequency( unsigned long ulTickRateHz )
|
289 |
|
|
{
|
290 |
|
|
const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
|
291 |
|
|
const unsigned short usPIT0 = ( unsigned short ) 0x40;
|
292 |
|
|
const unsigned long ulPIT_CONST = ( unsigned long ) 1193180UL;
|
293 |
|
|
const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
|
294 |
|
|
unsigned long ulOutput;
|
295 |
|
|
|
296 |
|
|
/* Setup the 8245 to tick at the wanted frequency. */
|
297 |
|
|
portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
|
298 |
|
|
ulOutput = ulPIT_CONST / ulTickRateHz;
|
299 |
|
|
portOUTPUT_BYTE( usPIT0, ( unsigned short )( ulOutput & ( unsigned long ) 0xff ) );
|
300 |
|
|
ulOutput >>= 8;
|
301 |
|
|
portOUTPUT_BYTE( usPIT0, ( unsigned short ) ( ulOutput & ( unsigned long ) 0xff ) );
|
302 |
|
|
}
|
303 |
|
|
/*-----------------------------------------------------------*/
|
304 |
|
|
|
305 |
|
|
static void prvSetTickFrequencyDefault( void )
|
306 |
|
|
{
|
307 |
|
|
const unsigned short usPIT_MODE = ( unsigned short ) 0x43;
|
308 |
|
|
const unsigned short usPIT0 = ( unsigned short ) 0x40;
|
309 |
|
|
const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;
|
310 |
|
|
|
311 |
|
|
portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
|
312 |
|
|
portOUTPUT_BYTE( usPIT0,0 );
|
313 |
|
|
portOUTPUT_BYTE( usPIT0,0 );
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
|
317 |
|
|
/*lint +e950 */
|
318 |
|
|
|