Jetson Nano, SPI bus: write bytes works but not read

Hi,

  • On jetson Nano I’ve ena ble the SPI0 (pins 19, 21, 23, 24, 26 on J41) with jeston-io utility. I’ts OK and I can see /dev/spidev0.0
  • I use spidev (in CC+) to manage the SPI data bus : See SPI userspace API — The Linux Kernel documentation
  • SPI bus speed is set to 1MHz
  • The transmit (from Nano to device) is working well and I can see (on a logic analyzer) that the device replies correctly: see attached
  • The pb is that the byte read in the SPI rx_buffer are always 0…like if only the SPI MOSI was enable but not MISO …!

I’ve tried also several things to fix that (changing the SPI_MODE, a jumper between pins 19/21 of J41 to get same data received than emitted, …)

here is the short C code I use:
To init the SPI bus /dev/spidev0.0
int SPI::initSPI(char *device, unsigned int speed, unsigned int delay_usecs, unsigned char bits_per_word, unsigned char cs_change)
{
memset(&config, 0, sizeof config);
memset(spi_rxBuf, 0, sizeof spi_rxBuf);
memset(spi_txBuf, 0, sizeof spi_txBuf);

config.rx_buf = (unsigned long) &spi_rxBuf;
config.tx_buf = (unsigned long) &spi_txBuf;
config.delay_usecs = delay_usecs; // delay after the last bit transfer before optionally deselecting the device before the next transfer
config.speed_hz = speed;  // Bus speed in Hz
config.bits_per_word = bits_per_word;
config.cs_change = cs_change;  // True to deselect device before starting the next transfer

fd = open(device, O_RDWR);  // fd is a private file descriptor
usleep(1000);  // Wait a bit after the opening
if (fd < 0)
    return -1;

return 0;  // return OK
}

To perform a synchronous transfer to/from device on /dev/spidev0.0
spi_dsPIC.spi_txBuf[0] = 0x55; // Fill buffer to be sent
spi_dsPIC.spi_txBuf[1] = 0xC0;
spi_dsPIC.spi_txBuf[2] = 4;
spi_dsPIC.spi_txBuf[3] = 0;
spi_dsPIC.spi_txBuf[4] = 0;
spi_dsPIC.spi_txBuf[5] = 0;
spi_dsPIC.spi_txBuf[6] = 0;
spi_dsPIC.spi_txBuf[7] = 0;
config.len = 8; // complete the len in the structure
ioctl(fd, SPI_IOC_MESSAGE(1), &config); // normally spi_rxBuf contains reply

We are stuck on this pb since 2 days.
Thanks for help.

Do you mean the loopback working as expect, but not working with device connect?
Have you try any others speed?

No The loop back is not working. Only the MOSI pin send the correct bytes but MISO do not read anything.
I ve tried all the speeds from 100KHz to 5MHz same pb remains.
It seems if only the SPI MOSI was enable but not MISO …! see the logic analyser chart:
the protocol is perfect (bytes send par Nano and reply done by device) but no character read

Did you working on r32.4.3?
Could you check the pin by sudo cat /sys/kernel/debug/tegra_gpio

pin read :

Name:Bank:Port CNF OE OUT IN INT_STA INT_ENB INT_LVL
A: 0:0 64 40 40 04 00 00 000000
B: 0:1 00 00 00 00 00 00 000000
C: 0:2 00 00 00 00 00 00 000000
D: 0:3 00 00 00 00 00 00 000000
E: 1:0 00 00 00 00 00 00 000000
F: 1:1 00 00 00 00 00 00 000000
G: 1:2 00 00 00 00 00 00 000000
H: 1:3 fd 99 00 60 00 00 000000
I: 2:0 07 07 03 02 00 00 000000
J: 2:1 00 00 00 00 00 00 000000
K: 2:2 00 00 00 00 00 00 000000
L: 2:3 00 00 00 00 00 00 000000
M: 3:0 00 00 00 00 00 00 000000
N: 3:1 00 00 00 00 00 00 000000
O: 3:2 00 00 00 00 00 00 000000
P: 3:3 00 00 00 00 00 00 000000
Q: 4:0 00 00 00 00 00 00 000000
R: 4:1 00 00 00 00 00 00 000000
S: 4:2 80 80 00 00 00 00 000000
T: 4:3 01 01 00 00 00 00 000000
U: 5:0 00 00 00 00 00 00 000000
V: 5:1 02 00 00 02 00 00 000000
W: 5:2 00 00 00 00 00 00 000000
X: 5:3 78 08 08 70 00 60 606000
Y: 6:0 02 00 00 02 00 00 000000
Z: 6:1 0e 08 08 04 00 06 020600
AA: 6:2 00 00 00 00 00 00 000000
BB: 6:3 00 00 00 00 00 00 000000
CC: 7:0 92 80 80 10 00 12 121200
DD: 7:1 00 00 00 00 00 00 000000
EE: 7:2 00 00 00 00 00 00 000000
FF: 7:3 00 00 00 00 00 00 000000

What do you mean by r32.4.3 ? How can I chech that ?

Check the version with below command. How do you test the loopback mode?
cat /etc/nv_tegra_release

version:

R32 (release), REVISION: 4.4, GCID: 23942405, BOARD: t210ref, EABI: aarch64, DATE: Fri Oct 16 19:44:43 UTC 2020

loop back: a jumper on J41 pin 19 and 21 with logic analyzer on this jumper (to see what is passing thru)
Then running my C++ code (sending a specific byte sequence of 8 bytes and receiving them synchronously):
result : I can see on logic analyser that the specific byte sequence of 8 bytes is sent correctly (and abviuosly on MISO because of the jumper) but never receive by my code (using the spidev library)

Could you verify the loopback by spidev_test?

https://elinux.org/Jetson/TX1_SPI#Running_the_Loopback_Test

OK but sources/kernel/Documentation/spi does not exist on my nano

OK I found the way to run the loopBackTest (L4T).
after run, results are :

spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

FF FF FF FF FF FF
40 00 00 00 00 95
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
DE AD BE EF BA AD
F0 0D
how do they mean?

Could you run below command to check if it pass or failed.

sudo ./spidev_test -D /dev/spidev3.0 -s4000000 -g16 -n10

The binary from below link

Hi Shane,

In fact the reply from loopBackTest (spidev_test) was correct. So I digged a bit in its source to compare with mine and I found the problem:
The “config” structure sent in ioctl(fd, SPI_IOC_MESSAGE(1), &config); must be totally initialized.
I simply added memset(&config, 0, sizeof config); before the init of this structure. Now it works well.

Thanks for your help.
bernard