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