]> begriffs open source - freertos/blob - portable/oWatcom/16BitDOS/Flsh186/port.c
Add SMP in the License Header (#402)
[freertos] / portable / oWatcom / 16BitDOS / Flsh186 / port.c
1 /*
2  * FreeRTOS SMP Kernel V202110.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29 Changes from V1.00:
30         
31         + Call to taskYIELD() from within tick ISR has been replaced by the more
32           efficient portSWITCH_CONTEXT().
33         + ISR function definitions renamed to include the prv prefix.
34
35 Changes from V1.2.0:
36
37         + portRESET_PIC() is now called last thing before the end of the preemptive
38           tick routine.
39
40 Changes from V2.6.1
41
42         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
43           macro to be consistent with the later ports.
44 */
45
46 /*-----------------------------------------------------------
47  * Implementation of functions defined in portable.h for the Flashlite 186
48  * port.
49  *----------------------------------------------------------*/
50
51 #include <stdlib.h>
52 #include <i86.h>
53 #include <dos.h>
54 #include <setjmp.h>
55
56 #include "FreeRTOS.h"
57 #include "task.h"
58 #include "portasm.h"
59
60 /*lint -e950 Non ANSI reserved words okay in this file only. */
61
62 #define portTIMER_EOI_TYPE              ( 8 )
63 #define portRESET_PIC()                 portOUTPUT_WORD( ( uint16_t ) 0xff22, portTIMER_EOI_TYPE )
64 #define portTIMER_INT_NUMBER    0x12
65
66 #define portTIMER_1_CONTROL_REGISTER    ( ( uint16_t ) 0xff5e )
67 #define portTIMER_0_CONTROL_REGISTER    ( ( uint16_t ) 0xff56 )
68 #define portTIMER_INTERRUPT_ENABLE              ( ( uint16_t ) 0x2000 )
69
70 /* Setup the hardware to generate the required tick frequency. */
71 static void prvSetTickFrequency( uint32_t ulTickRateHz );
72
73 /* Set the hardware back to the state as per before the scheduler started. */
74 static void prvExitFunction( void );
75
76 #if configUSE_PREEMPTION == 1
77         /* Tick service routine used by the scheduler when preemptive scheduling is
78         being used. */
79         static void __interrupt __far prvPreemptiveTick( void );
80 #else
81         /* Tick service routine used by the scheduler when cooperative scheduling is 
82         being used. */
83         static void __interrupt __far prvNonPreemptiveTick( void );
84 #endif
85
86 /* Trap routine used by taskYIELD() to manually cause a context switch. */
87 static void __interrupt __far prvYieldProcessor( void );
88
89 /*lint -e956 File scopes necessary here. */
90
91 /* Set true when the vectors are set so the scheduler will service the tick. */
92 static int16_t sSchedulerRunning = pdFALSE;
93
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(). */
95 static void ( __interrupt __far *pxOldSwitchISR )();
96
97 /* Used to restore the original DOS context when the scheduler is ended. */
98 static jmp_buf xJumpBuf;
99
100 /*lint +e956 */
101
102 /*-----------------------------------------------------------*/
103 BaseType_t xPortStartScheduler( void )
104 {
105         /* This is called with interrupts already disabled. */
106
107         /* Remember what was on the interrupts we are going to use
108         so we can put them back later if required. */
109         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
110
111         /* Put our manual switch (yield) function on a known
112         vector. */
113         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
114
115         #if configUSE_PREEMPTION == 1
116         {               
117                 /* Put our tick switch function on the timer interrupt. */
118                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
119         }
120         #else
121         {
122                 /* We want the timer interrupt to just increment the tick count. */
123                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
124         }
125         #endif
126
127         prvSetTickFrequency( configTICK_RATE_HZ );
128
129         /* Clean up function if we want to return to DOS. */
130         if( setjmp( xJumpBuf ) != 0 )
131         {
132                 prvExitFunction();
133                 sSchedulerRunning = pdFALSE;
134         }
135         else
136         {
137                 sSchedulerRunning = pdTRUE;
138
139                 /* Kick off the scheduler by setting up the context of the first task. */
140                 portFIRST_CONTEXT();
141         }
142
143         return sSchedulerRunning;
144 }
145 /*-----------------------------------------------------------*/
146
147 /* The tick ISR used depend on whether or not the preemptive or cooperative
148 kernel is being used. */
149 #if configUSE_PREEMPTION == 1
150         static void __interrupt __far prvPreemptiveTick( void )
151         {
152                 /* Get the scheduler to update the task states following the tick. */
153                 if( xTaskIncrementTick() != pdFALSE )
154                 {
155                         /* Switch in the context of the next task to be run. */
156                         portSWITCH_CONTEXT();
157                 }
158
159                 /* Reset the PIC ready for the next time. */
160                 portRESET_PIC();
161         }
162 #else
163         static void __interrupt __far prvNonPreemptiveTick( void )
164         {
165                 /* Same as preemptive tick, but the cooperative scheduler is being used
166                 so we don't have to switch in the context of the next task. */
167                 xTaskIncrementTick();
168                 portRESET_PIC();
169         }
170 #endif
171 /*-----------------------------------------------------------*/
172
173 static void __interrupt __far prvYieldProcessor( void )
174 {
175         /* Switch in the context of the next task to be run. */
176         portSWITCH_CONTEXT();
177 }
178 /*-----------------------------------------------------------*/
179
180 void vPortEndScheduler( void )
181 {
182         /* Jump back to the processor state prior to starting the
183         scheduler.  This means we are not going to be using a
184         task stack frame so the task can be deleted. */
185         longjmp( xJumpBuf, 1 );
186 }
187 /*-----------------------------------------------------------*/
188
189 static void prvExitFunction( void )
190 {
191 const uint16_t usTimerDisable = 0x0000;
192 uint16_t usTimer0Control;
193
194         /* Interrupts should be disabled here anyway - but no 
195         harm in making sure. */
196         portDISABLE_INTERRUPTS();
197         if( sSchedulerRunning == pdTRUE )
198         {
199                 /* Put back the switch interrupt routines that was in place
200                 before the scheduler started. */
201                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
202         }
203
204         /* Disable the timer used for the tick to ensure the scheduler is
205         not called before restoring interrupts.  There was previously nothing
206         on this timer so there is no old ISR to restore. */
207         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );
208
209         /* Restart the DOS tick. */
210         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
211         usTimer0Control |= portTIMER_INTERRUPT_ENABLE;
212         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
213
214
215         portENABLE_INTERRUPTS();
216 }
217 /*-----------------------------------------------------------*/
218
219 static void prvSetTickFrequency( uint32_t ulTickRateHz )
220 {
221 const uint16_t usMaxCountRegister = 0xff5a;
222 const uint16_t usTimerPriorityRegister = 0xff32;
223 const uint16_t usTimerEnable = 0xC000;
224 const uint16_t usRetrigger = 0x0001;
225 const uint16_t usTimerHighPriority = 0x0000;
226 uint16_t usTimer0Control;
227
228 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */
229
230 const uint32_t ulClockFrequency = 0x7f31a0;
231
232 uint32_t ulTimerCount = ulClockFrequency / ulTickRateHz;
233
234         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );
235         portOUTPUT_WORD( usMaxCountRegister, ( uint16_t ) ulTimerCount );
236         portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );
237
238         /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */
239         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
240         usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;
241         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
242 }
243
244
245 /*lint +e950 */
246