Jetson Nano trouble using SPI

I have a problem with configuring SPI on the Jetson Nano development board. Hopefully, anyone here can help me out with solving this problem. I have tried the following:

First I executed the following commands:
$ sudo find /opt/nvidia/jetson-io/ -mindepth 1 -maxdepth 1 -type d -exec touch {}/__init__.py \;

$ sudo mkdir /boot/dtb

$ sudo cp -v /boot/tegra210-p3448-0000-p3449-0000-[ab]0[02].dtb /boot/dtb/

Then I enabled SPI0 via the jetson-io.py
$ sudo /opt/nvidia/jetson-io/jetson-io.py

To see the results, I did
$ sudo gpioinfo | grep -i spi

With the following commands it’s visable to see that spi1 is enabled
$ sudo /opt/nvidia/jetson-io/config-by-function.py -l enabled

$ sudo /opt/nvidia/jetson-io/config-by-pin.py

Then I used the library from Milekium (GitHub - milekium/spidev-lib: simple spidev c/c++ wrapper library) to test it. It send the ‘hello world’ message without errors.

I plugged the Jetson’s pin 19 (also tried the other pins 19-26), but the osciloscoop showed a flat line (plain 5v and 3.3v is visable)

002

Please update to J4.6 or latest and use jetson-io to configure the SPI and short the pin19/21 to run the loopback test by spidev_test to confirm it.

@ShaneCCC Thanks for your reply. I don’t think I had Jetpack installed in the first place, so that is one step further. I disabled spi1. rebooted the Jetson, enabled it again, rebooted it, and shorted pin 19 and 21.

This is the output of the spidev_testc
Screenshot_2

I don’t think it works. I tried this on two different Jetson Nano’s sadly both without result.

I mean update your BSP to J4.6
What’s below show

cat /etc/nv_tegra_release

sudo cat /sys/kernel/debug/tegra_gpio

@ShaneCCC I executed the commands and the following is visable:

The pin status is correct. Could you check the loopback test by spidev_test?

1 Like

Hey @ShaneCCC, I really appreciate your fast response. I tried running the spidev_test and it gave me the same error. I could hook it up to an oscilloscope if that would be more conveniënt?
Screenshot_4

This is the wiring

The pin19/21 should be the spidev0.0 instead of spidev2.0
What below command shows up, below are the correct configure.

sudo cat /sys/kernel/debug/tegra_pinctrl_reg | grep -i spi

Bank: 1 Reg: 0x70003050 Val: 0x0000e044 -> spi1_mosi_pc0
Bank: 1 Reg: 0x70003054 Val: 0x0000e044 -> spi1_miso_pc1
Bank: 1 Reg: 0x70003058 Val: 0x0000e044 -> spi1_sck_pc2
Bank: 1 Reg: 0x7000305c Val: 0x0000e048 -> spi1_cs0_pc3
Bank: 1 Reg: 0x70003060 Val: 0x0000e048 -> spi1_cs1_pc4
Bank: 1 Reg: 0x70003064 Val: 0x00006044 -> spi2_mosi_pb4
Bank: 1 Reg: 0x70003068 Val: 0x00006044 -> spi2_miso_pb5
Bank: 1 Reg: 0x7000306c Val: 0x00006044 -> spi2_sck_pb6
Bank: 1 Reg: 0x70003070 Val: 0x00006048 -> spi2_cs0_pb7
Bank: 1 Reg: 0x70003074 Val: 0x00006048 -> spi2_cs1_pdd0

Here’s my test result.

nvidia@nvidia-desktop:~$ cat /boot/extlinux/extlinux.conf
TIMEOUT 30
DEFAULT JetsonIO

MENU TITLE L4T boot options

LABEL primary
      MENU LABEL primary kernel
      LINUX /boot/Image
      INITRD /boot/initrd
      APPEND ${cbootargs} root=/dev/mmcblk0p1 rw rootwait rootfstype=ext4 console=ttyS0,115200n8 console=tty0 fbcon=map:0 net.ifnames=0

