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