Problem with ATmega2560

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
wgreiman
Posts: 33
Joined: Sun Oct 23, 2011 3:21 pm
Been thanked: 2 times

Problem with ATmega2560

Post by wgreiman »

I have been able to get ChibiOS/RT working on Arduino ATmega328 and ATmega1280 processors but am having problems with the ATmega2560.

I believe the problem is due to the fact that the ATmega2560 has a 17-bit program counter. Subroutine calls and interrupts push three bytes on the stack. RET and RETI pop three bytes. ATmega328 and ATmega1280 push and pop two bytes for calls and interrupts.

Any suggestions for what needs to be modified to make the ATmega2560 work?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Problem with ATmega2560

Post by Giovanni »

Hi,

You are correct, it is the AVR port layer that requires a change, the files are chcore.c and chcore.h under ./os/port/AVR, you have to update the context structures and the context switch routine.

Giovanni
wgreiman
Posts: 33
Joined: Sun Oct 23, 2011 3:21 pm
Been thanked: 2 times

Re: Problem with ATmega2560

Post by wgreiman »

Thanks, I have the ATmega2560 working with small test programs. I have not dealt with EIND, the Extended Indirect Register. I assume it is zero for small programs.

I added a byte to the extctx struct:

Code: Select all

struct extctx {
  uint8_t       _next;
  uint8_t       r31;
  uint8_t       r30;
  uint8_t       r27;
  uint8_t       r26;
  uint8_t       r25;
  uint8_t       r24;
  uint8_t       r23;
  uint8_t       r22;
  uint8_t       r21;
  uint8_t       r20;
  uint8_t       r19;
  uint8_t       r18;
  uint8_t       sr;
  uint8_t       r1;
  uint8_t       r0;
#if THREE_BYTE_PC
  uint8_t       pcx;
#endif  // THREE_BYTE_PC
  uint16_t      pc;
};

and the intctx struct:

Code: Select all

struct intctx {
  uint8_t       _next;
  uint8_t       r29;
  uint8_t       r28;
  uint8_t       r17;
  uint8_t       r16;
  uint8_t       r15;
  uint8_t       r14;
  uint8_t       r13;
  uint8_t       r12;
  uint8_t       r11;
  uint8_t       r10;
#ifndef CH_CURRP_REGISTER_CACHE
  uint8_t       r9;
  uint8_t       r8;
#endif
  uint8_t       r7;
  uint8_t       r6;
  uint8_t       r5;
  uint8_t       r4;
  uint8_t       r3;
  uint8_t       r2;
#if THREE_BYTE_PC
  uint8_t       pcx;
#endif  // THREE_BYTE_PC
  uint8_t       pcl;
  uint8_t       pch;
};


And made two versions of the SETUP_CONTEXT macro:

Code: Select all

#if THREE_BYTE_PC
#define SETUP_CONTEXT(workspace, wsize, pf, arg) {                      \
  tp->p_ctx.sp = (struct intctx*)((uint8_t *)workspace + wsize  -       \
                                  sizeof(struct intctx));               \
  tp->p_ctx.sp->r2  = (int)pf;                                          \
  tp->p_ctx.sp->r3  = (int)pf >> 8;                                     \
  tp->p_ctx.sp->r4  = (int)arg;                                         \
  tp->p_ctx.sp->r5  = (int)arg >> 8;                                    \
  tp->p_ctx.sp->pcx = 0;                                                \
  tp->p_ctx.sp->pcl = (int)_port_thread_start >> 8;                     \
  tp->p_ctx.sp->pch = (int)_port_thread_start;                          \
}
#else  // THREE_BYTE_PC
#define SETUP_CONTEXT(workspace, wsize, pf, arg) {                      \
  tp->p_ctx.sp = (struct intctx*)((uint8_t *)workspace + wsize  -       \
                                  sizeof(struct intctx));               \
  tp->p_ctx.sp->r2  = (int)pf;                                          \
  tp->p_ctx.sp->r3  = (int)pf >> 8;                                     \
  tp->p_ctx.sp->r4  = (int)arg;                                         \
  tp->p_ctx.sp->r5  = (int)arg >> 8;                                    \
  tp->p_ctx.sp->pcl = (int)_port_thread_start >> 8;                     \
  tp->p_ctx.sp->pch = (int)_port_thread_start;                          \
}
#endif  // THREE_BYTE_PC
Post Reply