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