Giuseppe Parrello

 

Development Board FTDI FT232H



Introduction

This development board, based on the FTDI FT232H chipset, can be used to manage external digital devices, such as sensors, LEDs, displays, buzzers. This card provides synchronous/asynchronous serial connections, in this page we will mainly use it to manage external digital devices via I2C protocol and SPI protocol using its USB interface.
Note that this card provides 16 GPIO-type digital inputs/outputs that will be used to manage simple external digital components, such as relays.
Unfortunately, this board does not support external analog devices, in order to solve this lack we will use, on another page, another development board suitable for this purpose.


USB device installation

Before managing this development board, we need to make sure that the Operating System recognizes it correctly. Below we will face the problems on installing USB drivers in the Microsoft Windows operating system and in a router, assuming that whoever has minimum manual skills in managing the Linux operating system may make this development board visible to the operating system itself.


USB device installation in Microsoft Windows 7/8/10

As soon as we insert this development board into a USB socket on a computer with a Microsoft Windows operating system, the operating system will recognize it as a serial device, that is, as a COMx port. This could be useful for those who want to manage this development board as a simple USB-Serial adapter, but in this case we need the USB drivers to manage this device as a real external USB device. To do this, we need to perform the following steps:

  1. Download "Zadig" application from the following website and run it as an administrator user.
  2. Within the above-mentioned application, open the "Options" menu and select the "List All Devices" item.
  3. Select the device "Single RS232-HS" (USB ID 0403 6014).
  4. As driver select "libusb-win32" in the driver list.
  5. Click on the "Replace Driver" button and wait for the message confirming the driver installation.
  6. Inside the Windows "Device Manager" window check that the "libusb-win32 Usb Devices" entry exists directly as the main entry (and not within the main entries).

To manage this board we need the Python language, so we need to install Python on computer by performing the following steps:

  1. Go to Python website and download the full .exe version (Python version 3 or later) and install it as an administrator user.
  2. To set the environment variables and use Python directly from the Windows CMD window, restart the computer or log out and log back in with the current user.
  3. Open a Windows CMD window, and run the following command "pip install --upgrade pip wheel pyserial pyusb".
  4. Open the Python interpreter by executing the command "python" and within the interpreter execute the two instructions "import usb.core" and "usb.core.find()", as output should be returned the "DEVICE ID" of this development board - an example of the output of the above two instructions is as follows:
    c:\Python37-32\Lib\site-packages\usb>python
    Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import usb.core
    >>> usb.core.find()
    <DEVICE ID 0403:6014 on Bus 000 Address 001>
    
    If the "DEVICE ID" is not returned or if an error is returned, see the next paragraph dedicated to errors.

USB device installation in a router

As soon as we plug this development board into a USB socket on a router with a Linux operating system, the operating system will recognize it as a USB device. We can open a Telnet or SSH terminal to connect to the router and type the command "dmesg", an example of the output of the above command is the following:

usb 3-1.3: new high-speed USB device number 7 using ehci-platform
usbcore: registered new interface driver usbserial
usbcore: registered new interface driver usbserial_generic
usbserial: USB Serial support registered for generic
usbcore: registered new interface driver option
usbserial: USB Serial support registered for GSM modem (1-port)

Then we enter the command "lsusb", an example of the output of the above command is the following:

Bus 003 Device 007: ID 0403:6014

