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