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.

14 comments:

  1. Great article I built the Printer cable version however to use it I have to set-up an old PC every time its a pain this is a much better method for me .. printed linked and in my to-do list Thank you.

    ReplyDelete
  2. Thank you for posting this tutorial. We've used it to successfully burn and configure the Cupcake Makerbot. I don't believe that this is a problem with your tutorial, but I wasn't able to get the boot loader to with the Arduino software. I flashed the Cupcake images manually after having them compiled by the Arduino software.

    ReplyDelete
  3. Great! So what changes would I have to make to flash a bootloader on some blank 328p's I have?

    ReplyDelete
  4. Hi Arvin

    Find an example of using Avdude with another programmer for your chip and then change the "-c" value to "-c ft232u" for this programmer and specify a -B data rate that works and it should work (your mileage might vary).

    ReplyDelete
  5. I've also had complete success in getting the bootloader on the Extruder Controller and communicating with the Arduino host software. I ended up using the fuses and commands from the Diecimila tutorial that you adapted for the Sanguino on the Motherboard: http://www.geocities.jp/arduino_diecimila/bootloader/index_en.html .

    I still haven't had success with geting the Motherboard (Sanguino) bootloader working with the host software, but as I mentioned before I just used the compiled image the Arduino host software produced. Once Arduino compiled the image, it was located in C:\Users\"username"\AppData\Local\Temp\buildxxxx.tmp on Vista. The .hex file is the image for use with avrdude.

    Arvin:
    I think you want to make sure that the fuse settings are correct as well. They aren't the same for each chip/circuit.

    ReplyDelete
  6. Nice.
    And here's one you can build for about 1US$.
    The programmer requires a printer port and doesn't protect the port against misuse. but I have been using this design for about 8 years with hundreds if not thousands of processors.

    The parts are 1 db25 plug, 4 resistors and a capacitor.

    http://opend.co.za/tutorials/avrtut2.htm
    Murray Horn.

    ReplyDelete
  7. Quick note on building this cable at home:

    I made my adapter cable by running an old PC IDE hard disk cable though a band saw which took care of the PCB board side.

    For the mating connector to the USB TTL adapter I took individual pins for a DB-25 male connector and after inserting them into the USB TTL plug I then hot glued the whole mess together along with a label I printed out in Word.

    I have seen others take two three pin connectors and glue them together to get the necessary six pin connector.

    ReplyDelete
  8. Is your cable the 5v or 3.3v version? I can get Kosaka's method to work, but I can't seem to get my 3.3v cable to operate the same.

    ReplyDelete
  9. My cable is the 5v version, I haven't tried a 3.3v cable.

    ReplyDelete
  10. So what do you think - does that sound like why I might be having a problem? Everything else looks right. I'm also curious about the ftdi pinout on the arduino. Kosaka points out that the X3 pins refer to CTS, DSR, DCD, and RI. Other than TXD and RXD (I'm assuming they go directly to 0 and 1), are any of the other pins more readily available (RTS, CTS, DTR, DSR, DCD, RI)?

    ReplyDelete
  11. Hi Eric

    This response is probally too late for you.

    His example is for the four unused pins on the X3 pinout area and is reflected in his defination:

    #arduino diecimila
    programmer
    id="diecimila";
    desc = "FT232R Synchronous BitBang";
    type = ft245r;
    miso = 3; # CTS X3(1)
    sck = 5; # DSR X3(2)
    mosi = 6; # DCD X3(3)
    reset = 7; # RI X3(4)


    Basically for bit banger mode/ICSP programming you need four signal pins, ground, power and a clock signal for the chip.

    The clock signal is the internal oscillator for virgin chips or external if the target board is functioning correctly and the fuses on the chip are setup for external clock input.

    Note I have recently seen a arduino diecimila board and that sucker is tiny! Plus you have to unsolder and install header pins in the X3 area.

    ReplyDelete
  12. Didn't you mean

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

    instead of

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

    -Notice the p on the hex file.

    Also, can you post the commands to write the extruder controller bootloader, please?

    ReplyDelete
  13. I do not think my revision of the boot loader did not have the p after it, but obviously use the appropiate file with it's correct spelling.

    I have not programmed a extruder controller, see the post by Javier Rosa above. The difference between his source would be my programmer defination of "ft232u" verses ft245r.

    ReplyDelete
  14. Hi viewers, I'm posting this Because i found lots of people having marriage problems which I also experienced. I recently found help from a man called Dr. Oselumen he did a great job that made my man fell back in love with me again after ten months of separation. I want you to know there are fake online spell Dr .s if someone out there needs help, can email this great spell caster, droselumen@gmail. com check or connect with him on WhatsApp +2348054265852. He will give you the best result. 
    CONTACT HIM FOR ANY KIND OF HELP,MONEY SPELL,LOTTERY SPELL,PREGNANCY SPELL,DIVORCE SPELL,SPELL TO STOP COURT CASE AND WIN ANY COURT PROBLEM,DEATH SPELL,BUSINESS SPELL,HEALING SPELL,SPELL TO REMOVE BLACK MAGIC OUT FROM YOUR LIFE,FAMILY AND MARRIAGE PROBLEM.  

    ReplyDelete