]> begriffs open source - freertos/blob - portable/oWatcom/16BitDOS/PC/port.c
Add SMP in the License Header (#402)
[freertos] / portable / oWatcom / 16BitDOS / PC / port.c
1 /*
2  * FreeRTOS SMP Kernel V202110.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29 Changes from V1.00:
30         
31         + Call to taskYIELD() from within tick ISR has been replaced by the more
32           efficient portSWITCH_CONTEXT().
33         + ISR function definitions renamed to include the prv prefix.
34
35 Changes from V1.2.0:
36
37         + prvPortResetPIC() is now called last thing before the end of the 
38           preemptive tick routine.
39
40 Changes from V2.6.1
41
42         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
43           macro to be consistent with the later ports.
44
45 Changes from V4.0.1
46         
47         + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
48           its proper value when the scheduler exits. 
49 */
50
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <i86.h>
54 #include <dos.h>
55 #include <setjmp.h>
56
57 #include "FreeRTOS.h"
58 #include "task.h"
59 #include "portasm.h"
60
61 /*-----------------------------------------------------------
62  * Implementation of functions defined in portable.h for the industrial
63  * PC port.
64  *----------------------------------------------------------*/
65
66 /*lint -e950 Non ANSI reserved words okay in this file only. */
67
68 #define portTIMER_INT_NUMBER    0x08
69
70 /* Setup hardware for required tick interrupt rate. */
71 static void prvSetTickFrequency( uint32_t ulTickRateHz );
72
73 /* Restore hardware to as it was prior to starting the scheduler. */
74 static void prvExitFunction( void );
75
76 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
77 directly.  We chain to the DOS tick as close as possible to the standard DOS
78 tick rate. */
79 static void prvPortResetPIC( void );
80
81 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
82 is being used. */
83 #if configUSE_PREEMPTION == 1
84         /* Tick service routine used by the scheduler when preemptive scheduling is
85         being used. */
86         static void __interrupt __far prvPreemptiveTick( void );
87 #else
88         /* Tick service routine used by the scheduler when cooperative scheduling is 
89         being used. */
90         static void __interrupt __far prvNonPreemptiveTick( void );
91 #endif
92 /* Trap routine used by taskYIELD() to manually cause a context switch. */
93 static void __interrupt __far prvYieldProcessor( void );
94
95 /* Set the tick frequency back so the floppy drive works correctly when the
96 scheduler exits. */
97 static void prvSetTickFrequencyDefault( void );
98
99 /*lint -e956 File scopes necessary here. */
100
101 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
102 static int16_t sDOSTickCounter;                                                 
103
104 /* Set true when the vectors are set so the scheduler will service the tick. */
105 static int16_t sSchedulerRunning = pdFALSE;                             
106
107 /* 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(). */
108 static void ( __interrupt __far *pxOldSwitchISR )();            
109
110 /* 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(). */
111 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();       
112
113 /* Used to restore the original DOS context when the scheduler is ended. */
114 static jmp_buf xJumpBuf;
115
116 /*lint +e956 */
117
118 /*-----------------------------------------------------------*/
119 BaseType_t xPortStartScheduler( void )
120 {
121 pxISR pxOriginalTickISR;
122         
123         /* This is called with interrupts already disabled. */
124
125         /* Remember what was on the interrupts we are going to use
126         so we can put them back later if required. */
127         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
128         pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
129         pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
130
131         prvSetTickFrequency( configTICK_RATE_HZ );
132
133         /* Put our manual switch (yield) function on a known
134         vector. */
135         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
136
137         /* Put the old tick on a different interrupt number so we can
138         call it when we want. */
139         _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
140
141         #if configUSE_PREEMPTION == 1
142         {               
143                 /* Put our tick switch function on the timer interrupt. */
144                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
145         }
146         #else
147         {
148                 /* We want the timer interrupt to just increment the tick count. */
149                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
150         }
151         #endif
152
153         /* Setup a counter that is used to call the DOS interrupt as close
154         to it's original frequency as can be achieved given our chosen tick
155         frequency. */
156         sDOSTickCounter = portTICKS_PER_DOS_TICK;
157
158         /* Clean up function if we want to return to DOS. */
159         if( setjmp( xJumpBuf ) != 0 )
160         {
161                 prvExitFunction();
162                 sSchedulerRunning = pdFALSE;
163         }
164         else
165         {
166                 sSchedulerRunning = pdTRUE;
167
168                 /* Kick off the scheduler by setting up the context of the first task. */
169                 portFIRST_CONTEXT();
170         }
171
172         return sSchedulerRunning;
173 }
174 /*-----------------------------------------------------------*/
175
176 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
177 is being used. */
178 #if configUSE_PREEMPTION == 1
179         /* Tick service routine used by the scheduler when preemptive scheduling is
180         being used. */
181         static void __interrupt __far prvPreemptiveTick( void )
182         {
183                 /* Get the scheduler to update the task states following the tick. */
184                 if( xTaskIncrementTick() != pdFALSE )
185                 {
186                         /* Switch in the context of the next task to be run. */
187                         portSWITCH_CONTEXT();
188                 }
189
190                 /* Reset the PIC ready for the next time. */
191                 prvPortResetPIC();
192         }
193 #else
194         static void __interrupt __far prvNonPreemptiveTick( void )
195         {
196                 /* Same as preemptive tick, but the cooperative scheduler is being used
197                 so we don't have to switch in the context of the next task. */
198                 xTaskIncrementTick();
199                 prvPortResetPIC();
200         }
201 #endif
202 /*-----------------------------------------------------------*/
203
204
205 static void __interrupt __far prvYieldProcessor( void )
206 {
207         /* Switch in the context of the next task to be run. */
208         portSWITCH_CONTEXT();
209 }
210 /*-----------------------------------------------------------*/
211
212 static void prvPortResetPIC( void )
213 {
214         /* We are going to call the DOS tick interrupt at as close a
215         frequency to the normal DOS tick as possible. */
216
217         /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
218         --sDOSTickCounter;
219         if( sDOSTickCounter <= 0 )
220         {
221                 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
222                 __asm{ int      portSWITCH_INT_NUMBER + 1 };             
223         }
224         else
225         {
226                 /* Reset the PIC as the DOS tick is not being called to
227                 do it. */
228                 __asm
229                 {
230                         mov     al, 20H
231                         out 20H, al
232                 };
233         }
234 }
235 /*-----------------------------------------------------------*/
236
237 void vPortEndScheduler( void )
238 {
239         /* Jump back to the processor state prior to starting the
240         scheduler.  This means we are not going to be using a
241         task stack frame so the task can be deleted. */
242         longjmp( xJumpBuf, 1 );
243 }
244 /*-----------------------------------------------------------*/
245
246 static void prvExitFunction( void )
247 {
248 void ( __interrupt __far *pxOriginalTickISR )();
249
250         /* Interrupts should be disabled here anyway - but no 
251         harm in making sure. */
252         portDISABLE_INTERRUPTS();
253         if( sSchedulerRunning == pdTRUE )
254         {
255                 /* Set the DOS tick back onto the timer ticker. */
256                 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
257                 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
258                 prvSetTickFrequencyDefault();
259
260                 /* Put back the switch interrupt routines that was in place
261                 before the scheduler started. */
262                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
263                 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
264         }
265         /* The tick timer is back how DOS wants it.  We can re-enable
266         interrupts without the scheduler being called. */
267         portENABLE_INTERRUPTS();
268 }
269 /*-----------------------------------------------------------*/
270
271 static void prvSetTickFrequency( uint32_t ulTickRateHz )
272 {
273 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
274 const uint16_t usPIT0 = ( uint16_t ) 0x40;
275 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180;
276 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
277 uint32_t ulOutput;
278
279         /* Setup the 8245 to tick at the wanted frequency. */
280         portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
281         ulOutput = ulPIT_CONST / ulTickRateHz;
282    
283         portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
284         ulOutput >>= 8;
285         portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
286 }
287 /*-----------------------------------------------------------*/
288
289 static void prvSetTickFrequencyDefault( void )
290 {
291 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
292 const uint16_t usPIT0 = ( uint16_t ) 0x40;
293 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
294
295         portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
296         portOUTPUT_BYTE( usPIT0,0 );
297         portOUTPUT_BYTE( usPIT0,0 );
298 }
299
300
301 /*lint +e950 */
302