2 * FreeRTOS Kernel V10.4.3 LTS Patch 3
3 * Copyright (C) 2017 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. If you wish to use our Amazon
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * https://www.FreeRTOS.org
24 * https://github.com/FreeRTOS
30 #include <avr/interrupt.h>
31 #include "porthardware.h"
35 /*-----------------------------------------------------------
36 * Implementation of functions defined in portable.h for the AVR port.
37 *----------------------------------------------------------*/
39 /* Start tasks with interrupts enables. */
40 #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x80 )
42 /*-----------------------------------------------------------*/
44 /* We require the address of the pxCurrentTCB variable, but don't want to know
45 * any details of its type. */
46 typedef void RTOS_TCB_t;
47 extern volatile RTOS_TCB_t * volatile pxCurrentTCB;
49 /*-----------------------------------------------------------*/
52 * Macro to save all the general purpose registers, the save the stack pointer
55 * The first thing we do is save the flags then disable interrupts. This is to
56 * guard our stack against having a context switch interrupt after we have already
57 * pushed the registers onto the stack - causing the 32 registers to be on the
60 * r1 is set to zero as the compiler expects it to be thus, however some
61 * of the math routines make use of R1.
63 * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
64 * so we need not worry about reading/writing to the stack pointer.
67 #define portSAVE_CONTEXT() \
68 asm volatile ( "push r0 \n\t" \
69 "in r0, __SREG__ \n\t" \
104 "lds r26, pxCurrentTCB \n\t" \
105 "lds r27, pxCurrentTCB + 1 \n\t" \
106 "in r0, __SP_L__ \n\t" \
108 "in r0, __SP_H__ \n\t" \
112 * Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
113 * the context save so we can write to the stack pointer.
116 #define portRESTORE_CONTEXT() \
117 asm volatile ( "lds r26, pxCurrentTCB \n\t" \
118 "lds r27, pxCurrentTCB + 1 \n\t" \
120 "out __SP_L__, r28 \n\t" \
122 "out __SP_H__, r29 \n\t" \
155 "out __SREG__, r0 \n\t" \
158 /*-----------------------------------------------------------*/
161 * Perform hardware setup to enable ticks from timer.
163 static void prvSetupTimerInterrupt( void );
164 /*-----------------------------------------------------------*/
167 * See header file for description.
169 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
170 TaskFunction_t pxCode,
171 void * pvParameters )
175 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
177 /* Place a few bytes of known values on the bottom of the stack.
178 * This is just useful for debugging. Uncomment if needed. */
179 /* *pxTopOfStack = 0x11; */
180 /* pxTopOfStack--; */
181 /* *pxTopOfStack = 0x22; */
182 /* pxTopOfStack--; */
183 /* *pxTopOfStack = 0x33; */
184 /* pxTopOfStack--; */
186 /* The start of the task code will be popped off the stack last, so place
188 usAddress = ( uint16_t ) pxCode;
189 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
193 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
196 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
197 * portSAVE_CONTEXT places the flags on the stack immediately after r0
198 * to ensure the interrupts get disabled as soon as possible, and so ensuring
199 * the stack use is minimal should a context switch interrupt occur. */
200 *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
202 *pxTopOfStack = portFLAGS_INT_ENABLED;
205 /* Now the remaining registers. The compiler expects R1 to be 0. */
206 *pxTopOfStack = ( StackType_t ) 0x00; /* R1 */
208 /* Leave R2 - R23 untouched */
211 /* Place the parameter on the stack in the expected location. */
212 usAddress = ( uint16_t ) pvParameters;
213 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
217 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
219 /* Leave register R26 - R31 untouched */
222 /*lint +e950 +e611 +e923 */
226 /*-----------------------------------------------------------*/
228 BaseType_t xPortStartScheduler( void )
230 /* Setup the hardware to generate the tick. */
231 prvSetupTimerInterrupt();
233 /* Restore the context of the first task that is going to run. */
234 portRESTORE_CONTEXT();
236 /* Simulate a function call end as generated by the compiler. We will now
237 * jump to the start of the task the context of which we have just restored. */
238 asm volatile ( "ret" );
240 /* Should not get here. */
243 /*-----------------------------------------------------------*/
245 void vPortEndScheduler( void )
247 /* vPortEndScheduler is not implemented in this port. */
249 /*-----------------------------------------------------------*/
252 * Manual context switch. The first thing we do is save the registers so we
253 * can use a naked attribute.
255 void vPortYield( void ) __attribute__( ( naked ) );
256 void vPortYield( void )
259 vTaskSwitchContext();
260 portRESTORE_CONTEXT();
261 asm volatile ( "ret" );
263 /*-----------------------------------------------------------*/
266 * Manual context switch callable from ISRs. The first thing
267 * we do is save the registers so we can use a naked attribute.
269 void vPortYieldFromISR( void ) __attribute__( ( naked ) );
270 void vPortYieldFromISR( void )
273 vTaskSwitchContext();
274 portRESTORE_CONTEXT();
275 asm volatile ( "reti" );
277 /*-----------------------------------------------------------*/
280 * Context switch function used by the tick. This must be identical to
281 * vPortYield() from the call to vTaskSwitchContext() onwards. The only
282 * difference from vPortYield() is the tick count is incremented as the
283 * call comes from the tick ISR.
285 void vPortYieldFromTick( void ) __attribute__( ( naked ) );
286 void vPortYieldFromTick( void )
290 if( xTaskIncrementTick() != pdFALSE )
292 vTaskSwitchContext();
295 portRESTORE_CONTEXT();
297 asm volatile ( "reti" );
299 /*-----------------------------------------------------------*/
302 * Setup timer to generate a tick interrupt.
304 static void prvSetupTimerInterrupt( void )
308 /*-----------------------------------------------------------*/
310 #if configUSE_PREEMPTION == 1
313 * Tick ISR for preemptive scheduler. We can use a naked attribute as
314 * the context is saved at the start of vPortYieldFromTick(). The tick
315 * count is incremented after the context is saved.
317 ISR( TICK_INT_vect, ISR_NAKED )
319 /* Clear tick interrupt flag. */
320 CLR_INT( INT_FLAGS, INT_MASK );
322 vPortYieldFromTick();
324 asm volatile ( "reti" );
326 #else /* if configUSE_PREEMPTION == 1 */
329 * Tick ISR for the cooperative scheduler. All this does is increment the
330 * tick count. We don't need to switch context, this can only be done by
331 * manual calls to taskYIELD();
335 /* Clear tick interrupt flag. */
336 INT_FLAGS = INT_MASK;
337 xTaskIncrementTick();
339 #endif /* if configUSE_PREEMPTION == 1 */