Nav:  [home][elec][periph][usb-fx2][software] > [advanced_examples]
 
Go:  [local] [io] [advanced] [fx2pipe]

Electronics -- USB-FX2: Advanced Software Examples

A collection of advanced software for the USB-FX2 board.

Download All Advanced Examples in an Archive

In order to try the examples on this page, you should download this tarball since the code on this page does not contain the necessary include files which are (of course) part of the archive.

Source: usb-fx2-advanced-examples-0.5.tar.gz   [23kb gzipped source tarball]
Version:0.5   (2009-08-23)
Author:Several authors; report bugs to Wolfgang Wieser   (report bugs here)
License:GNU GPL (Version 2)
Requires:libusb, cycfx2prog

Building an example source is just as simple as with the local examples.

Note: All these examples have been tested with my USB-FX2 board connected to a Debian Linux box running kernel-2.6.29 and libusb-1.0. The USB-FX2 was connected to a USB-2.0 (high speed) port on an high speed hub connected to an ICH9-based chipset from Intel.

If you experience trouble, please fix them and drop me a note. Also check out the troubleshooting hints.

convert_serial: Emulate a Virtual Serial Port Made by FTDI

convert_serial is a very interesting example program written by Brent Baccala. Many thanks to him for making it available! The main features are:

  • The FX2 re-numerates to present itself as a USB-to-serial converter made by FTDI.
  • Then, the FX2 reads data sent over the /dev/ttyUSB device, and sends it back with lowercase converted to uppercase.
  • It installs a timeout to send back data even if the result buffer is not yet full.

But let's do it step-by-step. The source code convert_serial.c is fairly well commented, so we'll focus on how to run the demo code on our Linux box.

After calling make run, you should see in the syslog that the FX2 is re-numerating (disconnecting from the USB and re-connecting with different vendor and product IDs): Since the Linux kernel comes with built-in support for the FTDI USB-to-serial converters, the associated module ftdi_sio is loaded automatically. (Well, if you compile the kernel yourself, make sure to select the FTDI module.)

kernel: usb 8-5.1: USB disconnect, address 123
kernel: usb 8-5.1: new high speed USB device using ehci_hcd and address 124
kernel: usb 8-5.1: config 1 interface 0 altsetting 0 bulk endpoint 0x81 has invalid maxpacket 64
kernel: usb 8-5.1: config 1 interface 0 altsetting 0 bulk endpoint 0x1 has invalid maxpacket 64
kernel: usb 8-5.1: New USB device found, idVendor=0403, idProduct=8372
kernel: usb 8-5.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
kernel: usb 8-5.1: Product: FX2 case converter
kernel: usb 8-5.1: Manufacturer: freesoft.org
kernel: usb 8-5.1: configuration #1 chosen from 1 choice
kernel: ftdi_sio 8-5.1:1.0: FTDI USB Serial Device converter detected
kernel: usb 8-5.1: Detected SIO
kernel: usb 8-5.1: FTDI USB Serial Device converter now attached to ttyUSB0

We can ignore the messages about "invalid maxpacket 64". It merely states that the FX2 registers endpoint 1 with a buffer size of 64 bytes instead of 512 bytes as required by the USB-2.0 high speed standard. However, there are 2 reasons for using 64 bytes: First, the FX2 internally only supports 64 bytes for EP 1 and second, the ftdi_sio kernel module expects 64 bytes as well since FTDI's USB-to-serial converters are all USB-1.1 "full speed" devices.

Now, the FX2 is attached to /dev/ttyUSB0 and we can actually write to and read from that interface. The convert_serial example is written to echo back everything and convert lowercase characters to uppercase. You can test this using a terminal program such as minicom or even easier by opening two terminals:

user@bash# cat /dev/ttyUSB0
HELLO
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG AND DOES THIS
WITHOUT PAYING ATTENTION TO THE TRAFFIC LIGHTS DOWN THE ROAD.
 
user@bash# cat > /dev/ttyUSB0
hello
The quick brown fox jumps over the lazy dog and does this
without paying attention to the traffic lights down the road.

Of course, the right terminal is where messages can be typed in and sent to the FX2. The result which gets sent back from the FX2 can be seen in the left termial window.

Note that the convert_serial demo here does not implement any of the special control commands used by the FTDI SIOs like setting the baud rate, latency, stop bits and so on. After all, electrically, there is no serial port invlolved here, it's just a virtual serial port in software.

convert_serial2: Emulate a Sierra Wireless Modem

convert_serial2 is my own modification of the above convert_serial program. It works just as described above, but the main differences are:

  • The FX2 re-numerates as Sierra Wireless modem.
  • Instead of EP 1, the FX2 uses endpoints 2 and 6 which are configured as quad-buffered with 512 bytes per buffer.

After calling make run, you should see in the syslog that the FX2 is re-numerating as Sierra Wireless device and the associated linux kernel module sierra gets loaded:

kernel: usb 8-5.1: USB disconnect, address 33
kernel: usb 8-5.1: new high speed USB device using ehci_hcd and address 34
kernel: usb 8-5.1: New USB device found, idVendor=1199, idProduct=0017
kernel: usb 8-5.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
kernel: usb 8-5.1: Product: FX2 case converter
kernel: usb 8-5.1: Manufacturer: WieserLabs & freesoft.org
kernel: usb 8-5.1: configuration #1 chosen from 1 choice
kernel: sierra 8-5.1:1.0: Sierra USB modem converter detected
kernel: usb 8-5.1: Sierra USB modem converter now attached to ttyUSB0

We now have the FX2 attached to /dev/ttyUSB0 just like in the example above and and we can actually write to and read from that interface.

The main benefit of this code is that the Sierra Wireless driver is already in the Linux kernel and does not send any control characters along with the data stream. So, with minor modifications to the firmware, you can stream data directly between /dev/ttyUSB0 and the FX2's FIFO interface - without writing special host software.

erase_eeprom: Erase an Attached EEPROM

This is a basic program making use of the I2C interface. The program will do byte writes of 0xff to all 8192 EEPROM cells of a standard 24LC64 EEPROM from microchip. It assumes that A0 is tied to positive supply and A1,A2 of the EEPROM are tied to ground like on the USB-FX2 board. Of course, you can modify the program do different things.

Features:

  • Writes I2C address, EEPROM address and data; and checks for ACK from the attached EEPROM after each of these steps.
  • Does ACK polling to wait for the write cycle to complete.

Note: The program writes each byte once (no page writes) and for 8192 bytes of a 24LC64 it takes about 30 seconds! So, if you really want to erase the EEPROM, wait long enough.


[home] [site map] [Impressum] [Datenschutz/privacy policy]
Valid HTML 4.01!
Copyright © 2006-2009 by Wolfgang Wieser
Last modified: 2009-08-23 23:41:39