WCH CH341 USB to Serial Chip Gets Linux Drivers to Control GPIOs over USB

USB to serial chips are often used as a debug interface either directly on the target board, or via a dedicated debug board. But some models have extra pins exposed, and one of those is WCH CH341, which also includes I2C & SPI interfaces and up to 8 GPIOs.

But software support for those extra pins is not currently built-in into the drivers found in Linux mainline, and you’d also have to find a board that breakout the relevant pins. It turns out there are few of things including “CH341A ALL IN 1 USB to SPI/I2C/IIC/UART/TTL/ISP serial adapter” board going for $10 shipped on Aliexpress, and which Zoobab successfully used to control 6 (out of 8) GPIOs over USB.

The board comes with a USB board to connect to your computer, several header for I2C, UART, SPI, some LEDs, and jumper to select I2C/SPI or UART mode and voltage (5V/3.3V).

The board is recognized differently whether you use I2C/SPI or UART mode in Linux:

  • I2C-SPI mode

  • UART mode

You’ll however need the out of tree i2c-ch341-usb driver to expose the 8 GPIOs and control them over USB/I2C. He set the direction to output for 6 of the pin in the source code (6-7 could not be set to output for whatever reason):

After after build the module, and load the module:


gpio0 to gpio5 would should in /sys/class/gpio, which means you can control them with the usual sysfs commands:


He also benchmarked the pins with a shell script to turn on and off connected LED, and managed to do that at around 2.2KHz. It may be a little faster by changing the I2C bus speed (100 kHZ by default) and/or using C code instead. Alternatively, using spi-ch341-usb driver from the same developer (gschorcht) may speed things up a little bit despite the documented “slow SPI”.

Another shell script with 6 LEDs connected to the board is demonstrated below.

Anyway that good news since that means you can add (extra) GPIOs to any Linux board with USB relatively easily and cheaply. But if you’re not quite willing to spending $10 on that option, there are cheaper options such as a CH341 USB programmer going for $2.30 + shipping on Electrodragon, or CJMCU-341 board sold for around $5 including shipping on Aliexpress or eBay.

Share this:

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

Radxa Orion O6 Armv9 mini-ITX motherboard
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
22 Comments
oldest
newest
Boardcon CM3588 Rockchip RK3588 System-on-Module designed for AI and IoT applications