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 traditional
spidev0.0`, etc.