Serial programming AVR with FTDI FT232BM Bit Bang Mode

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Serial programming AVR with FTDI FT232BM Bit Bang Mode

Johnathan Corgan
This is my first AVR project.

I have built a board which uses an ATMega8 and an FTDI FT232BM USB to
serial converter chip.

For normal operation, the FT232BM serial TXD and RXD are routed to the
appropriate serial port pins on the ATMega8.

For programming, I have routed four pins from the FT232BM to RESET,
MISO, MOSI, and SCLK, so I can use the bit bang mode of the part for
programming operations.

I have started work on a programming library under Windows with Cygwin,
that encapsulates the FTDI driver DLL with the routines for sending and
receiving data over the SPI interface.  I've verified I can put the
ATMega8 into serial programming mode, read the device signature, erase
the chip, and read/write fuse bits.  So I'm pretty confident the
low-level SPI interface works well.

At this point, I was going to start building actual Flash and EEPROM
programming routines.  The end goal would be a standalone "updater"
program specifically for the project/device I'm building.

However, might it be possible to modify avrdude to use my routines for
the low-level SPI protocol?  I'd avoid massively reinventing the wheel...

I don't have any experience with avrdude itself, this being my first AVR
project.  Nor would I have any familiarity with the avrdude source code
structure, or how it might be modularized to accommodate replacing its
existing low-level routines with mine.  (Of course I have a long
background in C programming and microcontrollers, just new to AVR and
avrdude.)

Right now, my library is hard coded for the ATMega8 and the particular
pin mapping from the FT232BM USB chip to the SPI interface.  It would be
fairly easy to make the pin mapping configurable.

Anyway, I'm at a point where I need to decide what is easier:  write my
own special purpose, limited functionality programmer on top of my
library, or figure out how to add my library to avrdude and get all the
creamy richness of a mature programmer code base.  If I did the latter
then my "updater" program would simply be a script that invokes avrdude
with the right command line parameters.

Suggestions?

-Johnathan



_______________________________________________
avrdude-dev mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/avrdude-dev
Reply | Threaded
Open this post in threaded view
|

Re: Serial programming AVR with FTDI FT232BM Bit Bang Mode

Johnathan Corgan
Johnathan Corgan wrote:

> Suggestions?

Let me be the first to suggest to myself to read the list archives. :-)

-Johnathan


_______________________________________________
avrdude-dev mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/avrdude-dev
Reply | Threaded
Open this post in threaded view
|

Re: Serial programming AVR with FTDI FT232BM Bit Bang Mode

Johnathan Corgan
Johnathan Corgan wrote:
> Johnathan Corgan wrote:
> Let me be the first to suggest to myself to read the list archives. :-)

Just to continue the practice of talking to myself, I realize all the
recent bit bang traffic was using a serial port, not the FTDI bit bang
stuff.  So my original post/questions still stand.

-Johnathan


_______________________________________________
avrdude-dev mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/avrdude-dev
Reply | Threaded
Open this post in threaded view
|

Re: Serial programming AVR with FTDI FT232BM Bit Bang Mode

Joerg Wunsch
In reply to this post by Johnathan Corgan
As Johnathan Corgan wrote:

> For normal operation, the FT232BM serial TXD and RXD are routed to
> the appropriate serial port pins on the ATMega8.

> For programming, I have routed four pins from the FT232BM to RESET,
> MISO, MOSI, and SCLK, so I can use the bit bang mode of the part for
> programming operations.

Curious, I'd assume bit-banging across USB to be really slow.  It's
already really slow on a standard serial port, but for USB, each bit
change will require a USB packet of its own.  Why not simply use a
bootloader?

> Suggestions?

