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