SPI setup on jetson nano board

Hi all

I have a need to use SPI0 on my board.
I have already read many posts on this subject.
As I understand it, the spi periphery should already be enabled in the basic configuration, but (ls / dev / | grep spi) does not give anything.
I also wanted to check the hardware configuration in sudo /opt/nvidia/jetson-io/jetson-io.py
but I don’t have the option to “Configure 40-pins expansions header”, I see only setting up csi connector

How do I enable SPI0 support?

besides this, I need the ability to switch from Spi mode to GPIO mode in user space

Hi georgiy.lutsenko,

Are you using the devkit or custom board for Jetson Nano? with SD or eMMC module?
What’s your Jetpack version in use?

It would appear after you load spi driver through modprobe spidev.

Hi

I have both versions (SD or eMMC). And I’m using the latest version of jetpack for that platform.
modprobe spidev worked, but how do I fix this result so that I don’t have to enter this command after every reboot of the device

how to find out device matching from /dev/spi
with physical ports that are on the board
provided that I am using an unmodified version of JetPack

I also wanted to know about examples
I did not find any examples from Nvidia on using peripherals in C/C ++

Hi

I can’t figure out how the pins match
I downloaded the file (Jetson Nano Hardware Design Documentation) in which pinmux discribe, but why are there two columns for spi, and one column for matching with 40 pins header

You could configure the kernel config to enable it.

CONFIG_SPI_SPIDEV=y

You could check the device tree for SPI and also the carrier board specification for the details of SPI pins.

Which SPI would you like to use?

Hi

I want to use SPI0 [89,91,93,95] (in the pinmux table, spi0 and spi1 are superimposed on these pins)


https://developer.download.nvidia.com/assets/embedded/secure/jetson/Nano/docs/NVIDIA_JetsonNano_DataSheet_v0.7.pdf?wGKUNpZ3p0gIh35EPGEF85jYQCaDZbd5_fhAHt7yFwjfM4qaLwZuvZ59oamMhF4ex9lYJGwqDU6ggE-Gz6pNThQYLins-AVRmKLrXa7ZcJ6K9NxqjqjxaE8pNtoptMWJbNodlDBRybCLbzMufF1r2ibOUI_naWfo01Ah8hN9EPIaX5fPJ8aWzkSTlg==&t=eyJscyI6ImdzZW8iLCJsc2QiOiJodHRwczovL3d3dy5nb29nbGUuY29tLyJ9

Left column(Module Pin) counts from SPI0, right column(Tegra X1 Signal) counts from SPI1.

For this SPI0, you could refer to spi0: spi@7000d400 in device tree.

Are there any official examples from nvidia in C/C++?

would it be ok to use this library?

Please refer to the following topic for the SPI example.
Jetson Nano SPI C/C++ Example - Jetson & Embedded Systems / Jetson Nano - NVIDIA Developer Forums

so is it spi0 or spi1?

For the name of 40-header pins, it is SPI1.
For the spi node, it is spi0.
Just refer to this spi@7000d400 address.

I looked through this topic and did not find any links to official libraries from Nvidia, is this true?

The only link that might help is this:

Did I understand correctly?

And in this example the following path to the device is indicated:

Here’s what I have on my system:
image

and here’s what’s in the device tree:
image

SPI0[89,91,93,95] matches spidev0.0?

Yes, it is open source and you could use it to verify with SPI loopback test.

spidev0.0 and spidev0.1, it depends on which CS pin you are using.

thanks for answers

I’m currently working on running this example and I’m running into another problem.
In the test example there are many structures, macros, etc. related to spi, but this is not in jetpack
For example, I searched the system for the file spi.h, it is located in a completely different place and its contents are different

Hi all
I took the initialization and sending part, here is the code snippet

#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>

int main()
{
    int fd;
    int ret = 0;

    const char *device = "/dev/spidev0.0";

    uint8_t mode = SPI_MODE_0;
    uint32_t speed = 500000;

    fd = open(device, O_RDWR);
    if (fd < 0) {
        std::cerr << "Could not open SPI device." << std::endl;
        return -1;
    }

    ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
    if (ret == -1) {
        std::cerr << "Could not set SPI mode." << std::endl;
        close(fd);
        return -1;
    }

    ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);

    if (ret == -1) {
        std::cerr << "Could not set SPI speed." << std::endl;
        close(fd);
        return -1;
    }

    uint8_t tx[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
    uint8_t rx[sizeof(tx)] = { 0 };

    struct spi_ioc_transfer tr = {
            .tx_buf = (unsigned long)tx,
            .rx_buf = (unsigned long)rx,
            .len = sizeof(tx),
            .speed_hz = speed,
            .delay_usecs = 0,
            .bits_per_word = 8,
    };


    while(1) {
        ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
        if (ret == -1) {
            std::cerr << "SPI transmission failed." << std::endl;
            close(fd);
            return -1;
        } else {
            std::cout << "ret = " << ret << std::endl;
        }
        usleep(100000);
    }

    close(fd);

    return 0;
}

the code compiles and runs but I don’t get anything in the output

maybe I did something wrong?

maybe it’s worth using this library?

Or do you not recommend it?
Are there any official examples?

I checked pins with the program running using this command:
sudo cat /sys/kernel/debug/gpio

and I don’t see anyone using these pins

What can you recommend?