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