2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
99 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
100 macro to be consistent with the later ports.
\r
102 Changes from V4.0.1
\r
104 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
105 its proper value when the scheduler exits.
\r
108 #include <stdlib.h>
\r
110 #include <setjmp.h>
\r
112 #include "FreeRTOS.h"
\r
114 #include "portasm.h"
\r
116 /*-----------------------------------------------------------
\r
117 * Implementation of functions defined in portable.h for the industrial
\r
119 *----------------------------------------------------------*/
\r
121 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
123 #define portTIMER_INT_NUMBER 0x08
\r
125 /* Setup hardware for required tick interrupt rate. */
\r
126 static void prvSetTickFrequency( uint32_t ulTickRateHz );
\r
128 /* Restore hardware to as it was prior to starting the scheduler. */
\r
129 static void prvExitFunction( void );
\r
131 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
132 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
134 static void prvPortResetPIC( void );
\r
136 /* The ISR used depends on whether the preemptive or cooperative
\r
137 scheduler is being used. */
\r
138 #if( configUSE_PREEMPTION == 1 )
\r
139 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
141 static void __interrupt __far prvPreemptiveTick( void );
\r
143 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
145 static void __interrupt __far prvNonPreemptiveTick( void );
\r
148 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
149 static void __interrupt __far prvYieldProcessor( void );
\r
151 /* Set the tick frequency back so the floppy drive works correctly when the
\r
152 scheduler exits. */
\r
153 static void prvSetTickFrequencyDefault( void );
\r
155 /*lint -e956 File scopes necessary here. */
\r
157 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
158 static int16_t sDOSTickCounter;
\r
160 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
161 static BaseType_t xSchedulerRunning = pdFALSE;
\r
163 /* 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
164 static void ( __interrupt __far *pxOldSwitchISR )();
\r
166 /* 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
167 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
169 /* Used to restore the original DOS context when the scheduler is ended. */
\r
170 static jmp_buf xJumpBuf;
\r
174 /*-----------------------------------------------------------*/
\r
175 BaseType_t xPortStartScheduler( void )
\r
177 pxISR pxOriginalTickISR;
\r
179 /* This is called with interrupts already disabled. */
\r
181 /* Remember what was on the interrupts we are going to use
\r
182 so we can put them back later if required. */
\r
183 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
184 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
185 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
187 prvSetTickFrequency( configTICK_RATE_HZ );
\r
189 /* Put our manual switch (yield) function on a known
\r
191 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
193 /* Put the old tick on a different interrupt number so we can
\r
194 call it when we want. */
\r
195 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
197 /* The ISR used depends on whether the preemptive or cooperative
\r
198 scheduler is being used. */
\r
199 #if( configUSE_PREEMPTION == 1 )
\r
201 /* Put our tick switch function on the timer interrupt. */
\r
202 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
206 /* We want the timer interrupt to just increment the tick count. */
\r
207 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
211 /* Setup a counter that is used to call the DOS interrupt as close
\r
212 to it's original frequency as can be achieved given our chosen tick
\r
214 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
216 /* Clean up function if we want to return to DOS. */
\r
217 if( setjmp( xJumpBuf ) != 0 )
\r
220 xSchedulerRunning = pdFALSE;
\r
224 xSchedulerRunning = pdTRUE;
\r
226 /* Kick off the scheduler by setting up the context of the first task. */
\r
227 portFIRST_CONTEXT();
\r
230 return xSchedulerRunning;
\r
232 /*-----------------------------------------------------------*/
\r
234 /* The ISR used depends on whether the preemptive or cooperative
\r
235 scheduler is being used. */
\r
236 #if( configUSE_PREEMPTION == 1 )
\r
237 static void __interrupt __far prvPreemptiveTick( void )
\r
239 /* Get the scheduler to update the task states following the tick. */
\r
240 if( xTaskIncrementTick() != pdFALSE )
\r
242 /* Switch in the context of the next task to be run. */
\r
243 portSWITCH_CONTEXT();
\r
246 /* Reset the PIC ready for the next time. */
\r
250 static void __interrupt __far prvNonPreemptiveTick( void )
\r
252 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
253 so we don't have to switch in the context of the next task. */
\r
254 xTaskIncrementTick();
\r
258 /*-----------------------------------------------------------*/
\r
260 static void __interrupt __far prvYieldProcessor( void )
\r
262 /* Switch in the context of the next task to be run. */
\r
263 portSWITCH_CONTEXT();
\r
265 /*-----------------------------------------------------------*/
\r
267 static void prvPortResetPIC( void )
\r
269 /* We are going to call the DOS tick interrupt at as close a
\r
270 frequency to the normal DOS tick as possible. */
\r
272 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
274 if( sDOSTickCounter <= 0 )
\r
276 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
\r
277 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
281 /* Reset the PIC as the DOS tick is not being called to
\r
290 /*-----------------------------------------------------------*/
\r
292 void vPortEndScheduler( void )
\r
294 /* Jump back to the processor state prior to starting the
\r
295 scheduler. This means we are not going to be using a
\r
296 task stack frame so the task can be deleted. */
\r
297 longjmp( xJumpBuf, 1 );
\r
299 /*-----------------------------------------------------------*/
\r
301 static void prvExitFunction( void )
\r
303 void ( __interrupt __far *pxOriginalTickISR )();
\r
305 /* Interrupts should be disabled here anyway - but no
\r
306 harm in making sure. */
\r
307 portDISABLE_INTERRUPTS();
\r
308 if( xSchedulerRunning == pdTRUE )
\r
310 /* Set the DOS tick back onto the timer ticker. */
\r
311 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
312 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
313 prvSetTickFrequencyDefault();
\r
315 /* Put back the switch interrupt routines that was in place
\r
316 before the scheduler started. */
\r
317 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
318 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
320 /* The tick timer is back how DOS wants it. We can re-enable
\r
321 interrupts without the scheduler being called. */
\r
322 portENABLE_INTERRUPTS();
\r
324 /*-----------------------------------------------------------*/
\r
326 static void prvSetTickFrequency( uint32_t ulTickRateHz )
\r
328 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
329 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
330 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180UL;
\r
331 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
334 /* Setup the 8245 to tick at the wanted frequency. */
\r
335 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
336 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
337 portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
\r
339 portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
\r
341 /*-----------------------------------------------------------*/
\r
343 static void prvSetTickFrequencyDefault( void )
\r
345 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
346 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
347 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
349 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
350 portOUTPUT_BYTE( usPIT0,0 );
\r
351 portOUTPUT_BYTE( usPIT0,0 );
\r