2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
31 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
32 macro to be consistent with the later ports.
\r
36 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
37 its proper value when the scheduler exits.
\r
44 #include "FreeRTOS.h"
\r
46 #include "portasm.h"
\r
48 /*-----------------------------------------------------------
\r
49 * Implementation of functions defined in portable.h for the industrial
\r
51 *----------------------------------------------------------*/
\r
53 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
55 #define portTIMER_INT_NUMBER 0x08
\r
57 /* Setup hardware for required tick interrupt rate. */
\r
58 static void prvSetTickFrequency( uint32_t ulTickRateHz );
\r
60 /* Restore hardware to as it was prior to starting the scheduler. */
\r
61 static void prvExitFunction( void );
\r
63 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
64 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
66 static void prvPortResetPIC( void );
\r
68 /* The ISR used depends on whether the preemptive or cooperative
\r
69 scheduler is being used. */
\r
70 #if( configUSE_PREEMPTION == 1 )
\r
71 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
73 static void __interrupt __far prvPreemptiveTick( void );
\r
75 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
77 static void __interrupt __far prvNonPreemptiveTick( void );
\r
80 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
81 static void __interrupt __far prvYieldProcessor( void );
\r
83 /* Set the tick frequency back so the floppy drive works correctly when the
\r
85 static void prvSetTickFrequencyDefault( void );
\r
87 /*lint -e956 File scopes necessary here. */
\r
89 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
90 static int16_t sDOSTickCounter;
\r
92 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
93 static BaseType_t xSchedulerRunning = pdFALSE;
\r
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(). */
\r
96 static void ( __interrupt __far *pxOldSwitchISR )();
\r
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(). */
\r
99 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
101 /* Used to restore the original DOS context when the scheduler is ended. */
\r
102 static jmp_buf xJumpBuf;
\r
106 /*-----------------------------------------------------------*/
\r
107 BaseType_t xPortStartScheduler( void )
\r
109 pxISR pxOriginalTickISR;
\r
111 /* This is called with interrupts already disabled. */
\r
113 /* Remember what was on the interrupts we are going to use
\r
114 so we can put them back later if required. */
\r
115 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
116 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
117 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
119 prvSetTickFrequency( configTICK_RATE_HZ );
\r
121 /* Put our manual switch (yield) function on a known
\r
123 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
125 /* Put the old tick on a different interrupt number so we can
\r
126 call it when we want. */
\r
127 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
129 /* The ISR used depends on whether the preemptive or cooperative
\r
130 scheduler is being used. */
\r
131 #if( configUSE_PREEMPTION == 1 )
\r
133 /* Put our tick switch function on the timer interrupt. */
\r
134 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
138 /* We want the timer interrupt to just increment the tick count. */
\r
139 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
143 /* Setup a counter that is used to call the DOS interrupt as close
\r
144 to it's original frequency as can be achieved given our chosen tick
\r
146 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
148 /* Clean up function if we want to return to DOS. */
\r
149 if( setjmp( xJumpBuf ) != 0 )
\r
152 xSchedulerRunning = pdFALSE;
\r
156 xSchedulerRunning = pdTRUE;
\r
158 /* Kick off the scheduler by setting up the context of the first task. */
\r
159 portFIRST_CONTEXT();
\r
162 return xSchedulerRunning;
\r
164 /*-----------------------------------------------------------*/
\r
166 /* The ISR used depends on whether the preemptive or cooperative
\r
167 scheduler is being used. */
\r
168 #if( configUSE_PREEMPTION == 1 )
\r
169 static void __interrupt __far prvPreemptiveTick( void )
\r
171 /* Get the scheduler to update the task states following the tick. */
\r
172 if( xTaskIncrementTick() != pdFALSE )
\r
174 /* Switch in the context of the next task to be run. */
\r
175 portSWITCH_CONTEXT();
\r
178 /* Reset the PIC ready for the next time. */
\r
182 static void __interrupt __far prvNonPreemptiveTick( void )
\r
184 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
185 so we don't have to switch in the context of the next task. */
\r
186 xTaskIncrementTick();
\r
190 /*-----------------------------------------------------------*/
\r
192 static void __interrupt __far prvYieldProcessor( void )
\r
194 /* Switch in the context of the next task to be run. */
\r
195 portSWITCH_CONTEXT();
\r
197 /*-----------------------------------------------------------*/
\r
199 static void prvPortResetPIC( void )
\r
201 /* We are going to call the DOS tick interrupt at as close a
\r
202 frequency to the normal DOS tick as possible. */
\r
204 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
206 if( sDOSTickCounter <= 0 )
\r
208 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
\r
209 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
213 /* Reset the PIC as the DOS tick is not being called to
\r
222 /*-----------------------------------------------------------*/
\r
224 void vPortEndScheduler( void )
\r
226 /* Jump back to the processor state prior to starting the
\r
227 scheduler. This means we are not going to be using a
\r
228 task stack frame so the task can be deleted. */
\r
229 longjmp( xJumpBuf, 1 );
\r
231 /*-----------------------------------------------------------*/
\r
233 static void prvExitFunction( void )
\r
235 void ( __interrupt __far *pxOriginalTickISR )();
\r
237 /* Interrupts should be disabled here anyway - but no
\r
238 harm in making sure. */
\r
239 portDISABLE_INTERRUPTS();
\r
240 if( xSchedulerRunning == pdTRUE )
\r
242 /* Set the DOS tick back onto the timer ticker. */
\r
243 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
244 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
245 prvSetTickFrequencyDefault();
\r
247 /* Put back the switch interrupt routines that was in place
\r
248 before the scheduler started. */
\r
249 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
250 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
252 /* The tick timer is back how DOS wants it. We can re-enable
\r
253 interrupts without the scheduler being called. */
\r
254 portENABLE_INTERRUPTS();
\r
256 /*-----------------------------------------------------------*/
\r
258 static void prvSetTickFrequency( uint32_t ulTickRateHz )
\r
260 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
261 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
262 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180UL;
\r
263 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
266 /* Setup the 8245 to tick at the wanted frequency. */
\r
267 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
268 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
269 portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
\r
271 portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
\r
273 /*-----------------------------------------------------------*/
\r
275 static void prvSetTickFrequencyDefault( void )
\r
277 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
278 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
279 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
281 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
282 portOUTPUT_BYTE( usPIT0,0 );
\r
283 portOUTPUT_BYTE( usPIT0,0 );
\r