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