]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/oWatcom/16BitDOS/Flsh186/port.c
984ed8af5849ad6b52c1882509bb582723a6da18
[freertos] / FreeRTOS / Source / portable / oWatcom / 16BitDOS / Flsh186 / port.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67 Changes from V1.00:\r
68         \r
69         + Call to taskYIELD() from within tick ISR has been replaced by the more\r
70           efficient portSWITCH_CONTEXT().\r
71         + ISR function definitions renamed to include the prv prefix.\r
72 \r
73 Changes from V1.2.0:\r
74 \r
75         + portRESET_PIC() is now called last thing before the end of the preemptive\r
76           tick routine.\r
77 \r
78 Changes from V2.6.1\r
79 \r
80         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
81           macro to be consistent with the later ports.\r
82 */\r
83 \r
84 /*-----------------------------------------------------------\r
85  * Implementation of functions defined in portable.h for the Flashlite 186\r
86  * port.\r
87  *----------------------------------------------------------*/\r
88 \r
89 #include <stdlib.h>\r
90 #include <i86.h>\r
91 #include <dos.h>\r
92 #include <setjmp.h>\r
93 \r
94 #include "FreeRTOS.h"\r
95 #include "task.h"\r
96 #include "portasm.h"\r
97 \r
98 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
99 \r
100 #define portTIMER_EOI_TYPE              ( 8 )\r
101 #define portRESET_PIC()                 portOUTPUT_WORD( ( uint16_t ) 0xff22, portTIMER_EOI_TYPE )\r
102 #define portTIMER_INT_NUMBER    0x12\r
103 \r
104 #define portTIMER_1_CONTROL_REGISTER    ( ( uint16_t ) 0xff5e )\r
105 #define portTIMER_0_CONTROL_REGISTER    ( ( uint16_t ) 0xff56 )\r
106 #define portTIMER_INTERRUPT_ENABLE              ( ( uint16_t ) 0x2000 )\r
107 \r
108 /* Setup the hardware to generate the required tick frequency. */\r
109 static void prvSetTickFrequency( uint32_t ulTickRateHz );\r
110 \r
111 /* Set the hardware back to the state as per before the scheduler started. */\r
112 static void prvExitFunction( void );\r
113 \r
114 #if configUSE_PREEMPTION == 1\r
115         /* Tick service routine used by the scheduler when preemptive scheduling is\r
116         being used. */\r
117         static void __interrupt __far prvPreemptiveTick( void );\r
118 #else\r
119         /* Tick service routine used by the scheduler when cooperative scheduling is \r
120         being used. */\r
121         static void __interrupt __far prvNonPreemptiveTick( void );\r
122 #endif\r
123 \r
124 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
125 static void __interrupt __far prvYieldProcessor( void );\r
126 \r
127 /*lint -e956 File scopes necessary here. */\r
128 \r
129 /* Set true when the vectors are set so the scheduler will service the tick. */\r
130 static int16_t sSchedulerRunning = pdFALSE;\r
131 \r
132 /* 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
133 static void ( __interrupt __far *pxOldSwitchISR )();\r
134 \r
135 /* Used to restore the original DOS context when the scheduler is ended. */\r
136 static jmp_buf xJumpBuf;\r
137 \r
138 /*lint +e956 */\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 BaseType_t xPortStartScheduler( void )\r
142 {\r
143         /* This is called with interrupts already disabled. */\r
144 \r
145         /* Remember what was on the interrupts we are going to use\r
146         so we can put them back later if required. */\r
147         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
148 \r
149         /* Put our manual switch (yield) function on a known\r
150         vector. */\r
151         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
152 \r
153         #if configUSE_PREEMPTION == 1\r
154         {               \r
155                 /* Put our tick switch function on the timer interrupt. */\r
156                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );\r
157         }\r
158         #else\r
159         {\r
160                 /* We want the timer interrupt to just increment the tick count. */\r
161                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );\r
162         }\r
163         #endif\r
164 \r
165         prvSetTickFrequency( configTICK_RATE_HZ );\r
166 \r
167         /* Clean up function if we want to return to DOS. */\r
168         if( setjmp( xJumpBuf ) != 0 )\r
169         {\r
170                 prvExitFunction();\r
171                 sSchedulerRunning = pdFALSE;\r
172         }\r
173         else\r
174         {\r
175                 sSchedulerRunning = pdTRUE;\r
176 \r
177                 /* Kick off the scheduler by setting up the context of the first task. */\r
178                 portFIRST_CONTEXT();\r
179         }\r
180 \r
181         return sSchedulerRunning;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 /* The tick ISR used depend on whether or not the preemptive or cooperative\r
186 kernel is being used. */\r
187 #if configUSE_PREEMPTION == 1\r
188         static void __interrupt __far prvPreemptiveTick( void )\r
189         {\r
190                 /* Get the scheduler to update the task states following the tick. */\r
191                 if( xTaskIncrementTick() != pdFALSE )\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                 /* 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                 xTaskIncrementTick();\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 uint16_t usTimerDisable = 0x0000;\r
230 uint16_t 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( uint32_t ulTickRateHz )\r
258 {\r
259 const uint16_t usMaxCountRegister = 0xff5a;\r
260 const uint16_t usTimerPriorityRegister = 0xff32;\r
261 const uint16_t usTimerEnable = 0xC000;\r
262 const uint16_t usRetrigger = 0x0001;\r
263 const uint16_t usTimerHighPriority = 0x0000;\r
264 uint16_t usTimer0Control;\r
265 \r
266 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */\r
267 \r
268 const uint32_t ulClockFrequency = 0x7f31a0;\r
269 \r
270 uint32_t ulTimerCount = ulClockFrequency / ulTickRateHz;\r
271 \r
272         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );\r
273         portOUTPUT_WORD( usMaxCountRegister, ( uint16_t ) 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