1 /*
2 * FreeRTOS Kernel V10.6.2
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 portPRESCALE_VALUE ( 16 )
46 #define portTIMER_COMPARE ( configCPU_CLOCK_HZ / ( configTICK_RATE_HZ * 4UL ) )
47
48 /* From the RDC data sheet. */
49 #define portENABLE_TIMER_AND_INTERRUPT ( uint16_t ) 0xe00b
50 #define portENABLE_TIMER ( uint16_t ) 0xC001
51
52 /* Interrupt control. */
53 #define portEIO_REGISTER 0xff22
54 #define portCLEAR_INTERRUPT 0x0008
55
56 /* Setup the hardware to generate the required tick frequency. */
57 static void prvSetupTimerInterrupt( void );
58
59 /* The ISR used depends on whether the preemptive or cooperative scheduler
60 is being used. */
61 #if( configUSE_PREEMPTION == 1 )
62 /* Tick service routine used by the scheduler when preemptive scheduling is
63 being used. */
64 static void __interrupt __far prvPreemptiveTick( void );
65 #else
66 /* Tick service routine used by the scheduler when cooperative scheduling is
67 being used. */
68 static void __interrupt __far prvNonPreemptiveTick( void );
69 #endif
70
71 /* Trap routine used by taskYIELD() to manually cause a context switch. */
72 static void __interrupt __far prvYieldProcessor( void );
73
74 /*-----------------------------------------------------------*/
75 /* See header file for description. */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)76 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
77 {
78 StackType_t DS_Reg = 0;
79
80 /* We need the true data segment. */
81 __asm{ MOV DS_Reg, DS };
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_OFF( pvParameters );
98 pxTopOfStack--;
99 *pxTopOfStack = FP_OFF( pxCode );
100 pxTopOfStack--;
101
102 /* Next the status register and interrupt return address. */
103 *pxTopOfStack = portINITIAL_SW;
104 pxTopOfStack--;
105 *pxTopOfStack = FP_SEG( pxCode );
106 pxTopOfStack--;
107 *pxTopOfStack = FP_OFF( pxCode );
108 pxTopOfStack--;
109
110 /* The remaining registers would be pushed on the stack by our context
111 switch function. These are loaded with values simply to make debugging
112 easier. */
113 *pxTopOfStack = ( StackType_t ) 0xAAAA; /* AX */
114 pxTopOfStack--;
115 *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BX */
116 pxTopOfStack--;
117 *pxTopOfStack = ( StackType_t ) 0xCCCC; /* CX */
118 pxTopOfStack--;
119 *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DX */
120 pxTopOfStack--;
121 *pxTopOfStack = ( StackType_t ) 0xEEEE; /* ES */
122 pxTopOfStack--;
123
124 *pxTopOfStack = DS_Reg; /* DS */
125 pxTopOfStack--;
126 *pxTopOfStack = ( StackType_t ) 0x0123; /* SI */
127 pxTopOfStack--;
128 *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DI */
129 pxTopOfStack--;
130 *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BP */
131
132 return pxTopOfStack;
133 }
134 /*-----------------------------------------------------------*/
135
xPortStartScheduler(void)136 BaseType_t xPortStartScheduler( void )
137 {
138 /* This is called with interrupts already disabled. */
139
140 /* Put our manual switch (yield) function on a known
141 vector. */
142 setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
143
144 /* Setup the tick interrupt. */
145 prvSetupTimerInterrupt();
146
147 /* Kick off the scheduler by setting up the context of the first task. */
148 portFIRST_CONTEXT();
149
150 /* Should not get here! */
151 return pdFALSE;
152 }
153 /*-----------------------------------------------------------*/
154
155 /* The ISR used depends on whether the preemptive or cooperative scheduler
156 is being used. */
157 #if( configUSE_PREEMPTION == 1 )
prvPreemptiveTick(void)158 static void __interrupt __far prvPreemptiveTick( void )
159 {
160 /* Get the scheduler to update the task states following the tick. */
161 if( xTaskIncrementTick() != pdFALSE )
162 {
163 /* Switch in the context of the next task to be run. */
164 portEND_SWITCHING_ISR();
165 }
166
167 /* Reset interrupt. */
168 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
169 }
170 #else
prvNonPreemptiveTick(void)171 static void __interrupt __far prvNonPreemptiveTick( void )
172 {
173 /* Same as preemptive tick, but the cooperative scheduler is being used
174 so we don't have to switch in the context of the next task. */
175 xTaskIncrementTick();
176
177 /* Reset interrupt. */
178 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
179 }
180 #endif
181 /*-----------------------------------------------------------*/
182
prvYieldProcessor(void)183 static void __interrupt __far prvYieldProcessor( void )
184 {
185 /* Switch in the context of the next task to be run. */
186 portEND_SWITCHING_ISR();
187 }
188 /*-----------------------------------------------------------*/
189
vPortEndScheduler(void)190 void vPortEndScheduler( void )
191 {
192 /* Not implemented. */
193 }
194 /*-----------------------------------------------------------*/
195
prvSetupTimerInterrupt(void)196 static void prvSetupTimerInterrupt( void )
197 {
198 const uint32_t ulCompareValue = portTIMER_COMPARE;
199 uint16_t usTimerCompare;
200
201 usTimerCompare = ( uint16_t ) ( ulCompareValue >> 4 );
202 t2_init( portENABLE_TIMER, portPRESCALE_VALUE, NULL );
203
204 #if( configUSE_PREEMPTION == 1 )
205 /* Tick service routine used by the scheduler when preemptive scheduling is
206 being used. */
207 t1_init( portENABLE_TIMER_AND_INTERRUPT, usTimerCompare, usTimerCompare, prvPreemptiveTick );
208 #else
209 /* Tick service routine used by the scheduler when cooperative scheduling is
210 being used. */
211 t1_init( portENABLE_TIMER_AND_INTERRUPT, usTimerCompare, usTimerCompare, prvNonPreemptiveTick );
212 #endif
213 }
214