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.
The Adafruit FT232H board has general purpose input/output (“GPIO”) pins D0−D7 (ADBUS0−ADBUS7) and C0−C7 (ACBUS0−ACBUS7). In “Application Note AN_108” from FTDI these are referred to as “Bit0” though “Bit7” and “Bit8” through “Bit15”.
The functions of these pins are shown in the picture above.
Adafruit GPIO library
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
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.
As an alternative, I also installed an actively maintained fork of
libmpsse. But this didn’t work well for me either.
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
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
as root. The second step is accomplished by commenting out the
/etc/devd/usb.conf that loads
uftdi driver and restarting
devd by running
service devd restart as root.
- On the nature of GUI programs
- Have Python log to syslog
- Including binary data in Python scripts
- A simple feed reader for Youtube
- Attempting a conky replacement in Python (part 2)