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