2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
69 + Call to taskYIELD() from within tick ISR has been replaced by the more
\r
70 efficient portSWITCH_CONTEXT().
\r
71 + ISR function definitions renamed to include the prv prefix.
\r
73 Changes from V1.2.0:
\r
75 + prvPortResetPIC() is now called last thing before the end of the
\r
76 preemptive tick routine.
\r
80 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
81 macro to be consistent with the later ports.
\r
85 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
86 its proper value when the scheduler exits.
\r
95 #include "FreeRTOS.h"
\r
97 #include "portasm.h"
\r
99 /*-----------------------------------------------------------
\r
100 * Implementation of functions defined in portable.h for the industrial
\r
102 *----------------------------------------------------------*/
\r
104 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
106 #define portTIMER_INT_NUMBER 0x08
\r
108 /* Setup hardware for required tick interrupt rate. */
\r
109 static void prvSetTickFrequency( uint32_t ulTickRateHz );
\r
111 /* Restore hardware to as it was prior to starting the scheduler. */
\r
112 static void prvExitFunction( void );
\r
114 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
115 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
117 static void prvPortResetPIC( void );
\r
119 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
121 #if configUSE_PREEMPTION == 1
\r
122 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
124 static void __interrupt __far prvPreemptiveTick( void );
\r
126 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
128 static void __interrupt __far prvNonPreemptiveTick( void );
\r
130 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
131 static void __interrupt __far prvYieldProcessor( void );
\r
133 /* Set the tick frequency back so the floppy drive works correctly when the
\r
134 scheduler exits. */
\r
135 static void prvSetTickFrequencyDefault( void );
\r
137 /*lint -e956 File scopes necessary here. */
\r
139 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
140 static int16_t sDOSTickCounter;
\r
142 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
143 static int16_t sSchedulerRunning = pdFALSE;
\r
145 /* 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
146 static void ( __interrupt __far *pxOldSwitchISR )();
\r
148 /* 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
149 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
151 /* Used to restore the original DOS context when the scheduler is ended. */
\r
152 static jmp_buf xJumpBuf;
\r
156 /*-----------------------------------------------------------*/
\r
157 BaseType_t xPortStartScheduler( void )
\r
159 pxISR pxOriginalTickISR;
\r
161 /* This is called with interrupts already disabled. */
\r
163 /* Remember what was on the interrupts we are going to use
\r
164 so we can put them back later if required. */
\r
165 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
166 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
167 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
169 prvSetTickFrequency( configTICK_RATE_HZ );
\r
171 /* Put our manual switch (yield) function on a known
\r
173 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
175 /* Put the old tick on a different interrupt number so we can
\r
176 call it when we want. */
\r
177 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
179 #if configUSE_PREEMPTION == 1
\r
181 /* Put our tick switch function on the timer interrupt. */
\r
182 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
186 /* We want the timer interrupt to just increment the tick count. */
\r
187 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
191 /* Setup a counter that is used to call the DOS interrupt as close
\r
192 to it's original frequency as can be achieved given our chosen tick
\r
194 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
196 /* Clean up function if we want to return to DOS. */
\r
197 if( setjmp( xJumpBuf ) != 0 )
\r
200 sSchedulerRunning = pdFALSE;
\r
204 sSchedulerRunning = pdTRUE;
\r
206 /* Kick off the scheduler by setting up the context of the first task. */
\r
207 portFIRST_CONTEXT();
\r
210 return sSchedulerRunning;
\r
212 /*-----------------------------------------------------------*/
\r
214 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
216 #if configUSE_PREEMPTION == 1
\r
217 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
219 static void __interrupt __far prvPreemptiveTick( void )
\r
221 /* Get the scheduler to update the task states following the tick. */
\r
222 if( xTaskIncrementTick() != pdFALSE )
\r
224 /* Switch in the context of the next task to be run. */
\r
225 portSWITCH_CONTEXT();
\r
228 /* Reset the PIC ready for the next time. */
\r
232 static void __interrupt __far prvNonPreemptiveTick( void )
\r
234 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
235 so we don't have to switch in the context of the next task. */
\r
236 xTaskIncrementTick();
\r
240 /*-----------------------------------------------------------*/
\r
243 static void __interrupt __far prvYieldProcessor( void )
\r
245 /* Switch in the context of the next task to be run. */
\r
246 portSWITCH_CONTEXT();
\r
248 /*-----------------------------------------------------------*/
\r
250 static void prvPortResetPIC( void )
\r
252 /* We are going to call the DOS tick interrupt at as close a
\r
253 frequency to the normal DOS tick as possible. */
\r
255 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
257 if( sDOSTickCounter <= 0 )
\r
259 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
\r
260 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
264 /* Reset the PIC as the DOS tick is not being called to
\r
273 /*-----------------------------------------------------------*/
\r
275 void vPortEndScheduler( void )
\r
277 /* Jump back to the processor state prior to starting the
\r
278 scheduler. This means we are not going to be using a
\r
279 task stack frame so the task can be deleted. */
\r
280 longjmp( xJumpBuf, 1 );
\r
282 /*-----------------------------------------------------------*/
\r
284 static void prvExitFunction( void )
\r
286 void ( __interrupt __far *pxOriginalTickISR )();
\r
288 /* Interrupts should be disabled here anyway - but no
\r
289 harm in making sure. */
\r
290 portDISABLE_INTERRUPTS();
\r
291 if( sSchedulerRunning == pdTRUE )
\r
293 /* Set the DOS tick back onto the timer ticker. */
\r
294 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
295 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
296 prvSetTickFrequencyDefault();
\r
298 /* Put back the switch interrupt routines that was in place
\r
299 before the scheduler started. */
\r
300 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
301 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
303 /* The tick timer is back how DOS wants it. We can re-enable
\r
304 interrupts without the scheduler being called. */
\r
305 portENABLE_INTERRUPTS();
\r
307 /*-----------------------------------------------------------*/
\r
309 static void prvSetTickFrequency( uint32_t ulTickRateHz )
\r
311 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
312 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
313 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180;
\r
314 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
317 /* Setup the 8245 to tick at the wanted frequency. */
\r
318 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
319 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
321 portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
\r
323 portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
\r
325 /*-----------------------------------------------------------*/
\r
327 static void prvSetTickFrequencyDefault( void )
\r
329 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
330 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
331 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
333 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
334 portOUTPUT_BYTE( usPIT0,0 );
\r
335 portOUTPUT_BYTE( usPIT0,0 );
\r