Hacker News new | ask | show | jobs
by jodoherty 764 days ago
FTDI also makes a lot of USB chips with software controllable GPIO pins:

https://www.adafruit.com/product/2264

https://ftdichip.com/products/ft2232h-mini-module/

Bit banging on a modern OS subjects you to a lot of jitter though. It's not like using a parallel port in DOS where you just have to worry about interrupts. The preemptive scheduler can really mess up your timing.

That said, the FT232H, FT2232H, and FT4232H have an FTDI Multi-Protocol Synchronous Serial Engine (MPSSE) cores that you can program to protocols like SPI and I2C where the high speed part doesn't require any smart logic to handle. It's a bit of a special skill though (you send MPSSE specific command bytes over the USB interface into the chip's command buffer and tell it to execute them).

If you need more high speed smarts, it's also convenient to use a Raspberry Pi Pico with MicroPython or CircuitPython with Programmable I/O (Pio) with an interactive session:

https://www.raspberrypi.com/news/what-is-pio/

https://docs.micropython.org/en/latest/rp2/quickref.html#pro...

But yeah, beyond that, you're better off using an Arduino or something and doing it all on the microcontroller.

On the plus side, all of these things are relatively cheap and easy to obtain.

2 comments

I wanted this for GPIO on my PC to interface with some hardware, but all the prebuilt USB TF232 adapters had the GPIO pins closed off, and AFAICT the TF232 requires flashing using some proprietary windows binary to get into a mode where the GPIO pins can be used as GPIO (since it has multiple modes of operation).

I can't believe I'm not missing something... Is there an off the shelf USB GPIO device somewhere? Plug it in and start using the linux GPIO driver?

The solution my friends gave me was "buy an arduino", flash the arduino, and use the arduino's gpio... which yeah, I could do, but is that really what it takes for a $2000 desktop to flip a bit these days?

Here a few options for you:

https://www.adafruit.com/product/2264 - USB to GPIO (and other stuff)

"Bus Pirate"

Or get a RasPi - it's not your desktop PC, but they're running Linux with direct GPIO access available in userspace.

The FT232H you linked in the default UART configuration has no GPIO pins according to the datasheet pin description table. You need to change to MPSSE mode or similar using the flashing tool.
The Adafruit FT232H works out of the box with pyftdi and libftdi. You don't have to use any special tools or flash it in any way. The USB commands are handled by the underlying libraries.

See page 9 of the datasheet here:

https://ftdichip.com/wp-content/uploads/2020/07/DS_FT232H.pd...

The async and sync bitbang columns denote the GPIO pins as D0-D7 and show them assigned to the 8 ADBUS pins that are provided on the breakout board.

Assuming the udev rules are set up in Linux, you can simply install pyftdi, open the device, and start using the ADBUS pins as GPIO pins:

https://eblot.github.io/pyftdi/gpio.html#setting-gpio-pin-st...

If you're using libftdi, you want to call ftdi_set_bitmode with the BITMODE_BITBANG enum value for the mode:

https://www.intra2net.com/en/developer/libftdi/documentation...

Then the ftdi_read_data and ftdi_write_data functions can be used to read or write to the ADBUS pins:

https://www.intra2net.com/en/developer/libftdi/documentation...

https://www.intra2net.com/en/developer/libftdi/documentation...

You can then build a nice, simple high level GPIO interface over that if you want.

That (or an ESP) is a really effective, easy, and cheap solution, which makes it hard for a more limited and more expensive solution to take hold. Most everyone who wants a digital output is capable of following the Arduino route to the end.
There's little market for the product you crave. Most people who know what GPIO is know how to buy a $5 microcontroller with a USB port and upload some firmware to convert serial commands to the pin states/transitions they need.
Parallel port adapters maybe?
Also, while libftdi isn't hard to work with:

http://developer.intra2net.com/git/?p=libftdi;a=blob;f=examp...

It's dead simple to also use these FTDI devices with Python:

https://eblot.github.io/pyftdi/api/index.html