How to use SPI to read/write specific slave register

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
ivanlinlin12
Posts: 2
Joined: Fri Jul 19, 2019 9:42 pm

How to use SPI to read/write specific slave register

Post by ivanlinlin12 » Mon Jul 22, 2019 3:31 am

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

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

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

Post by jimmo » Mon Jul 22, 2019 4:47 am

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

ivanlinlin12
Posts: 2
Joined: Fri Jul 19, 2019 9:42 pm

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

Post by ivanlinlin12 » Mon Jul 22, 2019 10:28 pm

Hi Jimmo,

Thank you very much, that makes a lot of sense

Post Reply