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);
// 

Accepted Answer

I found it out myself. MSB first is correct and in my case I'm lucky. It is set in the spi_api.c. I guess you would have to use the mbed source code and not the pre-compiled blob in order to change it.

posted by Karsten Jarke 18 Oct 2014