]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/oWatcom/16BitDOS/PC/port.c
***IMMINENT RELEASE NOTICE***
[freertos] / FreeRTOS / Source / portable / oWatcom / 16BitDOS / PC / port.c
1 /*\r
2     FreeRTOS V8.1.0 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67 Changes from V1.00:\r
68         \r
69         + Call to taskYIELD() from within tick ISR has been replaced by the more\r
70           efficient portSWITCH_CONTEXT().\r
71         + ISR function definitions renamed to include the prv prefix.\r
72 \r
73 Changes from V1.2.0:\r
74 \r
75         + prvPortResetPIC() is now called last thing before the end of the \r
76           preemptive tick routine.\r
77 \r
78 Changes from V2.6.1\r
79 \r
80         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
81           macro to be consistent with the later ports.\r
82 \r
83 Changes from V4.0.1\r
84         \r
85         + Add function prvSetTickFrequencyDefault() to set the DOS tick back to\r
86           its proper value when the scheduler exits. \r
87 */\r
88 \r
89 #include <stdlib.h>\r
90 #include <stdio.h>\r
91 #include <i86.h>\r
92 #include <dos.h>\r
93 #include <setjmp.h>\r
94 \r
95 #include "FreeRTOS.h"\r
96 #include "task.h"\r
97 #include "portasm.h"\r
98 \r
99 /*-----------------------------------------------------------\r
100  * Implementation of functions defined in portable.h for the industrial\r
101  * PC port.\r
102  *----------------------------------------------------------*/\r
103 \r
104 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
105 \r
106 #define portTIMER_INT_NUMBER    0x08\r
107 \r
108 /* Setup hardware for required tick interrupt rate. */\r
109 static void prvSetTickFrequency( uint32_t ulTickRateHz );\r
110 \r
111 /* Restore hardware to as it was prior to starting the scheduler. */\r
112 static void prvExitFunction( void );\r
113 \r
114 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC\r
115 directly.  We chain to the DOS tick as close as possible to the standard DOS\r
116 tick rate. */\r
117 static void prvPortResetPIC( void );\r
118 \r
119 /* The tick ISR used depends on whether the preemptive or cooperative scheduler\r
120 is being used. */\r
121 #if configUSE_PREEMPTION == 1\r
122         /* Tick service routine used by the scheduler when preemptive scheduling is\r
123         being used. */\r
124         static void __interrupt __far prvPreemptiveTick( void );\r
125 #else\r
126         /* Tick service routine used by the scheduler when cooperative scheduling is \r
127         being used. */\r
128         static void __interrupt __far prvNonPreemptiveTick( void );\r
129 #endif\r
130 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
131 static void __interrupt __far prvYieldProcessor( void );\r
132 \r
133 /* Set the tick frequency back so the floppy drive works correctly when the\r
134 scheduler exits. */\r
135 static void prvSetTickFrequencyDefault( void );\r
136 \r
137 /*lint -e956 File scopes necessary here. */\r
138 \r
139 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */\r
140 static int16_t sDOSTickCounter;                                                 \r
141 \r
142 /* Set true when the vectors are set so the scheduler will service the tick. */\r
143 static int16_t sSchedulerRunning = pdFALSE;                             \r
144 \r
145 /* 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
146 static void ( __interrupt __far *pxOldSwitchISR )();            \r
147 \r
148 /* 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
149 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();       \r
150 \r
151 /* Used to restore the original DOS context when the scheduler is ended. */\r
152 static jmp_buf xJumpBuf;\r
153 \r
154 /*lint +e956 */\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 BaseType_t xPortStartScheduler( void )\r
158 {\r
159 pxISR pxOriginalTickISR;\r
160         \r
161         /* This is called with interrupts already disabled. */\r
162 \r
163         /* Remember what was on the interrupts we are going to use\r
164         so we can put them back later if required. */\r
165         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
166         pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );\r
167         pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );\r
168 \r
169         prvSetTickFrequency( configTICK_RATE_HZ );\r
170 \r
171         /* Put our manual switch (yield) function on a known\r
172         vector. */\r
173         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
174 \r
175         /* Put the old tick on a different interrupt number so we can\r
176         call it when we want. */\r
177         _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );\r
178 \r
179         #if configUSE_PREEMPTION == 1\r
180         {               \r
181                 /* Put our tick switch function on the timer interrupt. */\r
182                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );\r
183         }\r
184         #else\r
185         {\r
186                 /* We want the timer interrupt to just increment the tick count. */\r
187                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );\r
188         }\r
189         #endif\r
190 \r
191         /* Setup a counter that is used to call the DOS interrupt as close\r
192         to it's original frequency as can be achieved given our chosen tick\r
193         frequency. */\r
194         sDOSTickCounter = portTICKS_PER_DOS_TICK;\r
195 \r
196         /* Clean up function if we want to return to DOS. */\r
197         if( setjmp( xJumpBuf ) != 0 )\r
198         {\r
199                 prvExitFunction();\r
200                 sSchedulerRunning = pdFALSE;\r
201         }\r
202         else\r
203         {\r
204                 sSchedulerRunning = pdTRUE;\r
205 \r
206                 /* Kick off the scheduler by setting up the context of the first task. */\r
207                 portFIRST_CONTEXT();\r
208         }\r
209 \r
210         return sSchedulerRunning;\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 /* The tick ISR used depends on whether the preemptive or cooperative scheduler\r
215 is being used. */\r
216 #if configUSE_PREEMPTION == 1\r
217         /* Tick service routine used by the scheduler when preemptive scheduling is\r
218         being used. */\r
219         static void __interrupt __far prvPreemptiveTick( void )\r
220         {\r
221                 /* Get the scheduler to update the task states following the tick. */\r
222                 if( xTaskIncrementTick() != pdFALSE )\r
223                 {\r
224                         /* Switch in the context of the next task to be run. */\r
225                         portSWITCH_CONTEXT();\r
226                 }\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                 xTaskIncrementTick();\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 = ( int16_t ) 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( uint32_t ulTickRateHz )\r
310 {\r
311 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;\r
312 const uint16_t usPIT0 = ( uint16_t ) 0x40;\r
313 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180;\r
314 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;\r
315 uint32_t 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, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );\r
322         ulOutput >>= 8;\r
323         portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 static void prvSetTickFrequencyDefault( void )\r
328 {\r
329 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;\r
330 const uint16_t usPIT0 = ( uint16_t ) 0x40;\r
331 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 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