Programmer

CH341A USB to SPI and GPIO Linux kernel driver

CH341A USB to SPI and GPIO Linux kernel driver

CH341A USB to SPI and GPIO Linux part driver

The driver can be utilized with CH341A USB to UART/I2C/SPI connector sheets to interface SPI captives to a Linux have. It utilizes either the quick SPI equipment interface which is, notwithstanding, restricted to SPI mode 0 or a sluggish SPI bit banging execution.

Moreover, CH341A information sticks that are not utilized for the SPI point of interaction can be arranged as GPIO pins. The driver can create programming hinders for all information pins. One info pin can be associated with the CH341A intrude on pin to create equipment interferes. Nonetheless, since USB is an offbeat correspondence framework, ensuring accurate timings for GPIOs and interrupts is absurd.
Constraints of the SPI interface

The SPI equipment interface execution is restricted to

SPI mode 0 (CPOL=0, CPHA=0)
fixed clock recurrence of around 1.4 MHz,
low dynamic CS signal,
single piece moves,
8 pieces for each word, and
3 slaves at most extreme.

In view of the extremely restricted documentation and applications that are practically all in Chinese, it is difficult to sort out whether these boundaries can be changed through control orders. Subsequently you need to live with this arrangement for what it’s worth to utilize the equipment execution :- (

The piece banging execution permits the accompanying SPI modes

SPI mode 1 (CPOL=0, CPHA=1)
SPI mode 2 (CPOL=0, CPHA=1)
SPI mode 3 (CPOL=0, CPHA=1)

as well as high dynamic CS signals. It is exceptionally sluggish. Just a SCK clock recurrence of around 400 kHz can be reached, so one byte takes around 14 us. Nonetheless, every byte of a message must be sent as a different USB message to the connector in light of its bitwise execution and the extremely restricted USB endpoint support sizes. This outcomes into a deferral of around 6.5 ms between every byte. Moreover, dealing with the CS signal before the exchange and after the exchange creates an unexpected setback of around 20 ms. Consequently, it takes about

20 ms + n * 0.014 ms + (n-1) * 6.5 ms

to move a message of n bytes.

The two executions permit the transmission with MSB first (SPI_MSB_FIRST) and LSB first (SPI_LSB_FIRST).
SPI setup

 

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

 

GPIO configuration

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:

  • Direction of pins that are configured as input or output can be changed during runtime.
  • Pin 21 (D6/IN2) can only be configured as input. It’s direction can’t be changed during runtime.
  • At least one of the CS data pins D0…D2 has to be configured as CS signal.
  • One of the inputs can be configured to generate hardware interrupts for rising edges of signals. For that purpose, the pin has to be connected with the CH341A INT pin 7.

 

Installation of the driver

Prerequisites

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

githubn Full Information

Related Articles