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