]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/oWatcom/16BitDOS/Flsh186/port.c
00ec4dda3b76bdf8a4ec4004ece6bb3f3936a2ed
[freertos] / FreeRTOS / Source / portable / oWatcom / 16BitDOS / Flsh186 / port.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70 Changes from V1.00:\r
71         \r
72         + Call to taskYIELD() from within tick ISR has been replaced by the more\r
73           efficient portSWITCH_CONTEXT().\r
74         + ISR function definitions renamed to include the prv prefix.\r
75 \r
76 Changes from V1.2.0:\r
77 \r
78         + portRESET_PIC() is now called last thing before the end of the preemptive\r
79           tick routine.\r
80 \r
81 Changes from V2.6.1\r
82 \r
83         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
84           macro to be consistent with the later ports.\r
85 */\r
86 \r
87 /*-----------------------------------------------------------\r
88  * Implementation of functions defined in portable.h for the Flashlite 186\r
89  * port.\r
90  *----------------------------------------------------------*/\r
91 \r
92 #include <stdlib.h>\r
93 #include <i86.h>\r
94 #include <dos.h>\r
95 #include <setjmp.h>\r
96 \r
97 #include "FreeRTOS.h"\r
98 #include "task.h"\r
99 #include "portasm.h"\r
100 \r
101 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
102 \r
103 #define portTIMER_EOI_TYPE              ( 8 )\r
104 #define portRESET_PIC()                 portOUTPUT_WORD( ( unsigned short ) 0xff22, portTIMER_EOI_TYPE )\r
105 #define portTIMER_INT_NUMBER    0x12\r
106 \r
107 #define portTIMER_1_CONTROL_REGISTER    ( ( unsigned short ) 0xff5e )\r
108 #define portTIMER_0_CONTROL_REGISTER    ( ( unsigned short ) 0xff56 )\r
109 #define portTIMER_INTERRUPT_ENABLE              ( ( unsigned short ) 0x2000 )\r
110 \r
111 /* Setup the hardware to generate the required tick frequency. */\r
112 static void prvSetTickFrequency( unsigned long ulTickRateHz );\r
113 \r
114 /* Set the hardware back to the state as per before the scheduler started. */\r
115 static void prvExitFunction( void );\r
116 \r
117 #if configUSE_PREEMPTION == 1\r
118         /* Tick service routine used by the scheduler when preemptive scheduling is\r
119         being used. */\r
120         static void __interrupt __far prvPreemptiveTick( void );\r
121 #else\r
122         /* Tick service routine used by the scheduler when cooperative scheduling is \r
123         being used. */\r
124         static void __interrupt __far prvNonPreemptiveTick( void );\r
125 #endif\r
126 \r
127 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
128 static void __interrupt __far prvYieldProcessor( void );\r
129 \r
130 /*lint -e956 File scopes necessary here. */\r
131 \r
132 /* Set true when the vectors are set so the scheduler will service the tick. */\r
133 static short sSchedulerRunning = pdFALSE;\r
134 \r
135 /* 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
136 static void ( __interrupt __far *pxOldSwitchISR )();\r
137 \r
138 /* Used to restore the original DOS context when the scheduler is ended. */\r
139 static jmp_buf xJumpBuf;\r
140 \r
141 /*lint +e956 */\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 portBASE_TYPE xPortStartScheduler( void )\r
145 {\r
146         /* This is called with interrupts already disabled. */\r
147 \r
148         /* Remember what was on the interrupts we are going to use\r
149         so we can put them back later if required. */\r
150         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
151 \r
152         /* Put our manual switch (yield) function on a known\r
153         vector. */\r
154         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
155 \r
156         #if configUSE_PREEMPTION == 1\r
157         {               \r
158                 /* Put our tick switch function on the timer interrupt. */\r
159                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );\r
160         }\r
161         #else\r
162         {\r
163                 /* We want the timer interrupt to just increment the tick count. */\r
164                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );\r
165         }\r
166         #endif\r
167 \r
168         prvSetTickFrequency( configTICK_RATE_HZ );\r
169 \r
170         /* Clean up function if we want to return to DOS. */\r
171         if( setjmp( xJumpBuf ) != 0 )\r
172         {\r
173                 prvExitFunction();\r
174                 sSchedulerRunning = pdFALSE;\r
175         }\r
176         else\r
177         {\r
178                 sSchedulerRunning = pdTRUE;\r
179 \r
180                 /* Kick off the scheduler by setting up the context of the first task. */\r
181                 portFIRST_CONTEXT();\r
182         }\r
183 \r
184         return sSchedulerRunning;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 /* The tick ISR used depend on whether or not the preemptive or cooperative\r
189 kernel is being used. */\r
190 #if configUSE_PREEMPTION == 1\r
191         static void __interrupt __far prvPreemptiveTick( void )\r
192         {\r
193                 /* Get the scheduler to update the task states following the tick. */\r
194                 vTaskIncrementTick();\r
195 \r
196                 /* Switch in the context of the next task to be run. */\r
197                 portSWITCH_CONTEXT();\r
198 \r
199                 /* Reset the PIC ready for the next time. */\r
200                 portRESET_PIC();\r
201         }\r
202 #else\r
203         static void __interrupt __far prvNonPreemptiveTick( void )\r
204         {\r
205                 /* Same as preemptive tick, but the cooperative scheduler is being used\r
206                 so we don't have to switch in the context of the next task. */\r
207                 vTaskIncrementTick();\r
208                 portRESET_PIC();\r
209         }\r
210 #endif\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 static void __interrupt __far prvYieldProcessor( void )\r
214 {\r
215         /* Switch in the context of the next task to be run. */\r
216         portSWITCH_CONTEXT();\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 void vPortEndScheduler( void )\r
221 {\r
222         /* Jump back to the processor state prior to starting the\r
223         scheduler.  This means we are not going to be using a\r
224         task stack frame so the task can be deleted. */\r
225         longjmp( xJumpBuf, 1 );\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 static void prvExitFunction( void )\r
230 {\r
231 const unsigned short usTimerDisable = 0x0000;\r
232 unsigned short usTimer0Control;\r
233 \r
234         /* Interrupts should be disabled here anyway - but no \r
235         harm in making sure. */\r
236         portDISABLE_INTERRUPTS();\r
237         if( sSchedulerRunning == pdTRUE )\r
238         {\r
239                 /* Put back the switch interrupt routines that was in place\r
240                 before the scheduler started. */\r
241                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );\r
242         }\r
243 \r
244         /* Disable the timer used for the tick to ensure the scheduler is\r
245         not called before restoring interrupts.  There was previously nothing\r
246         on this timer so there is no old ISR to restore. */\r
247         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );\r
248 \r
249         /* Restart the DOS tick. */\r
250         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
251         usTimer0Control |= portTIMER_INTERRUPT_ENABLE;\r
252         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
253 \r
254 \r
255         portENABLE_INTERRUPTS();\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static void prvSetTickFrequency( unsigned long ulTickRateHz )\r
260 {\r
261 const unsigned short usMaxCountRegister = 0xff5a;\r
262 const unsigned short usTimerPriorityRegister = 0xff32;\r
263 const unsigned short usTimerEnable = 0xC000;\r
264 const unsigned short usRetrigger = 0x0001;\r
265 const unsigned short usTimerHighPriority = 0x0000;\r
266 unsigned short usTimer0Control;\r
267 \r
268 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */\r
269 \r
270 const unsigned long ulClockFrequency = 0x7f31a0;\r
271 \r
272 unsigned long ulTimerCount = ulClockFrequency / ulTickRateHz;\r
273 \r
274         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );\r
275         portOUTPUT_WORD( usMaxCountRegister, ( unsigned short ) ulTimerCount );\r
276         portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );\r
277 \r
278         /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */\r
279         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
280         usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;\r
281         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
282 }\r
283 \r
284 \r
285 /*lint +e950 */\r
286 \r