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