2025-06-13 4:20 AM
Hi all,
I'm trying to setup the SPI communication with a VL53L8CX which is connected to a STM32F401RE-Nucleo. I took some code snippets from the STSW-IMG040 package and I wonder why the bytes to be send in VL53L8CX_WrMulti and VL53L8CX_RdMulti are treated with a special mask instead of just separating the bytes like shown below:
#define SPI_WRITE_MASK(x) (uint16_t)(x | 0x8000) // 1
#define SPI_READ_MASK(x) (uint16_t)(x & ~0x8000)
data_write[0] = SPI_WRITE_MASK(temp) >> 8;
data_write[1] = SPI_WRITE_MASK(temp) & 0xFF;
respectively
data_write[0] = SPI_READ_MASK(temp) >> 8;
data_write[1] = SPI_READ_MASK(temp) & 0xFF;
instead of just
data_write[0] = (temp >> 8) & 0xFF;
data_write[1] = temp & 0xFF;
Is there a special reason for doing this?
Regards
Bastian
Solved! Go to Solution.
2025-06-16 10:22 AM
On an i2C, if the sensor has a base address of 0x29, one shifts the base address one bit and uses the LSB as a write/read. So write to address 0x52 and read from address 0x53.
According to Bing A/I, "In SPI communications, there is no specific "read/write" bit. Instead, the first byte contains the SPI address, and the following bytes contain the data. The first bit of the first byte indicates whether it is a read (1) or write (0) operation123."
So that mask sets (or clears) the MSB.
and the rest is to swap the bytes as SPI (and I2C) are big-endian and the STM32 is little endian.
- john
2025-06-16 10:22 AM
On an i2C, if the sensor has a base address of 0x29, one shifts the base address one bit and uses the LSB as a write/read. So write to address 0x52 and read from address 0x53.
According to Bing A/I, "In SPI communications, there is no specific "read/write" bit. Instead, the first byte contains the SPI address, and the following bytes contain the data. The first bit of the first byte indicates whether it is a read (1) or write (0) operation123."
So that mask sets (or clears) the MSB.
and the rest is to swap the bytes as SPI (and I2C) are big-endian and the STM32 is little endian.
- john