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