Code: Select all
void spi_out_byte(unsigned char data)
{
// MSb-first bit order - SPI standard
unsigned x = bitrev(data) >> 24;
spi_mosi <: x;
spi_sclk <: 0xAA;
spi_sclk <: 0xAA;
sync(spi_sclk);
spi_miso :> void;
}
bitrev() appears to be an assembler instruction that does what it says: reverses the bits in a word. I'm assuming therefore that chars are stored in the fourth byte of a register? The code fragment also appears to imply that they are stored little-endian bit order, so that applying bitrev and then right-shifting by 24 will leave the fourth byte of the register holding the char in big-endian bit order. Is this correct?
The rest of the SPI interface assumes a big-endian byte order.
Finally, the output to the 1-bit 8-place buffered port spi_mosi using an unsigned (32-bit) integer will presumably do some conversion too - I assume it knows to read the least-significant byte assuming a big-endian format as the 8 bits?
Appreciate help with this. I think the way the code is written is far from obvious, and I'll comment it up with any help I receive before putting it somewhere helpful for others to use.