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