Declaration of a cstring causes incorrect behavior

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

I am puzzled by this issue I have been running into with avr-gcc on an ATMEGA328P. I am using the USART in my project, and wrote a simple function to send strings. When using a character array initialized per-character, ie.

char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

the program works perfectly. If, however, I declare the same string in the same line of main as:

char str[6] = "Hello";

the program does not function at all, and it seems to be stuck in a reset loop. Even if I never use the string, and only try to do things like set an output, the device does nothing. To make things more bizarre, when I try to debug the issue in simavr with gdb, the program works as expected; I can even print the string to a serial terminal. Does anyone have any pointers as to why this is happening?

This topic has a solution.
Last Edited: Fri. Mar 16, 2018 - 01:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Not without seeing the code!

 

Recommend you post a small complete project that demo's the problem, use the "<>" code button when you post.

Tell the toolchain used.

 

 

 

Jim

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

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

ki0bk wrote:
use the "<>" code button when you post

For illustrated instructions, see Tip #1

 

thanatos2996 wrote:
To make things more bizarre, when I try to debug the issue in simavr with gdb, the program works as expected

Are you using a different build configuration for the simulator vs the real chip?

 

Do you have Atmel Studio?

What if you use the Simulator in that?

 

What if you use on-chip debug?

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This string us a stack frame auto?

 

How are you doing your objcopy?

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

clawson wrote:
This string us a stack frame auto?

Good question - but how would the form of the initialisation make a difference?

 

Unless one was auto and one wasn't - which is where the requested example would come in ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Take a look at generated code. Sometimes init us LDI/STs and sometimes it is .data _do_copy_data.

 

My guess is this is an objcopy without "-j .data"

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

thanatos2996 wrote:
simavr 

You mean this: https://github.com/buserror/simavr ?

 

Sounds interesting:

 

simavr wrote:

simavr is really made to be the center for emulating your own AVR projects, not just a debugger, but also the emulating the peripherals you will use in your firmware, so you can test and develop offline, and now and then try it on the hardware.

Seems to have "hooks" to allow external integration - which a few posters have asked about recently ...

 

 

EDIT

 

typo

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Mar 14, 2018 - 08:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I found the issue, though I don't know how to solve it. Turns out it has noting to do with strings. Looking at the dissassembled file, the functions in my other file are being put before main, starting at 0x00, so it's running the functions in RS232.c before crashing and reseting. How can I specify the reset vector to point to main?

 

The compile commands:

avr-gcc -DF_CPU=16000000UL -mmcu=atmega328p -DBAUD=9600 -Os -Wall -c -o bar.o bar.c
avr-gcc -DF_CPU=16000000UL -mmcu=atmega328p -DBAUD=9600 -Os -Wall -c -o RS232.o RS232.c
avr-gcc -o bar.elf bar.o RS232.o
avr-objcopy -O ihex -R .eeprom bar.elf bar.hex

 

I'll add a pastebin with the code shortly.

 

 

Last Edited: Thu. Mar 15, 2018 - 11:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanatos2996 wrote:
How can I specify the reset vector to point to main?

Actually, it shouldn't point to main(); it should point to the 'C' startup - which will call main() when it's done

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanatos2996 wrote:
The compile commands:

...

avr-gcc -o bar.elf bar.o RS232.o

Linking without a -mmcu setting is harmful, and is very likely the root of your problems.

Stefan Ernst

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

That was it. Now it sets up a vector table and everything. Thanks.