Problem with itoa and eeprom_read_word

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using the itoa along with eeprom_read_word.

The line I want is:
itoa(eeprom_read_word(&idut),s,10);

When I ran the program with the line it crashed and restarted. I think it was corrupting the stack since I don't have the watchdog enabled yet.

To try and debug it, I created the following main.c file.

#include 
#include 

/****************************************************************************
*    Function Prototype Section
* Add prototypes for all functions called by this
* module, with the exception of runtime routines.
*
****************************************************************************/
int main(void);

/****************************************************************************
*    Module Variables
****************************************************************************/
uint16_t	EEMEM idut;

/****************************************************************************
* Function name	: int main(void)
*
* Created by	: J. Dombach,JLD Systems
* Date created	: 1/31/07
****************************************************************************/
int main(void)
{
int i=123;
char s[10];

itoa(1234,s,10);                //1

itoa(i,s,10);                   //2

eeprom_write_word(&idut,1234);  //3

i = eeprom_read_word(&idut);    //4
itoa(i,s,10);                   //5

itoa(eeprom_read_word(&idut),s,10); //6

do{
}while(1);

}

When I try to single step thru the code the yellow arrow in Studio jumps from line 1 to the empty line after line 3. The string appears to be correct in ram. But the processor is totally confused.

I hope it's something I am doing incorrectly.

I am using:
WinAVR 20070122
Studio 4.13 Build 522
Atmega 1280
JTAGMKII
STK500/503
Slightly modified makefile from WinAVR that I been using for 8 months.
Compiler Optimization s

Any ideas?

Jeff

Edit: typos and optimization note

Attachment(s): 

Jeff Dombach, JLD Systems
"We do the stuff behind the buttons!"
Your source for embedded solutions with a 100% Guarantee.
http://www.jldsystems.com
Phone 717.892.1100

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
When I try to single step thru the code the yellow arrow in Studio jumps from line 1 to the empty line after line 3. The string appears to be correct in ram. But the processor is totally confused.

That's because the compiler has optimized your code. There is an item in the FAQ section of the avr-libc-manual.
In AVR Studio you can look at the mixed code when you select the menu "View -> Disassembler" after starting the simulation. This will give you a more detailed understanding of how any compiler works.

Regards
Sebastian

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have never had a problem tracing c code before with previous versions of Studio and WinAVR. If the code did not crash I would not have to single step it.

I have since turned off optimization and Studio still seems to get lost after line 2.

Jeff Dombach, JLD Systems
"We do the stuff behind the buttons!"
Your source for embedded solutions with a 100% Guarantee.
http://www.jldsystems.com
Phone 717.892.1100

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you post the content of the disassembler window, please? (Mixed code)

Regards
Sebastian

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I changed the opt switch on my app an it seems to work correctly but it increase my size from 8706 to 16744.

Here is the disassembler window when it appears to be lost. The closest c statement is up at 1E78.

