Failed to initialize SPI on buster

Failed to initialize SPI is returned when trying to run the test spi.c program. The steps were tested using the 20210608 release. The steps taken were:

# 1. Add buster-testing to sources: 
echo "deb http://apt.radxa.com/buster-testing/ buster main" >> /etc/apt/sources.list`

# 2. Get pub key: 
wget -O -  apt.radxa.com/buster-testing/public.key | sudo apt-key add -

# 3. Update and install packages
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install -y rockchip-overlay && sudo apt-get install -y rockpi4-dtbo
 
# 4. Enable SPI 1 and console
cat boot/hw_intfc.conf 
   
   # Hardware Interface Config   
   # For more details, check https://wiki.radxa.com/Rockpi4/hardware/devtree_overlays.  
   # Set "on" to enable the optional hardware interfaces while set "off" to disable.
   
   intfc:pwm0=off
   intfc:pwm1=off
   intfc:uart2=off
   intfc:uart4=off
   intfc:spi1=on
   intfc:spi2=off
   intfc:i2c2=off
   intfc:i2c6=off
   intfc:i2c7=off
   
   # Devicetree Overlay Enable, uncomment to enable .dtbo under /boot/overlays/.
   
   #intfc:dtoverlay=at24c02
   #intfc:dtoverlay=two-color-led
   ifrr intfc:dtoverlay=console-on-ttyS2
   #intfc:dtoverlay=console-on-ttyS4
   intfc:dtoverlay=devspi1
   #intfc:dtoverlay=devspi2

5. Install libmraa
 apt-get install libmraa

6. Restart computer

7. Copy test code
# cat spi.c
   #include <signal.h>
   #include <stdlib.h>
   #include <unistd.h>
   
   /* mraa header */
   #include "mraa/spi.h"
   
   /* SPI declaration */
   #define SPI_BUS 0
   
   /* SPI frequency in Hz */
   #define SPI_FREQ 400000
   int
   main(int argc, char** argv)
   {
       mraa_result_t status = MRAA_SUCCESS;
       mraa_spi_context spi;
       int i, j;
   
       /* initialize mraa for the platform (not needed most of the times) */
       mraa_init();
   
       //! [Interesting]
       /* initialize SPI bus */
       spi = mraa_spi_init(SPI_BUS);
       if (spi == NULL) {
           fprintf(stderr, "Failed to initialize SPI\n");
           mraa_deinit();
           return EXIT_FAILURE;
       }
   
       /* set SPI frequency */
       status = mraa_spi_frequency(spi, SPI_FREQ);
       if (status != MRAA_SUCCESS)
           goto err_exit;
   
       /* set big endian mode */
       status = mraa_spi_lsbmode(spi, 0);
       if (status != MRAA_SUCCESS) {
           goto err_exit;
       }
       while(1) {
           printf("0x%x\n",mraa_spi_write(spi, 0xaa));
       }
   err_exit:
       mraa_result_print(status);
   
       /* stop spi */
       mraa_spi_stop(spi);
   
       /* deinitialize mraa for the platform (not needed most of the times) */
       mraa_deinit();
   
       return EXIT_FAILURE;
   }

# 8. Compile
gcc spi.c -lmraa

#9. Error when running
./a.out
Failed to initialize SPI

The only command that gives any results is:

spi-config -d /dev/spidev32766.0 -q
/dev/spidev32766.0: mode=0, lsb=0, bits=8, speed=50000000

Lastly, I’m unclear why only spidev32766.0is exposed and not the traditionalspidev0.0`, etc.

Hi @marvin

You need to set spi1 to ‘on’ instead of ‘ok’.

I’ve updated the original post to reflect intfc:spi1=on. I had the correct setting on the RockPi but copied wrong into the question originally. Good catch, but that did not resolve things since it was originally correct on the device.

Hi, @marvin

I suggest that you do the following troubleshooting.

1.Check the mraa version. It should be v2.1.0-15-g03d3059.

root@rockpi4b:/home/rock# mraa-gpio version
Version v2.1.0-15-g03d3059 on ROCK Pi 4

2.Check spi1 bus is enabled and devspi1 dtbo is loaded.

With serial console cable, please check the u-boot log.

