2

I got a adafruit BME280 sensor board and I wired it to the raspberry pi zero via spi. According to the datasheet I can read sensor data from the device via the userspace spi interface. Everything works fine from userspace.

I discovered, that there is a kernel module for this sensor available utilizing the iio subsystem. And it implements the algorithms from the datasheet. A naive modprobe bcmp280_spi loaded the module:

# lsmod
Module                  Size  Used by
bmp280_spi              3451  0
bmp280                 11976  1 bmp280_spi
industrialio           50711  1 bmp280
spidev                  7034  0
...

But there is nothing to interface with:

# tree /sys/bus/iio/
/sys/bus/iio/
|-- devices
|-- drivers
|-- drivers_autoprobe
|-- drivers_probe
`-- uevent

2 directories, 3 files

Could someone help me with setting up the BMP280 SPI Kernel Module for the raspberry pi zero? I guess I need a device tree overlay, but I can't find any documentation how to interface this sensor module correctly. The raspberry pi includes a configuration for i2c but I want to use spi.

2

After searching in google for several painful hours days, I found the solution. Here is my device tree overlay:

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2708";

    fragment@0 {
        target = <&spidev0>;

        __overlay__ {
            status = "disabled";
        };
    };

    fragment@1 {
        target = <&spi0>;

        __overlay__ {
            status = "okay";
            #address-cells = <1>;
            #size-cells = <0>;

            bme280@0 {
                compatible = "bosch,bme280";
                reg = <0x0>;
                spi-max-frequency = <500000>;
                default-oversampling = <1>;
                status = "okay";
            };
        };
    };
};

It is important to disable the spidev node, since otherwise it conflicts with the chipselect. If you see something like this in dmesg, then you have disabled the wrong node:

[   85.280101] spi-bcm2835 20204000.spi: chipselect 0 already in use
[   85.280123] spi_master spi0: spi_device register error /soc/spi@7e204000/bme280@0
[   85.280138] of_spi_notify: failed to create for '/soc/spi@7e204000/bme280@0'
[   85.280144] OF: changeset notifier error @/soc/spi@7e204000/bme280@0

You can use @spidev0 for chipselect 0 and @spidev1 for chipselect 1. It also also important to disable the spidev node (=fragment@0) before enabling the sensor driver (=fragment@1).

An out of tree device tree compiler is needed to compile the overlay. I used this script to get one. The device tree overlay can be compiled and applied as follows:

# dtc -@ -I dts -O dtb -o /boot/overlays/bmp280-spi.dtbo bmp280-spi.dts
# dtoverlay /boot/overlays/bmp280-spi.dtbo

After the device tree overlay has been applied, sensor is there:

# cat /sys/bus/iio/devices/iio:device0/in_temp_input 
26980

To apply the overlay at bootup, add this line to /boot/config.txt

dtoverlay=bmp280-spi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.