]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/BCC/16BitDOS/PC/port.c
8bca8ed1494f3649dd61aa958b450184931406fe
[freertos] / FreeRTOS / Source / portable / BCC / 16BitDOS / PC / port.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70 Changes from V2.6.1\r
71 \r
72         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
73           macro to be consistent with the later ports.\r
74 \r
75 Changes from V4.0.1\r
76         \r
77         + Add function prvSetTickFrequencyDefault() to set the DOS tick back to\r
78           its proper value when the scheduler exits. \r
79 */\r
80 \r
81 #include <stdlib.h>\r
82 #include <dos.h>\r
83 #include <setjmp.h>\r
84 \r
85 #include "FreeRTOS.h"\r
86 #include "task.h"\r
87 #include "portasm.h"\r
88 \r
89 /*-----------------------------------------------------------\r
90  * Implementation of functions defined in portable.h for the industrial\r
91  * PC port.\r
92  *----------------------------------------------------------*/\r
93 \r
94 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
95 \r
96 #define portTIMER_INT_NUMBER    0x08\r
97 \r
98 /* Setup hardware for required tick interrupt rate. */\r
99 static void prvSetTickFrequency( unsigned long ulTickRateHz );\r
100 \r
101 /* Restore hardware to as it was prior to starting the scheduler. */\r
102 static void prvExitFunction( void );\r
103 \r
104 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC\r
105 directly.  We chain to the DOS tick as close as possible to the standard DOS\r
106 tick rate. */\r
107 static void prvPortResetPIC( void );\r
108 \r
109 /* The ISR used depends on whether the preemptive or cooperative\r
110 scheduler is being used. */\r
111 #if( configUSE_PREEMPTION == 1 )\r
112         /* Tick service routine used by the scheduler when preemptive scheduling is\r
113         being used. */\r
114         static void __interrupt __far prvPreemptiveTick( void );\r
115 #else\r
116         /* Tick service routine used by the scheduler when cooperative scheduling is\r
117         being used. */\r
118         static void __interrupt __far prvNonPreemptiveTick( void );\r
119 #endif\r
120 \r
121 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
122 static void __interrupt __far prvYieldProcessor( void );\r
123 \r
124 /* Set the tick frequency back so the floppy drive works correctly when the\r
125 scheduler exits. */\r
126 static void prvSetTickFrequencyDefault( void );\r
127 \r
128 /*lint -e956 File scopes necessary here. */\r
129 \r
130 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */\r
131 static short sDOSTickCounter;\r
132 \r
133 /* Set true when the vectors are set so the scheduler will service the tick. */\r
134 static portBASE_TYPE xSchedulerRunning = pdFALSE;                               \r
135 \r
136 /* 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
137 static void ( __interrupt __far *pxOldSwitchISR )();            \r
138 \r
139 /* 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
140 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();       \r
141 \r
142 /* Used to restore the original DOS context when the scheduler is ended. */\r
143 static jmp_buf xJumpBuf;\r
144 \r
145 /*lint +e956 */\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 portBASE_TYPE xPortStartScheduler( void )\r
149 {\r
150 pxISR pxOriginalTickISR;\r
151         \r
152         /* This is called with interrupts already disabled. */\r
153 \r
154         /* Remember what was on the interrupts we are going to use\r
155         so we can put them back later if required. */\r
156         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
157         pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );\r
158         pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );\r
159 \r
160         prvSetTickFrequency( configTICK_RATE_HZ );\r
161 \r
162         /* Put our manual switch (yield) function on a known\r
163         vector. */\r
164         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
165 \r
166         /* Put the old tick on a different interrupt number so we can\r
167         call it when we want. */\r
168         _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );\r
169 \r
170         /* The ISR used depends on whether the preemptive or cooperative\r
171         scheduler is being used. */\r
172         #if( configUSE_PREEMPTION == 1 )\r
173         {\r
174                 /* Put our tick switch function on the timer interrupt. */\r
175                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );\r
176         }\r
177         #else\r
178         {\r
179                 /* We want the timer interrupt to just increment the tick count. */\r
180                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );\r
181         }\r
182     #endif\r
183 \r
184         /* Setup a counter that is used to call the DOS interrupt as close\r
185         to it's original frequency as can be achieved given our chosen tick\r
186         frequency. */\r
187         sDOSTickCounter = portTICKS_PER_DOS_TICK;\r
188 \r
189         /* Clean up function if we want to return to DOS. */\r
190         if( setjmp( xJumpBuf ) != 0 )\r
191         {\r
192                 prvExitFunction();\r
193                 xSchedulerRunning = pdFALSE;\r
194         }\r
195         else\r
196         {\r
197                 xSchedulerRunning = pdTRUE;\r
198 \r
199                 /* Kick off the scheduler by setting up the context of the first task. */\r
200                 portFIRST_CONTEXT();\r
201         }\r
202 \r
203         return xSchedulerRunning;\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 /* The ISR used depends on whether the preemptive or cooperative\r
208 scheduler is being used. */\r
209 #if( configUSE_PREEMPTION == 1 )\r
210         static void __interrupt __far prvPreemptiveTick( void )\r
211         {\r
212                 /* Get the scheduler to update the task states following the tick. */\r
213                 vTaskIncrementTick();\r
214 \r
215                 /* Switch in the context of the next task to be run. */\r
216                 portSWITCH_CONTEXT();\r
217 \r
218                 /* Reset the PIC ready for the next time. */\r
219                 prvPortResetPIC();\r
220         }\r
221 #else\r
222         static void __interrupt __far prvNonPreemptiveTick( void )\r
223         {\r
224                 /* Same as preemptive tick, but the cooperative scheduler is being used\r
225                 so we don't have to switch in the context of the next task. */\r
226                 vTaskIncrementTick();\r
227                 prvPortResetPIC();\r
228         }\r
229 #endif\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static void __interrupt __far prvYieldProcessor( void )\r
233 {\r
234         /* Switch in the context of the next task to be run. */\r
235         portSWITCH_CONTEXT();\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 static void prvPortResetPIC( void )\r
240 {\r
241         /* We are going to call the DOS tick interrupt at as close a\r
242         frequency to the normal DOS tick as possible. */\r
243 \r
244         /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */\r
245         --sDOSTickCounter;\r
246         if( sDOSTickCounter <= 0 )\r
247         {\r
248                 sDOSTickCounter = ( short ) portTICKS_PER_DOS_TICK;\r
249                 __asm{ int      portSWITCH_INT_NUMBER + 1 };             \r
250         }\r
251         else\r
252         {\r
253                 /* Reset the PIC as the DOS tick is not being called to\r
254                 do it. */\r
255                 __asm\r
256                 {\r
257                         mov     al, 20H\r
258                         out 20H, al\r
259                 };\r
260         }\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 void vPortEndScheduler( void )\r
265 {\r
266         /* Jump back to the processor state prior to starting the\r
267         scheduler.  This means we are not going to be using a\r
268         task stack frame so the task can be deleted. */\r
269         longjmp( xJumpBuf, 1 );\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 static void prvExitFunction( void )\r
274 {\r
275 void ( __interrupt __far *pxOriginalTickISR )();\r
276 \r
277         /* Interrupts should be disabled here anyway - but no \r
278         harm in making sure. */\r
279         portDISABLE_INTERRUPTS();\r
280         if( xSchedulerRunning == pdTRUE )\r
281         {\r
282                 /* Set the DOS tick back onto the timer ticker. */\r
283                 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );\r
284                 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );\r
285                 prvSetTickFrequencyDefault();\r
286 \r
287                 /* Put back the switch interrupt routines that was in place\r
288                 before the scheduler started. */\r
289                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );\r
290                 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );\r
291         }\r
292         /* The tick timer is back how DOS wants it.  We can re-enable\r
293         interrupts without the scheduler being called. */\r
294         portENABLE_INTERRUPTS();\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 static void prvSetTickFrequency( unsigned long ulTickRateHz )\r
299 {\r
300 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;\r
301 const unsigned short usPIT0 = ( unsigned short ) 0x40;\r
302 const unsigned long ulPIT_CONST = ( unsigned long ) 1193180UL;\r
303 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;\r
304 unsigned long ulOutput;\r
305 \r
306         /* Setup the 8245 to tick at the wanted frequency. */\r
307         portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );\r
308         ulOutput = ulPIT_CONST / ulTickRateHz;\r
309         portOUTPUT_BYTE( usPIT0, ( unsigned short )( ulOutput & ( unsigned long ) 0xff ) );\r
310         ulOutput >>= 8;\r
311         portOUTPUT_BYTE( usPIT0, ( unsigned short ) ( ulOutput & ( unsigned long ) 0xff ) );\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static void prvSetTickFrequencyDefault( void )\r
316 {\r
317 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;\r
318 const unsigned short usPIT0 = ( unsigned short ) 0x40;\r
319 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;\r
320 \r
321         portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );\r
322         portOUTPUT_BYTE( usPIT0,0 );\r
323         portOUTPUT_BYTE( usPIT0,0 );\r
324 }\r
325 \r
326 \r
327 /*lint +e950 */\r
328 \r