Bootloader only works once?

I'm trying to set up some ATMega328 parts with the Arduino bootloader. I'm using the following commands to upload the bootloader to the part:

avrdude -B 25 -c stk500v2 -P com3 -p m328p -u -U hfuse:w:0xDA:m

avrdude -B 25 -c stk500v2 -P com3 -p m328p -u -U efuse:w:0x05:m

avrdude -B 25 -c stk500v2 -P com3 -p m328p -u -U lfuse:w:0xFF:m

avrdude -B 1 -c stk500v2 -P com3 -p m328p -u -U flash:w:ATmegaBOOT_168_atmega328.hex

The operation completes successfully. When I use the Arduino IDE (v 0018) to upload the "Blink" sketch, it works. However, subsequent attempts to upload another sketch fails until I re-load the bootloader code.

Any idea what I've screwed up? Thanks all.

g.

I suspect the problem is with the proper use of the locking fuse:

atmega328.bootloader.unlock_bits=0x3F
atmega328.bootloader.lock_bits=0x0F

I suspect that the chip isn’t being locked after the bootloader is burned in and then when a new sketch is uploaded it clobbers the bootloader. Can’t help with the gory details of how to do all that properly, but I’ve read of this problem before from others.

Lefty

Have you implemented auto-reset, or are you using a manual reset button? The bootloader is only active immediately after reset, unless of course the chip contains no sketch at all, in which case the bootloader will continue to run...

I don’t know about the lock bits - The avrdude commands were in a batch file I found linked from a tutorial video…

I’m using the firmware that came with the Arduino IDE - I’ve tried uploading the sketch both before and after hitting the reset button on my STK500, where the chip is installed.

[Update]
I should also mention that the sketch that was originally uploaded continues to operate, it just won’t accept new sketches.

Thnx.

g.

C'mon guys, throw me a bone here. :)

I know it's gotta be something stupid that I'm missing. Help!

g.

Well perhaps if you could say what hardware programmer you are using and how you are using AVRDUDE. Are you using command line staatements to control AVRDUDE or using some script?

I still think it has to do with the locking and unlocking fuse settings, but can't give you help as I've never mastered manual control of AVRDUDE.

Lefty

The exact commands used to program the bootloader are in the first message I posted. I'm using an STK500 to do the programming of the bootloader. I'm also using it to download sketches to the '328.

Events are:

  1. Program bootloader
  2. Download "blink" sketch via Aruino IDE
  3. Attempt to download new sketch fails, first downloaded sketch is undisturbed and still running.

I've tried hitting the RESET button the STK500 both before and after starting the 2nd download and it doesn't appear to make any difference.

tnx.

g.

I've tried hitting the RESET button the STK500 both before and after starting the 2nd download and it doesn't appear to make any difference.

That's a little confusing to me. Does that mean you are using the STK500 to load the second sketch, after loading the first sketch using the Arduino IDE?

The Arduino IDE uses the serial USB link to upload sketches with the active bootloader running on the board talking to AVRDUDE. Loading the bootloader into the AVR chip with the STK500 is a one time operation, after that normal uploading is done via the serial link, not via a STK500. If you use a STK500 to load a sketch it will overwrite the bootloader.

I may just not be understanding your last statement. ;)

  1. Program bootloader With a hardware STK500 programmer, correct?

  2. Download "blink" sketch via Aruino IDE Using normal USB serial connection?

  3. Attempt to download new sketch fails Using USB serial connection or a hardware STK500 programmer?, first downloaded sketch is undisturbed and still running.

Lefty

After programming the bootloader, I remove the ISP cable and move the serial connector to the second DB9 on the STK500. This port is cabled into the '328 via a 2 wire jumper (rx/tx). I then use the Arduino IDE to upload the sketch. It works once, but only once.

tnx.

I remove the ISP cable and move the serial connector to the second DB9 on the STK500.

I'm not familiar enough with that hardware programmer. I use a USBtiny programmer for burning bootloaders and it uses it's own separate USB connection to the PC and has a ISP cable to attach to the Arduino board, I then use the normal build-in USB serial connection from the Arduino board to the PC for normal IDE uploads of sketches, works fine.

