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