]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/oWatcom/16BitDOS/PC/port.c
7e81ecc48629c62f3c48bceef158c967471de450
[freertos] / FreeRTOS / Source / portable / oWatcom / 16BitDOS / PC / port.c
1 /*\r
2  * FreeRTOS Kernel V10.1.0\r
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29 Changes from V1.00:\r
30         \r
31         + Call to taskYIELD() from within tick ISR has been replaced by the more\r
32           efficient portSWITCH_CONTEXT().\r
33         + ISR function definitions renamed to include the prv prefix.\r
34 \r
35 Changes from V1.2.0:\r
36 \r
37         + prvPortResetPIC() is now called last thing before the end of the \r
38           preemptive tick routine.\r
39 \r
40 Changes from V2.6.1\r
41 \r
42         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
43           macro to be consistent with the later ports.\r
44 \r
45 Changes from V4.0.1\r
46         \r
47         + Add function prvSetTickFrequencyDefault() to set the DOS tick back to\r
48           its proper value when the scheduler exits. \r
49 */\r
50 \r
51 #include <stdlib.h>\r
52 #include <stdio.h>\r
53 #include <i86.h>\r
54 #include <dos.h>\r
55 #include <setjmp.h>\r
56 \r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 #include "portasm.h"\r
60 \r
61 /*-----------------------------------------------------------\r
62  * Implementation of functions defined in portable.h for the industrial\r
63  * PC port.\r
64  *----------------------------------------------------------*/\r
65 \r
66 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
67 \r
68 #define portTIMER_INT_NUMBER    0x08\r
69 \r
70 /* Setup hardware for required tick interrupt rate. */\r
71 static void prvSetTickFrequency( uint32_t ulTickRateHz );\r
72 \r
73 /* Restore hardware to as it was prior to starting the scheduler. */\r
74 static void prvExitFunction( void );\r
75 \r
76 /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC\r
77 directly.  We chain to the DOS tick as close as possible to the standard DOS\r
78 tick rate. */\r
79 static void prvPortResetPIC( void );\r
80 \r
81 /* The tick ISR used depends on whether the preemptive or cooperative scheduler\r
82 is being used. */\r
83 #if configUSE_PREEMPTION == 1\r
84         /* Tick service routine used by the scheduler when preemptive scheduling is\r
85         being used. */\r
86         static void __interrupt __far prvPreemptiveTick( void );\r
87 #else\r
88         /* Tick service routine used by the scheduler when cooperative scheduling is \r
89         being used. */\r
90         static void __interrupt __far prvNonPreemptiveTick( void );\r
91 #endif\r
92 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
93 static void __interrupt __far prvYieldProcessor( void );\r
94 \r
95 /* Set the tick frequency back so the floppy drive works correctly when the\r
96 scheduler exits. */\r
97 static void prvSetTickFrequencyDefault( void );\r
98 \r
99 /*lint -e956 File scopes necessary here. */\r
100 \r
101 /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */\r
102 static int16_t sDOSTickCounter;                                                 \r
103 \r
104 /* Set true when the vectors are set so the scheduler will service the tick. */\r
105 static int16_t sSchedulerRunning = pdFALSE;                             \r
106 \r
107 /* 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
108 static void ( __interrupt __far *pxOldSwitchISR )();            \r
109 \r
110 /* Points to the original routine installed on the vector we use to chain to the DOS tick.  This is then used to restore the original routine during prvExitFunction(). */\r
111 static void ( __interrupt __far *pxOldSwitchISRPlus1 )();       \r
112 \r
113 /* Used to restore the original DOS context when the scheduler is ended. */\r
114 static jmp_buf xJumpBuf;\r
115 \r
116 /*lint +e956 */\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 BaseType_t xPortStartScheduler( void )\r
120 {\r
121 pxISR pxOriginalTickISR;\r
122         \r
123         /* This is called with interrupts already disabled. */\r
124 \r
125         /* Remember what was on the interrupts we are going to use\r
126         so we can put them back later if required. */\r
127         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
128         pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );\r
129         pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );\r
130 \r
131         prvSetTickFrequency( configTICK_RATE_HZ );\r
132 \r
133         /* Put our manual switch (yield) function on a known\r
134         vector. */\r
135         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
136 \r
137         /* Put the old tick on a different interrupt number so we can\r
138         call it when we want. */\r
139         _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );\r
140 \r
141         #if configUSE_PREEMPTION == 1\r
142         {               \r
143                 /* Put our tick switch function on the timer interrupt. */\r
144                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );\r
145         }\r
146         #else\r
147         {\r
148                 /* We want the timer interrupt to just increment the tick count. */\r
149                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );\r
150         }\r
151         #endif\r
152 \r
153         /* Setup a counter that is used to call the DOS interrupt as close\r
154         to it's original frequency as can be achieved given our chosen tick\r
155         frequency. */\r
156         sDOSTickCounter = portTICKS_PER_DOS_TICK;\r
157 \r
158         /* Clean up function if we want to return to DOS. */\r
159         if( setjmp( xJumpBuf ) != 0 )\r
160         {\r
161                 prvExitFunction();\r
162                 sSchedulerRunning = pdFALSE;\r
163         }\r
164         else\r
165         {\r
166                 sSchedulerRunning = pdTRUE;\r
167 \r
168                 /* Kick off the scheduler by setting up the context of the first task. */\r
169                 portFIRST_CONTEXT();\r
170         }\r
171 \r
172         return sSchedulerRunning;\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 /* The tick ISR used depends on whether the preemptive or cooperative scheduler\r
177 is being used. */\r
178 #if configUSE_PREEMPTION == 1\r
179         /* Tick service routine used by the scheduler when preemptive scheduling is\r
180         being used. */\r
181         static void __interrupt __far prvPreemptiveTick( void )\r
182         {\r
183                 /* Get the scheduler to update the task states following the tick. */\r
184                 if( xTaskIncrementTick() != pdFALSE )\r
185                 {\r
186                         /* Switch in the context of the next task to be run. */\r
187                         portSWITCH_CONTEXT();\r
188                 }\r
189 \r
190                 /* Reset the PIC ready for the next time. */\r
191                 prvPortResetPIC();\r
192         }\r
193 #else\r
194         static void __interrupt __far prvNonPreemptiveTick( void )\r
195         {\r
196                 /* Same as preemptive tick, but the cooperative scheduler is being used\r
197                 so we don't have to switch in the context of the next task. */\r
198                 xTaskIncrementTick();\r
199                 prvPortResetPIC();\r
200         }\r
201 #endif\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 \r
205 static void __interrupt __far prvYieldProcessor( void )\r
206 {\r
207         /* Switch in the context of the next task to be run. */\r
208         portSWITCH_CONTEXT();\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static void prvPortResetPIC( void )\r
213 {\r
214         /* We are going to call the DOS tick interrupt at as close a\r
215         frequency to the normal DOS tick as possible. */\r
216 \r
217         /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */\r
218         --sDOSTickCounter;\r
219         if( sDOSTickCounter <= 0 )\r
220         {\r
221                 sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;\r
222                 __asm{ int      portSWITCH_INT_NUMBER + 1 };             \r
223         }\r
224         else\r
225         {\r
226                 /* Reset the PIC as the DOS tick is not being called to\r
227                 do it. */\r
228                 __asm\r
229                 {\r
230                         mov     al, 20H\r
231                         out 20H, al\r
232                 };\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 void vPortEndScheduler( void )\r
238 {\r
239         /* Jump back to the processor state prior to starting the\r
240         scheduler.  This means we are not going to be using a\r
241         task stack frame so the task can be deleted. */\r
242         longjmp( xJumpBuf, 1 );\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 static void prvExitFunction( void )\r
247 {\r
248 void ( __interrupt __far *pxOriginalTickISR )();\r
249 \r
250         /* Interrupts should be disabled here anyway - but no \r
251         harm in making sure. */\r
252         portDISABLE_INTERRUPTS();\r
253         if( sSchedulerRunning == pdTRUE )\r
254         {\r
255                 /* Set the DOS tick back onto the timer ticker. */\r
256                 pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );\r
257                 _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );\r
258                 prvSetTickFrequencyDefault();\r
259 \r
260                 /* Put back the switch interrupt routines that was in place\r
261                 before the scheduler started. */\r
262                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );\r
263                 _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );\r
264         }\r
265         /* The tick timer is back how DOS wants it.  We can re-enable\r
266         interrupts without the scheduler being called. */\r
267         portENABLE_INTERRUPTS();\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 static void prvSetTickFrequency( uint32_t ulTickRateHz )\r
272 {\r
273 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;\r
274 const uint16_t usPIT0 = ( uint16_t ) 0x40;\r
275 const uint32_t ulPIT_CONST = ( uint32_t ) 1193180;\r
276 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;\r
277 uint32_t ulOutput;\r
278 \r
279         /* Setup the 8245 to tick at the wanted frequency. */\r
280         portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );\r
281         ulOutput = ulPIT_CONST / ulTickRateHz;\r
282    \r
283         portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );\r
284         ulOutput >>= 8;\r
285         portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 static void prvSetTickFrequencyDefault( void )\r
290 {\r
291 const uint16_t usPIT_MODE = ( uint16_t ) 0x43;\r
292 const uint16_t usPIT0 = ( uint16_t ) 0x40;\r
293 const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;\r
294 \r
295         portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );\r
296         portOUTPUT_BYTE( usPIT0,0 );\r
297         portOUTPUT_BYTE( usPIT0,0 );\r
298 }\r
299 \r
300 \r
301 /*lint +e950 */\r
302 \r