Tuesday, May 12, 2009

Burning AVR boot loader with USB TTL Serial Cable

I am working with some kids at local high school that ordered a Sanguino kit to put together just after Makerbot went online.

The kit was missing some components and others were the wrong size. How ever the big problem was that processor chip did not get the boot strap loader for the Arduino IDE development environment flashed into it.

Generally you use a programmer to burn your target program onto an AVR chip, for the Arduino IDE environment we burn a boot loader that lets us upload new programs into the flash memory via the Arduino IDE.

There are multiple ways to burn the loader that we need. Some of them are:



  • External ISP or JTAG programmer

  • Use the Arduino-IDE via a parallel port and adapter

  • Use another Sanguino or Arduino as a programmer

  • Use the USB Serial cable TTL-232R in bit banger mode

A good article on AVR family programming options is at:

http://www.ladyada.net/library/avrdevtut/programmers.html

I am blogging about the last approach of using the USB Serial cable as it doesn’t require any additional hardware other then what you would normally have to run a Sanguino or Arduino board.

Credit where credit is due I got the basic information and links to the required software from a blog by Kimio Kosaka.

http://www.geocities.jp/arduino_diecimila/bootloader/index_old_en.html

What is bit banger mode?

The USB-Serial chip that is molded into our serial cable and mounted on most Arduino boards that have USB connectors has a mode where software can control the state of the normal serial port signal lines.

The typical USB chip is a FTDI FT232 chip for serial operations. This is not to be confused with RS-232 which is a signal level standard with positive and negative voltages.

Since our USB cable length is short it uses standard TTL voltages between 0 and 5 or 0 and 3.3 depending on which cable you have.

We differ in approach from Kimio Kosaka examples in that we have to remap which software controlled pins are used for programming; as the USB-Serial cable only brings out to the connector four of the eight pins that are used for full serial port control.

If you want to know the terminology of these lines here’s a good link.

http://tldp.org/HOWTO/Serial-HOWTO-20.html

Our USB cable is lacking the following serial port control lines DTR, DSR, RI, DTR, so we have to make use of the ones that we do have available at the connector which are CTS, RTS, TXD and RXD.

It’s also handy to power the chip up via the voltage delivered by the USB cable. (Note the board that has the chip you are trying to program should not be powered up as it might interfer in the programming process.)

Here’s a link to the documentation for our USB serial port cable.

http://www.ftdichip.com/Documents/DataSheets/Modules/DS_TTL232R.pdf

Downloading the software

The next step is to download the software from the web page above. Here’s the link that I used to download software for windows. The orginal web page also has links for Linux and Mac.

http://www.geocities.jp/arduino_diecimila/bootloader/files/serjtag-0.3.zip


Adding definition to use USB cable as a programmer

After unzipping the content locate the following programmer configuration segment in the avrdude.conf file.

programmer
id = "ft232r";
desc = "FT232R Synchronous BitBang";
type = ft245r;
miso = 1; # RxD
sck = 0; # RTS
mosi = 2; # TxD
reset = 4; # DTR
;

This one is one that is closest to the definition that we need for our USB to serial cable. So we will use it as a template to add our required definitions. The pin assignments we get from the following document.

http://www.ftdichip.com/Documents/AppNotes/AN232B-01_BitBang.pdf

Our resulting programming cable definition is:

programmer
id = "ft232u";
desc = "FT232U USB Cable Synch BitBang";
type = ft245r;
miso = 1; # RxD
sck = 2; # RTS
mosi = 0; # TxD
reset = 3; # CTS
;

Adding support for the ATmega644P chip

The AVRDUDE program that we downloaded with the link above is not the latest release of AVRDUDE. The USB serial chip programming seems to be a branch that has not made it back to the main code base.

Again since this is an older version there is no definition in the avrdude.conf file for the ATMEGA644P chip that we are using for the Sanguino.

So we need to clone the entire section for the ATmega644 and then change the id and the chip signature bytes in the header portion too look like the following fragment:

