2.3.2 IN/OUT Instructions May Be Executed Twice When Stack Is In External RAM

If an IN or an OUT instruction is executed directly before an interrupt occurs, and the stack pointer is located in external RAM, the instruction will be executed twice. In some cases, this will cause a problem, for example:
  • If reading SREG, it will appear that the I-flag is cleared
  • If writing to the PIN registers, the port will toggle twice
  • If reading registers with interrupt flags, the flags will appear to be cleared

Work Around

There are two application workarounds; either one of them will avoid the issue:
  • Replace IN and OUT with LD/LDS/LDD and ST/STS/STD instructions
  • Use internal RAM for the stack pointer

Affected Silicon Revisions

ATmega2560/2561
Rev. A Rev. C Rev. E Rev. F
X - - -