Banana Pi BPI-M4: How to get I2C and SPI running (tutorial)

Hello, I took me some hours to figure out how to get I2C and SPI running on the BPI-M4 (RTD1395), so I document it here:

The below should work for all Banana PI Linux images (Raspbian, Ubuntu etc.), however I tested it with Raspbian and Kernel 4.9.119 (2020-05-18-raspbian-stretch-bpi-m4-sd-emmc.img)

  1. I2C should work out of the box - finding I2C devices with i2cdetect may not work as expected, however communication works - example (read WHOAMI ID from MPU6050 connected to CON1-P03/I2C1_SDA and CON1-P05/I2C1_SCL): Screenshot%20from%202021-12-11%2010-40-05

  2. For SPI, I needed to activate it in the Linux device tree.

Approach A): Patching device-tree using fdtput command without building kernel. Make sure boot partition mounted before doing this:

fdtput -t s /media/pi/BPI-BOOT/bananapi/bpi-m4/linux/rtd-1395-bananapi-m4-2GB.dtb /spi@9801BD00 status okay
fdtput -t s /media/pi/BPI-BOOT/bananapi/bpi-m4/linux/rtd-1395-bananapi-m4-1GB.dtb /spi@9801BD00 status okay
reboot

Approach B): Rebuilding kernel package

2.1 Download kernel package to your Linux PC: https://github.com/BPI-SINOVOIP/BPI-M4-bsp

2.2 Activate SPI in device tree (linux-rtk/arch/arm64/boot/dts/realtek/rtd139x/rtd-1395-bananapi-m4-1GB.dts and rtd-1395-bananapi-m4-2GB.dts): Screenshot%20from%202021-12-11%2011-43-00

2.3 Optional: change kernel options ./build 4

2.4 Compile kernel: ./build 1

2.5 Transfer kernel to SD card (with ready Raspbian on it): ./build 7

  1. Test SPI with a ‘loopback cable’ (connect together CON1-P19/GSPI-MOSI and CON1-P21/GSPI-MISO)

wget https://raw.githubusercontent.com/torvalds/linux/master/tools/spi/spidev_test.c

gcc -o spidev_test spidev_test.c

RX should return TX data: Screenshot%20from%202021-12-11%2010-38-24

banana_pi_m4

Feel free to ask if you have questions, comments etc. :slight_smile:

2 Likes

very good job, thank you share this

Great article. Could you share how to enable multiple spi devices? Like /dev/spidev0.0, /dev/spidev0.1, /dev/spidev0.2, /dev/spidev0.3, etc.

They can share the MOSI, MISO and CLK pins, but just use different chip select pins. Thanks

Hi,

I’ve been trying to replicate your instructions and failing, the spi-dev-test code does not even build.

Can you please advise? Further, I do not see any /dev/spi* after building custom kernel or patching kernel tree.

Thank you! Mo

This is my experience to get the SPI to show /dev/spidev0.0. Modifying device tree did not work.

In short … rebuild the Kernel and replace the files in your Boot directory with the files generated within the SD directory.

Steps I took:

  1. get the docker image!! so you can build the kernel
  2. ensure the correct SPI ROM is applied to pi
  3. use configure kernel option of build - enable module for the SPI - to use SPIDEV
  4. build everythinggg
  5. copy generated kernel files to boot device…

This looks like exactly what I need. Forgive the simple question but for option A… how does one mount the boot partition?

Thanks!

Hello,

Can you advice if this is valid for BPI m2 zero as well? I am trying to get SPI+GPIO working on BPI m2 zero.

Thanks

Hello! I just wanted to mention that adding the following lines to armbianEnv.txt had helped me to get SPI working on Banana Pi M2 Zero:

overlays=spi-spidev
param_spidev_spi_bus=0

After I rebooted, the /dev/spidev0.0 showed up normally

I hope that helps!

may I ask which base image are you using?

I’ve been using this custom image, since I needed working OV5640 support:

You can find the base image on the github page

nice, thank you! I will take a look at it.