Programming my custom HEX to my Arduino Duemilanove makes it reset continually

Hi.
I’m currently developing some software for the Arduino that I compile outside the IDE, using avr-gcc.
Basically I get my Intel Hex-file then I upload it to the Arduino using avr-dude. Problem is, after a (according to avr-dude) succesful upload, my Duemilanove goes into a reset loop or something similar. The led on pin 13 is just flashing continually and it [the Arduino] will no longer accept new uploads; neither through the Arduino IDE nor avr-dude.

I found out that you can get it “back” so to speak, by holding reset, unplugging the USB-cable, plugging it back, and then uploading a new Sketch from the IDE. Just before upload starts you release the reset button. That works, so at least it isn’t bricked.

But still, I would like to upload my own code. I hope someone out there has an idea why that is. My code is pretty long, but even if I reduce it to this, the problem persists:

int main(void)
{
   while (1)
   {
      _delay_ms(1000);
   }
   return 0;
}

Here’s my programming command:

avrdude -V -C /Arduino/hardware/tools/avr/etc/avrdude.conf -v -v -v -p ATMEGA328P -c arduino -P COM2 -b 57600 -Uflash:w:main.hex:i

And my makefile:

CC      = avr-gcc
SOURCES = main.c DS18B20_SO.c
OBJECTS = ${SOURCES:.c=.o}
# Define this to your target AVR.
MCU     = atmega328p
# Set the frequency in hertz of the CPU.
F_CPU	= 16000000UL
CFLAGS  = -mmcu=$(MCU) -Wall -Os -funsigned-char -std=c99 -DF_CPU=$(F_CPU)
ELF     = main.elf
HEX		= main.hex

all: $(HEX)

$(HEX): $(ELF)
	avr-objcopy -O ihex $(ELF) $(HEX)

$(ELF): $(OBJECTS)
	$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.c.o:
	$(CC) $(CFLAGS) -c $< -o $@

clean:
	@rm -rf *.o
	@rm -rf $(ELF)
	@rm -rf $(HEX)

Thanks in advance!

skurmedel:
But still, I would like to upload my own code. I hope someone out there has an idea why that is. My code is pretty long, but even if I reduce it to this, the problem persists:

int main(void)

{
  while (1)
  {
     _delay_ms(1000);
  }
  return 0;
}

main () is not supposed to return! Think about it... where will it "return" to?

An Arduino program without setup() and loop() needs to use this skeleton:

int main (void)
{
    init (); // setup timers & stuff
    Serial.begin (115200); // if serial used, otherwise omit
    while (1) {
        // all code goes here
    }
}

Also, are your fuses set properly (specifically, the watchdog timer not active)?

Hope this helps.

Try to compile and run this code as-is:

#include <util/delay.h> // for _delay_ms ()

// pin 13 on the ATMEGA328P
#define LED_PORT PORTB
#define LED_DDR  DDRB
#define LED_BIT  _BV (5)

int main (void)
{
    LED_DDR |= LED_BIT; // pinmode (13, OUTPUT);
    while (1) {
        LED_PORT |= LED_BIT; // digitalWrite (13, HIGH);
        _delay_ms (500);
        LED_PORT &= ~LED_BIT; // digitalWrite (13, LOW);
        _delay_ms (500);
    }
}

(works on my UNO R3 which is virtually the same as your Duemilanove).

Hope this helps

Hi, thanks for the response.

I have an infinite loop in there :slight_smile: and in any case a main without a return statement has an implicit return according to the C standard.

However, I looked into the fuses as you said, and I can't read them since the bootloader doesn't seem to support fuse reading (I get 0x0 for all fuses.) A bit of a shame tbh.

I tried a new, clean main without anything but the includes and a delay, not even unused functions. This seems to actually work. So there's definately something with my other code. Your example works fine, so that's reassuring.

Well I've narrowed it down to this line:

static unsigned char const text[] = "hello";

Even if I instruct avr-objcopy to copy .data and .text it doesn't like it. It makes my Arduino cry :roll_eyes:

Anybody know why?