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