Forums

How to read SPI data with FTDI chip FT232R?

Started by Sven September 8, 2009
Hello all

I'm trying to read and write serial data with a FTDI chip (FT232R).

I'm using the IC in Synchronous Bit Bang Mode using D0 as a CLK output and 
D1 as a DATA input/output.

First i shall shift out 16 bits on DATA and then change it to an input and 
shift back 16 bits.

I have successfully shifted out DATA but i have trouble reading back.

Anyone here having any example code?





>Hello all > >I'm trying to read and write serial data with a FTDI chip (FT232R). > >I'm using the IC in Synchronous Bit Bang Mode using D0 as a CLK output
and
>D1 as a DATA input/output. > >First i shall shift out 16 bits on DATA and then change it to an input
and
>shift back 16 bits. > >I have successfully shifted out DATA but i have trouble reading back. > >Anyone here having any example code? > > >
Have you looked at this: http://www.ftdichip.com/Documents/AppNotes/AN232R-01_FT232RBitBangModes.pdf --------------------------------------- This message was sent using the comp.arch.embedded web interface on http://www.EmbeddedRelated.com
> Have you looked at this: >
Yes But I don't understand the FT_Read() function. Does the chip read all pinns during FT_Write() and then FT_Read() returns all read values?
>> Have you looked at this: >> >Yes > >But I don't understand the FT_Read() function. > >Does the chip read all pinns during FT_Write() and then FT_Read() returns
>all read values? > >
Sorry - I just saw it implemented on a micro for in application programming over USB, I didn’t write the code myself so I can’t help you further. Best of luck to you. --------------------------------------- This message was sent using the comp.arch.embedded web interface on http://www.EmbeddedRelated.com
In article <4aa6b821$0$293$14726298@news.sunsite.dk>, sven@sven.se 
says...
> Hello all > > I'm trying to read and write serial data with a FTDI chip (FT232R). > > I'm using the IC in Synchronous Bit Bang Mode using D0 as a CLK output and > D1 as a DATA input/output.
It is easier to use a SEPARATE pin for input and output. Use FT_SetBitMode to determine which pins are input and output, once.
> First i shall shift out 16 bits on DATA and then change it to an input and > shift back 16 bits. > > I have successfully shifted out DATA but i have trouble reading back.
Because it fifos the data as you change the output, inputs are read. Using the Baud rate clock to clock out and in the values at the same time. If you read data sheet mentioned below it does say it reads FIRST ALL pins, so you will have to mask the bits to get the bit you want. So data bit input must be PRELOADED before the write occurs. You can ONLY read in that mode by writing at same time. You may have to use GPIO of other pins to enable or disable external hardware, and possibly Write 16 bits (external device enabled for write) Write 17 or 16 bits (external device DISabled for write) Read the data back possibly discarding first byte if using 17 bits for external data not preloaded. Similar to FT2232. I assume you have looked at data sheets for AN232R-01 Bit Bang Modes for the FT232R and FT245R D2XX programmers guide. Yes their guides and examples leave a lot to be desired for things missing, like timing diagrams for EACH mode and operation, as well as basic programme flow for minimal operation.
> Anyone here having any example code?
Double check the FTDI site for ALL languages as they also do not put examples for all modes in each language. Don't even get me going on their pseudo I2C implementations. -- Paul Carpenter | paul@pcserviceselectronics.co.uk <http://www.pcserviceselectronics.co.uk/> PC Services <http://www.pcserviceselectronics.co.uk/fonts/> Timing Diagram Font <http://www.gnuh8.org.uk/> GNU H8 - compiler & Renesas H8/H8S/H8 Tiny <http://www.badweb.org.uk/> For those web sites you hate
Hello again.

I'm looking more (intensively) at application note AN232R-01 now.

http://www.ftdichip.com/Documents/AppNotes/AN232R-01_FT232RBitBangModes.pdf

I notice something at page 5 about Synchronous Bit Bang Mode.

The timing diagram shows that the chip uses 6 time slots in this mode of 
operation, t1 to t6. And during t1, t3, t4, t5, t6 it reads the pins (5 time 
slots). And only at t2 (1 time slot) it writes to the pin.

So when I send X bytes with FT_Write() I should have X*5 bytes to read with 
FT_Read()?

Any thoughts about that?

I don't know if it's right. I'm going to test this tomorrow. :-)

[This followup was posted to comp.arch.embedded and a copy was sent to 
the cited author.]

In article <4aa80c7e$0$285$14726298@news.sunsite.dk>, sven@sven.se 
says...
> Hello again. > > I'm looking more (intensively) at application note AN232R-01 now. > > http://www.ftdichip.com/Documents/AppNotes/AN232R-01_FT232RBitBangModes.pdf
Did you read my previous post?
> I notice something at page 5 about Synchronous Bit Bang Mode. > > The timing diagram shows that the chip uses 6 time slots in this mode of > operation, t1 to t6. And during t1, t3, t4, t5, t6 it reads the pins (5 time > slots). And only at t2 (1 time slot) it writes to the pin. > > So when I send X bytes with FT_Write() I should have X*5 bytes to read with > FT_Read()?
First RE-read the paragraph at the TOP of that page "Synchronous Bit Bang mode is the same as the FT2232 Synchronous Bit Bang mode. With Synchronous Bit Bang mode, data will only be sent out if there is space in the device for data to be read from the pins. This Synchronous Bit Bang mode will read the data bus pins first, before it sends out the byte that has just been transmitted. It is therefore 1 byte behind the output and so to read the inputs for the byte that you have just sent, another byte must be sent. "
> Any thoughts about that?
As explained in the paragraph and the timing digram it takes 6 clock cycles (BAUD rate set) to Read inputs pause (toggle read pin to clock next input pins from your devices) clock out data pins that are outputs pause Toggle write pin to clock outputs to your devices pause
> I don't know if it's right. I'm going to test this tomorrow. :-)
As described in the quoted paragraph above data is clocked in just before data is clocked out. So you CANNOT easily do your preferred scheme of write data change direction Read data The device does not work that way. You are aware that SPI data lines are NOT bidirectional? SPI has two data lines Master Slave MOSI output input MISO input output MOSI = Master OUT Slave IN MISO = Master IN Slave OUT By the way as described in the description above, make sure you flush the RX Fifo before writing data. -- Paul Carpenter | paul@pcserviceselectronics.co.uk <http://www.pcserviceselectronics.co.uk/> PC Services <http://www.pcserviceselectronics.co.uk/fonts/> Timing Diagram Font <http://www.gnuh8.org.uk/> GNU H8 - compiler & Renesas H8/H8S/H8 Tiny <http://www.badweb.org.uk/> For those web sites you hate
Hello again

I have solved the problem. I used the FT_GetStatus() right after calling 
FT_Write(). That way FT_GetStatus() sayed I had 0 bytes to receive. Now I 
simply call FT_Read() after FT_Write() and let it wait for the same amount 
of data to read as i wrote. And I also got a lot of junk bytes to read 
because I didn't always empty the rx buffer.

The FT232R is going to be connected to a PIC MCU to the MCLR (reset), PGC 
(clock) and a two way PGD (data).

Thank's all :-)

And also thank you for the nice timing diagram font. :-)

> Paul Carpenter | paul@pcserviceselectronics.co.uk > <http://www.pcserviceselectronics.co.uk/> PC Services > <http://www.pcserviceselectronics.co.uk/fonts/> Timing Diagram Font > <http://www.gnuh8.org.uk/> GNU H8 - compiler & Renesas H8/H8S/H8 Tiny > <http://www.badweb.org.uk/> For those web sites you hate