Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 2 months ago.
Bit order
In which order are the bits transmitted and received. Most significant or least significant bit first? How can this be set if the micro controller allows either option?
Question relating to:
1 Answer
10 years, 2 months ago.
I think the standard SPI in mbed only supports MSB first. At least on FRDM platforms that's what I've seen.
If you need LSB first
this macro will flip the bits
#define REVERSE_BITS(byte) (((reverse_lookup[(byte & 0x0F)]) << 4) + reverse_lookup[((byte & 0xF0) >> 4)]) static const uint8_t reverse_lookup[] = { 0, 8, 4, 12, 2, 10, 6, 14,1, 9, 5, 13,3, 11, 7, 15 };
Then something like this when you transmit and receive
you'll need something like this when you send the data
/ flip command byte to LSB txByte=REVERSE_BITS(txCommand); // transmit and receive rxByte=spiInstance.write(txByte); // flip received byte to MSB first rxByte=REVERSE_BITS(rxByte); //