# When testing a custom kernel, it is recommended that you create a backup of
# the original kernel and add a new entry to this file so that the device can
# fallback to the original kernel. To do this:
#
# 1, Make a backup of the original kernel
#      sudo cp /boot/Image /boot/Image.backup
#
# 2, Copy your custom kernel into /boot/Image
#
# 3, Uncomment below menu setting lines for the original kernel
#
# 4, Reboot

# LABEL backup
#    MENU LABEL backup kernel
#    LINUX /boot/Image.backup
#    INITRD /boot/initrd
#    APPEND ${cbootargs}

#LABEL JetsonIO
#       MENU LABEL Custom Header Config: <HDR40 User Custom [2021-08-10-180616]>
#       LINUX /boot/Image
#       FDT /boot/kernel_tegra210-p3448-0000-p3449-0000-b00-user-custom.dtb
#       INITRD /boot/initrd
#       APPEND ${cbootargs} quiet root=/dev/mmcblk0p1 rw rootwait rootfstype=ext4 console=ttyS0,115200n8 console=tty0 fbcon=map:0 net.ifnames=0

LABEL JetsonIO
        MENU LABEL Custom Header Config: <HDR40 User Custom [2021-09-07-182023]>
        LINUX /boot/Image
        FDT /boot/kernel_tegra210-p3448-0000-p3449-0000-b00-user-custom.dtb
        INITRD /boot/initrd
        APPEND ${cbootargs}
nvidia@nvidia-desktop:~$ sudo ./spidev_test -D /dev/spidev0.0 -g16 -zz
using device: /dev/spidev0.0
setting spi mode for read,write
setting spi bpw
setting max speed for rd/wr
spi mode: 0
bits per word: 8 bytes per word: 1
max speed: 10000000 Hz (10000 KHz)
no. runs: 1
Using seed:0x619f31ad
loop count = 0
using sequential pattern ....
transfer bytes [16]
0000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
transfer: Return actual transfer length: 16
receive bytes [16]
0000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/dev/spidev0.0: TEST PASSED
1 Like

Hey @ShaneCCC, I see some different values here.

Bank: 1 Reg: 0x70003050 Val: 0x0000e044 -> spi1_mosi_pc0
Bank: 1 Reg: 0x70003054 Val: 0x0000e044 -> spi1_miso_pc1
Bank: 1 Reg: 0x70003058 Val: 0x0000e044 -> spi1_sck_pc2
Bank: 1 Reg: 0x7000305c Val: 0x0000e048 -> spi1_cs0_pc3
Bank: 1 Reg: 0x70003060 Val: 0x0000e048 -> spi1_cs1_pc4
Bank: 1 Reg: 0x70003064 Val: 0x00006016 -> spi2_mosi_pb4
Bank: 1 Reg: 0x70003068 Val: 0x00006016 -> spi2_miso_pb5
Bank: 1 Reg: 0x7000306c Val: 0x00006016 -> spi2_sck_pb6
Bank: 1 Reg: 0x70003070 Val: 0x00006016 -> spi2_cs0_pb7
Bank: 1 Reg: 0x70003074 Val: 0x00006015 -> spi2_cs1_pdd0
Bank: 1 Reg: 0x70003078 Val: 0x0000e015 -> spi4_mosi_pc7
Bank: 1 Reg: 0x7000307c Val: 0x0000e015 -> spi4_miso_pd0
Bank: 1 Reg: 0x70003080 Val: 0x0000e015 -> spi4_sck_pc5
Bank: 1 Reg: 0x70003084 Val: 0x0000e015 -> spi4_cs0_pc6
Bank: 1 Reg: 0x70003088 Val: 0x00002040 -> qspi_sck_pee0
Bank: 1 Reg: 0x7000308c Val: 0x00002000 -> qspi_cs_n_pee1
Bank: 1 Reg: 0x70003090 Val: 0x00002040 -> qspi_io0_pee2
Bank: 1 Reg: 0x70003094 Val: 0x00002040 -> qspi_io1_pee3
Bank: 1 Reg: 0x70003098 Val: 0x00002040 -> qspi_io2_pee4
Bank: 1 Reg: 0x7000309c Val: 0x00002040 -> qspi_io3_pee5
Bank: 0 Reg: 0x70000b70 Val: 0x00000001 -> drive_qspi_comp_control
Bank: 0 Reg: 0x70000b78 Val: 0x00000001 -> drive_qspi_lpbk_control
Bank: 0 Reg: 0x70000a78 Val: 0x00808000 -> drive_qspi_comp

