2 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
41 + AVR port - Replaced the inb() and outb() functions with direct memory
\r
42 access. This allows the port to be built with the 20050414 build of
\r
47 #include <avr/interrupt.h>
\r
49 #include "FreeRTOS.h"
\r
52 /*-----------------------------------------------------------
\r
53 * Implementation of functions defined in portable.h for the AVR port.
\r
54 *----------------------------------------------------------*/
\r
56 /* Start tasks with interrupts enables. */
\r
57 #define portFLAGS_INT_ENABLED ( ( portSTACK_TYPE ) 0x80 )
\r
59 /* Hardware constants for timer 1. */
\r
60 #define portCLEAR_COUNTER_ON_MATCH ( ( unsigned portCHAR ) 0x08 )
\r
61 #define portPRESCALE_64 ( ( unsigned portCHAR ) 0x03 )
\r
62 #define portCLOCK_PRESCALER ( ( unsigned portLONG ) 64 )
\r
63 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( unsigned portCHAR ) 0x10 )
\r
65 /*-----------------------------------------------------------*/
\r
67 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
68 any details of its type. */
\r
69 typedef void tskTCB;
\r
70 extern volatile tskTCB * volatile pxCurrentTCB;
\r
72 /*-----------------------------------------------------------*/
\r
75 * Macro to save all the general purpose registers, the save the stack pointer
\r
78 * The first thing we do is save the flags then disable interrupts. This is to
\r
79 * guard our stack against having a context switch interrupt after we have already
\r
80 * pushed the registers onto the stack - causing the 32 registers to be on the
\r
83 * r1 is set to zero as the compiler expects it to be thus, however some
\r
84 * of the math routines make use of R1.
\r
86 * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
\r
87 * so we need not worry about reading/writing to the stack pointer.
\r
90 #define portSAVE_CONTEXT() \
\r
91 asm volatile ( "push r0 \n\t" \
\r
92 "in r0, __SREG__ \n\t" \
\r
127 "lds r26, pxCurrentTCB \n\t" \
\r
128 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
129 "in r0, 0x3d \n\t" \
\r
131 "in r0, 0x3e \n\t" \
\r
136 * Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
\r
137 * the context save so we can write to the stack pointer.
\r
140 #define portRESTORE_CONTEXT() \
\r
141 asm volatile ( "lds r26, pxCurrentTCB \n\t" \
\r
142 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
143 "ld r28, x+ \n\t" \
\r
144 "out __SP_L__, r28 \n\t" \
\r
145 "ld r29, x+ \n\t" \
\r
146 "out __SP_H__, r29 \n\t" \
\r
179 "out __SREG__, r0 \n\t" \
\r
183 /*-----------------------------------------------------------*/
\r
186 * Perform hardware setup to enable ticks from timer 1, compare match A.
\r
188 static void prvSetupTimerInterrupt( void );
\r
189 /*-----------------------------------------------------------*/
\r
192 * See header file for description.
\r
194 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
196 unsigned portSHORT usAddress;
\r
198 /* Place a few bytes of known values on the bottom of the stack.
\r
199 This is just useful for debugging. */
\r
201 *pxTopOfStack = 0x11;
\r
203 *pxTopOfStack = 0x22;
\r
205 *pxTopOfStack = 0x33;
\r
208 /* Simulate how the stack would look after a call to vPortYield() generated by
\r
211 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
\r
213 /* The start of the task code will be popped off the stack last, so place
\r
215 usAddress = ( unsigned portSHORT ) pxCode;
\r
216 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
220 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
223 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
\r
224 portSAVE_CONTEXT places the flags on the stack immediately after r0
\r
225 to ensure the interrupts get disabled as soon as possible, and so ensuring
\r
226 the stack use is minimal should a context switch interrupt occur. */
\r
227 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R0 */
\r
229 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
233 /* Now the remaining registers. The compiler expects R1 to be 0. */
\r
234 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R1 */
\r
236 *pxTopOfStack = ( portSTACK_TYPE ) 0x02; /* R2 */
\r
238 *pxTopOfStack = ( portSTACK_TYPE ) 0x03; /* R3 */
\r
240 *pxTopOfStack = ( portSTACK_TYPE ) 0x04; /* R4 */
\r
242 *pxTopOfStack = ( portSTACK_TYPE ) 0x05; /* R5 */
\r
244 *pxTopOfStack = ( portSTACK_TYPE ) 0x06; /* R6 */
\r
246 *pxTopOfStack = ( portSTACK_TYPE ) 0x07; /* R7 */
\r
248 *pxTopOfStack = ( portSTACK_TYPE ) 0x08; /* R8 */
\r
250 *pxTopOfStack = ( portSTACK_TYPE ) 0x09; /* R9 */
\r
252 *pxTopOfStack = ( portSTACK_TYPE ) 0x10; /* R10 */
\r
254 *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* R11 */
\r
256 *pxTopOfStack = ( portSTACK_TYPE ) 0x12; /* R12 */
\r
258 *pxTopOfStack = ( portSTACK_TYPE ) 0x13; /* R13 */
\r
260 *pxTopOfStack = ( portSTACK_TYPE ) 0x14; /* R14 */
\r
262 *pxTopOfStack = ( portSTACK_TYPE ) 0x15; /* R15 */
\r
264 *pxTopOfStack = ( portSTACK_TYPE ) 0x16; /* R16 */
\r
266 *pxTopOfStack = ( portSTACK_TYPE ) 0x17; /* R17 */
\r
268 *pxTopOfStack = ( portSTACK_TYPE ) 0x18; /* R18 */
\r
270 *pxTopOfStack = ( portSTACK_TYPE ) 0x19; /* R19 */
\r
272 *pxTopOfStack = ( portSTACK_TYPE ) 0x20; /* R20 */
\r
274 *pxTopOfStack = ( portSTACK_TYPE ) 0x21; /* R21 */
\r
276 *pxTopOfStack = ( portSTACK_TYPE ) 0x22; /* R22 */
\r
278 *pxTopOfStack = ( portSTACK_TYPE ) 0x23; /* R23 */
\r
281 /* Place the parameter on the stack in the expected location. */
\r
282 usAddress = ( unsigned portSHORT ) pvParameters;
\r
283 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
287 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
290 *pxTopOfStack = ( portSTACK_TYPE ) 0x26; /* R26 X */
\r
292 *pxTopOfStack = ( portSTACK_TYPE ) 0x27; /* R27 */
\r
294 *pxTopOfStack = ( portSTACK_TYPE ) 0x28; /* R28 Y */
\r
296 *pxTopOfStack = ( portSTACK_TYPE ) 0x29; /* R29 */
\r
298 *pxTopOfStack = ( portSTACK_TYPE ) 0x30; /* R30 Z */
\r
300 *pxTopOfStack = ( portSTACK_TYPE ) 0x031; /* R31 */
\r
303 /*lint +e950 +e611 +e923 */
\r
305 return pxTopOfStack;
\r
307 /*-----------------------------------------------------------*/
\r
309 portBASE_TYPE xPortStartScheduler( void )
\r
311 /* In this port we ignore the parameter and use the configUSE_PREEMPTION
\r
312 definition instead. */
\r
314 /* Setup the hardware to generate the tick. */
\r
315 prvSetupTimerInterrupt();
\r
317 /* Restore the context of the first task that is going to run. */
\r
318 portRESTORE_CONTEXT();
\r
320 /* Simulate a function call end as generated by the compiler. We will now
\r
321 jump to the start of the task the context of which we have just restored. */
\r
322 asm volatile ( "ret" );
\r
324 /* Should not get here. */
\r
327 /*-----------------------------------------------------------*/
\r
329 void vPortEndScheduler( void )
\r
331 /* It is unlikely that the AVR port will get stopped. If required simply
\r
332 disable the tick interrupt here. */
\r
334 /*-----------------------------------------------------------*/
\r
337 * Manual context switch. The first thing we do is save the registers so we
\r
338 * can use a naked attribute.
\r
340 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
341 void vPortYield( void )
\r
343 portSAVE_CONTEXT();
\r
344 vTaskSwitchContext();
\r
345 portRESTORE_CONTEXT();
\r
347 asm volatile ( "ret" );
\r
349 /*-----------------------------------------------------------*/
\r
352 * Context switch function used by the tick. This must be identical to
\r
353 * vPortYield() from the call to vTaskSwitchContext() onwards. The only
\r
354 * difference from vPortYield() is the tick count is incremented as the
\r
355 * call comes from the tick ISR.
\r
357 void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );
\r
358 void vPortYieldFromTick( void )
\r
360 portSAVE_CONTEXT();
\r
361 vTaskIncrementTick();
\r
362 vTaskSwitchContext();
\r
363 portRESTORE_CONTEXT();
\r
365 asm volatile ( "ret" );
\r
367 /*-----------------------------------------------------------*/
\r
370 * Setup timer 1 compare match A to generate a tick interrupt.
\r
372 static void prvSetupTimerInterrupt( void )
\r
374 unsigned portLONG ulCompareMatch;
\r
375 unsigned portCHAR ucHighByte, ucLowByte;
\r
377 /* Using 16bit timer 1 to generate the tick. Correct fuses must be
\r
378 selected for the configCPU_CLOCK_HZ clock. */
\r
380 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
382 /* We only have 16 bits so have to scale to get our required tick rate. */
\r
383 ulCompareMatch /= portCLOCK_PRESCALER;
\r
385 /* Adjust for correct value. */
\r
386 ulCompareMatch -= ( unsigned portLONG ) 1;
\r
388 /* Setup compare match value for compare match A. Interrupts are disabled
\r
389 before this is called so we need not worry here. */
\r
390 ucLowByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );
\r
391 ulCompareMatch >>= 8;
\r
392 ucHighByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );
\r
393 OCR1AH = ucHighByte;
\r
394 OCR1AL = ucLowByte;
\r
396 /* Setup clock source and compare match behaviour. */
\r
397 ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
\r
398 TCCR1B = ucLowByte;
\r
400 /* Enable the interrupt - this is okay as interrupt are currently globally
\r
403 ucLowByte |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
\r
406 /*-----------------------------------------------------------*/
\r
408 #if configUSE_PREEMPTION == 1
\r
411 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
412 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
413 * count is incremented after the context is saved.
\r
415 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal, naked ) );
\r
416 void SIG_OUTPUT_COMPARE1A( void )
\r
418 vPortYieldFromTick();
\r
419 asm volatile ( "reti" );
\r
424 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
425 * tick count. We don't need to switch context, this can only be done by
\r
426 * manual calls to taskYIELD();
\r
428 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal ) );
\r
429 void SIG_OUTPUT_COMPARE1A( void )
\r
431 vTaskIncrementTick();
\r