2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
31 #include <avr/interrupt.h>
32 #include "porthardware.h"
36 /*-----------------------------------------------------------
37 * Implementation of functions defined in portable.h for the AVR port.
38 *----------------------------------------------------------*/
40 /* Start tasks with interrupts enables. */
41 #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x80 )
43 /*-----------------------------------------------------------*/
45 /* We require the address of the pxCurrentTCB variable, but don't want to know
46 * any details of its type. */
47 typedef void RTOS_TCB_t;
48 extern volatile RTOS_TCB_t * volatile pxCurrentTCB;
50 /*-----------------------------------------------------------*/
53 * Macro to save all the general purpose registers, the save the stack pointer
56 * The first thing we do is save the flags then disable interrupts. This is to
57 * guard our stack against having a context switch interrupt after we have already
58 * pushed the registers onto the stack - causing the 32 registers to be on the
61 * r1 is set to zero as the compiler expects it to be thus, however some
62 * of the math routines make use of R1.
64 * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
65 * so we need not worry about reading/writing to the stack pointer.
68 #define portSAVE_CONTEXT() \
69 asm volatile ( "push r0 \n\t" \
70 "in r0, __SREG__ \n\t" \
105 "lds r26, pxCurrentTCB \n\t" \
106 "lds r27, pxCurrentTCB + 1 \n\t" \
107 "in r0, __SP_L__ \n\t" \
109 "in r0, __SP_H__ \n\t" \
113 * Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
114 * the context save so we can write to the stack pointer.
117 #define portRESTORE_CONTEXT() \
118 asm volatile ( "lds r26, pxCurrentTCB \n\t" \
119 "lds r27, pxCurrentTCB + 1 \n\t" \
121 "out __SP_L__, r28 \n\t" \
123 "out __SP_H__, r29 \n\t" \
156 "out __SREG__, r0 \n\t" \
159 /*-----------------------------------------------------------*/
162 * Perform hardware setup to enable ticks from timer.
164 static void prvSetupTimerInterrupt( void );
165 /*-----------------------------------------------------------*/
168 * See header file for description.
170 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
171 TaskFunction_t pxCode,
172 void * pvParameters )
176 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
178 /* Place a few bytes of known values on the bottom of the stack.
179 * This is just useful for debugging. Uncomment if needed. */
180 /* *pxTopOfStack = 0x11; */
181 /* pxTopOfStack--; */
182 /* *pxTopOfStack = 0x22; */
183 /* pxTopOfStack--; */
184 /* *pxTopOfStack = 0x33; */
185 /* pxTopOfStack--; */
187 /* The start of the task code will be popped off the stack last, so place
189 usAddress = ( uint16_t ) pxCode;
190 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
194 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
197 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
198 * portSAVE_CONTEXT places the flags on the stack immediately after r0
199 * to ensure the interrupts get disabled as soon as possible, and so ensuring
200 * the stack use is minimal should a context switch interrupt occur. */
201 *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
203 *pxTopOfStack = portFLAGS_INT_ENABLED;
206 /* Now the remaining registers. The compiler expects R1 to be 0. */
207 *pxTopOfStack = ( StackType_t ) 0x00; /* R1 */
209 /* Leave R2 - R23 untouched */
212 /* Place the parameter on the stack in the expected location. */
213 usAddress = ( uint16_t ) pvParameters;
214 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
218 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
220 /* Leave register R26 - R31 untouched */
223 /*lint +e950 +e611 +e923 */
227 /*-----------------------------------------------------------*/
229 BaseType_t xPortStartScheduler( void )
231 /* Setup the hardware to generate the tick. */
232 prvSetupTimerInterrupt();
234 /* Restore the context of the first task that is going to run. */
235 portRESTORE_CONTEXT();
237 /* Simulate a function call end as generated by the compiler. We will now
238 * jump to the start of the task the context of which we have just restored. */
239 asm volatile ( "ret" );
241 /* Should not get here. */
244 /*-----------------------------------------------------------*/
246 void vPortEndScheduler( void )
248 /* vPortEndScheduler is not implemented in this port. */
250 /*-----------------------------------------------------------*/
253 * Manual context switch. The first thing we do is save the registers so we
254 * can use a naked attribute.
256 void vPortYield( void ) __attribute__( ( naked ) );
257 void vPortYield( void )
260 vTaskSwitchContext();
261 portRESTORE_CONTEXT();
262 asm volatile ( "ret" );
264 /*-----------------------------------------------------------*/
267 * Manual context switch callable from ISRs. The first thing
268 * we do is save the registers so we can use a naked attribute.
270 void vPortYieldFromISR( void ) __attribute__( ( naked ) );
271 void vPortYieldFromISR( void )
274 vTaskSwitchContext();
275 portRESTORE_CONTEXT();
276 asm volatile ( "reti" );
278 /*-----------------------------------------------------------*/
281 * Context switch function used by the tick. This must be identical to
282 * vPortYield() from the call to vTaskSwitchContext() onwards. The only
283 * difference from vPortYield() is the tick count is incremented as the
284 * call comes from the tick ISR.
286 void vPortYieldFromTick( void ) __attribute__( ( naked ) );
287 void vPortYieldFromTick( void )
291 if( xTaskIncrementTick() != pdFALSE )
293 vTaskSwitchContext();
296 portRESTORE_CONTEXT();
298 asm volatile ( "reti" );
300 /*-----------------------------------------------------------*/
303 * Setup timer to generate a tick interrupt.
305 static void prvSetupTimerInterrupt( void )
309 /*-----------------------------------------------------------*/
311 #if configUSE_PREEMPTION == 1
314 * Tick ISR for preemptive scheduler. We can use a naked attribute as
315 * the context is saved at the start of vPortYieldFromTick(). The tick
316 * count is incremented after the context is saved.
318 ISR( TICK_INT_vect, ISR_NAKED )
320 /* Clear tick interrupt flag. */
321 CLR_INT( INT_FLAGS, INT_MASK );
323 vPortYieldFromTick();
325 asm volatile ( "reti" );
327 #else /* if configUSE_PREEMPTION == 1 */
330 * Tick ISR for the cooperative scheduler. All this does is increment the
331 * tick count. We don't need to switch context, this can only be done by
332 * manual calls to taskYIELD();
336 /* Clear tick interrupt flag. */
337 INT_FLAGS = INT_MASK;
338 xTaskIncrementTick();
340 #endif /* if configUSE_PREEMPTION == 1 */