2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the Tern EE 186
32 *----------------------------------------------------------*/
34 /* Library includes. */
38 /* Scheduler includes. */
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 )
46 /* From the RDC data sheet. */
47 #define portENABLE_TIMER_AND_INTERRUPT ( uint16_t ) 0xe001
49 /* Interrupt control. */
50 #define portEIO_REGISTER 0xff22
51 #define portCLEAR_INTERRUPT 0x0008
53 /* Setup the hardware to generate the required tick frequency. */
54 static void prvSetupTimerInterrupt( void );
56 /* The ISR used depends on whether the preemptive or cooperative scheduler
58 #if( configUSE_PREEMPTION == 1 )
59 /* Tick service routine used by the scheduler when preemptive scheduling is
61 static void __interrupt __far prvPreemptiveTick( void );
63 /* Tick service routine used by the scheduler when cooperative scheduling is
65 static void __interrupt __far prvNonPreemptiveTick( void );
68 /* Trap routine used by taskYIELD() to manually cause a context switch. */
69 static void __interrupt __far prvYieldProcessor( void );
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 );
76 /*-----------------------------------------------------------*/
77 /* See header file for description. */
78 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
80 StackType_t DS_Reg = 0;
82 /* Place a few bytes of known values on the bottom of the stack.
83 This is just useful for debugging. */
85 *pxTopOfStack = 0x1111;
87 *pxTopOfStack = 0x2222;
89 *pxTopOfStack = 0x3333;
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. */
96 *pxTopOfStack = FP_SEG( pvParameters );
98 *pxTopOfStack = FP_OFF( pvParameters );
100 *pxTopOfStack = FP_SEG( pxCode );
102 *pxTopOfStack = FP_OFF( pxCode );
105 /* Next the status register and interrupt return address. */
106 *pxTopOfStack = portINITIAL_SW;
108 *pxTopOfStack = FP_SEG( pxCode );
110 *pxTopOfStack = FP_OFF( pxCode );
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
116 *pxTopOfStack = ( StackType_t ) 0xAAAA; /* AX */
118 *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BX */
120 *pxTopOfStack = ( StackType_t ) 0xCCCC; /* CX */
122 *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DX */
124 *pxTopOfStack = ( StackType_t ) 0xEEEE; /* ES */
127 /* We need the true data segment. */
128 __asm{ MOV DS_Reg, DS };
130 *pxTopOfStack = DS_Reg; /* DS */
132 *pxTopOfStack = ( StackType_t ) 0x0123; /* SI */
134 *pxTopOfStack = ( StackType_t ) 0xDDDD; /* DI */
136 *pxTopOfStack = ( StackType_t ) 0xBBBB; /* BP */
140 /*-----------------------------------------------------------*/
142 BaseType_t xPortStartScheduler( void )
144 /* This is called with interrupts already disabled. */
146 /* Put our manual switch (yield) function on a known
148 setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
150 /* Setup the tick interrupt. */
151 prvSetupTimerInterrupt();
153 /* Kick off the scheduler by setting up the context of the first task. */
156 /* Should not get here! */
159 /*-----------------------------------------------------------*/
161 static void __interrupt __far prvDummyISR( void )
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 );
168 /*-----------------------------------------------------------*/
170 /* The ISR used depends on whether the preemptive or cooperative scheduler
172 #if( configUSE_PREEMPTION == 1 )
173 static void __interrupt __far prvPreemptiveTick( void )
175 /* Get the scheduler to update the task states following the tick. */
176 if( xTaskIncrementTick() != pdFALSE )
178 /* Switch in the context of the next task to be run. */
179 portSWITCH_CONTEXT();
182 /* Reset interrupt. */
183 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
186 static void __interrupt __far prvNonPreemptiveTick( void )
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();
192 /* Reset interrupt. */
193 outport( portEIO_REGISTER, portCLEAR_INTERRUPT );
196 /*-----------------------------------------------------------*/
198 static void __interrupt __far prvYieldProcessor( void )
200 /* Switch in the context of the next task to be run. */
201 portSWITCH_CONTEXT();
203 /*-----------------------------------------------------------*/
205 void vPortEndScheduler( void )
207 /* Not implemented. */
209 /*-----------------------------------------------------------*/
211 static void prvSetupTimerInterrupt( void )
213 const uint16_t usTimerACompare = portTIMER_COMPARE, usTimerAMode = portENABLE_TIMER_AND_INTERRUPT;
214 const uint16_t usT2_IRQ = 0x13;
216 /* Configure the timer, the dummy handler is used here as the init
217 function leaves interrupts enabled. */
218 t2_init( usTimerAMode, usTimerACompare, prvDummyISR );
220 /* Disable interrupts again before installing the real handlers. */
221 portDISABLE_INTERRUPTS();
223 #if( configUSE_PREEMPTION == 1 )
224 /* Tick service routine used by the scheduler when preemptive scheduling is
226 setvect( usT2_IRQ, prvPreemptiveTick );
228 /* Tick service routine used by the scheduler when cooperative scheduling is
230 setvect( usT2_IRQ, prvNonPreemptiveTick );