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