The settings do look the same

cat /boot/extlinux/extlinux.conf
TIMEOUT 30
DEFAULT JetsonIO

MENU TITLE L4T boot options

LABEL primary
      MENU LABEL primary kernel
      LINUX /boot/Image
      INITRD /boot/initrd
      APPEND ${cbootargs} quiet root=/dev/mmcblk0p1 rw rootwait rootfstype=ext4 console=ttyS0,115200n8 console=tty0 fbcon=map:0 net.ifnames=0 

# When testing a custom kernel, it is recommended that you create a backup of
# the original kernel and add a new entry to this file so that the device can
# fallback to the original kernel. To do this:
#
# 1, Make a backup of the original kernel
#      sudo cp /boot/Image /boot/Image.backup
#
# 2, Copy your custom kernel into /boot/Image
#
# 3, Uncomment below menu setting lines for the original kernel
#
# 4, Reboot

# LABEL backup
#    MENU LABEL backup kernel
#    LINUX /boot/Image.backup
#    INITRD /boot/initrd
#    APPEND ${cbootargs}

LABEL JetsonIO
	MENU LABEL Custom Header Config: <HDR40 User Custom [2021-11-24-085933]>
	LINUX /boot/Image
	FDT /boot/tegra210-p3448-0000-p3449-0000-b00-user-custom.dtb
	INITRD /boot/initrd
	APPEND ${cbootargs} quiet root=/dev/mmcblk0p1 rw rootwait rootfstype=ext4 console=ttyS0,115200n8 console=tty0 fbcon=map:0 net.ifnames=0

But when I try the following command:

sudo ./spidev_test -D /dev/spidev0.0 -g16 -zz

It uses SPI 2.0 again. Perhaps because I don’t have a /dev/ folder within my spitest library

blindsight@blindsight:~$ sudo ./spidev_test -D /dev/spidev0.0 -g16 -zz
sudo: ./spidev_test: command not found

Could you verify by attached tool.

sudo mv spidev_test.txt spidev_test
sudo chmod +x spidev_test

Thanks again for your help @ShaneCCC, I appreciate it. I added the file in the directory and made it executable. This is the output

blindsight@blindsight:~/spi_test/spidev-lib$ sudo chmod +x spidev_test
blindsight@blindsight:~/spi_test/spidev-lib$ ./spidev_test
Transaction length not specified
Usage: ./spidev_test [options]
 version 21
  -D --device   device to use (default /dev/spidev0.0)
  -s --speed    max speed (Hz)
  -g --length   transaction length in bytes. Multiple lengths can be comma separated.
  -z --debug    enable debug mode. Specifying more than once to increase the verboisty.
  -p --pattern  choose pattern type. Available patterns are:
                0: sequential bytes
                1: even bytes
                2: odd bytes
                3: reverse sequential bytes
                4: random bytes with crc
  -F prefix     Dump rx/tx buffer as raw to file. With no prefix specified, current pid will be used
  -f --file     Pattern file. User defined pattern. Not allowed with -p option.
                Pattern file must be in space seperated bytes in hex without
                "0x" prefix. Sample data: 01 AA 23 5F 3C 12
  -d --delay    delay (usec). For SPI master this will be the minimum delay 
                the driver waits before initiating each transfer. For slave
                this indicates the max delay that the slave waits before it times out.
  -E --expect   Expected transaction length in bytes.
                To test variable length data transfer feature.
                Without this option, spidev_test will assume it as normal transfer
                and will validate the actual transferred length with requested transaction length.
                Value should be less than or equal to the transaction length.
  -b --bpw      bits per word 
  -H --cpha     clock phase
  -O --cpol     clock polarity
  -L --lsb      least significant bit first
  -C --cs-high  chip select active high
  -n --nruns    Number of times to repeat the test. -1 is infinite.
  -u --udelay   delay b/w initiating each transfer (usec). Multiple delays can be comma separated
  -r --receive  only receive data
  -t --transmit only transmit data
  -v --minsize  variable length packet start
  -V --maxsize  variable length packet end
  -W --waitb4   wait for a keystroke before the first transfer
  -w --stoperr  stop on all errors
  -P --packet   packet mode

