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