#------------------------------------------------------------
# ATmega644P
#------------------------------------------------------------
# similar to ATmega164p
part
id = "m644p";
desc = "ATMEGA644P";
has_jtag = yes;
stk500_devcode = 0x82; # no STK500v1 support, use the ATmega16 one
avr910_devcode = 0x74;
signature = 0x1e 0x96 0x0a;

Building the programming cable


The final step is build an adapter cable to plug into the ISCP programming plug here is the pin out mapping for the cable:


















































Header pin



Name



Color



ISCP pin



ISCP Name



1



GND



Black



6



GND



2



CTS



Brown



5



reset



3



VCC



Red



2



VCC



4



TXD



Orange



4



mosi



5



RXD



Yellow



1



miso



6



RTS



Green



3



sck



Here's a picture of my adapter cable that I constructed:




Initial testing

After verifying that you have built the cable correctly, test that everything is running correctly by using the following command.

avrdude -c ft232u -P ft0 -p m644p -B 4800

It should result in output that looks like the following:

C:\serjtag-0.3\avrdude-serjtag\binary>avrdude -c ft232u -P ft0 -p m644p -B 4800
avrdude: BitBang OK
avrdude: pin assign miso 1 sck 2 mosi 0 reset 3
avrdude: drain OK

ft245r: bitclk 4800 -> ft baud 2400
avrdude: AVR device initialized and ready to accept instructions

Reading ################################################## 100% 0.00s

avrdude: Device signature = 0x1e960a

avrdude: safemode: Fuses OK

avrdude done. Thank you.


C:\serjtag-0.3\avrdude-serjtag\binary>


If you get the message "avrdude: ft245r_program_enable: failed" then there is probably something wrong with your cable or the changes you have made to your config file.

(Further edit: You might have previously set a fuse to use an external clock that is not working or is the wrong frequency for your target chip. If so you might be able to feed clock signal from a functioning chip or board to reset the fuses.)

Burning the Boot loader into the Sanguino ATmega644P chip

Only proceed to this step if the previous verification command works. Note the -p option selects a paticular chip so using another chip this value and others related to fuzes will change!

I found the necessary syntax for programming the chip on this blog entry, and translated from the Linux example for using it in the windows environment.

http://gorillarobotics.blogspot.com/2009/01/sanguino-burn-bootloader-script.html

From the Sanguino software distribution copy the ATmegaBOOT_644.hex file into your working directory and then issue the following commands.


avrdude -c ft232u -P ft0 -p m644p -U flash:w:ATmegaBOOT_644.hex:i

avrdude -c ft232u -P ft0 -p m644p -B 4800 -U lock:w:0x3F:m -U efuse:w:0xFD:m

avrdude -c ft232u -P ft0 -p m644p -B 4800 -U hfuse:w:0xDC:m

avrdude -c ft232u -P ft0 -p m644p -B 4800 -U lfuse:w:0xFF:m -U lock:w:0x0F:m

Note the first command did not have the "-B 4800" option so it automatically selected a faster data tranfer rate.

If the flash write operation will not verify or you get a RC=1 return code then you will need to select a lower data rate and add it to the command line.

Data rate values are:

-B 4800
-B 9600
-B 19200
-B 28800
-B 38400
-B 115200
-B 230400
-B 460800

Further edits:

Burning other chip types

The instructions in this post are only for the ATmega644P chip! The orginal link above from Kimio Kosaka delt with programming a Arduino Diecimila with a ATMEGA186 chip.

Fuzes vary between the processors, so you need to find an AVRdude example for your chip and usage environment (internal, external oscillator, etc) to get the correct fuze settings.

Also you need to have a bootstrap hex file that is compiled for your processor and oscillator configuration and speed!

The AVRdude program is used with lots of different AVR programmers so the parameter value “-c ft232u” references using USB TTL serial cable in bit banger mode and the “-B 4800” for the programming rate are the primary differences to consider when using other AVRdude examples as a baseline for your enviorment.

Please read comments from other people below as they relate further corrections, sucesses and pitfalls.