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