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