Examples:
To transfer 100 messages of 30 bytes each with random bytes,
  spidev_test -D/dev/spidev0.0 -s18000000 -n100 -g30 -p4

To transfer 100 messages of sizes 8 and 3986 with delay of 2ms and 8ms respectively,
  spidev_test -D/dev/spidev0.0 -s18000000 -n100 -g8,3968 -u2000,8000

To transfer all bytes from user defined pattern file,
  spidev_test -D/dev/spidev0.0 -s18000000 -f/path/to/patternfile

To test variable length feature,
  spidev_test -D/dev/spidev0.0 -s18000000 -g 30 -E 20

Return codes:
 0   successfull transfer. This would mean that what was
     transferred was actually received. Note that a success
     would make sense only when the spi is run in a loopback
     type configuration, i.e., a miso-mosi loopback or a
     master-slave loopback
 1   Invalid argument
 5   I/O error
 6   Data mismatch error. The transfer happened but there is 
     mismatch in tx and rx pattern. Again this makes sense only
     for loopback as explained above
 22  Invalid argument
blindsight@blindsight:~/spi_test/spidev-lib$ sudo ./spidev_test -D /dev/spidev0.0 -g16 -zz
using device: /dev/spidev0.0
can't open device: No such file or directory
Aborted
blindsight@blindsight:~/spi_test/spidev-lib$ 

It states ‘command not found’ when I try an example command

blindsight@blindsight:~/spi_test/spidev-lib$ spidev_test -D/dev/spidev0.0 -s18000000 -n100 -g30 -p4
bash: spidev_test: command not found

Run below command in the folder that you download the attached file instead of …/spidev-lib/

sudo ./spidev_test -D /dev/spidev0.0 -g16 -zz

Hey @ShaneCCC again thanks for your reply. I tried executing the command and it still couldn’t execute it.

blindsight@blindsight:~/Downloads$ ls
smartgit  smartgit-linux-21_1_3.tar.gz  spidev_test  spidev_test.txt
blindsight@blindsight:~/Downloads$ sudo mv spidev_test.txt spidev_test
[sudo] password for blindsight: 
blindsight@blindsight:~/Downloads$ sudo chmod +x spidev_test
blindsight@blindsight:~/Downloads$ ls
smartgit  smartgit-linux-21_1_3.tar.gz  spidev_test
blindsight@blindsight:~/Downloads$ sudo ./spidev_test -D /dev/spidev0.0 -g16 -zz
using device: /dev/spidev0.0
can't open device: No such file or directory
Aborted

Did you load spidev driver first.

sudo modprobe spidev

@ShaneCCC that was the solution. The test passed now. Do you have the source code for me for that spidev_test file? The .txt is already compiled. This way I can implement this solution now I know that SPI0 could work

blindsight@blindsight:~/Downloads$ sudo ./spidev_test -D /dev/spidev0.0 -g16 -zzusing device: /dev/spidev0.0
setting spi mode for read,write
setting spi bpw
setting max speed for rd/wr
spi mode: 0
bits per word: 8 bytes per word: 1
max speed: 10000000 Hz (10000 KHz)
no. runs: 1
Using seed:0x61a0d887
loop count = 0 
using sequential pattern ....
transfer bytes [16]
0000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
transfer: Return actual transfer length: 16
receive bytes [16]
0000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
/dev/spidev0.0: TEST PASSED 

Check below link.

Thank you very much!