To manage this board we need the Python language, therefore we need to install Python in the router, by performing the following steps:

  1. Go to the page "How to install the Python interpreter" page of this site to install Python on the router - to manage this development board we need version 3 (or later) of Python.
  2. Open a Telnet or SSH terminal to connect to the router and run the following command "pip3 install --upgrade pip wheel pyserial pyusb".
  3. Always inside terminal run the following command "opkg install libusb-1.0 libusb-compat gcc".
  4. Open the Python interpreter by executing the command "python3" and inside the interpreter execute the two instructions "import usb.core" e "usb.core.find()", the "DEVICE ID" of this development board should be returned as output - an example of the output of the above two instructions is as follows:
    admin@RT-AC86U:/# python3
    Python 3.7.4 (default, Oct 03 2019, 21:10:12)
    [GCC 7.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import usb.core
    >>> usb.core.find()
    <DEVICE ID 0403:6014 on Bus 003 Address 007>
    
    If the "DEVICE ID" is not returned or if an error is returned, see the next paragraph dedicated to errors.

Errors on recognition of the development board

If the Python interpreter does not recognize the development board or if an error is returned, then we need to activate the DEBUG mode within the Python interpreter to understand what is the problem. Open the Python interpreter and execute the following four instructions "import os", "os.environ ['PYUSB_DEBUG'] = 'debug' ", "import usb.core", "usb.core.find()" - an example of output of the above four instructions is as follows:

admin@RT-AC86U:/# python3
Python 3.7.4 (default, Oct 03 2019, 21:10:12)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['PYUSB_DEBUG'] = 'debug'
>>> import usb.core
>>> usb.core.find()
2021-01-05 18:01:09,600 ERROR:usb.libloader:'Libusb 1' could not be found
2021-01-05 18:01:09,603 ERROR:usb.backend.libusb1:Error loading libusb 1.0 backend
2021-01-05 18:01:09,620 ERROR:usb.libloader:'OpenUSB library' could not be found
2021-01-05 18:01:09,621 ERROR:usb.backend.openusb:Error loading OpenUSB backend
2021-01-05 18:01:09,671 ERROR:usb.libloader:'Libusb 0' could not be found
2021-01-05 18:01:09,673 ERROR:usb.backend.libusb0:Error loading libusb 0.1 backend
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/lib/python3.7/site-packages/usb/core.py", line 1297, in find
raise NoBackendError('No backend available')
usb.core.NoBackendError: No backend available

The above-mentioned error "No backend available" is mainly displayed on a router as the Python USB module cannot find the system USB libraries that are required to manage the connected external USB devices - the Entware libraries are located in the "/opt/lib" folder and not in the router's libraries folder (which is usually "/usr/lib").
In order to solve this problem:

  1. Open a Telnet or SSH terminal to connect to the router and run the command "cd /opt/lib/[python]/site-packages/usb/backend" (instead of "[python]" enter the correct folder where Python interpreter is installed) to enter the folder of the Python module that manages USB devices.
  2. Create a backup copy of the "libusb1.py" file by running the command "cp libusb1.py libusb1.py.src".
  3. Open the above-mentioned file "libusb1.py" with an editor (for example "nano") and go to the end of the file in the "get_backend" method and change the line from
     _lib = _load_library(find_library=find_library)
    to
     _lib = _load_library(find_library=lambda x: "/opt/lib/libusb-1.0.so.0")
  4. In the above line replace "/opt/lib/libusb-1.0.so.0" with the file actually placed in the folder "/opt/lib" whose name begins with "libusb-1".
  5. Enter the Python interpreter again and run the above four lines of DEBUG again to check if this time the development board is recognized correctly - for example a resolving output could be:
    admin@RT-AC86U:/# python3
    Python 3.7.4 (default, Oct 03 2019, 21:10:12)
    [GCC 7.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.environ['PYUSB_DEBUG'] = 'debug'
    >>> import usb.core
    >>> usb.core.find()
    2021-01-12 20:11:21,259 DEBUG:usb.backend.libusb1:_LibUSB.__init__(<CDLL '/opt/lib/libusb-1.0.so.0', handle 9e22d0 at 0x9bd8c0>)
    2021-01-12 20:11:21,260 INFO:usb.core:find(): using backend "usb.backend.libusb1"
    2021-01-12 20:11:21,260 DEBUG:usb.backend.libusb1:_LibUSB.enumerate_devices()
    2021-01-12 20:11:21,260 DEBUG:usb.backend.libusb1:_LibUSB.get_device_descriptor(<usb.backend.libusb1._Device object at 0x9cfe00>)
    <DEVICE ID 0403:6014 on Bus 003 Address 007>
    

 

FTDI device recognition

To recognize the FTDI device, first of all we need to run the command "pip install pyftdi" to install the dedicated Python module PyFTDI (for more information on this module, refer to this Internet site).
Then open the Python interpreter and run the following two instructions "from pyftdi.ftdi import Ftdi" and "Ftdi.show_devices()" - an example of the output of the above two instructions is the following:

admin@RT-AC86U:/# python3
Python 3.7.4 (default, Oct 03 2019, 21:10:12)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyftdi.ftdi import Ftdi
>>> Ftdi.show_devices()
Available interfaces:
ftdi://ftdi:232h:3:8/1 (Single RS232-HS)

In the above output, the line "ftdi://ftdi:232h:3:8/1" provides with the FTDI address of the development board - this address will be used in Python scripts to manage devices connected to this development board.
If the "No backend available" error appears in the above output, then refer to the paragraph "Errors on recognition of the development board" on this page to solve the above-mentioned error.

 

I2C mode

The I2C mode is the most important mode that we will consider since with this mode we will be able to manage many external devices by connecting them to this development board. The I2C mode, in this reference development board, is enabled by shorting the pins AD1 and AD2 (see image for more details). All I2C devices have at least 4 connectors, listed below:

Image Development Board External Device

Modalità I2C

AD0 SCL
AD1 + AD2 SDA
+5V / +3.3V VCC / V+
GND GND

Since I2C mode reserves AD0-AD2 pins, only the AD3-AD7 and AC0-AC9 pins are available as GPIO pins on this reference development board. These pins could be used by external devices to activate some options or modes, such as resetting the external device.

In I2C mode it is possible to manage the external device, connected to this development board, through a hexadecimal address, used by all Python scripts to manage external devices. To get the hexadecimal address of the external device, go to the PyFTDI website and download the following Python script. Once downloaded, run it via the Python interpreter. An example of the output of the above script is the following:

admin@RT-AC86U:/# python3 i2cscan.py

   0 1 2 3 4 5 6 7 8 9 A B C D E F
0: . . . . . . . . . . . . . . . .
1: . . . . . . . . . . . . . . . .
2: . . . . . . . . . . . . . . . .
3: . . . . . . . . . . . . . . . .
4: . . . . . . . . . . . . . . . .
5: . . . . . . . . . . . . R . . .
6: . . . . . . . . . . . . . . . .
7: . . . . . . . . .

In the above example, the hexadecimal address of the device is "5D". Note the "R" indicating that the device is read-only (the example device is a temperature sensor). If a "W" appears in place of the "R", then we can write to the external device.

 

SPI mode

The SPI mode is the other most important mode that we will consider since, even with this mode, we will be able to manage external devices by connecting them to this development board. SPI devices all have at least 5 connectors, listed below (see image for more details):

Image Development Board External Device

Modalità SPI

AD0 SCLK / SCK
AD1 MOSI / SDO / DO
AD2 MISO / SDI / DI
AD3 CS / SS
+5V / +3.3V VCC / V+
GND GND

Since SPI mode reserves the AD0-AD3 pins, only the AD4-AD7 and AC0-AC9 pins are available as GPIO pins on this reference development board. These pins could be used by external devices to activate some options or modes, such as resetting the external device.

 

Python library installation

Adafruit CircuitPython library installation

Since some Python scripts require the presence of the Adafruit CircuitPython library (a fork of the MicroPython library), instructions for its installation are provided below. To install this library, execute the command "pip install Adafruit-Blinka", also keep in mind the following installation guidelines as this library requires necessarily, to properly work with the FT232H development reference board, the presence of the environment variable "BLINKA_FT232H=1" (more information can be found on the Adafruit website):

Then open the Python interpreter and run the following two instructions "import board" and "dir(board)" - an example of the output of the above two instructions is the following:

admin@RT-AC86U:/# python3
Python 3.7.4 (default, Oct 03 2019, 21:10:12)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import board
>>> dir(board)
['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'D4', 'D5', 'D6', 'D7', 'I2C', 'MISO', 'MOSI', 'SCK', 'SCL', 'SCLK', 'SDA', 'SPI', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'ap_board', 'board_id', 'detector', 'pin', 'sys']

The two instructions display the pins supported by the Adafruit CircuitPython library for the reference development board, pins from "C0" to "D7" are the GPIO pins, while pins from "I2C" to "SPI" are the pins reserved for I2C and SPI modes.


Pillow module installation

Since some Python scripts require the presence of the Pillow module (a fork of the Python Imaging Library), instructions for installing it are provided below.