]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/oWatcom/16BitDOS/PC/port.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Source / portable / oWatcom / 16BitDOS / PC / port.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66 Changes from V1.00:\r
67         \r
68         + Call to taskYIELD() from within tick ISR has been replaced by the more\r
69           efficient portSWITCH_CONTEXT().\r
70         + ISR function definitions renamed to include the prv prefix.\r
71 \r
72 Changes from V1.2.0:\r
73 \r
74         + prvPortResetPIC() is now called last thing before the end of the \r
75           preemptive tick routine.\r
76 \r
77 Changes from V2.6.1\r
78 \r
79         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
80           macro to be consistent with the later ports.\r
81 \r
82 Changes from V4.0.1\r
83         \r
84         + Add function prvSetTickFrequencyDefault() to set the DOS tick back to\r
85           its proper value when the scheduler exits. \r
86 */\r
87 \r
88 #include <stdlib.h>\r
89 #include <stdio.h>\r
90 #include <i86.h>\r
91 #include <dos.h>\r
92 #include <setjmp.h>\r
93 \r
94 #include "FreeRTOS.h"\r
95 #include "task.h"\r
96 #include "portasm.h"\r
97 \r
98 /*-----------------------------------------------------------\r
99  * Implementation of functions defined in portable.h for the industrial\r
100  * PC port.\r
101  *----------------------------------------------------------*/\r
102 \r
103 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
104 \r
105 #define portTIMER_INT_NUMBER    0x08\r
106 \r
107 /* Setup hardware for required tick interrupt rate. */\r
108 static void prvSetTickFrequency( unsigned long ulTickRateHz );\r
109 \r
110 /* Restore hardware to as it was prior to starting the scheduler. */\r
111 static void prvExitFunction( void );\r
112 \r
113 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC\r
114 directly.  We chain to the DOS tick as close as possible to the standard DOS\r
115 tick rate. */\r
116 static void prvPortResetPIC( void );\r
117 \r
118 /* The tick ISR used depends on whether the preemptive or cooperative scheduler\r
119 is being used. */\r
120 #if configUSE_PREEMPTION == 1\r
121         /* Tick service routine used by the scheduler when preemptive scheduling is\r
122         being used. */\r
123         static void __interrupt __far prvPreemptiveTick( void );\r
124 #else\r
125         /* Tick service routine used by the scheduler when cooperative scheduling is \r
126         being used. */\r
127         static void __interrupt __far prvNonPreemptiveTick( void );\r
128 #endif\r
129 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
130 static void __interrupt __far prvYieldProcessor( void );\r
131 \r
132 /* Set the tick frequency back so the floppy drive works correctly when the\r
133 scheduler exits. */\r
134 static void prvSetTickFrequencyDefault( void );\r
135 \r
136 /*lint -e956 File scopes necessary here. */\r
137 \r
138 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */\r
139 static short sDOSTickCounter;                                                   \r
140 \r
141 /* Set true when the vectors are set so the scheduler will service the tick. */\r
142 static short sSchedulerRunning = pdFALSE;                               \r
143 \r
144 /* 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
145 static void ( __interrupt __far *pxOldSwitchISR )();            \r
146 \r
147 /* 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
148 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();       \r
149 \r
150 /* Used to restore the original DOS context when the scheduler is ended. */\r
151 static jmp_buf xJumpBuf;\r
152 \r
153 /*lint +e956 */\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 portBASE_TYPE xPortStartScheduler( void )\r
157 {\r
158 pxISR pxOriginalTickISR;\r
159         \r
160         /* This is called with interrupts already disabled. */\r
161 \r
162         /* Remember what was on the interrupts we are going to use\r
163         so we can put them back later if required. */\r
164         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
165         pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );\r
166         pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );\r
167 \r
168         prvSetTickFrequency( configTICK_RATE_HZ );\r
169 \r
170         /* Put our manual switch (yield) function on a known\r
171         vector. */\r
172         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
173 \r
174         /* Put the old tick on a different interrupt number so we can\r
175         call it when we want. */\r
176         _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );\r
177 \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                 sSchedulerRunning = pdFALSE;\r
200         }\r
201         else\r
202         {\r
203                 sSchedulerRunning = 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 sSchedulerRunning;\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /* The tick ISR used depends on whether the preemptive or cooperative scheduler\r
214 is being used. */\r
215 #if configUSE_PREEMPTION == 1\r
216         /* Tick service routine used by the scheduler when preemptive scheduling is\r
217         being used. */\r
218         static void __interrupt __far prvPreemptiveTick( void )\r
219         {\r
220                 /* Get the scheduler to update the task states following the tick. */\r
221                 if( xTaskIncrementTick() != pdFALSE )\r
222                 {\r
223                         /* Switch in the context of the next task to be run. */\r
224                         portSWITCH_CONTEXT();\r
225                 }\r
226 \r
227                 /* Reset the PIC ready for the next time. */\r
228                 prvPortResetPIC();\r
229         }\r
230 #else\r
231         static void __interrupt __far prvNonPreemptiveTick( void )\r
232         {\r
233                 /* Same as preemptive tick, but the cooperative scheduler is being used\r
234                 so we don't have to switch in the context of the next task. */\r
235                 xTaskIncrementTick();\r
236                 prvPortResetPIC();\r
237         }\r
238 #endif\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 \r
242 static void __interrupt __far prvYieldProcessor( void )\r
243 {\r
244         /* Switch in the context of the next task to be run. */\r
245         portSWITCH_CONTEXT();\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 static void prvPortResetPIC( void )\r
250 {\r
251         /* We are going to call the DOS tick interrupt at as close a\r
252         frequency to the normal DOS tick as possible. */\r
253 \r
254         /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */\r
255         --sDOSTickCounter;\r
256         if( sDOSTickCounter <= 0 )\r
257         {\r
258                 sDOSTickCounter = ( short ) portTICKS_PER_DOS_TICK;\r
259                 __asm{ int      portSWITCH_INT_NUMBER + 1 };             \r
260         }\r
261         else\r
262         {\r
263                 /* Reset the PIC as the DOS tick is not being called to\r
264                 do it. */\r
265                 __asm\r
266                 {\r
267                         mov     al, 20H\r
268                         out 20H, al\r
269                 };\r
270         }\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 void vPortEndScheduler( void )\r
275 {\r
276         /* Jump back to the processor state prior to starting the\r
277         scheduler.  This means we are not going to be using a\r
278         task stack frame so the task can be deleted. */\r
279         longjmp( xJumpBuf, 1 );\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 static void prvExitFunction( void )\r
284 {\r
285 void ( __interrupt __far *pxOriginalTickISR )();\r
286 \r
287         /* Interrupts should be disabled here anyway - but no \r
288         harm in making sure. */\r
289         portDISABLE_INTERRUPTS();\r
290         if( sSchedulerRunning == pdTRUE )\r
291         {\r
292                 /* Set the DOS tick back onto the timer ticker. */\r
293                 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );\r
294                 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );\r
295                 prvSetTickFrequencyDefault();\r
296 \r
297                 /* Put back the switch interrupt routines that was in place\r
298                 before the scheduler started. */\r
299                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );\r
300                 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );\r
301         }\r
302         /* The tick timer is back how DOS wants it.  We can re-enable\r
303         interrupts without the scheduler being called. */\r
304         portENABLE_INTERRUPTS();\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 static void prvSetTickFrequency( unsigned long ulTickRateHz )\r
309 {\r
310 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;\r
311 const unsigned short usPIT0 = ( unsigned short ) 0x40;\r
312 const unsigned long ulPIT_CONST = ( unsigned long ) 1193180;\r
313 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;\r
314 unsigned long ulOutput;\r
315 \r
316         /* Setup the 8245 to tick at the wanted frequency. */\r
317         portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );\r
318         ulOutput = ulPIT_CONST / ulTickRateHz;\r
319    \r
320         portOUTPUT_BYTE( usPIT0, ( unsigned short )( ulOutput & ( unsigned long ) 0xff ) );\r
321         ulOutput >>= 8;\r
322         portOUTPUT_BYTE( usPIT0, ( unsigned short ) ( ulOutput & ( unsigned long ) 0xff ) );\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 static void prvSetTickFrequencyDefault( void )\r
327 {\r
328 const unsigned short usPIT_MODE = ( unsigned short ) 0x43;\r
329 const unsigned short usPIT0 = ( unsigned short ) 0x40;\r
330 const unsigned short us8254_CTR0_MODE3 = ( unsigned short ) 0x36;\r
331 \r
332         portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );\r
333         portOUTPUT_BYTE( usPIT0,0 );\r
334         portOUTPUT_BYTE( usPIT0,0 );\r
335 }\r
336 \r
337 \r
338 /*lint +e950 */\r
339 \r