]> begriffs open source - freertos/blob - 20080217/Source/portable/oWatcom/16BitDOS/Flsh186/port.c
Prepare Fujitsu ports for release.
[freertos] / 20080217 / Source / portable / oWatcom / 16BitDOS / Flsh186 / port.c
1 /*\r
2         FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 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 \r
28         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 /*\r
44 Changes from V1.00:\r
45         \r
46         + Call to taskYIELD() from within tick ISR has been replaced by the more\r
47           efficient portSWITCH_CONTEXT().\r
48         + ISR function definitions renamed to include the prv prefix.\r
49 \r
50 Changes from V1.2.0:\r
51 \r
52         + portRESET_PIC() is now called last thing before the end of the preemptive\r
53           tick routine.\r
54 \r
55 Changes from V2.6.1\r
56 \r
57         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
58           macro to be consistent with the later ports.\r
59 */\r
60 \r
61 /*-----------------------------------------------------------\r
62  * Implementation of functions defined in portable.h for the Flashlite 186\r
63  * port.\r
64  *----------------------------------------------------------*/\r
65 \r
66 #include <stdlib.h>\r
67 #include <i86.h>\r
68 #include <dos.h>\r
69 #include <setjmp.h>\r
70 \r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 #include "portasm.h"\r
74 \r
75 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
76 \r
77 #define portTIMER_EOI_TYPE              ( 8 )\r
78 #define portRESET_PIC()                 portOUTPUT_WORD( ( unsigned portSHORT ) 0xff22, portTIMER_EOI_TYPE )\r
79 #define portTIMER_INT_NUMBER    0x12\r
80 \r
81 #define portTIMER_1_CONTROL_REGISTER    ( ( unsigned portSHORT ) 0xff5e )\r
82 #define portTIMER_0_CONTROL_REGISTER    ( ( unsigned portSHORT ) 0xff56 )\r
83 #define portTIMER_INTERRUPT_ENABLE              ( ( unsigned portSHORT ) 0x2000 )\r
84 \r
85 /* Setup the hardware to generate the required tick frequency. */\r
86 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz );\r
87 \r
88 /* Set the hardware back to the state as per before the scheduler started. */\r
89 static void prvExitFunction( void );\r
90 \r
91 #if configUSE_PREEMPTION == 1\r
92         /* Tick service routine used by the scheduler when preemptive scheduling is\r
93         being used. */\r
94         static void __interrupt __far prvPreemptiveTick( void );\r
95 #else\r
96         /* Tick service routine used by the scheduler when cooperative scheduling is \r
97         being used. */\r
98         static void __interrupt __far prvNonPreemptiveTick( void );\r
99 #endif\r
100 \r
101 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
102 static void __interrupt __far prvYieldProcessor( void );\r
103 \r
104 /*lint -e956 File scopes necessary here. */\r
105 \r
106 /* Set true when the vectors are set so the scheduler will service the tick. */\r
107 static portSHORT sSchedulerRunning = pdFALSE;\r
108 \r
109 /* 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
110 static void ( __interrupt __far *pxOldSwitchISR )();\r
111 \r
112 /* Used to restore the original DOS context when the scheduler is ended. */\r
113 static jmp_buf xJumpBuf;\r
114 \r
115 /*lint +e956 */\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 portBASE_TYPE xPortStartScheduler( void )\r
119 {\r
120         /* This is called with interrupts already disabled. */\r
121 \r
122         /* Remember what was on the interrupts we are going to use\r
123         so we can put them back later if required. */\r
124         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
125 \r
126         /* Put our manual switch (yield) function on a known\r
127         vector. */\r
128         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
129 \r
130         #if configUSE_PREEMPTION == 1\r
131         {               \r
132                 /* Put our tick switch function on the timer interrupt. */\r
133                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );\r
134         }\r
135         #else\r
136         {\r
137                 /* We want the timer interrupt to just increment the tick count. */\r
138                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );\r
139         }\r
140         #endif\r
141 \r
142         prvSetTickFrequency( configTICK_RATE_HZ );\r
143 \r
144         /* Clean up function if we want to return to DOS. */\r
145         if( setjmp( xJumpBuf ) != 0 )\r
146         {\r
147                 prvExitFunction();\r
148                 sSchedulerRunning = pdFALSE;\r
149         }\r
150         else\r
151         {\r
152                 sSchedulerRunning = pdTRUE;\r
153 \r
154                 /* Kick off the scheduler by setting up the context of the first task. */\r
155                 portFIRST_CONTEXT();\r
156         }\r
157 \r
158         return sSchedulerRunning;\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 /* The tick ISR used depend on whether or not the preemptive or cooperative\r
163 kernel is being used. */\r
164 #if configUSE_PREEMPTION == 1\r
165         static void __interrupt __far prvPreemptiveTick( void )\r
166         {\r
167                 /* Get the scheduler to update the task states following the tick. */\r
168                 vTaskIncrementTick();\r
169 \r
170                 /* Switch in the context of the next task to be run. */\r
171                 portSWITCH_CONTEXT();\r
172 \r
173                 /* Reset the PIC ready for the next time. */\r
174                 portRESET_PIC();\r
175         }\r
176 #else\r
177         static void __interrupt __far prvNonPreemptiveTick( void )\r
178         {\r
179                 /* Same as preemptive tick, but the cooperative scheduler is being used\r
180                 so we don't have to switch in the context of the next task. */\r
181                 vTaskIncrementTick();\r
182                 portRESET_PIC();\r
183         }\r
184 #endif\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static void __interrupt __far prvYieldProcessor( void )\r
188 {\r
189         /* Switch in the context of the next task to be run. */\r
190         portSWITCH_CONTEXT();\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void vPortEndScheduler( void )\r
195 {\r
196         /* Jump back to the processor state prior to starting the\r
197         scheduler.  This means we are not going to be using a\r
198         task stack frame so the task can be deleted. */\r
199         longjmp( xJumpBuf, 1 );\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 static void prvExitFunction( void )\r
204 {\r
205 const unsigned portSHORT usTimerDisable = 0x0000;\r
206 unsigned portSHORT usTimer0Control;\r
207 \r
208         /* Interrupts should be disabled here anyway - but no \r
209         harm in making sure. */\r
210         portDISABLE_INTERRUPTS();\r
211         if( sSchedulerRunning == pdTRUE )\r
212         {\r
213                 /* Put back the switch interrupt routines that was in place\r
214                 before the scheduler started. */\r
215                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );\r
216         }\r
217 \r
218         /* Disable the timer used for the tick to ensure the scheduler is\r
219         not called before restoring interrupts.  There was previously nothing\r
220         on this timer so there is no old ISR to restore. */\r
221         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );\r
222 \r
223         /* Restart the DOS tick. */\r
224         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
225         usTimer0Control |= portTIMER_INTERRUPT_ENABLE;\r
226         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
227 \r
228 \r
229         portENABLE_INTERRUPTS();\r
230 \r
231         /* This will free up all the memory used by the scheduler.\r
232         exiting back to dos with INT21 AH=4CH will do this anyway so\r
233         it is not necessary to call this. */\r
234         vTaskCleanUpResources(); \r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static void prvSetTickFrequency( unsigned portLONG ulTickRateHz )\r
239 {\r
240 const unsigned portSHORT usMaxCountRegister = 0xff5a;\r
241 const unsigned portSHORT usTimerPriorityRegister = 0xff32;\r
242 const unsigned portSHORT usTimerEnable = 0xC000;\r
243 const unsigned portSHORT usRetrigger = 0x0001;\r
244 const unsigned portSHORT usTimerHighPriority = 0x0000;\r
245 unsigned portSHORT usTimer0Control;\r
246 \r
247 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */\r
248 \r
249 const unsigned portLONG ulClockFrequency = 0x7f31a0;\r
250 \r
251 unsigned portLONG ulTimerCount = ulClockFrequency / ulTickRateHz;\r
252 \r
253         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );\r
254         portOUTPUT_WORD( usMaxCountRegister, ( unsigned portSHORT ) ulTimerCount );\r
255         portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );\r
256 \r
257         /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */\r
258         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
259         usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;\r
260         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
261 }\r
262 \r
263 \r
264 /*lint +e950 */\r
265 \r