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
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.
Hey @Stephen, thanks for your input. I don’t have a serial console to test #2 right now but will track one down tomorrow.
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
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
Compile spi.c
Compiled without error.
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:
Why does spi.c set SPI_FREQ=400000 given spi-config -d /dev/spidev32766.0 -q returns speed=50000000?
When using a real SPI device, does intfc:dtoverlay=devspi1 still need to be set? Or is this only for testing?