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