]> git.sur5r.net Git - freertos/blob - Source/portable/BCC/16BitDOS/Flsh186/port.c
Update version number to V4.1.1.
[freertos] / Source / portable / BCC / 16BitDOS / Flsh186 / port.c
1 /*\r
2         FreeRTOS.org V4.1.1 - Copyright (C) 2003-2006 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 /*\r
34 Changes from V1.00:\r
35 \r
36         + Call to taskYIELD() from within tick ISR has been replaced by the more\r
37           efficient portSWITCH_CONTEXT().\r
38         + ISR function definitions renamed to include the prv prefix.\r
39 \r
40 Changes from V2.6.1\r
41 \r
42         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
43           macro to be consistent with the later ports.\r
44 */\r
45 \r
46 /*-----------------------------------------------------------\r
47  * Implementation of functions defined in portable.h for the Flashlite 186\r
48  * port.\r
49  *----------------------------------------------------------*/\r
50 \r
51 #include <dos.h>\r
52 #include <stdlib.h>\r
53 #include <setjmp.h>\r
54 \r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 #include "portasm.h"\r
58 \r
59 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
60 \r
61 #define portTIMER_EOI_TYPE              ( 8 )\r
62 #define portRESET_PIC()                 portOUTPUT_WORD( ( unsigned portSHORT ) 0xff22, portTIMER_EOI_TYPE )\r
63 #define portTIMER_INT_NUMBER    0x12\r
64 \r
65 #define portTIMER_1_CONTROL_REGISTER    ( ( unsigned portSHORT ) 0xff5e )\r
66 #define portTIMER_0_CONTROL_REGISTER    ( ( unsigned portSHORT ) 0xff56 )\r
67 #define portTIMER_INTERRUPT_ENABLE              ( ( unsigned portSHORT ) 0x2000 )\r
68 \r
69 /* Setup the hardware to generate the required tick frequency. */\r
70 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz );\r
71 \r
72 /* Set the hardware back to the state as per before the scheduler started. */\r
73 static void prvExitFunction( void );\r
74 \r
75 /* The ISR used depends on whether the preemptive or cooperative scheduler\r
76 is being used. */\r
77 #if( configUSE_PREEMPTION == 1 )\r
78         /* Tick service routine used by the scheduler when preemptive scheduling is\r
79         being used. */\r
80         static void __interrupt __far prvPreemptiveTick( void );\r
81 #else\r
82         /* Tick service routine used by the scheduler when cooperative scheduling is\r
83         being used. */\r
84         static void __interrupt __far prvNonPreemptiveTick( void );\r
85 #endif\r
86 \r
87 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
88 static void __interrupt __far prvYieldProcessor( void );\r
89 \r
90 /*lint -e956 File scopes necessary here. */\r
91 \r
92 /* Set true when the vectors are set so the scheduler will service the tick. */\r
93 static portBASE_TYPE xSchedulerRunning = pdFALSE;\r
94 \r
95 /* Points to the original routine installed on the vector we use for manual\r
96 context switches.  This is then used to restore the original routine during\r
97 prvExitFunction(). */\r
98 static void ( __interrupt __far *pxOldSwitchISR )();\r
99 \r
100 /* Used to restore the original DOS context when the scheduler is ended. */\r
101 static jmp_buf xJumpBuf;\r
102 \r
103 /*lint +e956 */\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 portBASE_TYPE xPortStartScheduler( void )\r
107 {\r
108         /* This is called with interrupts already disabled. */\r
109 \r
110         /* Remember what was on the interrupts we are going to use\r
111         so we can put them back later if required. */\r
112         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
113 \r
114         /* Put our manual switch (yield) function on a known\r
115         vector. */\r
116         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
117 \r
118         #if( configUSE_PREEMPTION == 1 )\r
119         {\r
120                 /* Put our tick switch function on the timer interrupt. */\r
121                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );\r
122         }\r
123         #else\r
124         {\r
125                 /* We want the timer interrupt to just increment the tick count. */\r
126                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );\r
127         }\r
128         #endif\r
129 \r
130         prvSetTickFrequency( configTICK_RATE_HZ );\r
131 \r
132         /* Clean up function if we want to return to DOS. */\r
133         if( setjmp( xJumpBuf ) != 0 )\r
134         {\r
135                 prvExitFunction();\r
136                 xSchedulerRunning = pdFALSE;\r
137         }\r
138         else\r
139         {\r
140                 xSchedulerRunning = pdTRUE;\r
141 \r
142                 /* Kick off the scheduler by setting up the context of the first task. */\r
143                 portFIRST_CONTEXT();\r
144         }\r
145 \r
146         return xSchedulerRunning;\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /* The ISR used depends on whether the preemptive or cooperative scheduler\r
151 is being used. */\r
152 #if( configUSE_PREEMPTION == 1 )\r
153         static void __interrupt __far prvPreemptiveTick( void )\r
154         {\r
155                 /* Get the scheduler to update the task states following the tick. */\r
156                 vTaskIncrementTick();\r
157 \r
158                 /* Switch in the context of the next task to be run. */\r
159                 portSWITCH_CONTEXT();\r
160 \r
161                 /* Reset the PIC ready for the next time. */\r
162                 portRESET_PIC();\r
163         }\r
164 #else\r
165         static void __interrupt __far prvNonPreemptiveTick( void )\r
166         {\r
167                 /* Same as preemptive tick, but the cooperative scheduler is being used\r
168                 so we don't have to switch in the context of the next task. */\r
169                 vTaskIncrementTick();\r
170                 portRESET_PIC();\r
171         }\r
172 #endif\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 static void __interrupt __far prvYieldProcessor( void )\r
176 {\r
177         /* Switch in the context of the next task to be run. */\r
178         portSWITCH_CONTEXT();\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vPortEndScheduler( void )\r
183 {\r
184         /* Jump back to the processor state prior to starting the\r
185         scheduler.  This means we are not going to be using a\r
186         task stack frame so the task can be deleted. */\r
187         longjmp( xJumpBuf, 1 );\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvExitFunction( void )\r
192 {\r
193 const unsigned portSHORT usTimerDisable = 0x0000;\r
194 unsigned portSHORT usTimer0Control;\r
195 \r
196         /* Interrupts should be disabled here anyway - but no\r
197         harm in making sure. */\r
198         portDISABLE_INTERRUPTS();\r
199         if( xSchedulerRunning == pdTRUE )\r
200         {\r
201                 /* Put back the switch interrupt routines that was in place\r
202                 before the scheduler started. */\r
203                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );\r
204         }\r
205 \r
206         /* Disable the timer used for the tick to ensure the scheduler is\r
207         not called before restoring interrupts.  There was previously nothing\r
208         on this timer so there is no old ISR to restore. */\r
209         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );\r
210 \r
211         /* Restart the DOS tick. */\r
212         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
213         usTimer0Control |= portTIMER_INTERRUPT_ENABLE;\r
214         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
215 \r
216 \r
217         portENABLE_INTERRUPTS();\r
218 \r
219         /* This will free up all the memory used by the scheduler.\r
220         exiting back to dos with INT21 AH=4CH will do this anyway so\r
221         it is not necessary to call this. */\r
222         vTaskCleanUpResources();\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz )\r
227 {\r
228 const unsigned portSHORT usMaxCountRegister = 0xff5a;\r
229 const unsigned portSHORT usTimerPriorityRegister = 0xff32;\r
230 const unsigned portSHORT usTimerEnable = 0xC000;\r
231 const unsigned portSHORT usRetrigger = 0x0001;\r
232 const unsigned portSHORT usTimerHighPriority = 0x0000;\r
233 unsigned portSHORT usTimer0Control;\r
234 \r
235 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */\r
236 \r
237 const unsigned portLONG ulClockFrequency = ( unsigned portLONG ) 0x7f31a0UL;\r
238 \r
239 unsigned portLONG ulTimerCount = ulClockFrequency / ulTickRateHz;\r
240 \r
241         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );\r
242         portOUTPUT_WORD( usMaxCountRegister, ( unsigned portSHORT ) ulTimerCount );\r
243         portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );\r
244 \r
245         /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */\r
246         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
247         usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;\r
248         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
249 }\r
250 \r
251 \r
252 /*lint +e950 */\r
253 \r