Porting to ATMega644 for GCC

hlachmann wrote on Monday, April 30, 2007:

I started to port FreeRTOS to ATMega644. Now I got stuck with the following simulation error "AVR Simulator: Invalid opcode 0x0053 at address 0x000017" (AVR Studio 4.13). This error occurs right after the first call to portRESTORE_CONTEXT() in function xPortStartScheduler (file port.c). Did anybody successfully port FreeRTOS to ATMega644 for GCC and could help me? Thanks.

What I did so far:
1) created new folder for ATMega644 and added #ifdef to portable.h
2) adjusted timer1 in port.c (it works)
3) added the third byte of program counter (PC) in function pxPortInitialiseStack (file port.c) as described at http://sourceforge.net/forum/message.php?msg_id=3823030. The PC of ATMega644 is three byte, but the highest program memory address is 0x007FFC. IMHO I can push just another 0x00 on the stack.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    /* The start of the task code will be popped off the stack last, so place
    it on first. */
    usAddress = ( unsigned portSHORT ) pxCode;
    *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff  );
    pxTopOfStack–;

    usAddress >>= 8;
    *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff  );
    pxTopOfStack–;
   
    /* The third byte of program counter is always 0x00. */
    *pxTopOfStack = ( portSTACK_TYPE ) 0x00;
    pxTopOfStack–;
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

(Just doing another 8-bit-shift didn’t work because of compiler complaints: pxCode is a pointer to a 16bit integer.)

nobody wrote on Monday, April 30, 2007:

Have you tried putting the extra byte first rather than last of the three.  I have run code designed for 16bit program counter in the simulator when a 24bit program counter part was selected and it worked.  It seemed illogical and I never worked out how come, but then I did not really study it that closely.

Failing that, can you step through the restore context assembly code and say at what point the error occurs?

hlachmann wrote on Monday, April 30, 2007:

Putting the extra byte first results in the same error with just different opcode / address. That your code worked has most likely something to do with a different endianess.

Unfortunately, I can’t step through the assembly code as the debugger doesn’t support it (maybe there is a switch and I don’t know about it?). The error actually occurs at “asm volatile ( “ret” );” after portRESTORE_CONTEXT().

I don’t know how the stack is or should be organized for the ATMega644. Then I could check, if I see the expected values.

hlachmann wrote on Tuesday, May 01, 2007:

After debugging I finally solved the problem:
1) Even though the PC of ATMega644 is 3 byte long (compared to 2 byte of ATMega32), an additional third byte for the PC must NOT be added in function pxPortInitialiseStack.
2) The simulator errors occured due to a stack overflow (a different problem)

kd5uwl wrote on Saturday, October 03, 2009:

You say you adjusted Timer1 in port.c.  Could you please share the specifics of that?  I’m trying to port to the 1284p which is very much like the 644.

Thanks!