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