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 /[email protected] status okay
fdtput -t s /media/pi/BPI-BOOT/bananapi/bpi-m4/linux/rtd-1395-bananapi-m4-1GB.dtb /[email protected] 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:

1 Like

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!