2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
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.
37 + prvPortResetPIC() is now called last thing before the end of the
38 preemptive tick routine.
42 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
43 macro to be consistent with the later ports.
47 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
48 its proper value when the scheduler exits.
61 /*-----------------------------------------------------------
62 * Implementation of functions defined in portable.h for the industrial
64 *----------------------------------------------------------*/
66 /*lint -e950 Non ANSI reserved words okay in this file only. */
68 #define portTIMER_INT_NUMBER 0x08
70 /* Setup hardware for required tick interrupt rate. */
71 static void prvSetTickFrequency( uint32_t ulTickRateHz );
73 /* Restore hardware to as it was prior to starting the scheduler. */
74 static void prvExitFunction( void );
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
79 static void prvPortResetPIC( void );
81 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
83 #if configUSE_PREEMPTION == 1
84 /* Tick service routine used by the scheduler when preemptive scheduling is
86 static void __interrupt __far prvPreemptiveTick( void );
88 /* Tick service routine used by the scheduler when cooperative scheduling is
90 static void __interrupt __far prvNonPreemptiveTick( void );
92 /* Trap routine used by taskYIELD() to manually cause a context switch. */
93 static void __interrupt __far prvYieldProcessor( void );
95 /* Set the tick frequency back so the floppy drive works correctly when the
97 static void prvSetTickFrequencyDefault( void );
99 /*lint -e956 File scopes necessary here. */
101 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
102 static int16_t sDOSTickCounter;
104 /* Set true when the vectors are set so the scheduler will service the tick. */
105 static int16_t sSchedulerRunning = pdFALSE;
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 )();
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 )();
113 /* Used to restore the original DOS context when the scheduler is ended. */
114 static jmp_buf xJumpBuf;
118 /*-----------------------------------------------------------*/
119 BaseType_t xPortStartScheduler( void )
121 pxISR pxOriginalTickISR;
123 /* This is called with interrupts already disabled. */
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 );
131 prvSetTickFrequency( configTICK_RATE_HZ );
133 /* Put our manual switch (yield) function on a known
135 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
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 );
141 #if configUSE_PREEMPTION == 1
143 /* Put our tick switch function on the timer interrupt. */
144 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
148 /* We want the timer interrupt to just increment the tick count. */
149 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
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
156 sDOSTickCounter = portTICKS_PER_DOS_TICK;
158 /* Clean up function if we want to return to DOS. */
159 if( setjmp( xJumpBuf ) != 0 )
162 sSchedulerRunning = pdFALSE;
166 sSchedulerRunning = pdTRUE;
168 /* Kick off the scheduler by setting up the context of the first task. */
172 return sSchedulerRunning;
174 /*-----------------------------------------------------------*/
176 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
178 #if configUSE_PREEMPTION == 1
179 /* Tick service routine used by the scheduler when preemptive scheduling is
181 static void __interrupt __far prvPreemptiveTick( void )
183 /* Get the scheduler to update the task states following the tick. */
184 if( xTaskIncrementTick() != pdFALSE )
186 /* Switch in the context of the next task to be run. */
187 portSWITCH_CONTEXT();
190 /* Reset the PIC ready for the next time. */
194 static void __interrupt __far prvNonPreemptiveTick( void )
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();
202 /*-----------------------------------------------------------*/
205 static void __interrupt __far prvYieldProcessor( void )
207 /* Switch in the context of the next task to be run. */
208 portSWITCH_CONTEXT();
210 /*-----------------------------------------------------------*/
212 static void prvPortResetPIC( void )
214 /* We are going to call the DOS tick interrupt at as close a
215 frequency to the normal DOS tick as possible. */
217 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
219 if( sDOSTickCounter <= 0 )
221 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
222 __asm{ int portSWITCH_INT_NUMBER + 1 };
226 /* Reset the PIC as the DOS tick is not being called to
235 /*-----------------------------------------------------------*/
237 void vPortEndScheduler( void )
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 );
244 /*-----------------------------------------------------------*/
246 static void prvExitFunction( void )
248 void ( __interrupt __far *pxOriginalTickISR )();
250 /* Interrupts should be disabled here anyway - but no
251 harm in making sure. */
252 portDISABLE_INTERRUPTS();
253 if( sSchedulerRunning == pdTRUE )
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();
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 );
265 /* The tick timer is back how DOS wants it. We can re-enable
266 interrupts without the scheduler being called. */
267 portENABLE_INTERRUPTS();
269 /*-----------------------------------------------------------*/
271 static void prvSetTickFrequency( uint32_t ulTickRateHz )
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;
279 /* Setup the 8245 to tick at the wanted frequency. */
280 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
281 ulOutput = ulPIT_CONST / ulTickRateHz;
283 portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
285 portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
287 /*-----------------------------------------------------------*/
289 static void prvSetTickFrequencyDefault( void )
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;
295 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
296 portOUTPUT_BYTE( usPIT0,0 );
297 portOUTPUT_BYTE( usPIT0,0 );