2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 + Call to taskYIELD() from within tick ISR has been replaced by the more
\r
71 efficient portSWITCH_CONTEXT().
\r
72 + ISR function definitions renamed to include the prv prefix.
\r
74 Changes from V1.2.0:
\r
76 + portRESET_PIC() is now called last thing before the end of the preemptive
\r
81 + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
\r
82 macro to be consistent with the later ports.
\r
85 /*-----------------------------------------------------------
\r
86 * Implementation of functions defined in portable.h for the Flashlite 186
\r
88 *----------------------------------------------------------*/
\r
95 #include "FreeRTOS.h"
\r
97 #include "portasm.h"
\r
99 /*lint -e950 Non ANSI reserved words okay in this file only. */
\r
101 #define portTIMER_EOI_TYPE ( 8 )
\r
102 #define portRESET_PIC() portOUTPUT_WORD( ( unsigned short ) 0xff22, portTIMER_EOI_TYPE )
\r
103 #define portTIMER_INT_NUMBER 0x12
\r
105 #define portTIMER_1_CONTROL_REGISTER ( ( unsigned short ) 0xff5e )
\r
106 #define portTIMER_0_CONTROL_REGISTER ( ( unsigned short ) 0xff56 )
\r
107 #define portTIMER_INTERRUPT_ENABLE ( ( unsigned short ) 0x2000 )
\r
109 /* Setup the hardware to generate the required tick frequency. */
\r
110 static void prvSetTickFrequency( unsigned long ulTickRateHz );
\r
112 /* Set the hardware back to the state as per before the scheduler started. */
\r
113 static void prvExitFunction( void );
\r
115 #if configUSE_PREEMPTION == 1
\r
116 /* Tick service routine used by the scheduler when preemptive scheduling is
\r
118 static void __interrupt __far prvPreemptiveTick( void );
\r
120 /* Tick service routine used by the scheduler when cooperative scheduling is
\r
122 static void __interrupt __far prvNonPreemptiveTick( void );
\r
125 /* Trap routine used by taskYIELD() to manually cause a context switch. */
\r
126 static void __interrupt __far prvYieldProcessor( void );
\r
128 /*lint -e956 File scopes necessary here. */
\r
130 /* Set true when the vectors are set so the scheduler will service the tick. */
\r
131 static short sSchedulerRunning = pdFALSE;
\r
133 /* 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
134 static void ( __interrupt __far *pxOldSwitchISR )();
\r
136 /* Used to restore the original DOS context when the scheduler is ended. */
\r
137 static jmp_buf xJumpBuf;
\r
141 /*-----------------------------------------------------------*/
\r
142 portBASE_TYPE xPortStartScheduler( void )
\r
144 /* This is called with interrupts already disabled. */
\r
146 /* Remember what was on the interrupts we are going to use
\r
147 so we can put them back later if required. */
\r
148 pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
\r
150 /* Put our manual switch (yield) function on a known
\r
152 _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
\r
154 #if configUSE_PREEMPTION == 1
\r
156 /* Put our tick switch function on the timer interrupt. */
\r
157 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
\r
161 /* We want the timer interrupt to just increment the tick count. */
\r
162 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
\r
166 prvSetTickFrequency( configTICK_RATE_HZ );
\r
168 /* Clean up function if we want to return to DOS. */
\r
169 if( setjmp( xJumpBuf ) != 0 )
\r
172 sSchedulerRunning = pdFALSE;
\r
176 sSchedulerRunning = pdTRUE;
\r
178 /* Kick off the scheduler by setting up the context of the first task. */
\r
179 portFIRST_CONTEXT();
\r
182 return sSchedulerRunning;
\r
184 /*-----------------------------------------------------------*/
\r
186 /* The tick ISR used depend on whether or not the preemptive or cooperative
\r
187 kernel is being used. */
\r
188 #if configUSE_PREEMPTION == 1
\r
189 static void __interrupt __far prvPreemptiveTick( void )
\r
191 /* Get the scheduler to update the task states following the tick. */
\r
192 vTaskIncrementTick();
\r
194 /* Switch in the context of the next task to be run. */
\r
195 portSWITCH_CONTEXT();
\r
197 /* Reset the PIC ready for the next time. */
\r
201 static void __interrupt __far prvNonPreemptiveTick( void )
\r
203 /* Same as preemptive tick, but the cooperative scheduler is being used
\r
204 so we don't have to switch in the context of the next task. */
\r
205 vTaskIncrementTick();
\r
209 /*-----------------------------------------------------------*/
\r
211 static void __interrupt __far prvYieldProcessor( void )
\r
213 /* Switch in the context of the next task to be run. */
\r
214 portSWITCH_CONTEXT();
\r
216 /*-----------------------------------------------------------*/
\r
218 void vPortEndScheduler( void )
\r
220 /* Jump back to the processor state prior to starting the
\r
221 scheduler. This means we are not going to be using a
\r
222 task stack frame so the task can be deleted. */
\r
223 longjmp( xJumpBuf, 1 );
\r
225 /*-----------------------------------------------------------*/
\r
227 static void prvExitFunction( void )
\r
229 const unsigned short usTimerDisable = 0x0000;
\r
230 unsigned short usTimer0Control;
\r
232 /* Interrupts should be disabled here anyway - but no
\r
233 harm in making sure. */
\r
234 portDISABLE_INTERRUPTS();
\r
235 if( sSchedulerRunning == pdTRUE )
\r
237 /* Put back the switch interrupt routines that was in place
\r
238 before the scheduler started. */
\r
239 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
\r
242 /* Disable the timer used for the tick to ensure the scheduler is
\r
243 not called before restoring interrupts. There was previously nothing
\r
244 on this timer so there is no old ISR to restore. */
\r
245 portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );
\r
247 /* Restart the DOS tick. */
\r
248 usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
\r
249 usTimer0Control |= portTIMER_INTERRUPT_ENABLE;
\r
250 portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
\r
253 portENABLE_INTERRUPTS();
\r
255 /*-----------------------------------------------------------*/
\r
257 static void prvSetTickFrequency( unsigned long ulTickRateHz )
\r
259 const unsigned short usMaxCountRegister = 0xff5a;
\r
260 const unsigned short usTimerPriorityRegister = 0xff32;
\r
261 const unsigned short usTimerEnable = 0xC000;
\r
262 const unsigned short usRetrigger = 0x0001;
\r
263 const unsigned short usTimerHighPriority = 0x0000;
\r
264 unsigned short usTimer0Control;
\r
266 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */
\r
268 const unsigned long ulClockFrequency = 0x7f31a0;
\r
270 unsigned long ulTimerCount = ulClockFrequency / ulTickRateHz;
\r
272 portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );
\r
273 portOUTPUT_WORD( usMaxCountRegister, ( unsigned short ) ulTimerCount );
\r
274 portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );
\r
276 /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */
\r
277 usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
\r
278 usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;
\r
279 portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
\r