More FT232 Bit Banging with the Arduino Board


We’re guessing that all the confusion came about because of the way people were used to bit banging: Set a pin state, read a pin state, set a new state, etc. This naturally lends itself to a programming style where each pin state is a separate write to the LPT port, the GPIO port, etc.

Trying to run the FT232 like this gives pretty slow operation, since each write must be transformed into a USB packet, wrapped with all the other data to tell the FT232 which endpoint to send this stuff to, and sprinkled with a few cordialities that are just part of the USB protocol “conversation”. So the overhead of using one USB transaction for each bit transition is very inefficient.

That’s where we usually see people giving up, and getting into flame wars on forums about how useless the FTDI bit banging is. These poor souls are just misunderstanding the way that this circuit was meant to operate – so while we can get some results this way, it’s really quite arduous.

A more efficient method is to group many bit transitions together, and send them all down the USB pipeline at one time. You still have the same overhead of making a USB transaction, but it is “amortized” over 8, 16, 256 bits so the overall process is much faster.

The structure of ftd2xx.dll makes this pretty easy – FT_Write expects an array of data as the second argument. If you’re using VB, this array of data will be a string of characters generated by piping the actual byte data through CHR() to convert it to a character.

Let’s look at an example. It is good practice when bit-banging to first read the state of the pins, and then to use OR and AND to set bits high or low so the other bits are not disturbed. You’ll see that in the source code, but for brevity we will assume that the port reads back 00 initially. We want to toggle 4 clocks on bit 7, so we will need an array of 8 bytes – one byte each for bringing the pin high, then low. Our array will look like

  • mydata[0] = $80 //Pull bit 7 high – first rising edge
  • mydata[1] = $00 //Bit 7 low – first falling edge
  • mydata[2] = $80 //Bit 7 high
  • mydata[3] = $00 //Bit 7 low
  • mydata[4] = $80 //Bit 7 high
  • mydata[5] = $00 //Bit 7 low
  • mydata[6] = $80 //Bit 7 high
  • mydata[7] = $00 //Bit 7 low

And then we send this whole chunk of 8 edge transitions down USB to be shifted out of the FT232 pins at a rate defined by the baud clock:

lResult = FT_Write(lHandle, mydata, lBytesToWrite, lBytesThatWereWritten)

Which brings us to the concept of baud clock. Usually, this means something like 9600 or 14400. But we can think of it as the clock that is used to send each of our data out to the pins, and it can be anything up to 3 million. 300, 12,000, 1,700,000 are all valid clock rates for the bit bang data. In the above example, mydata[0] is shifted out the first time the FT232′s internal baud clock goes high. mydata[1] is shifted out on the second clock, and so on. So each transition (or read) requires one baud clock, and making up/down toggles like we are doing above requires two baud clocks each – one for up, one for down.


Continued on Next Page…


This entry was posted in Hacks. Bookmark the permalink.

4 Responses to "More FT232 Bit Banging with the Arduino Board"

Leave a reply