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
How to use SPI to read/write specific slave register
-
- Posts: 2
- Joined: Fri Jul 19, 2019 9:42 pm
Re: How to use SPI to read/write specific slave register
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:
Or to write CTRL1_XL (0x10)
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
Code: Select all
buf = bytearray(2)
buf[0] = 0x10
buf[1] = # new value
#set cs low
spi.write(buf)
# set cs high
-
- Posts: 2
- Joined: Fri Jul 19, 2019 9:42 pm
Re: How to use SPI to read/write specific slave register
Hi Jimmo,
Thank you very much, that makes a lot of sense
Thank you very much, that makes a lot of sense