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