Cannot communicate over spidev

Hi. I want to communicate with an SPI sensor (BME280). I found spidev2.0 in the /dev directory as follows:

ls /dev | grep spi

I installed Python spidev to use SPI (in a priviledged container with the mapping /dev:/dev) and run the following code:

import spidev
    spi = spidev.SpiDev(), 0) # bus, device
    spi.max_speed_hz = 1000000
    spi.mode = 0x0
    print('SPI enabled')
    address = 0x80 | 0xD0 # read chip ID
    response = spi.xfer2([address,0x00])
    print('ID SPI')
except Exception as e:

The problem is that it always returns 0 instead of 0x60. I’ve checked the SPI transaction with a logic analyzer and got the following result every time I run the script above:


It seems that the clock signal starts before the data transfer on MOSI, which is really weird. Also, the data on MOSI is incorrect.

Does anybody know what is the problem? Is it related to the spidev library? Am I missing something here?

I don’t see anything immediately wrong, though I admit I’m unfamiliar with the python spidev library. Though I don’t think you’re missing anything at least at base with spidev. You seem to have all the permissions and access sorted correctly in the container otherwise I’d imagine your code would error out.

As for your code itself, if understand correctly you’re trying to read specific registers off your device. From the datasheet you linked as you said I see that 0xD0 should return the chip ID of 0x60. Let’s see according to the datasheet this register “can be read as soon as the device finished the power-on-reset”. Perhaps this is the issue?

Are the other registers reporting off/strange values as well? Or just the chip ID register?

Also I do agree your signals do seem a bit strange especially the clock, but I don’t really have anymore comments to add towards that point.

Best Regards,

Hi. Yes, I am trying to read registers from the device. I have succeeded communicating with the device via I2C, but now I am interested to see if SPI is working correctly (the sensor is on a breakout board and I can easily switch the wires to use the I2C or SPI interfaces).

I have tried reading another register, e.g. 0xF4. Unfortunately the signals were the same (the clock before data on MOSI) and the same single pulse on MOSI. Thus, as the capture from the logic analyzer shows, the values read were 0.

Maybe this is a problem with the spidev library. Can you try it out to see if you can recreate the issue?

I’ve tried the spidev C example here just to see if it transmits OK (with this command ./spidev_test -D /dev/spidev2.0) and to my surprise there was no clock signal (see below). I retried my Python script and I got the same signals as shown from the logic analyzer in my first message (so this is not a hardware connection problem related to the wires). What is going on?

I figure it out. It seems with was a wire connection problem. It works as expected.

Good to know. Thanks for the feedback, and we are glad that it’s working now.