Page 1 of 1

How to use SPI to read/write specific slave register

Posted: Mon Jul 22, 2019 3:31 am
by ivanlinlin12
Hi,

I am trying to extract data from lsm6dsl. In order to read the data out, I first need to configure the IMU, which means I need to write/read some value to its register. However, from the the Micropython SPI library, I did not see(I might missed) how to read or write from slaves' specific register address. The library is mostly transferring the data to the slave, but not some specific register. I am wondering if anyone has done something similar? Thank you very much for your time and help

Best

Re: How to use SPI to read/write specific slave register

Posted: Mon Jul 22, 2019 4:47 am
by jimmo
Hi,

The SPI library provides just the basic primitives to read and write bytes to the device. On this particular device, you transmit a 7-bit register address (with the high-bit set to 1=read, 0=write), then either read or write the value of the register from there.

https://www.st.com/resource/en/datasheet/lsm6dsl.pdf has some good waveform diagrams showing reads and writes.

So for example, to read STEP_COUNTER_L (0x4B), you would do something like:

Code: Select all

buf = bytearray(1)
buf[0] = 0b10000000 | 0x4B
#set cs low
spi.write(buf)
spi.readinto(buf, 1)
# set cs high
buf[0] is now the low byte of the step counter
Or to write CTRL1_XL (0x10)

Code: Select all

buf = bytearray(2)
buf[0] = 0x10
buf[1] = # new value
#set cs low
spi.write(buf)
# set cs high

Re: How to use SPI to read/write specific slave register

Posted: Mon Jul 22, 2019 10:28 pm
by ivanlinlin12
Hi Jimmo,

Thank you very much, that makes a lot of sense