ATmega2560 Porting Help !!

nobody wrote on Friday, July 14, 2006:

I’ve successfully ported you RTOS on my board equipped with ATMega1280 microcontroller and all seems to works great.
In my next project I need to use ATMega2560 microcontroller but when I try to port the OS on this device IAR compiler
gives me some warnings and demo software doesn’t work.

I suppose that It is due to the different size of 2560 program counter register, but I’m not sure.

Is freeRTOS compatible with ATMega2560 ?

Thank you

rtel wrote on Saturday, July 15, 2006:

I am not familiar with the 2560, but if the program counter is 3 bytes then this will definitely prevent the standard code from executing.  I think this should be simple to fix.

Going into and out of interrupts the hardware will handle the three bytes.  This area can therefore probably remain the same.

A change will definitely be required where the initial stack of the task is set up.  Take a look in the function pxPortInitialiseStack() within Source/Portable/IAR/ATMega323.  Here you will see the following code (I have taken out the casting to make it more readable):

///////////////////////////////////////////////////////////

The first part of the stack is the hardware stack.  Place the start
address of the task on the hardware stack. */
usAddress = pxCode;
*pxTopOfStack = ( usAddress & 0x00ff );
pxTopOfStack–;

usAddress >>= 8;
*pxTopOfStack = ( usAddress & 0x00ff );
pxTopOfStack–;

/* Leave enough space for the hardware stack before starting the software
stack.  The ‘- 2’ is because we have already used two spaces for the
address of the start of the task. */
pxTopOfStack -= ( configCALL_STACK_SIZE - 2 );

///////////////////////////////////////////////////////////   

This only places two bytes at the stop of the stack for the return address (in this case the start of the task).  You will need to modify this to something like:

///////////////////////////////////////////////////////////

The first part of the stack is the hardware stack.  Place the start
address of the task on the hardware stack. */
usAddress = pxCode;
*pxTopOfStack = ( usAddress & 0x00ff );
pxTopOfStack–;

usAddress >>= 8;
*pxTopOfStack = ( usAddress & 0x00ff );
pxTopOfStack–;

/* AND THE THIRD BYTE! */
usAddress >>= 8;
*pxTopOfStack = ( usAddress & 0x00ff );
pxTopOfStack–;

/* Leave enough space for the hardware stack before starting the software
stack.  The ‘- 3’ is because we have already used two spaces for the
address of the start of the task. */
pxTopOfStack -= ( configCALL_STACK_SIZE - 3 );

///////////////////////////////////////////////////////////   

The size required by the hardware stack is going to be larger as each time the program counter is saved you will require an extra byte.  It is likely therefore that you will want to increase the value of configCALL_STACK_SIZE.

Regards.

nobody wrote on Tuesday, July 18, 2006:

Thank you Richard,

with your suggestion now your OS works also with ATMega 2560. I’ve done only an additional modification:

/* The number of bytes used on the hardware stack by the task start address. */
#define portBYTES_USED_BY_RETURN_ADDRESS        ( 3 )