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