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 V2.6.1
37 
38     + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
39       macro to be consistent with the later ports.
40 */
41 
42 /*-----------------------------------------------------------
43  * Implementation of functions defined in portable.h for the Flashlite 186
44  * port.
45  *----------------------------------------------------------*/
46 
47 #include <dos.h>
48 #include <stdlib.h>
49 #include <setjmp.h>
50 
51 #include "FreeRTOS.h"
52 #include "task.h"
53 #include "portasm.h"
54 
55 /*lint -e950 Non ANSI reserved words okay in this file only. */
56 
57 #define portTIMER_EOI_TYPE      ( 8 )
58 #define portRESET_PIC()         portOUTPUT_WORD( ( uint16_t ) 0xff22, portTIMER_EOI_TYPE )
59 #define portTIMER_INT_NUMBER    0x12
60 
61 #define portTIMER_1_CONTROL_REGISTER    ( ( uint16_t ) 0xff5e )
62 #define portTIMER_0_CONTROL_REGISTER    ( ( uint16_t ) 0xff56 )
63 #define portTIMER_INTERRUPT_ENABLE      ( ( uint16_t ) 0x2000 )
64 
65 /* Setup the hardware to generate the required tick frequency. */
66 static void prvSetTickFrequency( uint32_t ulTickRateHz );
67 
68 /* Set the hardware back to the state as per before the scheduler started. */
69 static void prvExitFunction( void );
70 
71 /* The ISR used depends on whether the preemptive or cooperative scheduler
72 is being used. */
73 #if( configUSE_PREEMPTION == 1 )
74     /* Tick service routine used by the scheduler when preemptive scheduling is
75     being used. */
76     static void __interrupt __far prvPreemptiveTick( void );
77 #else
78     /* Tick service routine used by the scheduler when cooperative scheduling is
79     being used. */
80     static void __interrupt __far prvNonPreemptiveTick( void );
81 #endif
82 
83 /* Trap routine used by taskYIELD() to manually cause a context switch. */
84 static void __interrupt __far prvYieldProcessor( void );
85 
86 /*lint -e956 File scopes necessary here. */
87 
88 /* Set true when the vectors are set so the scheduler will service the tick. */
89 static BaseType_t xSchedulerRunning = pdFALSE;
90 
91 /* Points to the original routine installed on the vector we use for manual
92 context switches.  This is then used to restore the original routine during
93 prvExitFunction(). */
94 static void ( __interrupt __far *pxOldSwitchISR )();
95 
96 /* Used to restore the original DOS context when the scheduler is ended. */
97 static jmp_buf xJumpBuf;
98 
99 /*lint +e956 */
100 
101 /*-----------------------------------------------------------*/
xPortStartScheduler(void)102 BaseType_t xPortStartScheduler( void )
103 {
104     /* This is called with interrupts already disabled. */
105 
106     /* Remember what was on the interrupts we are going to use
107     so we can put them back later if required. */
108     pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
109 
110     /* Put our manual switch (yield) function on a known
111     vector. */
112     _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
113 
114     #if( configUSE_PREEMPTION == 1 )
115     {
116         /* Put our tick switch function on the timer interrupt. */
117         _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
118     }
119     #else
120     {
121         /* We want the timer interrupt to just increment the tick count. */
122         _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
123     }
124     #endif
125 
126     prvSetTickFrequency( configTICK_RATE_HZ );
127 
128     /* Clean up function if we want to return to DOS. */
129     if( setjmp( xJumpBuf ) != 0 )
130     {
131         prvExitFunction();
132         xSchedulerRunning = pdFALSE;
133     }
134     else
135     {
136         xSchedulerRunning = pdTRUE;
137 
138         /* Kick off the scheduler by setting up the context of the first task. */
139         portFIRST_CONTEXT();
140     }
141 
142     return xSchedulerRunning;
143 }
144 /*-----------------------------------------------------------*/
145 
146 /* The ISR used depends on whether the preemptive or cooperative scheduler
147 is being used. */
148 #if( configUSE_PREEMPTION == 1 )
prvPreemptiveTick(void)149     static void __interrupt __far prvPreemptiveTick( void )
150     {
151         /* Get the scheduler to update the task states following the tick. */
152         if( xTaskIncrementTick() != pdFALSE )
153         {
154             /* Switch in the context of the next task to be run. */
155             portSWITCH_CONTEXT();
156         }
157 
158         /* Reset the PIC ready for the next time. */
159         portRESET_PIC();
160     }
161 #else
prvNonPreemptiveTick(void)162     static void __interrupt __far prvNonPreemptiveTick( void )
163     {
164         /* Same as preemptive tick, but the cooperative scheduler is being used
165         so we don't have to switch in the context of the next task. */
166         xTaskIncrementTick();
167         portRESET_PIC();
168     }
169 #endif
170 /*-----------------------------------------------------------*/
171 
prvYieldProcessor(void)172 static void __interrupt __far prvYieldProcessor( void )
173 {
174     /* Switch in the context of the next task to be run. */
175     portSWITCH_CONTEXT();
176 }
177 /*-----------------------------------------------------------*/
178 
vPortEndScheduler(void)179 void vPortEndScheduler( void )
180 {
181     /* Jump back to the processor state prior to starting the
182     scheduler.  This means we are not going to be using a
183     task stack frame so the task can be deleted. */
184     longjmp( xJumpBuf, 1 );
185 }
186 /*-----------------------------------------------------------*/
187 
prvExitFunction(void)188 static void prvExitFunction( void )
189 {
190 const uint16_t usTimerDisable = 0x0000;
191 uint16_t usTimer0Control;
192 
193     /* Interrupts should be disabled here anyway - but no
194     harm in making sure. */
195     portDISABLE_INTERRUPTS();
196     if( xSchedulerRunning == pdTRUE )
197     {
198         /* Put back the switch interrupt routines that was in place
199         before the scheduler started. */
200         _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
201     }
202 
203     /* Disable the timer used for the tick to ensure the scheduler is
204     not called before restoring interrupts.  There was previously nothing
205     on this timer so there is no old ISR to restore. */
206     portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );
207 
208     /* Restart the DOS tick. */
209     usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
210     usTimer0Control |= portTIMER_INTERRUPT_ENABLE;
211     portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
212 
213 
214     portENABLE_INTERRUPTS();
215 }
216 /*-----------------------------------------------------------*/
217 
prvSetTickFrequency(uint32_t ulTickRateHz)218 static void prvSetTickFrequency( uint32_t ulTickRateHz )
219 {
220 const uint16_t usMaxCountRegister = 0xff5a;
221 const uint16_t usTimerPriorityRegister = 0xff32;
222 const uint16_t usTimerEnable = 0xC000;
223 const uint16_t usRetrigger = 0x0001;
224 const uint16_t usTimerHighPriority = 0x0000;
225 uint16_t usTimer0Control;
226 
227 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */
228 
229 const uint32_t ulClockFrequency = ( uint32_t ) 0x7f31a0UL;
230 
231 uint32_t ulTimerCount = ulClockFrequency / ulTickRateHz;
232 
233     portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );
234     portOUTPUT_WORD( usMaxCountRegister, ( uint16_t ) ulTimerCount );
235     portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );
236 
237     /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */
238     usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
239     usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;
240     portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
241 }
242 
243 
244 /*lint +e950 */
245