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