Skip to main content
Graduate
April 23, 2023
Question

How to change endian in a 16bit SPI transmission?

  • April 23, 2023
  • 1 reply
  • 1047 views

HI,

I m trying to send a 16 bit word using SPI HAL to a SPI external interface driver.

SPI is configured as:

-Transmission master

-16bit data TX

-MSB first.

This driver requires first address of the register and then the content to be received.

I created a struct:

typedef struct

{

uint8_t regAddr;

uint8_t regData;

} SDR_TypeDef;

and then I define a variable with the contents:

SDR_TypeDef decodeMode_Reg={0};

decodeMode_Reg.regAddr = DECODE_MODE_ADDR; // 0x9

decodeMode_Reg.regData = 0b10001010; //0x8A

and then transmission by HAL:

HAL_SPI_Transmit(&hspi, (uint8_t*) &decodeMode_Reg, 1, HAL_MAX_DELAY);

On the scope I have a reverse endian transmission: 8A09 instead of 098A.

Any ideas?

I would like to let the struct in this way, without reversing the order of registers, since it fits better the definition of the register of the receiver driver datasheet. Some ideas?

Thanks

    This topic has been closed for replies.

    1 reply

    Graduate
    April 23, 2023

    Either use 8-bit frame size or declare the structure as:

    typedef struct
    {
     uint8_t regData;
     uint8_t regAddr;
    } SDR_TypeDef;