I am trying to transfer a hexadecimal value (32bit) to the slave using SPI.transfer(0x43BD50A3)
, does this work ?
The SPI.transfer()
function is expecting a byte - I imagine the compiler produced a warning about truncating the 32-bit value?
What will happen is that only the least significant byte (0xA3
here) is sent.
To send 4 bytes, have 4 calls to SPI.transfer():
SPI.transfer(0x43);
SPI.transfer(0xBD);
SPI.transfer(0x50);
SPI.transfer(0xA3);
Here’s some background on why sending more than one byte could be confusing:
Since the STM32 is little-endian, it stores the value in memory like this
0xA3, 0x50, 0xBD, 0x43
And writing this out as a single value would write the digits in the reverse order to the way they were original written, which may not be what you were expecting!
@mdma, Thanks for your explanation, should there be a delay between each SPI transfer (to prevent data collision) ? i.e to ensure that the second byte is transferred only after the first byte has been fully transmitted to the slave?
The transfer waits for the data to be received, so you don’t need a delay. in fact it does more than waiting, it fetches the byte sent from the slave also (since SPI is a full-duplex protocol.)
So, no delays needed!