I'm trying to send data over serial peripheral interface (SPI) which takes a buffer and sends it MSbit first through to data out. For this to succeed, I need my structs to be translated to a bit buffer in exactly the right way and I'm not really sure how I can guarantee cross platform that it always works.
Here's the struct that I want to send:
using data_settings = uint8_t;
enum class channel_settings : uint8_t
{
power_down_setting = 0,
DA1,
DA2,
DA3,
DA4,
DA5,
DA6,
DA7,
DA8,
power_down_release,
NA1,
NA2,
io_da_select,
io_serial_parallel,
io_parallel_serial,
io_status_setting,
};
struct message // send this over SPI
{
data_settings data;
channel_settings channel;
};
As you can see the struct message is 2 bytes in size and I suppose it will be layed out differently on big endian / little endian systems. But what about the bit ordering? In theory, the bits could also be layed out either way regardless of the byte endianness, or can't they? But the SPI driver only accepts exactly one solution where most significant bytes are sent first and most significant bits are also sent first. Here's how I think about it:
Now my questions:
- Do htonl and htons also flip the bit ordering?
- Does something like big endian with LSbit first even exist in real life? Or is the byte ordering always indicative of the bit ordering?
- How can I guarantee cross platform that the datastruct in question is always layed out exactly right in the bit buffer?