2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
32 + Call to taskYIELD() from within tick ISR has been replaced by the more
\r
33 efficient portSWITCH_CONTEXT().
\r
34 + ISR function definitions renamed to include the prv prefix.
\r
36 Changes from V1.2.0:
\r
38 + prvPortResetPIC() is now called last thing before the end of the
\r
39 preemptive tick routine.
\r
43 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
44 macro to be consistent with the later ports.
\r
48 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
49 its proper value when the scheduler exits.
\r
58 #include "FreeRTOS.h"
\r
60 #include "portasm.h"
\r
62 /*-----------------------------------------------------------
\r
63 * Implementation of functions defined in portable.h for the industrial
\r
65 *----------------------------------------------------------*/
\r
67 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
69 #define portTIMER_INT_NUMBER 0x08
\r
71 /* Setup hardware for required tick interrupt rate. */
\r
72 static void prvSetTickFrequency( uint32_t ulTickRateHz );
\r
74 /* Restore hardware to as it was prior to starting the scheduler. */
\r
75 static void prvExitFunction( void );
\r
77 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
78 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
80 static void prvPortResetPIC( void );
\r
82 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
84 #if configUSE_PREEMPTION == 1
\r
85 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
87 static void __interrupt __far prvPreemptiveTick( void );
\r
89 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
91 static void __interrupt __far prvNonPreemptiveTick( void );
\r
93 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
94 static void __interrupt __far prvYieldProcessor( void );
\r
96 /* Set the tick frequency back so the floppy drive works correctly when the
\r
98 static void prvSetTickFrequencyDefault( void );
\r
100 /*lint -e956 File scopes necessary here. */
\r
102 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
103 static int16_t sDOSTickCounter;
\r
105 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
106 static int16_t sSchedulerRunning = pdFALSE;
\r
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(). */
\r
109 static void ( __interrupt __far *pxOldSwitchISR )();
\r
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(). */
\r
112 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
114 /* Used to restore the original DOS context when the scheduler is ended. */
\r
115 static jmp_buf xJumpBuf;
\r
119 /*-----------------------------------------------------------*/
\r
120 BaseType_t xPortStartScheduler( void )
\r
122 pxISR pxOriginalTickISR;
\r
124 /* This is called with interrupts already disabled. */
\r
126 /* Remember what was on the interrupts we are going to use
\r
127 so we can put them back later if required. */
\r
128 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
129 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
130 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
132 prvSetTickFrequency( configTICK_RATE_HZ );
\r
134 /* Put our manual switch (yield) function on a known
\r
136 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
138 /* Put the old tick on a different interrupt number so we can
\r
139 call it when we want. */
\r
140 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
142 #if configUSE_PREEMPTION == 1
\r
144 /* Put our tick switch function on the timer interrupt. */
\r
145 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
149 /* We want the timer interrupt to just increment the tick count. */
\r
150 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
154 /* Setup a counter that is used to call the DOS interrupt as close
\r
155 to it's original frequency as can be achieved given our chosen tick
\r
157 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
159 /* Clean up function if we want to return to DOS. */
\r
160 if( setjmp( xJumpBuf ) != 0 )
\r
163 sSchedulerRunning = pdFALSE;
\r
167 sSchedulerRunning = pdTRUE;
\r
169 /* Kick off the scheduler by setting up the context of the first task. */
\r
170 portFIRST_CONTEXT();
\r
173 return sSchedulerRunning;
\r
175 /*-----------------------------------------------------------*/
\r
177 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
179 #if configUSE_PREEMPTION == 1
\r
180 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
182 static void __interrupt __far prvPreemptiveTick( void )
\r
184 /* Get the scheduler to update the task states following the tick. */
\r
185 if( xTaskIncrementTick() != pdFALSE )
\r
187 /* Switch in the context of the next task to be run. */
\r
188 portSWITCH_CONTEXT();
\r
191 /* Reset the PIC ready for the next time. */
\r
195 static void __interrupt __far prvNonPreemptiveTick( void )
\r
197 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
198 so we don't have to switch in the context of the next task. */
\r
199 xTaskIncrementTick();
\r
203 /*-----------------------------------------------------------*/
\r
206 static void __interrupt __far prvYieldProcessor( void )
\r
208 /* Switch in the context of the next task to be run. */
\r
209 portSWITCH_CONTEXT();
\r
211 /*-----------------------------------------------------------*/
\r
213 static void prvPortResetPIC( void )
\r
215 /* We are going to call the DOS tick interrupt at as close a
\r
216 frequency to the normal DOS tick as possible. */
\r
218 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
220 if( sDOSTickCounter <= 0 )
\r
222 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
\r
223 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
227 /* Reset the PIC as the DOS tick is not being called to
\r
236 /*-----------------------------------------------------------*/
\r
238 void vPortEndScheduler( void )
\r
240 /* Jump back to the processor state prior to starting the
\r
241 scheduler. This means we are not going to be using a
\r
242 task stack frame so the task can be deleted. */
\r
243 longjmp( xJumpBuf, 1 );
\r
245 /*-----------------------------------------------------------*/
\r
247 static void prvExitFunction( void )
\r
249 void ( __interrupt __far *pxOriginalTickISR )();
\r
251 /* Interrupts should be disabled here anyway - but no
\r
252 harm in making sure. */
\r
253 portDISABLE_INTERRUPTS();
\r
254 if( sSchedulerRunning == pdTRUE )
\r
256 /* Set the DOS tick back onto the timer ticker. */
\r
257 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
258 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
259 prvSetTickFrequencyDefault();
\r
261 /* Put back the switch interrupt routines that was in place
\r
262 before the scheduler started. */
\r
263 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
264 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
266 /* The tick timer is back how DOS wants it. We can re-enable
\r
267 interrupts without the scheduler being called. */
\r
268 portENABLE_INTERRUPTS();
\r
270 /*-----------------------------------------------------------*/
\r
272 static void prvSetTickFrequency( uint32_t ulTickRateHz )
\r
274 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
275 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
276 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180;
\r
277 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
280 /* Setup the 8245 to tick at the wanted frequency. */
\r
281 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
282 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
284 portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
\r
286 portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
\r
288 /*-----------------------------------------------------------*/
\r
290 static void prvSetTickFrequencyDefault( void )
\r
292 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
293 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
294 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
296 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
297 portOUTPUT_BYTE( usPIT0,0 );
\r
298 portOUTPUT_BYTE( usPIT0,0 );
\r