Controlling the DTR and RTS pins of the FT232 using API's from D2XX Library
Welcome to the second installment of the tutorial on using D2XX library with FT232.In the previous article we managed to communicate with FT232 chip and establish a connection.In this article we are going to access the pins of FT232 using the API's provided by the library.More specifically we are going to SET and CLEAR the DTR and RTS pins of the FT232.
In order to find out the pins numbers of RTS and DTR signals refer to the datasheet of the FT232 chip you are using .Some FT232 breakout boards may use QFN package of FT232 so there will be a slight difference in the pin numbers compared to what is mentioned here (which is a 28 pin package FT232RL).
If you are using USB2SERIAL board the Signal names are properly marked and is available outside as bergstrips.
All the C sourefiles along with D2XX library used in this tutorial can be downloaded from our GitHub Page.For this Tutorial use the "RTS_DTR_Control " Directory inside the code repository.
For this tutorial the only hardware you need are an FT232 breakout board / USB2SERIAL board,a couple of resistors(1K) and a couple of LED's .Even though you are going to interface only 2 signals, I urge you to build a small circuit with 8 LED's with all their negative terminal tied to ground and positive terminals brought out through 1 K resistors.You can use it easily to test the output of any 8 bit data port and would be quite useful when you go through the rest of the tutorials.Also you can use one colour for the first four LED's and another color for the rest of the four LED's,Its makes it easy to visualize nibbles (4 bit).
The circuit diagram is shown below D0....D7 are inputs where you can connect the signals from FT232 .The GND should be connected to the ground of the FT232.
This is the photo of the one made using the above circuit diagram.
In FT232RL which is the 28pin package chip also used in the USB2SERIAL board ,Signal DTR is available at pin 2 and RTS is available at pin 3 of the chip.
Please note that both the pins are active low,So clearing the pin makes it high while setting the pin makes the pin low.
In the USB2SERIAL board both RTS and DTR are available outside through berg strips.
Connect the pins DTR and RTS to the LED's in the board.Take the ground from a common point.If you are using the USB2SERIAL board you can take it from connector P4 or K2.
Accessing DTR and RTS pins using D2XX library
In the last tutorial we saw how to interface and compile the D2XX library with your C source code using GCC.In this section we are going to use two built in functions to clear and set the pins DTR and RTS of the FT232 chip.
The functions for manipulating DTR are
FT_ClrDtr(handle) -> Clears the DTR pins,(makes it High,since it is active low)
FT_SetDtr(handle) -> Sets the DTR Pins,(makes it Low,since it is active low)
The functions for manipulating RTS are
FT_ClrRts(handle) -> Clears the RTS pins,(makes it High,since it is active low)
FT_SetRts(handle) -> Sets the RTS Pins,(makes it Low,since it is active low)
handle parameter in the above functions is the one we use to open a connection using FT_Open(0,&handle);
The functions returns FT_OK if successful or an error code.
Now you can type the following code into your favorite text editor and compile it using gcc same as in the first tutorial.Please note that the example provided in the zip file and the one shown here is slightly different as the other one has a lot of bells and whistles while the one shown here has just enough to show case the functions mentioned above.
//open a connection to the device
if(ft_status == FT_OK)
//function to clear the DTR pin
//function to set the DTR pin
FT_Close(ft_handle); //Close the connection at the end
Save the file as DTR_D2XX.c (or Dtr.c )and compile it using GCC and run it .(Connect the device first).I have written another C file called DTR_RTS_D2XX.c which illustrate the use of both RTS and DTR pins.The below figure shows the output of that file.All codes along with library and header files can be downloaded from here.
Connection between USB2SERIAL and LED's.
In order to control the RTS pins replace FT_ClrDtr(ft_handle) with FT_ClrRts(ft_handle) and FT_SetDtr(ft_handle) with FT_SetRts(ft_handle).