2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * https://www.FreeRTOS.org
\r
23 * https://github.com/FreeRTOS
\r
30 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
31 macro to be consistent with the later ports.
\r
35 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
36 its proper value when the scheduler exits.
\r
43 #include "FreeRTOS.h"
\r
45 #include "portasm.h"
\r
47 /*-----------------------------------------------------------
\r
48 * Implementation of functions defined in portable.h for the industrial
\r
50 *----------------------------------------------------------*/
\r
52 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
54 #define portTIMER_INT_NUMBER 0x08
\r
56 /* Setup hardware for required tick interrupt rate. */
\r
57 static void prvSetTickFrequency( uint32_t ulTickRateHz );
\r
59 /* Restore hardware to as it was prior to starting the scheduler. */
\r
60 static void prvExitFunction( void );
\r
62 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
63 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
65 static void prvPortResetPIC( void );
\r
67 /* The ISR used depends on whether the preemptive or cooperative
\r
68 scheduler is being used. */
\r
69 #if( configUSE_PREEMPTION == 1 )
\r
70 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
72 static void __interrupt __far prvPreemptiveTick( void );
\r
74 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
76 static void __interrupt __far prvNonPreemptiveTick( void );
\r
79 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
80 static void __interrupt __far prvYieldProcessor( void );
\r
82 /* Set the tick frequency back so the floppy drive works correctly when the
\r
84 static void prvSetTickFrequencyDefault( void );
\r
86 /*lint -e956 File scopes necessary here. */
\r
88 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
89 static int16_t sDOSTickCounter;
\r
91 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
92 static BaseType_t xSchedulerRunning = pdFALSE;
\r
94 /* 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
95 static void ( __interrupt __far *pxOldSwitchISR )();
\r
97 /* 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
98 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
100 /* Used to restore the original DOS context when the scheduler is ended. */
\r
101 static jmp_buf xJumpBuf;
\r
105 /*-----------------------------------------------------------*/
\r
106 BaseType_t xPortStartScheduler( void )
\r
108 pxISR pxOriginalTickISR;
\r
110 /* This is called with interrupts already disabled. */
\r
112 /* Remember what was on the interrupts we are going to use
\r
113 so we can put them back later if required. */
\r
114 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
115 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
116 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
118 prvSetTickFrequency( configTICK_RATE_HZ );
\r
120 /* Put our manual switch (yield) function on a known
\r
122 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
124 /* Put the old tick on a different interrupt number so we can
\r
125 call it when we want. */
\r
126 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
128 /* The ISR used depends on whether the preemptive or cooperative
\r
129 scheduler is being used. */
\r
130 #if( configUSE_PREEMPTION == 1 )
\r
132 /* Put our tick switch function on the timer interrupt. */
\r
133 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
137 /* We want the timer interrupt to just increment the tick count. */
\r
138 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
142 /* Setup a counter that is used to call the DOS interrupt as close
\r
143 to it's original frequency as can be achieved given our chosen tick
\r
145 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
147 /* Clean up function if we want to return to DOS. */
\r
148 if( setjmp( xJumpBuf ) != 0 )
\r
151 xSchedulerRunning = pdFALSE;
\r
155 xSchedulerRunning = pdTRUE;
\r
157 /* Kick off the scheduler by setting up the context of the first task. */
\r
158 portFIRST_CONTEXT();
\r
161 return xSchedulerRunning;
\r
163 /*-----------------------------------------------------------*/
\r
165 /* The ISR used depends on whether the preemptive or cooperative
\r
166 scheduler is being used. */
\r
167 #if( configUSE_PREEMPTION == 1 )
\r
168 static void __interrupt __far prvPreemptiveTick( void )
\r
170 /* Get the scheduler to update the task states following the tick. */
\r
171 if( xTaskIncrementTick() != pdFALSE )
\r
173 /* Switch in the context of the next task to be run. */
\r
174 portSWITCH_CONTEXT();
\r
177 /* Reset the PIC ready for the next time. */
\r
181 static void __interrupt __far prvNonPreemptiveTick( void )
\r
183 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
184 so we don't have to switch in the context of the next task. */
\r
185 xTaskIncrementTick();
\r
189 /*-----------------------------------------------------------*/
\r
191 static void __interrupt __far prvYieldProcessor( void )
\r
193 /* Switch in the context of the next task to be run. */
\r
194 portSWITCH_CONTEXT();
\r
196 /*-----------------------------------------------------------*/
\r
198 static void prvPortResetPIC( void )
\r
200 /* We are going to call the DOS tick interrupt at as close a
\r
201 frequency to the normal DOS tick as possible. */
\r
203 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
205 if( sDOSTickCounter <= 0 )
\r
207 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
\r
208 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
212 /* Reset the PIC as the DOS tick is not being called to
\r
221 /*-----------------------------------------------------------*/
\r
223 void vPortEndScheduler( void )
\r
225 /* Jump back to the processor state prior to starting the
\r
226 scheduler. This means we are not going to be using a
\r
227 task stack frame so the task can be deleted. */
\r
228 longjmp( xJumpBuf, 1 );
\r
230 /*-----------------------------------------------------------*/
\r
232 static void prvExitFunction( void )
\r
234 void ( __interrupt __far *pxOriginalTickISR )();
\r
236 /* Interrupts should be disabled here anyway - but no
\r
237 harm in making sure. */
\r
238 portDISABLE_INTERRUPTS();
\r
239 if( xSchedulerRunning == pdTRUE )
\r
241 /* Set the DOS tick back onto the timer ticker. */
\r
242 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
243 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
244 prvSetTickFrequencyDefault();
\r
246 /* Put back the switch interrupt routines that was in place
\r
247 before the scheduler started. */
\r
248 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
249 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
251 /* The tick timer is back how DOS wants it. We can re-enable
\r
252 interrupts without the scheduler being called. */
\r
253 portENABLE_INTERRUPTS();
\r
255 /*-----------------------------------------------------------*/
\r
257 static void prvSetTickFrequency( uint32_t ulTickRateHz )
\r
259 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
260 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
261 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180UL;
\r
262 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
265 /* Setup the 8245 to tick at the wanted frequency. */
\r
266 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
267 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
268 portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
\r
270 portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
\r
272 /*-----------------------------------------------------------*/
\r
274 static void prvSetTickFrequencyDefault( void )
\r
276 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
277 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
278 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
280 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
281 portOUTPUT_BYTE( usPIT0,0 );
\r
282 portOUTPUT_BYTE( usPIT0,0 );
\r