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