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 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
33 macro to be consistent with the later ports.
\r
37 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
38 its proper value when the scheduler exits.
\r
45 #include "FreeRTOS.h"
\r
47 #include "portasm.h"
\r
49 /*-----------------------------------------------------------
\r
50 * Implementation of functions defined in portable.h for the industrial
\r
52 *----------------------------------------------------------*/
\r
54 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
56 #define portTIMER_INT_NUMBER 0x08
\r
58 /* Setup hardware for required tick interrupt rate. */
\r
59 static void prvSetTickFrequency( uint32_t ulTickRateHz );
\r
61 /* Restore hardware to as it was prior to starting the scheduler. */
\r
62 static void prvExitFunction( void );
\r
64 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
65 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
67 static void prvPortResetPIC( void );
\r
69 /* The ISR used depends on whether the preemptive or cooperative
\r
70 scheduler is being used. */
\r
71 #if( configUSE_PREEMPTION == 1 )
\r
72 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
74 static void __interrupt __far prvPreemptiveTick( void );
\r
76 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
78 static void __interrupt __far prvNonPreemptiveTick( void );
\r
81 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
82 static void __interrupt __far prvYieldProcessor( void );
\r
84 /* Set the tick frequency back so the floppy drive works correctly when the
\r
86 static void prvSetTickFrequencyDefault( void );
\r
88 /*lint -e956 File scopes necessary here. */
\r
90 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
91 static int16_t sDOSTickCounter;
\r
93 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
94 static BaseType_t xSchedulerRunning = pdFALSE;
\r
96 /* 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
97 static void ( __interrupt __far *pxOldSwitchISR )();
\r
99 /* 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
100 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
102 /* Used to restore the original DOS context when the scheduler is ended. */
\r
103 static jmp_buf xJumpBuf;
\r
107 /*-----------------------------------------------------------*/
\r
108 BaseType_t xPortStartScheduler( void )
\r
110 pxISR pxOriginalTickISR;
\r
112 /* This is called with interrupts already disabled. */
\r
114 /* Remember what was on the interrupts we are going to use
\r
115 so we can put them back later if required. */
\r
116 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
117 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
118 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
120 prvSetTickFrequency( configTICK_RATE_HZ );
\r
122 /* Put our manual switch (yield) function on a known
\r
124 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
126 /* Put the old tick on a different interrupt number so we can
\r
127 call it when we want. */
\r
128 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
130 /* The ISR used depends on whether the preemptive or cooperative
\r
131 scheduler is being used. */
\r
132 #if( configUSE_PREEMPTION == 1 )
\r
134 /* Put our tick switch function on the timer interrupt. */
\r
135 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
139 /* We want the timer interrupt to just increment the tick count. */
\r
140 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
144 /* Setup a counter that is used to call the DOS interrupt as close
\r
145 to it's original frequency as can be achieved given our chosen tick
\r
147 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
149 /* Clean up function if we want to return to DOS. */
\r
150 if( setjmp( xJumpBuf ) != 0 )
\r
153 xSchedulerRunning = pdFALSE;
\r
157 xSchedulerRunning = pdTRUE;
\r
159 /* Kick off the scheduler by setting up the context of the first task. */
\r
160 portFIRST_CONTEXT();
\r
163 return xSchedulerRunning;
\r
165 /*-----------------------------------------------------------*/
\r
167 /* The ISR used depends on whether the preemptive or cooperative
\r
168 scheduler is being used. */
\r
169 #if( configUSE_PREEMPTION == 1 )
\r
170 static void __interrupt __far prvPreemptiveTick( void )
\r
172 /* Get the scheduler to update the task states following the tick. */
\r
173 if( xTaskIncrementTick() != pdFALSE )
\r
175 /* Switch in the context of the next task to be run. */
\r
176 portSWITCH_CONTEXT();
\r
179 /* Reset the PIC ready for the next time. */
\r
183 static void __interrupt __far prvNonPreemptiveTick( void )
\r
185 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
186 so we don't have to switch in the context of the next task. */
\r
187 xTaskIncrementTick();
\r
191 /*-----------------------------------------------------------*/
\r
193 static void __interrupt __far prvYieldProcessor( void )
\r
195 /* Switch in the context of the next task to be run. */
\r
196 portSWITCH_CONTEXT();
\r
198 /*-----------------------------------------------------------*/
\r
200 static void prvPortResetPIC( void )
\r
202 /* We are going to call the DOS tick interrupt at as close a
\r
203 frequency to the normal DOS tick as possible. */
\r
205 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
207 if( sDOSTickCounter <= 0 )
\r
209 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
\r
210 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
214 /* Reset the PIC as the DOS tick is not being called to
\r
223 /*-----------------------------------------------------------*/
\r
225 void vPortEndScheduler( void )
\r
227 /* Jump back to the processor state prior to starting the
\r
228 scheduler. This means we are not going to be using a
\r
229 task stack frame so the task can be deleted. */
\r
230 longjmp( xJumpBuf, 1 );
\r
232 /*-----------------------------------------------------------*/
\r
234 static void prvExitFunction( void )
\r
236 void ( __interrupt __far *pxOriginalTickISR )();
\r
238 /* Interrupts should be disabled here anyway - but no
\r
239 harm in making sure. */
\r
240 portDISABLE_INTERRUPTS();
\r
241 if( xSchedulerRunning == pdTRUE )
\r
243 /* Set the DOS tick back onto the timer ticker. */
\r
244 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
245 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
246 prvSetTickFrequencyDefault();
\r
248 /* Put back the switch interrupt routines that was in place
\r
249 before the scheduler started. */
\r
250 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
251 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
253 /* The tick timer is back how DOS wants it. We can re-enable
\r
254 interrupts without the scheduler being called. */
\r
255 portENABLE_INTERRUPTS();
\r
257 /*-----------------------------------------------------------*/
\r
259 static void prvSetTickFrequency( uint32_t ulTickRateHz )
\r
261 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
262 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
263 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180UL;
\r
264 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
267 /* Setup the 8245 to tick at the wanted frequency. */
\r
268 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
269 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
270 portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
\r
272 portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
\r
274 /*-----------------------------------------------------------*/
\r
276 static void prvSetTickFrequencyDefault( void )
\r
278 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
279 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
280 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
282 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
283 portOUTPUT_BYTE( usPIT0,0 );
\r
284 portOUTPUT_BYTE( usPIT0,0 );
\r