Lefty

blinks

There's nothing wrong with the hardware. The programmer operates just fine. The programmed part also works - it takes a sketch download once. Subsequent tries fail.

I really need help getting this straightened out.

g.

Maybe you can get help over at the AVRfreaks site, they have a lot of professional types that really know a lot.

http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewforum&f=3&sid=08ebf312d189415fb5b177cde07c5a66

Lefty

So essentially, you're shoving me off on to another forum because you're not interested in helping me or understanding the problem.

Gee, thanks.

g.

So essentially, you're shoving me off on to another forum because you're not interested in helping me or understanding the problem.

Gee, thanks.

I'm sorry you feel that way. I have claimed no expertize that can solve your problem. I have explained how I accomplish the same task even if I don't understand all the internal complexity that is required. As no one else has been responding to your post lately, I gave you the only lead (avrfreaks) that I can think off. I am certainly not holding back any secret knowlege or solution for your problem.

Again sorry to not be able to rise to your expectations. I suspect it's more about your frustration with your problem, then about my responses.

Lefty

Have you tried using the Arduino IDE to burn the bootloader?

There's nothing wrong with the hardware. The programmer operates just fine. The programmed part also works - it takes a sketch download once. Subsequent tries fail.

It still sounds to me like you're not having RESET get you back to the bootloader, or aren't staying there long enough for avrdude and the bootloader to start talking. Are you sure your reset button works? (as your first sketch, try downloading something that behaves differently at the beginning than after running for a while.)

Can you get a memory dump of the programmed AVR ? (with bootloader only, and then with bootloader and sketch?) This should tell you whether you're overwriting the bootloader with the sketch... (although I didn't think that that was possible.) (The tutorial here: http://www.arduino.cc/playground/Code/OSXISPMKII does suggest that you need to lock the bootloader AFTER it is downloaded, which I don't see in your original message.)

You ARE doing things well outside the realm of what most people in the Arduino community are doing. While I consider myself unusually knowledgeable about the low level AVR details, I've only used my STK500 to reprogram a bootloader ONCE. And I used the Arduino IDE to do it, I think... That's why avrfreaks was suggested as a resource.

I agree with retrolefty, I think it's the missing lock bits. No lock bits = boot loader section not protected from being overwritten. Actually I'm surprised it can overwrite itself with a new sketch before stopping to work.

I'm by no means an expert in this myself. I've only done this once too (on some atmega 328's, one of which I think I bricked), and its half a years since already. I made some notes then though. Basically, fuse and lock bits settings for an atmega 328P at 16Mz with an external resonator, SPI programming enabled and a 2 kB (1k word) boot loader is:

L fuse: 0xFF H fuse: 0xDA E fuse: 0xFD

And the boot loader lock bits: 0xCF

No guarantee of the correctness, though.

As for the different efuse, I think it's the same thing, the 5 first bits are not used (0x05 vs 0xFD, also see table 25-6,page 296 of the atmega 44/88/168/328 datasheet). But it might have something to say with verify errors (which I got, but it seems to work nevertheless). Something to do with "1" being "unprogrammed", and "0" being "programmed". And probably it reads back "0" for unused bits (but I'm not sure about that).

While I'm at it, I'd like to recommend the modified ADABOOT: http://www.wulfden.org/TheShoppe/freeduino/ADABOOT.shtml It's really quick, starts sketched almost instantly. And its for atmega 168/328 and 644 (I've only tested 328 so far, but I'm going to test 644 soonish).

I've since discovered that adding:

-U lock:w:3F:m to the avrdude command line before the flash write and -U lock:w:0F:m to the avrdude command line after the flash write cures the problem.

tnx.

g.

I've since discovered that adding:

Kind of reminds me of my very first posting response to your question/problem. :P

Lefty

If you are still stuck, I will give you everything I use: exact avrdude commands, fuse bit files, and boot rom. You can try it to see if it works.