Linux kernel driver for CH341A USB to SPI and GPIO adapters
Linux kernel driver for CH341A USB to SPI and GPIO adapters
The driver can be used with CH341A USB to UART/I2C/SPI adapter boards to connect SPI slaves to a Linux host. It uses either the fast SPI hardware interface which is, however, limited to SPI mode 0 or a slow SPI bit banging implementation.
Additionally, CH341A data pins that are not used for the SPI interface can be configured as GPIO pins. The driver can generate software interrupts for all input pins. One input pin can be connected with the CH341A interrupt pin to generate hardware interrupts. However, since USB is an asynchronous communication system, it is not possible to guarantee exact timings for GPIOs and interrupts.
The SPI hardware interface implementation is limited to
Because of the very limited documentation and applications that are almost all in Chinese, it is impossible to figure out whether these parameters can be changed by means of control commands. Therefore you have to live with this configuration as it is if you want to use the hardware implementation :-(
The bit banging implementation allows the following SPI modes
as well as high active CS signals. It is very slow. Only a SCK clock frequency of about 400 kHz can be reached, so that one byte takes around 14 us. However, each byte of a message has to be sent as a separate USB message to the adapter because of its bitwise implementation and the very limited USB endpoint buffer sizes. This results into a delay of about 6.5 ms between each byte. Additionally, handling the CS signal before the transfer and after the transfer causes an additional delay of about 20 ms. Thus, it takes about
20 ms + n * 0.014 ms + (n-1) * 6.5 msto transfer a message of n bytes.
Both implementations allow the transmission with MSB first (SPI_MSB_FIRST) and LSB first (SPI_LSB_FIRST).
The driver uses following CH341A pins for the SPI interface.
| Pin | Name | Direction | Function SPI (CH341A) |
|---|---|---|---|
| 18 | D3 | output | SCK (DCK) |
| 20 | D5 | output | MOSI (DOUT) |
| 22 | D7 | input | MISO (DIN) |
| 15 | D0 | output | CS0 |
| 16 | D1 | output | CS1 |
| 17 | D2 | output | CS2 |
With these three CS signals, three slaves can be used at maximum at the interface.
Five of the data pins can be configured as GPIO pins if they are not used for the SPI interface or as chip select signals:
| Pin | Name | SPI Function (default) | Configurable as (CH341 default in bold face) |
|---|---|---|---|
| 15 | D0 | CS0 | Input, Output, CS |
| 16 | D1 | CS1 | Input, Output, CS |
| 17 | D2 | CS2 | Input, Output, CS |
| 19 | D4 | OUT2 | Input, Output |
| 21 | D6 | IN2 | Input |
Please note:
To compile the driver, you must have installed current kernel header files.
Even though it is not mandatory, it is highly recommended to use DKMS (dynamic kernel module support) for the installation of the driver. DKMS allows to manage kernel modules whose sources reside outside the kernel source tree. Such modules are then automatically rebuilt when a new kernel version is installed.
To use DKMS, it has to be installed before, e.g., with command
sudo apt-get install dkmson Debian based systems.
The driver can be compiled with following commands:
git clone https://github.com/gschorcht/spi-ch341-usb.git
cd spi-ch341-usb
make
sudo make installIf DKMS is installed (recommended), command sudo make install adds the driver to the DKMS tree so that the driver is recompiled automatically when a new kernel version is installed.
In case you have not installed DKMS, command sudo make install simply copies the driver after compilation to the kernel modules directory. However, the module will not be loadable anymore and have to be recompiled explicitly when kernel version changes.
If you do not want to install the driver in the kernel directory at all because you only want to load it manually when needed, simply omit the sudo make install.
Once the driver is installed, it should be loaded automatically when you connect a device with USB device id 1a86:5512. If not try to figure out, whether the USB device is detected correctly using command
lsusband try to load it manually with command:
insmod spi-ch341-usb.koTo uninstall the module simply use command
make uninstallin the source directory.
Since the CH341A also provides an I2C interface as USB device with same id, you have to unload the driver module with
rmmod spi-ch341-usbbefore you can load the driver module for the I2C interface.
Per default, the driver configures the GPIOs as following and polls the inputs with a default rate of 100 Hz and 10 ms period, respectively.
| Pin | SPI Function | GPIO function | GPIO name | IRQ |
|---|---|---|---|---|
| 15 | CS0 | - | - | - |
| 16 | CS1 | - | - | - |
| 17 | CS2 | - | - | - |
| 19 | - | Input | gpio4 | hardware |
| 21 | - | Input | gpio5 | software |
GPIO configuration as well their polling rate can be changed according to your requirements. The direction of GPIO pins configured as inputs or outputs can be changed during runtime.
To change GPIO configuration, simply change the variable ch341_board_config that should be self-explaining. This variable contains structured entries for each configurable pin. Each entry consists of the pin number, the GPIO mode used for the pin, the name used for the GPIO in the Linux host and a flag whether the pin is connected with the CH341A hardware interrupt pin INT. Default configuration is:
struct ch341_pin_config ch341_board_config[CH341_GPIO_NUM_PINS] =
{
// pin GPIO mode GPIO name hwirq
{ 15, CH341_PIN_MODE_CS , "cs0" , 0 }, // used as CS0
{ 16, CH341_PIN_MODE_CS , "cs1" , 0 }, // used as CS1
{ 17, CH341_PIN_MODE_CS , "cs2" , 0 }, // used as CS2
{ 19, CH341_PIN_MODE_IN , "gpio4" , 1 }, // used as input with hardware IRQ
{ 21, CH341_PIN_MODE_IN , "gpio5" , 0 } // used as input
};In this configuration, pins 15 to 17 are used as CS signals while pin 19 and 21 are used as inputs. Additionally, pin 19 is connected with the CH341A hardware interrupt pin INT that produces hardware interrupts on rising edge of the signal connected to pin 19.
To define a pin as output, simply change the GPIO mode to CH341_PIN_MODE_OUT. For example, if you would like to configure only one CS signal and the other CS signal pins as GPIO outputs, the configuration could look like the following:
struct ch341_pin_config ch341_board_config[CH341_GPIO_NUM_PINS] =
{
// pin GPIO mode GPIO name hwirq
{ 15, CH341_PIN_MODE_CS , "cs0" , 0 }, // used as CS0
{ 16, CH341_PIN_MODE_OUT, "gpio2" , 0 }, // used as output
{ 17, CH341_PIN_MODE_OUT, "gpio3" , 0 }, // used as output
{ 19, CH341_PIN_MODE_IN , "gpio4" , 1 }, // used as input with hardware IRQ
{ 21, CH341_PIN_MODE_IN , "gpio5" , 0 } // used as input
};Please note:
hwirq set to 1).hwirq set to 1) MUST also be connected to the CH341A INT pin 7.hwirq to 0 for all entries.GPIO inputs are polled periodically by a separate kernel thread. GPIO polling rate defines the rate at which the kernel thread reads GPIO inputs and determines whether to generate software interrupts. That is, it defines the maximum rate at which changes at GPIO inputs can be recognized and software interrupts can be generated.
The GPIO polling rate is defined by its period in milliseconds using the constant CH341_POLL_PERIOD_MS. The period must be at least 10 ms, but should be 20 ms or more if possible dependent on the performance of your system. Please check your syslog for messages like "GPIO poll period is too short by at least %n msecs". This message is thrown if the defined CH341_POLL_PERIOD_MS is shorter than the time required for one reading of the GPIOs.
The higher GPIO polling rate is, the higher is the system usage by the kernel thread. On the other hand, the probability that short interrupt events will be lost grows, the lower the GPIO polling rate becomes.
GPIO polling rate can also be changed using the module parameter poll_rate either when loading the module, e.g.,
sudo modprobe spi_ch341_usb poll_rate=50or as real root during runtime using sysfs, e.g.,
echo 50 > /sys/module/spi_ch341_usb/parameters/poll_periodPlease note: Since the CH341A hardware interrupt signal INT uses a separate USB endpoint, the maximum rate of hardware interrupts is independent on the GPIO polling rate and can reach up to 400 Hz.
Once the driver is loaded successfully, it provides up to three SPI slave devices on next available SPI bus, e.g.,
/dev/spidev0.0
/dev/spidev0.1
/dev/spidev0.2according to the naming scheme /dev/spidev.. is the bus number selected automatically by the driver and is the chip select signal of the according pin. Please note that independent on how many pins are configured as chip select signals, pin 15 gives always 0, pin 16 gives always 1, and pin 17 gives always 2 as chip select signal.
Standard I/O functions like open, ioctl and close can be used to communicate with one of the slaves connected to the SPI.
To open an SPI device simply use:
int spi = open("/dev/spidev0.0", O_RDWR));Once the device is opened successfully, you can modify SPI configurations and transfer data using ioctl function.
uint8_t mode = SPI_MODE_0;
uint8_t lsb = SPI_LSB_FIRST;
...
ioctl(spi, SPI_IOC_WR_MODE, &mode);
ioctl(spi, SPI_IOC_WR_LSB_FIRST, &lsb);Function ioctl is also used to transfer data:
uint8_t *mosi; // output data
uint8_t *miso; // input data
...
// fill mosi with output data
...
struct spi_ioc_transfer spi_trans;
memset(&spi_trans, 0, sizeof(spi_trans));
spi_trans.tx_buf = (unsiNo open issues yet, or sync has not completed.