Wed 10 Dec 2008 01:22:32 PM UTC, original submission:
After tracing the program flow through the simulator on AVR Studio, I have found that if I call sprintf_P, or any of the printf or scanf functions, the Stack Pointer(SP) on the ATXMega128a1 is changed to a constant value. This value is constant no matter what my previous SP value was, and changes to the following value: 0x3FD8
This causes the AVR to reset whenever a return is issued from a function, as the return jump address is stored on the stack.
Attached is a test case. The DEFINE BROKEN in the test_spf.c works as follows. If set to 0, the code executes as expected, because the main() function is never returned from, but sprintf_P still modifies the SP. If BROKEN is set to 1, sprintf_P is called from inside the sayHello() function, and the AVR resets when the end of the sayHello() function is reached.
Also, I tested the same code on an ATMega8, and everything there works fine. Tracing it through a simulator on the ATMega8, the SP remains the same before and after the call to sprintf_P.
Also, during simulating, it seems that the actual value that SP should be is stored in the Y Pointer, so it seems like the SP value is not restored from where it was saved when the sprintf_P call on an ATXMega completes. Perhaps writing to SP is not allowed on the ATXMega.
|