+0000202D:   9508        RET                      Subroutine return
+0000202E:   01FC        MOVW    R30,R24          Copy register pair
+0000202F:   9005        LPM     R0,Z+            Load program memory and postincrement
+00002030:   2000        TST     R0               Test for Zero or Minus
+00002031:   F7E9        BRNE    PC-0x02          Branch if not equal
+00002032:   9580        COM     R24              One's complement
+00002033:   9590        COM     R25              One's complement
+00002034:   0F8E        ADD     R24,R30          Add without carry
+00002035:   1F9F        ADC     R25,R31          Add with carry
+00002036:   9508        RET                      Subroutine return
+00002037:   01FB        MOVW    R30,R22          Copy register pair
+00002038:   019F        MOVW    R18,R30          Copy register pair
+00002039:   94E8        CLT                      Clear T in SREG
+0000203A:   3042        CPI     R20,0x02         Compare with immediate
+0000203B:   F0C4        BRLT    PC+0x19          Branch if less than, signed
+0000203C:   3245        CPI     R20,0x25         Compare with immediate
+0000203D:   F4B4        BRGE    PC+0x17          Branch if greater or equal, signed
+0000203E:   304A        CPI     R20,0x0A         Compare with immediate
+0000203F:   F429        BRNE    PC+0x06          Branch if not equal
+00002040:   FB97        BST     R25,7            Bit store from register to T
+00002041:   F41E        BRTC    PC+0x04          Branch if T flag cleared
+00002042:   9590        COM     R25              One's complement
+00002043:   9581        NEG     R24              Two's complement
+00002044:   4F9F        SBCI    R25,0xFF         Subtract immediate with carry
+00002045:   2F64        MOV     R22,R20          Copy register
+00002046:   2777        CLR     R23              Clear Register
+00002047:   940E20E8    CALL    0x000020E8       Call subroutine
+00002049:   5D80        SUBI    R24,0xD0         Subtract immediate
+0000204A:   338A        CPI     R24,0x3A         Compare with immediate
+0000204B:   F00C        BRLT    PC+0x02          Branch if less than, signed
+0000204C:   5D89        SUBI    R24,0xD9         Subtract immediate
+0000204D:   9381        ST      Z+,R24           Store indirect and postincrement
+0000204E:   01CB        MOVW    R24,R22          Copy register pair
+0000204F:   9700        SBIW    R24,0x00         Subtract immediate from word
+00002050:   F7A1        BRNE    PC-0x0B          Branch if not equal
+00002051:   F416        BRTC    PC+0x03          Branch if T flag cleared
+00002052:   E25D        LDI     R21,0x2D         Load immediate
+00002053:   9351        ST      Z+,R21           Store indirect and postincrement
+00002054:   8210        STD     Z+0,R1           Store indirect with displacement
+00002055:   01C9        MOVW    R24,R18          Copy register pair
+00002056:   940C2085    JMP     0x00002085       Jump
+00002058:   930F        PUSH    R16              Push register on stack
+00002059:   931F        PUSH    R17              Push register on stack
+0000205A:   93CF        PUSH    R28              Push register on stack
+0000205B:   93DF        PUSH    R29              Push register on stack
+0000205C:   018C        MOVW    R16,R24          Copy register pair
+0000205D:   01EB        MOVW    R28,R22          Copy register pair
+0000205E:   818B        LDD     R24,Y+3          Load indirect with displacement
+0000205F:   2799        CLR     R25              Clear Register
+00002060:   FF81        SBRS    R24,1            Skip if bit in register set
+00002061:   C01B        RJMP    PC+0x001C        Relative jump
+00002062:   FF82        SBRS    R24,2            Skip if bit in register set
+00002063:   C00D        RJMP    PC+0x000E        Relative jump
+00002064:   812E        LDD     R18,Y+6          Load indirect with displacement
+00002065:   813F        LDD     R19,Y+7          Load indirect with displacement
+00002066:   818C        LDD     R24,Y+4          Load indirect with displacement
+00002067:   819D        LDD     R25,Y+5          Load indirect with displacement
+00002068:   1728        CP      R18,R24          Compare
+00002069:   0739        CPC     R19,R25          Compare with carry
+0000206A:   F464        BRGE    PC+0x0D          Branch if greater or equal, signed
+0000206B:   81E8        LDD     R30,Y+0          Load indirect with displacement
+0000206C:   81F9        LDD     R31,Y+1          Load indirect with displacement
+0000206D:   9301        ST      Z+,R16           Store indirect and postincrement
+0000206E:   83F9        STD     Y+1,R31          Store indirect with displacement
+0000206F:   83E8        STD     Y+0,R30          Store indirect with displacement
+00002070:   C006        RJMP    PC+0x0007        Relative jump
+00002071:   85E8        LDD     R30,Y+8          Load indirect with displacement
+00002072:   85F9        LDD     R31,Y+9          Load indirect with displacement

The single step pointer is at 2038

I think you might be correct about the opt setting being part of the problem.

Jeff Dombach, JLD Systems
"We do the stuff behind the buttons!"
Your source for embedded solutions with a 100% Guarantee.
http://www.jldsystems.com
Phone 717.892.1100

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> But the processor is totally confused.

