cancel
Showing results for 
Search instead for 
Did you mean: 

How to change endian in a 16bit SPI transmission?

FAJOL.1
Associate II

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

1 REPLY 1
gbm
Lead III

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

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