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