Roland's homepage

My random knot in the Web

Using the FT232 with Python

In this article I will try to document how to with with the FT232H using Python on FreeBSD. I will be using the Adafruit FT232H breakout board.

Note that since I’m using FreeBSD, the library provided by FTDI doesn’t work. However, the library provided by Adafruit was written for Python 2, and doesn’t work with Python 3. I tried tinkering with it a bit to get it to work with Python 3, but that did not work out. In the end, I went with pyftdi since it doesn’t require a native library and it just works.

Hardware

Pin definitions

The Adafruit FT232H board has general purpose input/output (“GPIO”) pins D0−D7 (ADBUS0ADBUS7) and C0−C7 (ACBUS0ACBUS7). In “Application Note AN_108” from FTDI these are referred to as “Bit0” though “Bit7” and “Bit8” through “Bit15”.

Pinout diagram of the Adafruit FT232H board

The functions of these pins are shown in the picture above.

Software

Adafruit GPIO library

The adafruit article about using the FT232H board mentions different libraries. Their Adafruit GPIO library uses libftdi.

Since I’m using Python 3, I want to use the FT232H with that as well. However, this did not work. The libftdi from the FreeBSD ports system only supports Python 2. So I compiled and installed it myself for Python 3. And then I started hacking the adafruit code to get it to work with Python 3 which didn’t go smoothly. In Python 3, libftdi returns bytes, while the GPIO library assumes str. After fixing this assumption, it still didn’t work reliably. So then I started looking into swig to see how it creates the Python interface. This was becoming a deep-dive into a rabbit hole.

Libmpsse

As an alternative, I also installed an actively maintained fork of libmpsse. But this didn’t work well for me either.

Pyftdi

So I turned to pyftdi. This is a pure python library that does not require libftdi, and for me it worked out of the box. The nice thing is that with it you can use the FT232H for UART, I²C, SPI and GPIO.

Setting up pyftdi

The pyftdi software isn’t available as a FreeBSD port. But its dependencies py-serial and pyusb are in the ports tree, so I’ll install those first for use with Python 3.

# cd /usr/ports/comms/py-serial
# make FLAVOR=py36 install clean
# cd /usr/ports/devel/py-usb
# make FLAVOR=py36 install clean

Next I downloaded the pyftdi library from github and built it.

> cd ~/github
> git clone https://github.com/eblot/pyftdi.git
> cd pyftdi
# umask 022
# python3 setup.py install

Unloading the native driver

FreeBSD has a native serial driver that is automatically used when an FT232 device is connected. This needs to be unloaded and disabled in order to use pyftdi. On FreeBSD the first is achieved by running kldunload uftdi.ko as root. The second step is accomplished by commenting out the nomatch statement in /etc/devd/usb.conf that loads uftdi driver and restarting devd by running service devd restart as root.

Using pyftdi

With pyftdi I could write modules and applications for using several sensors that can connect to the FT232H. You can find them on github:

  • PMS5003 dust sensor
  • BME280 temperature, pressure and humidity sensor
  • BMP280 temperature and pressure sensor.

For comments, please send me an e-mail.


Related articles


←  Measuring particulate matter with the PMS5003 Setting the Razer ornata chroma color from userspace  →