My only concern for AVRDUDE would be that there's currently no
infrastructure to maintain separate device drivers as part of the
project.  (There's giveio.sys for the Win32 parallel-port bit-banging,
but that's quite different.)

--
cheers, J"org               .-.-.   --... ...--   -.. .  DL8DTL

http://www.sax.de/~joerg/                        NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)


_______________________________________________
avrdude-dev mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/avrdude-dev
Reply | Threaded
Open this post in threaded view
|

Re: Serial programming AVR with FTDI FT232BM Bit Bang Mode

Michael Holzt-3
In reply to this post by Johnathan Corgan
> Just to continue the practice of talking to myself, I realize all the
> recent bit bang traffic was using a serial port, not the FTDI bit bang
> stuff.  So my original post/questions still stand.

As you've probably seen, i'm currently working on adding serial bitbang and
moving bitbang code into a more general approach. It might be a good idea to
add your adapter as just another flavor of bitbanging...

Do you have some working code and schematic of your approach?



Regards
Michael

--
      It's an insane world, but i'm proud to be a part of it. -- Bill Hicks


_______________________________________________
avrdude-dev mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/avrdude-dev
Reply | Threaded
Open this post in threaded view
|

Re: Serial programming AVR with FTDI FT232BM Bit Bang Mode

Johnathan Corgan
In reply to this post by Joerg Wunsch
Joerg Wunsch wrote:

> Curious, I'd assume bit-banging across USB to be really slow.  It's
> already really slow on a standard serial port, but for USB, each bit
> change will require a USB packet of its own.  

That's not how the FT232BM bit bang mode works.

Basically, it turns the eight RS232 signals into a parallel port, but
retains the concept of "baud rate."  If you write a buffer of data out
to the device in this mode, the chip will clock out the bytes over this
parallel bus at a definable baud rate.  So the trick is to wire one of
these pins to SCLK, then toggle the value of that bit in successive
bytes in the buffer.  Some other pin gets mapped to MOSI, and you set
the corresponding pin to the bit value to send in two successive bytes.

The effect of this is that when the FT232BM clocks these successive
bytes over the (bit bang) bus, the SCLK bit is toggling and the MOSI bit
has the serial data.

So one byte of data to send to SPI becomes sixteen bytes to send over
USB (two per bit, one with SCLK low and one with SCLK high).  FTDI
supports 4096 byte USB packets, so you can send 256 bytes of real data
through the SPI per USB packet.

 From a throughput perspective, the FT232BM supports baud rates up to 1
Mbit/sec, and since it takes two bauds per SPI bit, the maximum SPI
clock rate is about 512Kpbs.  It takes 32 bits or 64 bauds to send a
command, so one can send a maximum of 16K commands per second, assuming
there is no requirement to pause between commands.

This also assumes one is doing "blind writing", with no intervening
reads.  Unfortunately, to read the pin status (in bit bang mode, each
pin is set to either an input or output), one has to do a read command
after *every byte* sent.  This does reduce throughput drastically, as it
turns into a single USB packet per byte in each direction.  Continuous
throughput in this mode is about 256 bytes per second, or about 64
commands per second.  This would be the case whenever you need to read
the response from the AVR to whatever command was sent.  (I think this
can be improved by fiddling with the USB timers but I haven't played
with it yet.)

Anyway, I've used this part and this technique before to program Xilinx
FPGAs upon start up over USB, eliminating the need for a configuration
PROM.  It does go pretty fast.  On my project it's already there to
provide a USB serial interface to the AVR, so it seems useful to take
advantage of its bit bang capabilities to program the part as well.

Here is a link to FTDI's application note:

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

> Why not simply use a bootloader?

I need to be able to fiddle with fuse bits and chip erase.  Can an AVR
boot loader do this?

> My only concern for AVRDUDE would be that there's currently no
> infrastructure to maintain separate device drivers as part of the
> project.  (There's giveio.sys for the Win32 parallel-port bit-banging,
> but that's quite different.)

Well, I assume there is some sort of abstraction in the software
architecture that becomes device independent at a point in the stack.
I'd have no problem GPLing some code that would export an interface at
this level and let it be statically linked into the code.  (If that is
what you meant.)

Right now the code I've written so far assumes my exact board
configuration and doesn't have any facility to dynamically set which
RS2323 pins are wired to the SPI pins.  It's missing a bunch of sanity
and error condition checks, and in general is just something I hacked
together to be able to help debug the hardware board. But it's working
so far :-)

-Johnathan






_______________________________________________
avrdude-dev mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/avrdude-dev
Reply | Threaded
Open this post in threaded view
|

Re: Serial programming AVR with FTDI FT232BM Bit Bang Mode

Johnathan Corgan
In reply to this post by Michael Holzt-3
Michael Holzt wrote:

> As you've probably seen, i'm currently working on adding serial bitbang and
> moving bitbang code into a more general approach. It might be a good idea to
> add your adapter as just another flavor of bitbanging...

I haven't looked at your patch yet, but I suspect there would be a
"abstraction mismatch" with how my code works.  See my previous post on
how the bit banging mode of the FT232BM is designed.  One doesn't toggle
bits so much as build a buffer of data to send as a batch.

The lowest level primitive in my library is 'ftavr_send_32bits_fast'
which builds a 64 byte buffer, based on a supplied 32 bit command value,
to send over the USB bit banged  This is used for sending commands one
at a time, when one doesn't need to read the result, e.g., erase_chip().

Another one is 'ftavr_send_32_slow', which sends 32 bits over SPI and
returns the value clocked out by the AVR.  This is very slow over USB
but is how commands like set_spi_pgm_mode(), read_signature_byte(),
read_fuse_bits_low(), etc., are implemented.

I'm in the middle of implementing an arbitrary length data send, which
expands a supplied buffer of data into the necessary bit bang bytes (per
my previous email) and sends them over USB.  This would be the case if
you wanted to send multiple commands back-to-back with no delay in between.

> Do you have some working code and schematic of your approach?

Working code, yes.  The schematic is basically the FT232BM reference
design with TXD, RXD, RTS, and DTR pins wired to an ATMega8 MISO, MOSI,
SCLK, and RESET pins.

-Johnathan


_______________________________________________
avrdude-dev mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/avrdude-dev
Reply | Threaded
Open this post in threaded view
|

Re: Serial programming AVR with FTDI FT232BM Bit Bang Mode

Michael Holzt-3
In reply to this post by Johnathan Corgan
> So one byte of data to send to SPI becomes sixteen bytes to send over
> USB (two per bit, one with SCLK low and one with SCLK high).  FTDI
> supports 4096 byte USB packets, so you can send 256 bytes of real data
> through the SPI per USB packet.

This means you "precalculate" the protocol and then transit it block-wise?

I don't see why this can't be made compatible with the bitbang design.
Instead of the direct output the par and serbb drvier does, the pin changes
can be stored into a buffer and transmitted as a block over usb later.

Regards
Michael

--
      It's an insane world, but i'm proud to be a part of it. -- Bill Hicks


_______________________________________________
avrdude-dev mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/avrdude-dev
Reply | Threaded
Open this post in threaded view
|

Re: Serial programming AVR with FTDI FT232BM Bit Bang Mode

Johnathan Corgan
Michael Holzt wrote:

> This means you "precalculate" the protocol and then transit it block-wise?

Yes.  You just summarized in one sentence what it took me five
paragraphs to explain :-)

> I don't see why this can't be made compatible with the bitbang design.
> Instead of the direct output the par and serbb drvier does, the pin changes
> can be stored into a buffer and transmitted as a block over usb later.

Well, this sounds right.  Guess I'll take the plunge and read through
the avrdude code and your patch.  This might be easier to do than I
expected.

-Johnathan


_______________________________________________
avrdude-dev mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/avrdude-dev