select kernel
1:      kernel-4.4.154-113-rockchip-gdb9dfc2cdd25
Enter choice: Retrieving file: /hw_intfc.conf
reading /hw_intfc.conf
1968 bytes read in 9 ms (212.9 KiB/s)
dtoverlay number: 0, name:/overlays/console-on-ttyS2.dtbo 
dtoverlay number: 1, name:/overlays/devspi1.dtbo 
hw_conf.valid = 1
hw_conf.pwm0 = 0
hw_conf.pwm1 = 0
hw_conf.uart2 = 0
hw_conf.uart4 = 0
hw_conf.spi1 = 1
hw_conf.spi2 = 0
hw_conf.i2c2 = 0
hw_conf.i2c6 = 0
hw_conf.i2c7 = 0
hw_conf.dts_overlay_count = 2
hw_conf.dts_overlay[0] = /overlays/console-on-ttyS2.dtbo
hw_conf.dts_overlay[1] = /overlays/devspi1.dtbo
1:      kernel-4.4.154-113-rockchip-gdb9dfc2cdd25
...
merge_dts_overlay
Retrieving file: /overlays/console-on-ttyS2.dtbo
reading /overlays/console-on-ttyS2.dtbo
774 bytes read in 13 ms (57.6 KiB/s)
overlay dtb(0x0000000008200000) is valid
fdt_overlay_apply 0000000008300000 0000000008200000
merge_dts_overlay
Retrieving file: /overlays/devspi1.dtbo
reading /overlays/devspi1.dtbo
643 bytes read in 13 ms (47.9 KiB/s)
overlay dtb(0x0000000008200000) is valid
fdt_overlay_apply 0000000008300000 0000000008200000

Check the system device.

root@rockpi4b:~# ls /dev/spi*
/dev/spidev32766.0

3.Compile spi.c

gcc spi.c -lmraa

4.Short pin-19 and pin-21 in 40-pin header and run a.out.
You would receive lots of 0x55.

Hey @Stephen, thanks for your input. I don’t have a serial console to test #2 right now but will track one down tomorrow.

  1. Check the mraa version. It should be v2.1.0-15-g03d3059.

Confirmed.

mraa-gpio version
Version v2.1.0-15-g03d3059 on ROCK Pi 4
  1. With serial console cable, please check the u-boot log.

I do not have a serial console cable. I will get one tomorrow and attempt these steps.

Check the system device.

Confirmed.

$ ls /dev/spi*
/dev/spidev32766.0
  1. Compile spi.c

Compiled without error.

  1. Short pin-19 and pin-21 in 40-pin header

Done. I had not done this originally because I was testing with a custom hat. Today, I am testing the RockPi board alone only. I connected (shorted) pins 19 and 21 together. Picture.

You would receive lots of 0x55.

Unfortunately, still unable to initialize SPI.

$ ./a.out
Failed to initialize SPI

If I run as sudo though, I get 0xaa.

$ sudo ./a.out
0xaa
0xaa
0xaa
...

I have two additional related questions:

  1. Why does spi.c set SPI_FREQ=400000 given spi-config -d /dev/spidev32766.0 -q returns speed=50000000?
  2. When using a real SPI device, does intfc:dtoverlay=devspi1 still need to be set? Or is this only for testing?

I change 0xaa to 0x55 in spi.c

Yes. We need to run it in root mode or execute command with sudo.

That means that you use the default SPI clock frequency, 50MHz.

intfc:spi1=on and intfc:dtoverlay=devspi1 aims to create device /dev/spidev32766.0 for user space application.

@Stephen
I change 0xaa to 0x55 in spi.c

Ok, I see now that 0xaa is the expected output: printf("0x%x\n",mraa_spi_write(spi, 0xaa));. This means my test succeeded, correct?

@Stephen
That means that you use the default SPI clock frequency, 50MHz.

Do you have any idea why I can’t change the clock rate? Note that the speed is unchanged.

$ sudo spi-config -d /dev/spidev32766.0 -s 10000000
$ sudo spi-config -d /dev/spidev32766.0 -q
/dev/spidev32766.0: mode=0, lsb=0, bits=8, speed=50000000

Yes. You are right.

The number 50000000 is from devspi1.dtbo.

You can try the following commands.

root@rockpi4b:/home/rock# spi-config -d /dev/spidev32766.0 -q
/dev/spidev32766.0: mode=0, lsb=0, bits=8, speed=50000000, spiready=0
root@rockpi4b:/home/rock# spi-config -d /dev/spidev32766.0 -s 10000000 -w &
[1] 617
root@rockpi4b:/home/rock# spi-config -d /dev/spidev32766.0 -q
/dev/spidev32766.0: mode=0, lsb=0, bits=8, speed=10000000, spiready=0