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