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