2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
33 + AVR port - Replaced the inb() and outb() functions with direct memory
\r
34 access. This allows the port to be built with the 20050414 build of
\r
39 #include <avr/interrupt.h>
\r
41 #include "FreeRTOS.h"
\r
44 /*-----------------------------------------------------------
\r
45 * Implementation of functions defined in portable.h for the AVR port.
\r
46 *----------------------------------------------------------*/
\r
48 /* Start tasks with interrupts enables. */
\r
49 #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x80 )
\r
51 /* Hardware constants for timer 1. */
\r
52 #define portCLEAR_COUNTER_ON_MATCH ( ( uint8_t ) 0x08 )
\r
53 #define portPRESCALE_64 ( ( uint8_t ) 0x03 )
\r
54 #define portCLOCK_PRESCALER ( ( uint32_t ) 64 )
\r
55 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( uint8_t ) 0x10 )
\r
57 /*-----------------------------------------------------------*/
\r
59 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
60 any details of its type. */
\r
62 extern volatile TCB_t * volatile pxCurrentTCB;
\r
64 /*-----------------------------------------------------------*/
\r
67 * Macro to save all the general purpose registers, the save the stack pointer
\r
70 * The first thing we do is save the flags then disable interrupts. This is to
\r
71 * guard our stack against having a context switch interrupt after we have already
\r
72 * pushed the registers onto the stack - causing the 32 registers to be on the
\r
75 * r1 is set to zero as the compiler expects it to be thus, however some
\r
76 * of the math routines make use of R1.
\r
78 * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
\r
79 * so we need not worry about reading/writing to the stack pointer.
\r
82 #define portSAVE_CONTEXT() \
\r
83 asm volatile ( "push r0 \n\t" \
\r
84 "in r0, __SREG__ \n\t" \
\r
119 "lds r26, pxCurrentTCB \n\t" \
\r
120 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
121 "in r0, 0x3d \n\t" \
\r
123 "in r0, 0x3e \n\t" \
\r
128 * Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
\r
129 * the context save so we can write to the stack pointer.
\r
132 #define portRESTORE_CONTEXT() \
\r
133 asm volatile ( "lds r26, pxCurrentTCB \n\t" \
\r
134 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
135 "ld r28, x+ \n\t" \
\r
136 "out __SP_L__, r28 \n\t" \
\r
137 "ld r29, x+ \n\t" \
\r
138 "out __SP_H__, r29 \n\t" \
\r
171 "out __SREG__, r0 \n\t" \
\r
175 /*-----------------------------------------------------------*/
\r
178 * Perform hardware setup to enable ticks from timer 1, compare match A.
\r
180 static void prvSetupTimerInterrupt( void );
\r
181 /*-----------------------------------------------------------*/
\r
184 * See header file for description.
\r
186 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
188 uint16_t usAddress;
\r
190 /* Place a few bytes of known values on the bottom of the stack.
\r
191 This is just useful for debugging. */
\r
193 *pxTopOfStack = 0x11;
\r
195 *pxTopOfStack = 0x22;
\r
197 *pxTopOfStack = 0x33;
\r
200 /* Simulate how the stack would look after a call to vPortYield() generated by
\r
203 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
\r
205 /* The start of the task code will be popped off the stack last, so place
\r
207 usAddress = ( uint16_t ) pxCode;
\r
208 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
212 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
215 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
\r
216 portSAVE_CONTEXT places the flags on the stack immediately after r0
\r
217 to ensure the interrupts get disabled as soon as possible, and so ensuring
\r
218 the stack use is minimal should a context switch interrupt occur. */
\r
219 *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
\r
221 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
225 /* Now the remaining registers. The compiler expects R1 to be 0. */
\r
226 *pxTopOfStack = ( StackType_t ) 0x00; /* R1 */
\r
228 *pxTopOfStack = ( StackType_t ) 0x02; /* R2 */
\r
230 *pxTopOfStack = ( StackType_t ) 0x03; /* R3 */
\r
232 *pxTopOfStack = ( StackType_t ) 0x04; /* R4 */
\r
234 *pxTopOfStack = ( StackType_t ) 0x05; /* R5 */
\r
236 *pxTopOfStack = ( StackType_t ) 0x06; /* R6 */
\r
238 *pxTopOfStack = ( StackType_t ) 0x07; /* R7 */
\r
240 *pxTopOfStack = ( StackType_t ) 0x08; /* R8 */
\r
242 *pxTopOfStack = ( StackType_t ) 0x09; /* R9 */
\r
244 *pxTopOfStack = ( StackType_t ) 0x10; /* R10 */
\r
246 *pxTopOfStack = ( StackType_t ) 0x11; /* R11 */
\r
248 *pxTopOfStack = ( StackType_t ) 0x12; /* R12 */
\r
250 *pxTopOfStack = ( StackType_t ) 0x13; /* R13 */
\r
252 *pxTopOfStack = ( StackType_t ) 0x14; /* R14 */
\r
254 *pxTopOfStack = ( StackType_t ) 0x15; /* R15 */
\r
256 *pxTopOfStack = ( StackType_t ) 0x16; /* R16 */
\r
258 *pxTopOfStack = ( StackType_t ) 0x17; /* R17 */
\r
260 *pxTopOfStack = ( StackType_t ) 0x18; /* R18 */
\r
262 *pxTopOfStack = ( StackType_t ) 0x19; /* R19 */
\r
264 *pxTopOfStack = ( StackType_t ) 0x20; /* R20 */
\r
266 *pxTopOfStack = ( StackType_t ) 0x21; /* R21 */
\r
268 *pxTopOfStack = ( StackType_t ) 0x22; /* R22 */
\r
270 *pxTopOfStack = ( StackType_t ) 0x23; /* R23 */
\r
273 /* Place the parameter on the stack in the expected location. */
\r
274 usAddress = ( uint16_t ) pvParameters;
\r
275 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
279 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
282 *pxTopOfStack = ( StackType_t ) 0x26; /* R26 X */
\r
284 *pxTopOfStack = ( StackType_t ) 0x27; /* R27 */
\r
286 *pxTopOfStack = ( StackType_t ) 0x28; /* R28 Y */
\r
288 *pxTopOfStack = ( StackType_t ) 0x29; /* R29 */
\r
290 *pxTopOfStack = ( StackType_t ) 0x30; /* R30 Z */
\r
292 *pxTopOfStack = ( StackType_t ) 0x031; /* R31 */
\r
295 /*lint +e950 +e611 +e923 */
\r
297 return pxTopOfStack;
\r
299 /*-----------------------------------------------------------*/
\r
301 BaseType_t xPortStartScheduler( void )
\r
303 /* Setup the hardware to generate the tick. */
\r
304 prvSetupTimerInterrupt();
\r
306 /* Restore the context of the first task that is going to run. */
\r
307 portRESTORE_CONTEXT();
\r
309 /* Simulate a function call end as generated by the compiler. We will now
\r
310 jump to the start of the task the context of which we have just restored. */
\r
311 asm volatile ( "ret" );
\r
313 /* Should not get here. */
\r
316 /*-----------------------------------------------------------*/
\r
318 void vPortEndScheduler( void )
\r
320 /* It is unlikely that the AVR port will get stopped. If required simply
\r
321 disable the tick interrupt here. */
\r
323 /*-----------------------------------------------------------*/
\r
326 * Manual context switch. The first thing we do is save the registers so we
\r
327 * can use a naked attribute.
\r
329 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
330 void vPortYield( void )
\r
332 portSAVE_CONTEXT();
\r
333 vTaskSwitchContext();
\r
334 portRESTORE_CONTEXT();
\r
336 asm volatile ( "ret" );
\r
338 /*-----------------------------------------------------------*/
\r
341 * Context switch function used by the tick. This must be identical to
\r
342 * vPortYield() from the call to vTaskSwitchContext() onwards. The only
\r
343 * difference from vPortYield() is the tick count is incremented as the
\r
344 * call comes from the tick ISR.
\r
346 void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );
\r
347 void vPortYieldFromTick( void )
\r
349 portSAVE_CONTEXT();
\r
350 if( xTaskIncrementTick() != pdFALSE )
\r
352 vTaskSwitchContext();
\r
354 portRESTORE_CONTEXT();
\r
356 asm volatile ( "ret" );
\r
358 /*-----------------------------------------------------------*/
\r
361 * Setup timer 1 compare match A to generate a tick interrupt.
\r
363 static void prvSetupTimerInterrupt( void )
\r
365 uint32_t ulCompareMatch;
\r
366 uint8_t ucHighByte, ucLowByte;
\r
368 /* Using 16bit timer 1 to generate the tick. Correct fuses must be
\r
369 selected for the configCPU_CLOCK_HZ clock. */
\r
371 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
373 /* We only have 16 bits so have to scale to get our required tick rate. */
\r
374 ulCompareMatch /= portCLOCK_PRESCALER;
\r
376 /* Adjust for correct value. */
\r
377 ulCompareMatch -= ( uint32_t ) 1;
\r
379 /* Setup compare match value for compare match A. Interrupts are disabled
\r
380 before this is called so we need not worry here. */
\r
381 ucLowByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
382 ulCompareMatch >>= 8;
\r
383 ucHighByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
384 OCR1AH = ucHighByte;
\r
385 OCR1AL = ucLowByte;
\r
387 /* Setup clock source and compare match behaviour. */
\r
388 ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
\r
389 TCCR1B = ucLowByte;
\r
391 /* Enable the interrupt - this is okay as interrupt are currently globally
\r
394 ucLowByte |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
\r
397 /*-----------------------------------------------------------*/
\r
399 #if configUSE_PREEMPTION == 1
\r
402 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
403 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
404 * count is incremented after the context is saved.
\r
406 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal, naked ) );
\r
407 void SIG_OUTPUT_COMPARE1A( void )
\r
409 vPortYieldFromTick();
\r
410 asm volatile ( "reti" );
\r
415 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
416 * tick count. We don't need to switch context, this can only be done by
\r
417 * manual calls to taskYIELD();
\r
419 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal ) );
\r
420 void SIG_OUTPUT_COMPARE1A( void )
\r
422 xTaskIncrementTick();
\r