Stellaris - USB Host FTDI
This wiki describes an implementation of USB Host FTDI on Stellaris LM4F microcontroller.
Since the FTDI driver is closed source, the implementation of the LM4F Stellaris USB Host FTDI is based on the open source libFTDI project. For more information regarding running libFTDI on Windows machine, please refer here.
The software block diagram of USB Host FTDI implementation is shown as follows:
As seen above, the USB Host FTDI library is built on top of the USB Host Control Driver (HCD) of the StellarisWare USB library. The application communicates with the USB Host FTDI library by means of a set of API functions and also callback functions.
USB Host FTDI API
The USB Host FTDI API consists of the following simple API functions (found in usbftdi.h header file):
- USBHFTDIDriveOpen(): opening an instance of FTDI device - must be called during initialization before any are FTDI present
- USBHFTDIDriveClose(): closing an opened FTDI device instance
- USBHFTDIWrite(): send data to the FTDI device to be sent out through UART output of FTDI device
- USBHFTDIRead(): read data from the FTDI (if available)coming from UART input of FTDI device
- USBHFTDISetBaudRate(): set baud rate of FTDI UART lines
- USBHFTDISetLineProperty(): set UART configuration of FTDI devices, including number of data bits, number of stop bits, parity type
- USBHFTDISetBitMode(): set bit mode of FTDI device (must FTDI_BITMODE_RESET for UART communication)
The callback function, whose pointer is passed by the application to the USB Host FTDI Library when calling USBHFTDIDriveOpen(), is used for notifying application of asynchronous events.
There are 4 types of event which can be passed by the library when calling the callback function:
- FTDI_EVENT_OPEN: a new FTDI device has been attached and successfully enumerated by the library and ready to use
- FTDI_EVENT_CLOSE: the FTDI device has been disconnected from the host
- FTDI_EVENT_RX_NEW_DATA: incoming data from FTDI device available in the library receive buffer and can be read using USBHFTDIRead() function
- FTDI_EVENT_RX_BUF_FULL : the internal buffer of library receive buffer is full
The source code for the USB Host FTDI can be download in the following link: File:Lm4f usb host ftdi.zip. It includes project files for CCS v5.3 and IAR EWARM.
Testing USB Host FTDI
Hardware and Software Tools
For testing the current implementation of USB Host FTDI, the following hardware and software tools are used:
- EK-LM4F232 Evaluation Kit with LM4F232H5QD microcontroller (USB-OTG capability) on-board as USB Master.
- Code Composer Studio version 5.3.0.00090 with TI ARM Compiler v5.0.3
- FTDI USB TTL Serial Cables as USB slave.
- Hyperterminal like software such as Teraterm or Coolterm
The hardware setup for testing the USB Host FTDI code above is shown as follows:
As shown above, the test requires two FTDI USB TTL Cables:
- FTDI USB TTL Cable 1 is the FTDI device which is connected to EK-LM4F232 as USB slave.
- FTDI USB TTL Cable 2 is optional and shall be connected to the PC. The purpose of this cable is to verify the UART data sent by the FTDI USB TTL Cable 1.
To simulate the data reception, the TX and RX line of the FTDI USB TTL Cable 1 shall be connected together. If FTDI USB TTL Cable 2 is used, the RX line shall be connected to TX and RX line of FTDI USB TTL Cable 1. The FTDI USB TTL Cable 2 is used in the test to verify the UART data sent by FTDI USB TTL Cable 1, because although the RX and TX FTDI USB TTL Cable 1are connected, this doesn't mean that the UART configuration is correct. FTDI USB TTL Cable 1 can send data in wrong baud rate on TX line but still the RX line can read the data because it is working on the same "wrong baud rate" as the TX line. Therefore to verify this, FTDI USB TTL Cable 2 is needed.
The EK-LM4F232 shall be connected to the PC by means a mini USB cable which is delivered with the evaluation kit.
- Connect the EK-LM4F232 to PC and open a hyperterminal like software on the PC connected to the "Stellaris Virtual Serial Port" with UART configuration 115200 baud, 8 data bits, No Parity, 1 stop bit.
- Press reset button on the kit, it should printout "== LM4F FTDI Host Example=="
- If FTDI USB TTL Cable 2 is used, connect it first to the FTDI USB TTL Cable 1 (see connection above) and the PC. Run another instance of hyperterminal on the PC connected to FTDI USB TTL Cable 2 with UART configuration 2400 baud, 8 data bits, No Parity, 1 stop bit.
- Connect FTDI USB TTL Cable 1 to EK-LM4F232, on the Stellaris hyperterminal, the EK-LM4F232 shall send message that it is changing the FTDI baud rate to 2400 baud and waiting for an input from the user.
- Press any key on the PC to continue the test and on hyperterminal of FTDI USB TTL Cable 2, it shall display the UART messages from FTDI USB TTL Cable 1 (coming from the EK-LM4F232) as follows:
- After 10 loops, the EK-LM4F232 will notify that it is changing the baud rate to another baud rate. In this case the UART configuration of hyperterminal of FTDI USB TTL Cable 2 shall be adapted to the new baud rate, before pressing any key on the PC to continue the test.
- Repeat the last three steps as many times as necessary.
- The example application is written in the way to show the receive buffering capability of USB Host FTDI. Therefore although it receives as soon as it sends something (RX and TX line are connected), it will wait until it receives 4 times FTDI_EVENT_RX_NEW_DATA before it reads out the data by using USBHFTDIRead().
- On the Debug hyperterminal of EK-LM4F232, the application will write the sent and received data. However in lower baud rate, it is possible that the FTDI doesn't deliver the whole string in one chunk data as follows:
Tweaking The Code
- Number of receive buffers and the size of each receive buffer are defined as with NUM_OF_RX_BUF and RX_BUF_SIZE in usbhftdi.c. This can be changed if necessary.
- Although the incoming data looks like asynchronous from application point of view, basically the receive data is polled by the stack. To adjust polling rate, change PIPE_BULK_IN_POLLING_INTERVAL in usbhftdi.c (Refer also to StellarisWare USB Library documentation for USBHCDPipeConfig()).
- To add new device types (currently this implementation only supports FT232R type) and other features (e.g. bit bang, etc.), please refer to the libFTDI project.
The following code is the port of the project running on Tiva Launchpad. Credit to Mr. Marco Casaroli.