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 + Call to taskYIELD() from within tick ISR has been replaced by the more
\r
100 efficient portSWITCH_CONTEXT().
\r
101 + ISR function definitions renamed to include the prv prefix.
\r
103 Changes from V1.2.0:
\r
105 + prvPortResetPIC() is now called last thing before the end of the
\r
106 preemptive tick routine.
\r
108 Changes from V2.6.1
\r
110 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
111 macro to be consistent with the later ports.
\r
113 Changes from V4.0.1
\r
115 + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
\r
116 its proper value when the scheduler exits.
\r
119 #include <stdlib.h>
\r
123 #include <setjmp.h>
\r
125 #include "FreeRTOS.h"
\r
127 #include "portasm.h"
\r
129 /*-----------------------------------------------------------
\r
130 * Implementation of functions defined in portable.h for the industrial
\r
132 *----------------------------------------------------------*/
\r
134 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
136 #define portTIMER_INT_NUMBER 0x08
\r
138 /* Setup hardware for required tick interrupt rate. */
\r
139 static void prvSetTickFrequency( uint32_t ulTickRateHz );
\r
141 /* Restore hardware to as it was prior to starting the scheduler. */
\r
142 static void prvExitFunction( void );
\r
144 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
\r
145 directly. We chain to the DOS tick as close as possible to the standard DOS
\r
147 static void prvPortResetPIC( void );
\r
149 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
151 #if configUSE_PREEMPTION == 1
\r
152 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
154 static void __interrupt __far prvPreemptiveTick( void );
\r
156 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
158 static void __interrupt __far prvNonPreemptiveTick( void );
\r
160 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
161 static void __interrupt __far prvYieldProcessor( void );
\r
163 /* Set the tick frequency back so the floppy drive works correctly when the
\r
164 scheduler exits. */
\r
165 static void prvSetTickFrequencyDefault( void );
\r
167 /*lint -e956 File scopes necessary here. */
\r
169 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
\r
170 static int16_t sDOSTickCounter;
\r
172 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
173 static int16_t sSchedulerRunning = pdFALSE;
\r
175 /* 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
176 static void ( __interrupt __far *pxOldSwitchISR )();
\r
178 /* 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
179 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
\r
181 /* Used to restore the original DOS context when the scheduler is ended. */
\r
182 static jmp_buf xJumpBuf;
\r
186 /*-----------------------------------------------------------*/
\r
187 BaseType_t xPortStartScheduler( void )
\r
189 pxISR pxOriginalTickISR;
\r
191 /* This is called with interrupts already disabled. */
\r
193 /* Remember what was on the interrupts we are going to use
\r
194 so we can put them back later if required. */
\r
195 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
196 pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
\r
197 pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
199 prvSetTickFrequency( configTICK_RATE_HZ );
\r
201 /* Put our manual switch (yield) function on a known
\r
203 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
205 /* Put the old tick on a different interrupt number so we can
\r
206 call it when we want. */
\r
207 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
\r
209 #if configUSE_PREEMPTION == 1
\r
211 /* Put our tick switch function on the timer interrupt. */
\r
212 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
216 /* We want the timer interrupt to just increment the tick count. */
\r
217 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
221 /* Setup a counter that is used to call the DOS interrupt as close
\r
222 to it's original frequency as can be achieved given our chosen tick
\r
224 sDOSTickCounter = portTICKS_PER_DOS_TICK;
\r
226 /* Clean up function if we want to return to DOS. */
\r
227 if( setjmp( xJumpBuf ) != 0 )
\r
230 sSchedulerRunning = pdFALSE;
\r
234 sSchedulerRunning = pdTRUE;
\r
236 /* Kick off the scheduler by setting up the context of the first task. */
\r
237 portFIRST_CONTEXT();
\r
240 return sSchedulerRunning;
\r
242 /*-----------------------------------------------------------*/
\r
244 /* The tick ISR used depends on whether the preemptive or cooperative scheduler
\r
246 #if configUSE_PREEMPTION == 1
\r
247 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
249 static void __interrupt __far prvPreemptiveTick( void )
\r
251 /* Get the scheduler to update the task states following the tick. */
\r
252 if( xTaskIncrementTick() != pdFALSE )
\r
254 /* Switch in the context of the next task to be run. */
\r
255 portSWITCH_CONTEXT();
\r
258 /* Reset the PIC ready for the next time. */
\r
262 static void __interrupt __far prvNonPreemptiveTick( void )
\r
264 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
265 so we don't have to switch in the context of the next task. */
\r
266 xTaskIncrementTick();
\r
270 /*-----------------------------------------------------------*/
\r
273 static void __interrupt __far prvYieldProcessor( void )
\r
275 /* Switch in the context of the next task to be run. */
\r
276 portSWITCH_CONTEXT();
\r
278 /*-----------------------------------------------------------*/
\r
280 static void prvPortResetPIC( void )
\r
282 /* We are going to call the DOS tick interrupt at as close a
\r
283 frequency to the normal DOS tick as possible. */
\r
285 /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
\r
287 if( sDOSTickCounter <= 0 )
\r
289 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
\r
290 __asm{ int portSWITCH_INT_NUMBER + 1 };
\r
294 /* Reset the PIC as the DOS tick is not being called to
\r
303 /*-----------------------------------------------------------*/
\r
305 void vPortEndScheduler( void )
\r
307 /* Jump back to the processor state prior to starting the
\r
308 scheduler. This means we are not going to be using a
\r
309 task stack frame so the task can be deleted. */
\r
310 longjmp( xJumpBuf, 1 );
\r
312 /*-----------------------------------------------------------*/
\r
314 static void prvExitFunction( void )
\r
316 void ( __interrupt __far *pxOriginalTickISR )();
\r
318 /* Interrupts should be disabled here anyway - but no
\r
319 harm in making sure. */
\r
320 portDISABLE_INTERRUPTS();
\r
321 if( sSchedulerRunning == pdTRUE )
\r
323 /* Set the DOS tick back onto the timer ticker. */
\r
324 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
\r
325 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
\r
326 prvSetTickFrequencyDefault();
\r
328 /* Put back the switch interrupt routines that was in place
\r
329 before the scheduler started. */
\r
330 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
331 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
\r
333 /* The tick timer is back how DOS wants it. We can re-enable
\r
334 interrupts without the scheduler being called. */
\r
335 portENABLE_INTERRUPTS();
\r
337 /*-----------------------------------------------------------*/
\r
339 static void prvSetTickFrequency( uint32_t ulTickRateHz )
\r
341 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
342 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
343 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180;
\r
344 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
347 /* Setup the 8245 to tick at the wanted frequency. */
\r
348 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
349 ulOutput = ulPIT_CONST / ulTickRateHz;
\r
351 portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
\r
353 portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
\r
355 /*-----------------------------------------------------------*/
\r
357 static void prvSetTickFrequencyDefault( void )
\r
359 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
\r
360 const uint16_t usPIT0 = ( uint16_t ) 0x40;
\r
361 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
\r
363 portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
\r
364 portOUTPUT_BYTE( usPIT0,0 );
\r
365 portOUTPUT_BYTE( usPIT0,0 );
\r