I've got a position sensor that I need to read out via SPI from the Raspberry Pi. The sensor is a MLX90316, that requires interbyte spacing in the SPI message. Typically, I need something like this (recorded from a Cortex M3 which reads the sensor without problems):

MLX90316 SPI

As you can see, there is interbyte spacing between the individual bytes. The MLX90316 needs a bit of time to fill the last 2 MISO bytes with the sensor value, after seeing the 0x55 in the first byte. When, I use the spidev driver on the Pi, it generates messages like:

MLX90316 spidev

While the MOSI part looks a lot similar, the sensor does not get enough time to fill the answer in the last two bytes. So I definitely need the interbyte spacing.

My current solution is to bitbang and use my own enable line to generate the packages with the interbyte spacing as needed. See https://gist.github.com/steviethecat/29d5e0e91dca0080a20c21fda33d7064 for the code. While this works, the load on the Pi is around 45% CPU.

Is there a more efficient solution that is accomplishes the same? I'd rather use the regular spidev library. I did notice a .pad option, but modifying that does not seem to have any effect on the interbyte spacing.


There use to be an inter-byte gap of 1.5 bit times. This annoyed some people so I think a fix was found to remove it from the main SPI device.

I believe the auxiliary SPI device still has the 1.5 bit time gap. If that is enough I suggest you use that device.

If you need a different gap you could use pigpio waves to generate the needed SPI transaction. However that would require decent programming ability. This would allow for much less load than the 45% you mention.

| improve this answer | |

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.