How would you know that?

You have to do "something real" afterwards in order to really
see whether "the processor is confused", say, flash a LED on
some port (or maybe flash 8 LEDs with the pattern you read from
EEPROM).

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In my application code the processor starts executing the startup code and transmits the help menu out of the serial port.

On the sample code after I click the "step into" button AVR studio never comes back and I have to click stop debugging an attempt to restart.

Jeff

Jeff Dombach, JLD Systems
"We do the stuff behind the buttons!"
Your source for embedded solutions with a 100% Guarantee.
http://www.jldsystems.com
Phone 717.892.1100

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And what's the code doing?

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you're in disassembler view, hitting Step In should step into the dissasembled code at the called location. If you do not then you may be receiving an interrupt which then hangs. Try disabling interrupts before stepping in by clearing the high bit of SREG using the AVRStudio GUI.

If you are getting an interrupt, and that's causing a problem, you set breakpoints in the vector table. Since you get only 3 at a time you may have to do this several times. Then you can see which interrupt handler is causing trouble.

Perhaps someone can suggest a better way, but these techniques have worked for me in the past.

C: i = "told you so";

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

cp, This is the entire code, there are no interrupts enabled.

Jörg, I added a line PORTC++ in the while loop and now the test code works as I expect it to. However, I find it odd that Studio jumps to eeprom.h on the eeprom lines when I do F10(Step-Over).

Thank you everyone for reading my post and responding.

Jeff

Jeff Dombach, JLD Systems
"We do the stuff behind the buttons!"
Your source for embedded solutions with a 100% Guarantee.
http://www.jldsystems.com
Phone 717.892.1100

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> However, I find it odd that Studio jumps to eeprom.h
> on the eeprom lines when I do F10(Step-Over).

What's odd on this? The actual access functions (or most of them) are
implemented as inline functions, so the compiler leaves the reference
to the inlined implementation (which is in the header file, obviously)
within the debugging information. Btw., this is one of the things
that has been completely impossible with the old COFF debugging
information.

I frequently use the trick you've been using here (insert a dummy port
output) when debugging (though not with AVR Studio but GDB, but the
"problem" is the same there). It makes for a good instruction that is
guaranteed to be neither optimized away nor moved around by the
compiler. Much better than turning optimization off for debugging.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess it just seems to me that displaying the inline function would be the F11 (step in) and F10 (step over) would go to the next line of code in the current file.

Strictly a personal preference. I love WinAVR/Studio toolset and I appreciate all the work the people here do to support it.

I keep telling myself that one of these days I will learn how to use GDB. Do you use GDB with the JTAGMkII USB interface?

Jeff

Jeff Dombach, JLD Systems
"We do the stuff behind the buttons!"
Your source for embedded solutions with a 100% Guarantee.
http://www.jldsystems.com
Phone 717.892.1100

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> I guess it just seems to me that displaying the inline function
> would be the F11 (step in) and F10 (step over) would go to the next
> line of code in the current file.

As there is no CALL (or related) instruction, AVR Studio will not
recognize this as being a separate function. It's inlined, so it is
the same as if you had copied the respective code from the header file
literally into that part of your source code.

> I keep telling myself that one of these days I will learn how to use
> GDB. Do you use GDB with the JTAGMkII USB interface?

Yes, I do, but if you love AVR Studio, I'm afraid GDB is not yours.
It has other strengths (like the ability to calculate arbitratry C
expressions on the command-line, using the "live" values from the
debugging target, or the ability to look at local variables of outer
stack frames), but it's probably nothing you'd consider to be
comfortable.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jeff,

Obviously I don't have your makefile but when I build that code the size is about 1.5 KB. That puts the end infinite loop at 0x30E. Not sure how you can be debugging around 0x2000 unless a bunch of libs are being linked in.

If you are still interested, post the offending HEX file.

Jim

EDIT: Sorry, missed Joerg's previous post!

C: i = "told you so";