Serial Peripheral Interface (SPI)
3 wire communication - SCK, MOSI, MISO.
spi-dev
https://github.com/gschorcht/spi-ch341-usb
insmod spi-ch341-usb.ko
modprobe spidev
rmmod ch341
echo spidev > /sys/bus/spi/devices/spi0.0/driver_override
echo spi0.0 > /sys/bus/spi/drivers/spidev/bind
ls /dev/spi*
CH341A USB to SPI adapter
The test
SPI on Arduino Nano
You can easily access devices through SPI by SPI.transfer()
with Arduino Nano.
I've connected an AT25DF321A 4MB SPI Flash for this test and tried to read ID of this IC. After a successful connection and programming, the serial monitor will give this result:
0x0 - 0x1f - 0x47 - 0x1 - 0x0
/*
* Sample code for reading AT25DF321A Manifacturer and Device ID through SPI - Arduino Nano
* 2022 - Tóthpál István <istvan@tothpal.eu>
*
* CS0 - PIN 10
* MOSI - PIN 11
* MISO - PIN 12
* SCK - PIN 13
*/
#include <SPI.h>
const int CS0Pin = 10;
void setup() {
Serial.begin(115200);
Serial.print("SPI start\n");
byte buffer[5]; // 1st for command + 4 for result
for (int i=0; i<sizeof(buffer); i++) buffer[i]=0;
buffer[0] = 0x9f; // command code - read Manufacturer and device Id AT25DF321A, returns 4 byte
pinMode(CS0Pin, OUTPUT);
digitalWrite(CS0Pin, HIGH);
SPI.begin();
SPI.beginTransaction(SPISettings(100000, MSBFIRST, SPI_MODE0));
digitalWrite(CS0Pin, LOW);
SPI.transfer(buffer,sizeof(buffer));
digitalWrite(CS0Pin, HIGH);
SPI.endTransaction();
SPI.end();
char s[255];
for (int j=0; j<sizeof(buffer)-1; j++) {
sprintf(s,"0x%x - ",buffer[j]);
Serial.print( s );
}
sprintf(s,"0x%x\n",buffer[sizeof(buffer)-1]);
Serial.print( s );
Serial.print("SPI end\n");
}
void loop() {
}