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