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