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 |
|
|
+ portRESET_PIC() is now called last thing before the end of the preemptive
|
64 |
|
|
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 |
|
|
|
72 |
|
|
/*-----------------------------------------------------------
|
73 |
|
|
* Implementation of functions defined in portable.h for the Flashlite 186
|
74 |
|
|
* port.
|
75 |
|
|
*----------------------------------------------------------*/
|
76 |
|
|
|
77 |
|
|
#include <stdlib.h>
|
78 |
|
|
#include <i86.h>
|
79 |
|
|
#include <dos.h>
|
80 |
|
|
#include <setjmp.h>
|
81 |
|
|
|
82 |
|
|
#include "FreeRTOS.h"
|
83 |
|
|
#include "task.h"
|
84 |
|
|
#include "portasm.h"
|
85 |
|
|
|
86 |
|
|
/*lint -e950 Non ANSI reserved words okay in this file only. */
|
87 |
|
|
|
88 |
|
|
#define portTIMER_EOI_TYPE ( 8 )
|
89 |
|
|
#define portRESET_PIC() portOUTPUT_WORD( ( unsigned short ) 0xff22, portTIMER_EOI_TYPE )
|
90 |
|
|
#define portTIMER_INT_NUMBER 0x12
|
91 |
|
|
|
92 |
|
|
#define portTIMER_1_CONTROL_REGISTER ( ( unsigned short ) 0xff5e )
|
93 |
|
|
#define portTIMER_0_CONTROL_REGISTER ( ( unsigned short ) 0xff56 )
|
94 |
|
|
#define portTIMER_INTERRUPT_ENABLE ( ( unsigned short ) 0x2000 )
|
95 |
|
|
|
96 |
|
|
/* Setup the hardware to generate the required tick frequency. */
|
97 |
|
|
static void prvSetTickFrequency( unsigned long ulTickRateHz );
|
98 |
|
|
|
99 |
|
|
/* Set the hardware back to the state as per before the scheduler started. */
|
100 |
|
|
static void prvExitFunction( void );
|
101 |
|
|
|
102 |
|
|
#if configUSE_PREEMPTION == 1
|
103 |
|
|
/* Tick service routine used by the scheduler when preemptive scheduling is
|
104 |
|
|
being used. */
|
105 |
|
|
static void __interrupt __far prvPreemptiveTick( void );
|
106 |
|
|
#else
|
107 |
|
|
/* Tick service routine used by the scheduler when cooperative scheduling is
|
108 |
|
|
being used. */
|
109 |
|
|
static void __interrupt __far prvNonPreemptiveTick( void );
|
110 |
|
|
#endif
|
111 |
|
|
|
112 |
|
|
/* Trap routine used by taskYIELD() to manually cause a context switch. */
|
113 |
|
|
static void __interrupt __far prvYieldProcessor( void );
|
114 |
|
|
|
115 |
|
|
/*lint -e956 File scopes necessary here. */
|
116 |
|
|
|
117 |
|
|
/* Set true when the vectors are set so the scheduler will service the tick. */
|
118 |
|
|
static short sSchedulerRunning = pdFALSE;
|
119 |
|
|
|
120 |
|
|
/* 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(). */
|
121 |
|
|
static void ( __interrupt __far *pxOldSwitchISR )();
|
122 |
|
|
|
123 |
|
|
/* Used to restore the original DOS context when the scheduler is ended. */
|
124 |
|
|
static jmp_buf xJumpBuf;
|
125 |
|
|
|
126 |
|
|
/*lint +e956 */
|
127 |
|
|
|
128 |
|
|
/*-----------------------------------------------------------*/
|
129 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
130 |
|
|
{
|
131 |
|
|
/* This is called with interrupts already disabled. */
|
132 |
|
|
|
133 |
|
|
/* Remember what was on the interrupts we are going to use
|
134 |
|
|
so we can put them back later if required. */
|
135 |
|
|
pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
|
136 |
|
|
|
137 |
|
|
/* Put our manual switch (yield) function on a known
|
138 |
|
|
vector. */
|
139 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
|
140 |
|
|
|
141 |
|
|
#if configUSE_PREEMPTION == 1
|
142 |
|
|
{
|
143 |
|
|
/* Put our tick switch function on the timer interrupt. */
|
144 |
|
|
_dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
|
145 |
|
|
}
|
146 |
|
|
#else
|
147 |
|
|
{
|
148 |
|
|
/* We want the timer interrupt to just increment the tick count. */
|
149 |
|
|
_dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
|
150 |
|
|
}
|
151 |
|
|
#endif
|
152 |
|
|
|
153 |
|
|
prvSetTickFrequency( configTICK_RATE_HZ );
|
154 |
|
|
|
155 |
|
|
/* Clean up function if we want to return to DOS. */
|
156 |
|
|
if( setjmp( xJumpBuf ) != 0 )
|
157 |
|
|
{
|
158 |
|
|
prvExitFunction();
|
159 |
|
|
sSchedulerRunning = pdFALSE;
|
160 |
|
|
}
|
161 |
|
|
else
|
162 |
|
|
{
|
163 |
|
|
sSchedulerRunning = pdTRUE;
|
164 |
|
|
|
165 |
|
|
/* Kick off the scheduler by setting up the context of the first task. */
|
166 |
|
|
portFIRST_CONTEXT();
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
return sSchedulerRunning;
|
170 |
|
|
}
|
171 |
|
|
/*-----------------------------------------------------------*/
|
172 |
|
|
|
173 |
|
|
/* The tick ISR used depend on whether or not the preemptive or cooperative
|
174 |
|
|
kernel is being used. */
|
175 |
|
|
#if configUSE_PREEMPTION == 1
|
176 |
|
|
static void __interrupt __far prvPreemptiveTick( void )
|
177 |
|
|
{
|
178 |
|
|
/* Get the scheduler to update the task states following the tick. */
|
179 |
|
|
vTaskIncrementTick();
|
180 |
|
|
|
181 |
|
|
/* Switch in the context of the next task to be run. */
|
182 |
|
|
portSWITCH_CONTEXT();
|
183 |
|
|
|
184 |
|
|
/* Reset the PIC ready for the next time. */
|
185 |
|
|
portRESET_PIC();
|
186 |
|
|
}
|
187 |
|
|
#else
|
188 |
|
|
static void __interrupt __far prvNonPreemptiveTick( void )
|
189 |
|
|
{
|
190 |
|
|
/* Same as preemptive tick, but the cooperative scheduler is being used
|
191 |
|
|
so we don't have to switch in the context of the next task. */
|
192 |
|
|
vTaskIncrementTick();
|
193 |
|
|
portRESET_PIC();
|
194 |
|
|
}
|
195 |
|
|
#endif
|
196 |
|
|
/*-----------------------------------------------------------*/
|
197 |
|
|
|
198 |
|
|
static void __interrupt __far prvYieldProcessor( void )
|
199 |
|
|
{
|
200 |
|
|
/* Switch in the context of the next task to be run. */
|
201 |
|
|
portSWITCH_CONTEXT();
|
202 |
|
|
}
|
203 |
|
|
/*-----------------------------------------------------------*/
|
204 |
|
|
|
205 |
|
|
void vPortEndScheduler( void )
|
206 |
|
|
{
|
207 |
|
|
/* Jump back to the processor state prior to starting the
|
208 |
|
|
scheduler. This means we are not going to be using a
|
209 |
|
|
task stack frame so the task can be deleted. */
|
210 |
|
|
longjmp( xJumpBuf, 1 );
|
211 |
|
|
}
|
212 |
|
|
/*-----------------------------------------------------------*/
|
213 |
|
|
|
214 |
|
|
static void prvExitFunction( void )
|
215 |
|
|
{
|
216 |
|
|
const unsigned short usTimerDisable = 0x0000;
|
217 |
|
|
unsigned short usTimer0Control;
|
218 |
|
|
|
219 |
|
|
/* Interrupts should be disabled here anyway - but no
|
220 |
|
|
harm in making sure. */
|
221 |
|
|
portDISABLE_INTERRUPTS();
|
222 |
|
|
if( sSchedulerRunning == pdTRUE )
|
223 |
|
|
{
|
224 |
|
|
/* Put back the switch interrupt routines that was in place
|
225 |
|
|
before the scheduler started. */
|
226 |
|
|
_dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
/* Disable the timer used for the tick to ensure the scheduler is
|
230 |
|
|
not called before restoring interrupts. There was previously nothing
|
231 |
|
|
on this timer so there is no old ISR to restore. */
|
232 |
|
|
portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );
|
233 |
|
|
|
234 |
|
|
/* Restart the DOS tick. */
|
235 |
|
|
usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
|
236 |
|
|
usTimer0Control |= portTIMER_INTERRUPT_ENABLE;
|
237 |
|
|
portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
portENABLE_INTERRUPTS();
|
241 |
|
|
|
242 |
|
|
/* This will free up all the memory used by the scheduler.
|
243 |
|
|
exiting back to dos with INT21 AH=4CH will do this anyway so
|
244 |
|
|
it is not necessary to call this. */
|
245 |
|
|
vTaskCleanUpResources();
|
246 |
|
|
}
|
247 |
|
|
/*-----------------------------------------------------------*/
|
248 |
|
|
|
249 |
|
|
static void prvSetTickFrequency( unsigned long ulTickRateHz )
|
250 |
|
|
{
|
251 |
|
|
const unsigned short usMaxCountRegister = 0xff5a;
|
252 |
|
|
const unsigned short usTimerPriorityRegister = 0xff32;
|
253 |
|
|
const unsigned short usTimerEnable = 0xC000;
|
254 |
|
|
const unsigned short usRetrigger = 0x0001;
|
255 |
|
|
const unsigned short usTimerHighPriority = 0x0000;
|
256 |
|
|
unsigned short usTimer0Control;
|
257 |
|
|
|
258 |
|
|
/* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */
|
259 |
|
|
|
260 |
|
|
const unsigned long ulClockFrequency = 0x7f31a0;
|
261 |
|
|
|
262 |
|
|
unsigned long ulTimerCount = ulClockFrequency / ulTickRateHz;
|
263 |
|
|
|
264 |
|
|
portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );
|
265 |
|
|
portOUTPUT_WORD( usMaxCountRegister, ( unsigned short ) ulTimerCount );
|
266 |
|
|
portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );
|
267 |
|
|
|
268 |
|
|
/* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */
|
269 |
|
|
usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
|
270 |
|
|
usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;
|
271 |
|
|
portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
/*lint +e950 */
|
276 |
|
|
|