Thursday, February 22

Linux Adds CH341 GPIO

There was a time when USB to serial hardware meant one company: FTDI. But today there are quite a few to choose from and one of the most common ones is the WCH CH341. There’s been support for these chips in Linux for a while, but only for use as a communication port. The device actually has RS232, I2C, SPI, and 8 general purpose I/O (GPIO) pins. [ZooBaB] took an out-of-tree driver that exposes the GPIO, and got it working with some frightening-looking CH341 boards.

He had to make a slight mod to the driver to get six GPIOs in /sys/class/gpio. Once there though, it is easy to manipulate the pins using a shell script or anything that can write to the virtual files corresponding to the GPIO pins.

For example, he did a speed test that was this simple:

#!/bin/bash
x=100000
while ((x--)); do
 i=$((i+1))
 echo 0 > /sys/class/gpio/gpio1/value
 echo 1 > /sys/class/gpio/gpio1/value
done

He got about 2.2 kHz out of the output pin, and although he didn’t say the exact hardware configuration it gives you some idea about the possible speed.

There are some other examples, and a look at several inexpensive boards that expose the I/O pins. There’s also some discussion of some mods of those boards.

The ability to share and hack drivers is one of the things that makes Linux so great for hackers. Your Linux system probably has all the tools you need and, if not, they are a package manager command away. Even if you aren’t comfortable building a whole driver, patching one like [ZooBab] did is very doable.

Of course, there are faster ways to drive I/O. We looked at the details of the CH340 and CH341 way back in 2014.

No comments:

Post a Comment