0

programming atmega168 with arduino bootloader
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

programming atmega168 with arduino bootloader

by chucklarge on Sun Jun 24, 2007 7:15 pm

Hi,
I just received the USBtinyISP and put it together and it works great! I got it to program the atmega168 to use with the Arduino. After some trial and error, I finally got it programed, so I thought I would share how to do it.
Wolf Paulus' entry really helped me out,
http://wolfpaulus.com/journal/embedded/arduino2.html

I just modified his code to work with the USBtinyISP.

1. Unlocking the boot loader segment, erase the chip, and set fuses (set boot loader size to 1024 words [=2KByte]: 0xE00-0xFFF, set clock speed to external 16 MHz)
2. Uploading the boot loader
3. Re-locking the boot loader segment
Code: Select all | TOGGLE FULL SIZE
avrdude -c usbtiny -p m168 -B 5 -V -e -U lock:w:0x3F:m -U hfuse:w:0xDF:m -U lfuse:w:0xFF:m -U efuse:w:0xF8:m
avrdude -c usbtiny -p m168 -B 1 -V -D -U flash:w:ATmegaBOOT_168.hex:i
avrdude -c usbtiny -p m168 -B 5 -V -U lock:w:0xCF:m


EDIT: try different -B values if you're having issues. Thanks! -ada
Last edited by chucklarge on Mon Jun 25, 2007 11:34 am, edited 1 time in total.
chucklarge
 
Posts: 2
Joined: Sun Jun 24, 2007 7:09 pm

by adafruit on Sun Jun 24, 2007 7:32 pm

cool thanks!

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by evilpaul on Sun Aug 12, 2007 2:10 am

Hi chucklarge, thanks for sharing. I used your commands and it worked for me nicely.
I changed a couple of the fuses, because I was burning an ATmega168V-10PU, so I wanted to use the internal 8 Mhz clock instead.
I also changed the extended fuse bit, based on this blog post that was referenced in the Makefile comments for the bootloader that was supplied with Arduino-0009.
evilpaul
 
Posts: 3
Joined: Sat Jun 16, 2007 1:23 am
Location: Australia

by xsmurf on Mon Aug 27, 2007 3:06 pm

I've tried the above and get the following:

Code: Select all | TOGGLE FULL SIZE
$ avrdude -c usbtiny -p m168 -b 115200 -V -e -U lock:w:0x3F:m -U hfuse:w:0xDF:m -U lfuse:w:0xFF:m -U efuse:w:0xF8:m

avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.


The red LED comes on for an instant when I do the above.

Any help would be appreciated, I bought the USBTiny specifically to burn Arduino bootloaders.
xsmurf
 
Posts: 28
Joined: Mon Aug 27, 2007 3:04 pm

by adafruit on Mon Aug 27, 2007 4:13 pm

that error means that its almost certainly a problem with the chip wiring and not the programmer

try "avrdude -c usbtiny -p m168 -B 25" to test

DO NOT USE -F TO OVERRIDE!

make sure you have power to the chip VCC AVCC and 2 GND pins, and that the header is correctly connected

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by xsmurf on Mon Aug 27, 2007 4:27 pm

It seems that part of my problem was self power mode. I have removed the jumper and powered the Arduino from an external source and...

Code: Select all | TOGGLE FULL SIZE
$ sudo avrdude -c usbtiny -p m168 -B 25

avrdude: Setting SCK period to 25 usec
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x1e9406

avrdude: safemode: Fuses OK

avrdude done.  Thank you.


I tried some other stuff (modified Makefile and the above commands with modified fuse) and the "USB read error: expected 4, got -1" message came back a few times (even running as sudo). I also got "expected 128" all time when reading or writing.

And this at the end
Code: Select all | TOGGLE FULL SIZE
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x3858
         0x0c != 0xff
avrdude: verification error; content mismatch


Any help would be very appreciated!
xsmurf
 
Posts: 28
Joined: Mon Aug 27, 2007 3:04 pm

by adafruit on Mon Aug 27, 2007 4:34 pm

OK well at least now its working,
take out the -b 115200 command as its not relevant here
then use -B 25 to burn the fuses
then to burn the flash try using -B 1

I have edited the first post above, to reflect the commands you should use!

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by xsmurf on Mon Aug 27, 2007 4:39 pm

Thanks for the super fast answer ^-^
Same thing, I keep getting "USB read error: expected 128, got -1" when ever it reads or writes.
xsmurf
 
Posts: 28
Joined: Mon Aug 27, 2007 3:04 pm

by adafruit on Mon Aug 27, 2007 4:51 pm

try unplugging it and re-plugging it
also try different -B values, i use -B 10 and it works OK for me...

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by xsmurf on Mon Aug 27, 2007 5:02 pm

I remember something about the NG Rev C and it's led on pin 13... I can't find the forum post anymore.. I'll see if I can dig it up.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1163734586

Some problem were having issue with SPI and ICSP and the led on pin 13. But I'm guessing if you haven't seen this issue before it's probably not the cause here.

P.S. I've also tried another, fresh, uC with no success
Last edited by xsmurf on Mon Aug 27, 2007 5:13 pm, edited 1 time in total.
xsmurf
 
Posts: 28
Joined: Mon Aug 27, 2007 3:04 pm

by adafruit on Mon Aug 27, 2007 5:13 pm

Hmm, at worst case, can you try it on a wind0z or Mac box?

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by xsmurf on Mon Aug 27, 2007 5:22 pm

I am on a mac... I don't really have a way to test under windows... maybe through VMWare? Is that known to work (or even putting the effort)?
xsmurf
 
Posts: 28
Joined: Mon Aug 27, 2007 3:04 pm

by xsmurf on Mon Aug 27, 2007 5:29 pm

It works, you were right. I fidgeted with SCK delay (-B). It worked using 5/1/5. :D

Hope this can help others :)

Thanks a bunch... I noticed I never thanked you in the first place either. Fast shipping and nice kit ^-^ Keep up the good work!
xsmurf
 
Posts: 28
Joined: Mon Aug 27, 2007 3:04 pm

by adafruit on Mon Aug 27, 2007 5:38 pm

thanks for trying out those values. ill try to track down this problem and fix it, i believe its a bug in my avrdude patch. :)

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Working after reading this post...

by jake on Mon Sep 03, 2007 1:57 am

I to was receiving this error message, ("initialization failed, rc=-1") and was not finding a lot of information into the reason for this, until I saw the suggestion that the Arduino required an external power source for the ICSP to function properly.

I first started by copying the usbtinyisp patched avrdude into arduino-0009/tools/avr/bin, and copied the avrdude.conf to arduino-0009/tools/avr/etc. The next step was to modify the Arduino IDE configuration file located at c:\Documents and Settings\<username>\Application Data\Arduino\preferences.txt and modified one line to look like this:

bootloader.atmega168-ng.programmer=usbtiny

I wired up a 12v power supply for the Arduino, disconnected the USB PWR jumper on the AVRTinyISP, connected the ICSP cable to the ICSP header on the Arduino in the appropriate direction (red to pin 1). I then connected the usb connector, ran the test from the command line, and found that the chip was finally being detected. Following that, I launched the Arduino IDE, and selected Tools > Burn NG/Mini Bootloader. The interface stalled for about 30 seconds, meanwhile the serial LEDs and the red LED on the USBTinyISP flashed solid.

Connected the Arduino to USB, burned the test LED flasher, and it worked.
jake
 
Posts: 1
Joined: Mon Sep 03, 2007 1:36 am

Please be positive and constructive with your questions and comments.