Skip to main content
Visitor II
March 23, 2020
Question

How to read multiple bytes in SPI with HAL

  • March 23, 2020
  • 2 replies
  • 1748 views

Hi everybody,

I've been trying for a while to read multiple bytes from an LIS3MDL accelerometer. I know how to read and write registers but one at a time. I can't figure it out the multiple part...

So, if I want to read from reg 0x27 to 0x2D, I understand that I need to set bit0 to 1 (for read operation) and MS bit to 1 (for multiple read). Then :

TxMagOut[0] = 0x27|0xC0;

and

RxMagOut[8] = {0}; // initialisation

I use transmiReceive with TxMagOut. I said that my data is 8 bytes so it know when to stop reading (at 0x2D)

HAL_SPI_TransmitReceive(&hspi3,TxMagOut ,RxMagOut , 8, 100);

I should get my data in RxMagOut from [1] to [8]. Right? At least this is my best idea.

Am I doing something wrong? Do someone see an error there?

If there is a simple example with HAL of multiple byte read that you know of ? I would be gratefull.

Thanks! :)

    This topic has been closed for replies.

    2 replies

    Visitor II
    July 25, 2020

    AFAIK HAL_SPI_TransmitReceive(&hspi3,TxMagOut ,RxMagOut , 8, 100);

    "8" here is number of tx bytes, not the rx bytes as you assumed.

    Visitor II
    August 3, 2020

    Hi @JMart.5, did you end up solving the issue? I have the same problem too.

    Thanks