Master-Slave Communication Between Two Jetson Nano over SPI

I have two Jetson Nano devices that need to exchange data over SPI protocol. It is my first and only project with Jetson Nano, so I am completely new to the SPI or anything regarding low-level coding.

In the project, I want to use Daisy Chain to communicate, for starters I have used only two devices to test the data communication between them.

Using the following link on page 4, I made one a slave. https://www.nxp.com/files-static/training_pdf/26821_68HC08_SPI_WBT.pdf
What it says is that in order to select one device as a slave you need to connect SS to ground to make it low. So I have connected pin 24 (SPI_1_CS0) to pin 20 which is ground. For the master, I have put pin 24 (SPI_1_CS0) to pin 2 (5.0 VDC). I intended to set it high for it to become the master.

I have used /opt/nvidia/jetson-io/jetson-io.py to configure the pins. After running sudo modprobe spidev the pins were configured.

Output of ls /dev/spi* is

/dev/spidev0.0 /dev/spidev0.1 /dev/spidev1.0 /dev/spidev1.1

After running the following code on the slave I received useless data. Since slave mode should have been activated, I should not be able to read data unless Master is sending some.

import spidev
import time

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 250000

def read_spi(channel):
  spidata = spi.xfer2([0,(8+channel)<<4,0])
  return ((spidata[1] & 3) << 8) + spidata[2]

def readData():
    spidata = spi.readbytes(8)
    return spidata


try:
  while True:
    #channelData = read_spi(0)
    channelData = readData()
    print (channelData)
    time.sleep(.1)

except KeyboardInterrupt:
  spi.close()

channelData can be received with the other function. But it does not make the result any different.

[20, 206, 54, 93, 19, 151, 211, 199]
[84, 10, 89, 184, 126, 82, 49, 78]
[189, 32, 110, 143, 231, 226, 76, 116]
[102, 56, 174, 123, 186, 145, 148, 161]
[105, 254, 152, 155, 88, 147, 191, 174]
[38, 221, 219, 179, 161, 102, 107, 31]
[101, 141, 98, 80, 20, 254, 25, 50]
[88, 0, 0, 44, 197, 73, 32, 49]
[107, 60, 44, 230, 91, 56, 172, 4]
[21, 156, 120, 165, 99, 137, 245, 204]
[15, 34, 164, 215, 255, 187, 34, 86]
[18, 215, 67, 227, 234, 1, 237, 142]
[71, 124, 36, 238, 86, 240, 105, 189]
[29, 27, 63, 232, 239, 40, 189, 61]
[5, 217, 209, 14, 96, 24, 181, 97]
[158, 121, 125, 93, 224, 125, 97, 129]
[75, 92, 95, 183, 47, 14, 111, 164]

Do I need more configuration to be done if I want to make one a slave or am I doing something wrong with the coding?

Any links or code example is appreciated.

Did you modify the device tree for slave mode.
Modify the compatible as below.
compatible = “nvidia,tegra210-spi-slave”;

Have reference to below topic for SPI slave mode configure.

In which file should I make the change?
I cannot be sure since there are many files under /boot with the name tegra*
And they are all binary files, so I cannot change them. I guess I am looking at the wrong place.

EDIT: I have found a way. As it was mentioned in the reference in your comment, I have decompiled the dtb file to dts file. In there I have found the spi@7000d400 configuration. But there are four of them. I would have guessed it to have 2 only since there are spi1 and spi2 only. Why do you think it may be the case?

I have done the necessary configuration. After rebooting, the above script throws an error

Traceback (most recent call last):
  File "/home/lla2/Caglar/readData.py", line 20, in <module>
    channelData = readData()
  File "/home/lla2/Caglar/readData.py", line 13, in readData
    spidata = spi.readbytes(8)

What could be the problem?

Sorry I don’t have idea about what this script do.
Please use spidev_test tools to verify it.

Hii, @ycaglar121 ,@ShaneCCC
Does your communication worked.

If run loopback test on two jetsons one as master another as slave without problem.

1 Like

Hii, @ShaneCCC
I want to interface jetson nano and BMP280 sensor via SPI, I have made changes on device tree and spi ports are visible on /dev / directory also added the driver for it and flshed it but I can’t find any compatible library, so can you help me how to proceed, means I don’t understand should I need library or not.

Sorry I don’t have experience for BMP280, Suppose you need driver and device tree configure.
Please consult with vendor to get support it.

1 Like

Hii, @ShaneCCC
I need to run spidev_test.c on jetson nano but don’t know the hardware setup can you please explain which pins should i connect and what to do for loopback mode and also where is spidev_test.c located.

Thank you.

Have reference to below link.

1 Like

Hii @ShaneCCC ,

Board used in above link is Jetson Xavier NX and I’m using Jetson nano after going through datasheet it seems all pins are same, so I want to ask that will it support for Jetson nano?
Also want to ask that does it work for 2 jetson nano connected via SPI?

Thank You.

Yes, just confirm the PINs connected for loopback test doesn’t matter NX or Nano

1 Like

Hii @ShaneCCC ,
Thank you!! Loopback test runs successfully, now I want to do SPI interface between 2 jetson nano, does loopback code supports for that or should I have to write the code if yes then please guide me with it. Hardware connections will not be tough but help me with software side like what steps should I follow, is there any library which I can use, do we need to add driver for it?

Thank you.

Suppose you can get the spidev_test source code from web.

Hii @ShaneCCC ,
Yes, I have the code will it work for 2 jetson nano, one as master and other is slave and for slave device should I follow this link: How to set to spi slave mode

Thank You.

Hii @ShaneCCC , Help me here.

Thank you.

Yes, follow the relative topic for it.

Thanks

1 Like

Hii @ShaneCCC ,
I set one Jetson nano as master and another as slave, could you please tell me
1.

Jetson nano 1 MISO → Jetson nano 2 MISO
Jetson nano 1 MOSI → Jetson nano 2 MOSI
Jetson nano 1 CLK → Jetson nano 2 CLK
Jetson nano 1 CS → Jetson nano 2 CS

is this hardware connection correct.

  1. should run spidev_test.c on master first and then on slave.
    Is there any changes should I make in spidev_test.c
    https://forums.developer.nvidia.com/t/master-slave-communication-between-two-jetson-nano-over-spi/221229/16?u=prasad.suralkar

Thank You.

Run the slave first. Also make sure your have modify the device tree as slave mode.

1 Like