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 /*-----------------------------------------------------------
31 * Implementation of functions defined in portable.h for the Tern EE 186
32 * port.
33 *----------------------------------------------------------*/
34
35 /* Library includes. */
36 #include <embedded.h>
37 #include <ae.h>
38
39 /* Scheduler includes. */
40 #include "FreeRTOS.h"
41 #include "task.h"
42 #include "portasm.h"
43
44 /* The timer increments every four clocks, hence the divide by 4. */
45 #define portTIMER_COMPARE ( uint16_t ) ( ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / ( uint32_t ) 4 )
46
47 /* From the RDC data sheet. */
48 #define portENABLE_TIMER_AND_INTERRUPT ( uint16_t ) 0xe001
49
50 /* Interrupt control. */
51 #define portEIO_REGISTER 0xff22
52 #define portCLEAR_INTERRUPT 0x0008
53
54 /* Setup the hardware to generate the required tick frequency. */
55 static void prvSetupTimerInterrupt( void );
56
57 /* The ISR used depends on whether the preemptive or cooperative scheduler
58 is being used. */
59 #if( configUSE_PREEMPTION == 1 )
60 /* Tick service routine used by the scheduler when preemptive scheduling is
61 being used. */
62 static void __interrupt __far prvPreemptiveTick( void );
63 #else
64 /* Tick service routine used by the scheduler when cooperative scheduling is
65 being used. */
66 static void __interrupt __far prvNonPreemptiveTick( void );
67 #endif
68
69 /* Trap routine used by taskYIELD() to manually cause a context switch. */
70 static void __interrupt __far prvYieldProcessor( void );
71
72 /* The timer initialisation functions leave interrupts enabled,
73 which is not what we want. This ISR is installed temporarily in case
74 the timer fires before we get a change to disable interrupts again. */
75 static void __interrupt __far prvDummyISR( void );
76
77 /*-----------------------------------------------------------*/
78 /* See header file for description. */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)79 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
80 {
81 StackType_t DS_Reg = 0;
82
83 /* Place a few bytes of known values on the bottom of the stack.
84 This is just useful for debugging. */
85
86 *pxTopOfStack = 0x1111;
87 pxTopOfStack--;
88 *pxTopOfStack = 0x2222;
89 pxTopOfStack--;
90 *pxTopOfStack = 0x3333;
91 pxTopOfStack--;
92
93 /* We are going to start the scheduler using a return from interrupt
94 instruction to load the program counter, so first there would be the
95 function call with parameters preamble. */
96
97 *pxTopOfStack = FP_SEG( pvParameters );
98 pxTopOfStack--;
99 *pxTopOfStack = FP_OFF( pvParameters );
100 pxTopOfStack--;
101 *pxTopOfStack = FP_SEG( pxCode );
102 pxTopOfStack--;
103 *pxTopOfStack = FP_OFF( pxCode );
104 pxTopOfStack--;
105
106 /* Next the status register and interrupt return address. */
107 *pxTopOfStack = portINITIAL_SW;
108 pxTopOfStack--;
109 *pxTopOfStack = FP_SEG( pxCode );
110 pxTopOfStack--;
111 *pxTopOfStack = FP_OFF( pxCode );
112 pxTopOfStack--;
113
114 /* The remaining registers would be pushed on the stack by our context
115 switch function. These are loaded with values simply to make debugging
116 easier. */
117 *pxTopOfStack = ( StackType_t ) 0xAAAA; /* AX */
118 pxTopOfStack--;
119 *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BX */
120 pxTopOfStack--;
121 *pxTopOfStack = ( StackType_t ) 0xCCCC; /* CX */
122 pxTopOfStack--;
123 *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DX */
124 pxTopOfStack--;
125 *pxTopOfStack = ( StackType_t ) 0xEEEE; /* ES */
126 pxTopOfStack--;
127
128 /* We need the true data segment. */
129 __asm{ MOV DS_Reg, DS };
130
131 *pxTopOfStack = DS_Reg; /* DS */
132 pxTopOfStack--;
133 *pxTopOfStack = ( StackType_t ) 0x0123; /* SI */
134 pxTopOfStack--;
135 *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DI */
136 pxTopOfStack--;
137 *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BP */
138
139 return pxTopOfStack;
140 }
141 /*-----------------------------------------------------------*/
142
xPortStartScheduler(void)143 BaseType_t xPortStartScheduler( void )
144 {
145 /* This is called with interrupts already disabled. */
146
147 /* Put our manual switch (yield) function on a known
148 vector. */
149 setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
150
151 /* Setup the tick interrupt. */
152 prvSetupTimerInterrupt();
153
154 /* Kick off the scheduler by setting up the context of the first task. */
155 portFIRST_CONTEXT();
156
157 /* Should not get here! */
158 return pdFALSE;
159 }
160 /*-----------------------------------------------------------*/
161
prvDummyISR(void)162 static void __interrupt __far prvDummyISR( void )
163 {
164 /* The timer initialisation functions leave interrupts enabled,
165 which is not what we want. This ISR is installed temporarily in case
166 the timer fires before we get a change to disable interrupts again. */
167 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
168 }
169 /*-----------------------------------------------------------*/
170
171 /* The ISR used depends on whether the preemptive or cooperative scheduler
172 is being used. */
173 #if( configUSE_PREEMPTION == 1 )
prvPreemptiveTick(void)174 static void __interrupt __far prvPreemptiveTick( void )
175 {
176 /* Get the scheduler to update the task states following the tick. */
177 if( xTaskIncrementTick() != pdFALSE )
178 {
179 /* Switch in the context of the next task to be run. */
180 portSWITCH_CONTEXT();
181 }
182
183 /* Reset interrupt. */
184 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
185 }
186 #else
prvNonPreemptiveTick(void)187 static void __interrupt __far prvNonPreemptiveTick( void )
188 {
189 /* Same as preemptive tick, but the cooperative scheduler is being used
190 so we don't have to switch in the context of the next task. */
191 xTaskIncrementTick();
192
193 /* Reset interrupt. */
194 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
195 }
196 #endif
197 /*-----------------------------------------------------------*/
198
prvYieldProcessor(void)199 static void __interrupt __far prvYieldProcessor( void )
200 {
201 /* Switch in the context of the next task to be run. */
202 portSWITCH_CONTEXT();
203 }
204 /*-----------------------------------------------------------*/
205
vPortEndScheduler(void)206 void vPortEndScheduler( void )
207 {
208 /* Not implemented. */
209 }
210 /*-----------------------------------------------------------*/
211
prvSetupTimerInterrupt(void)212 static void prvSetupTimerInterrupt( void )
213 {
214 const uint16_t usTimerACompare = portTIMER_COMPARE, usTimerAMode = portENABLE_TIMER_AND_INTERRUPT;
215 const uint16_t usT2_IRQ = 0x13;
216
217 /* Configure the timer, the dummy handler is used here as the init
218 function leaves interrupts enabled. */
219 t2_init( usTimerAMode, usTimerACompare, prvDummyISR );
220
221 /* Disable interrupts again before installing the real handlers. */
222 portDISABLE_INTERRUPTS();
223
224 #if( configUSE_PREEMPTION == 1 )
225 /* Tick service routine used by the scheduler when preemptive scheduling is
226 being used. */
227 setvect( usT2_IRQ, prvPreemptiveTick );
228 #else
229 /* Tick service routine used by the scheduler when cooperative scheduling is
230 being used. */
231 setvect( usT2_IRQ, prvNonPreemptiveTick );
232 #endif
233 }
234