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