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