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