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