FTDI Bitbang as AVR programmer

Discussion in 'The Projects Forum' started by Cue, Dec 12, 2012.

  1. Cue

    Thread Starter New Member

    Oct 16, 2012
    15
    0
    I'm a beginner so please go easy on me. I have an atmega328p-pu and I'm trying to program it with an FT232RL.

    I've built this circuit on a breadboard


    [​IMG]

    1) Am I right in assuming that with the left powerplug unplugged connecting the USB FTDI will destroy the LM7805 voltage regulator?

    2) Is this the right circuit to bitbang hex files on to the atmega328p?
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    First, your RX and TX aren't going to the TX and RX pins on the uC. This may not be a problem, as you could implement a UART in software...

    However, you can't program your uC like this. You'd need to program using a proper programmer in order to add a bootloader. The bootloader sits in a section of program memory and will receive UART commands and overwrite program memory, effectively reprogramming the device. You need the bootloader there first, though. The reason this works with Arduinos is because they are shipped with a preloaded bootloader. If you are using a chip taken from an Arduino, you should already have it, otherwise, you'll need a proper AVR programmer to put a bootloader on the device first.

    As far as the 7805 is concerned, you shouldn't have a problem, though I've never used it like this, so I can't be sure.
     
  3. Cue

    Thread Starter New Member

    Oct 16, 2012
    15
    0
    Hi tshuck, from what I have read you can burn a hex file directly with the FTDI in bitbang mode. As far as I understand it a bootloader is just another hex file, it is not necessary to program an AVR but it is for it to be compatible with the Arduino IDE which uses the TX/RX pins of the atmega328. Please correct me if I'm wrong because I'm still learning.

    I actually had some moderate success since I posted my previous post. With the circuit above I set up my FTDI with the bitbang patches as per the instructions here

    http://doswa.com/2010/08/24/avrdude-5-10-with-ftdi-bitbang.html

    I was able to read the high fuse byte with the command

    Code ( (Unknown Language)):
    1. sudo avrdude -c ftdi -p m168 -P ft0 -U hfuse:r:-:h -B 1
    and even set them successfully with

    Code ( (Unknown Language)):
    1. sudo avrdude -P ft0 -c ftdi -p m328p -v -e -U efuse:w:0x05:m -U hfuse:w:0xD6:m -U lfuse:w:0xFF:m
    I then tried to burn a bootloader

    Code ( (Unknown Language)):
    1. avrdude -P ft0 -c ftdi -p m328p -v -e -U flash:w:Optiboot_atmega328.hex -U lock:w:0x0F:m
    everything was going fine until it reached "writing 23%" which it took a very long time on. Not knowing if it had crashed eventually my patience ran out and I hit ctrl+c on the command to try and burn a much smaller LED_blink.hex instead of a bootloader. Before I even tried to I thought I would read a fuse again just to see if it was still working. Unfortunately the same command to read a fuse that I did earlier no longer worked. Thinking that perhaps I had bricked my AVR I swapped it for another completely blank one. that one didn't work either.

    Code ( (Unknown Language)):
    1. avrdude: BitBang OK
    2. avrdude: pin assign miso 1 sck 3 mosi 0 reset 4
    3. avrdude: drain OK
    4.  
    5.  ft245r:  bitclk 4800 -> ft baud 2400
    6. avrdude: ft245r_program_enable: failed
    7. avrdude: initialization failed, rc=-1
    8.          Double check connections and try again, or use -F to override
    9.          this check.
    Needless to say I no longer have any hair left and could do with some advice as to what exactly is going wrong.
     
    Last edited: Dec 12, 2012
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Ah, I see, they used to FTDI cable to emulate the signals from a AVR programmer, interesting.....I didn't know this was possible...:D

    I'd say try running avrdude with the verbose output for a more detailed analysis of the possible errors....
     
  5. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    Just a wild guess, but did you try setting the baudrate in avrdude to 2400?

    BTW, bitbanging through an FTDI, even when it works, is said to be painfully slow. If you really want to go bitbang, then a real RS232 port is best. There are a bunch of cheap and easy programmer designs for RS232.

    If all else fails, there are USB programmers available on eBay for less than $10. Like this one. If you have Vista or Windows 7, there are others that work too. Just look for them.
     
  6. Cue

    Thread Starter New Member

    Oct 16, 2012
    15
    0
    Code ( (Unknown Language)):
    1. avrdude: Version 5.10, compiled on Dec 12 2012 at 13:31:44
    2.          Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
    3.          Copyright (c) 2007-2009 Joerg Wunsch
    4.  
    5.          System wide configuration file is "avrdude.conf"
    6.          User configuration file is "/home/user/.avrduderc"
    7.          User configuration file does not exist or is not a regular file, skipping
    8.  
    9.          Using Port                    : ft0
    10.          Using Programmer              : ftdi
    11.          Setting bit clk period        : 1.0
    12. avrdude: BitBang OK
    13. avrdude: pin assign miso 1 sck 3 mosi 0 reset 4
    14. avrdude: drain OK
    15.          AVR Part                      : ATMEGA328P
    16.          Chip Erase delay              : 9000 us
    17.          PAGEL                         : PD7
    18.          BS2                           : PC2
    19.          RESET disposition             : dedicated
    20.          RETRY pulse                   : SCK
    21.          serial program mode           : yes
    22.          parallel program mode         : yes
    23.          Timeout                       : 200
    24.          StabDelay                     : 100
    25.          CmdexeDelay                   : 25
    26.          SyncLoops                     : 32
    27.          ByteDelay                     : 0
    28.          PollIndex                     : 3
    29.          PollValue                     : 0x53
    30.          Memory Detail                 :
    31.  
    32.                                   Block Poll               Page                       Polled
    33.            Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
    34.            ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
    35.            eeprom        65     5     4    0 no       1024    4      0  3600  3600 0xff 0xff
    36.            flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
    37.            lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
    38.            hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
    39.            efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
    40.            lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
    41.            calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
    42.            signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
    43.  
    44.          Programmer Type : ft245r
    45.          Description     : SparkFun FTDI Basic Breakout

    this is the ouput from avrdude with the -vv flag. The baud rate seems to be 2400 by default with the -B 1 flag but I set it with -b to 2400 anyway and still had no luck. I'm starting to think that the ft232rl is damaged. Is there some diagnostic tests I can do?

    You are right bitbanging was painfully slow when it was working I wish I hadn't exited the bootloader burn because at the time at least it was working. I have a lot of atmega328s The beauty of the plan was that I could burn a bootloader on to one using the FT232R bitbang method, then that atmega328p can be used to build a much faster AVR programmer. that way I could learn a thing or two and save money to boot (no pun intended).


    The thing with the FTDI bitbang tutorials is that I have never seen the actual circuit. I don't even know if the crystal oscillator is needed when programming a blank chip.
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Ac external oscillator isn't necessary as the programmer is supposed to clock the chip.

    I finally looked through the bitbanged link....you have Tx and Rx swapped...according to the link:
     
    Cue likes this.
  8. Cue

    Thread Starter New Member

    Oct 16, 2012
    15
    0
    Thank you tshuck, I owe you one. TX and RX were OK on my board, it was a mistake I made on the diagram, but I swapped for yet another blank atmega328p, took out the oscillator and it's working again. Which makes me wonder, why did it work the first time with the oscillator and then not on subsequent tries?
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Did you have power applied to your uC from the 7805 while you were trying to program it?
     
  10. Cue

    Thread Starter New Member

    Oct 16, 2012
    15
    0
    Not at the same time as using the programmer but that particular blank AVR (with the crystal oscillator) had been plugged in to the regulated 7805 supply before.

    Does that affect the AVR?
     
  11. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I'm just thinking that the capacitor on RESET may have prevented the programmer from pulling this line low, which is necessary for programming an AVR. It's a thought...

    I don't see how the crystal would affect the ability to program the device...
     
    Cue likes this.
  12. Cue

    Thread Starter New Member

    Oct 16, 2012
    15
    0
    I don't know how the crystal affected it to be honest seeing as it was always using the internal clock by default. Now that you mention it I have a feeling it is the power source causing the problems. Maybe the USB is not providing a steady 5V or enough current because while I can read and write fuses again I've found that I cannot write to flash at all. AVRdude seems to hang on "writing 0%" this time on every try. I thought I would show a little patience but I've given it more than 4 hours without it even changing at all. I plan to try the 7805 source while connecting the USB FTDI at the same time, but not connect Vcc on the FTDI to my board. I'll report back once I try it out. If you think this is a bad idea, please let me know.
     
  13. DickCappels

    Moderator

    Aug 21, 2008
    2,651
    632
    The uC ships from the factory set up to operate from its internal 8 MHz RC clock divided by 8, giving the CPU a 1 MHz clock. You don't need the crystal unless somebody set the fuses to use an external crystal.

    Have you considered buying a programmer on eBay for $3? Save what little hair you have left :)
     
  14. Cue

    Thread Starter New Member

    Oct 16, 2012
    15
    0
    Hi DickCappels. Thanks for the info, I had checked all the fuses using this fuse calculator
    http://www.engbedded.com/fusecalc/ and they were all set for internal clock but would the presence of an external crystal interfere even if set for an internal clock?

    I took out a multimeter to check the FTDI USB power source and it was fine. Turns out a rather bizarre problem which I still do not fully understand was preventing me from writing to flash. A problem I only stumbled upon by sheer chance.

    I was using the command given in some of the tutorials out there

    Code ( (Unknown Language)):
    1. avrdude -P ft0 -c ftdi -p m328p -v -e -U flash:w:Optiboot_atmega328.hex -U lock:w:0x0F:m
    2.  
    This time when attempting to write to flash I decided to remove the "-e" because I thought it was redundant seeing as there is always a chip erase when writing to flash. When I removed this "-e", it worked!

    why it doesn't work with the redundant "-e" or why does AVRDUDE hang you ask? who knows. If anybody does then I would love to learn the possible reason why but for now I can program my AVRs thanks to everybody here.

    I did consider buying an AVR ISP suggested earlier. I'm in the UK so it is coming from China which often takes 2-3 weeks to arrive so I thought I would continue on while I wait, but soon enough I will have another programmer too. Hair was a small price to pay in the meantime, I wish it were a currency. :D
     
Loading...