I want to say something like this is what you want.
Union is plural interpretation of a storage element with different representations. It is basically casting.
You write a most significant bit left value to the hardware in 16-bit representation. The hardware will sort it out from there. The lower 9 bits are the only bits read. (Right justified, big endian, msb left)
The union and struct give a nice software abstraction of your intentions. The compiler can understand your needs. Then a critical piece of code exists to translate it down to hardware. This piece of code understands the hardware's needs and intentions.
Edit:
C does not follow a standard with bit fields. So you should ignore that. Union/overlay is pretty much a sin.
Code:
typedef struct word {unsigned int data : 9;unsigned int unused : 7;} word_t;typedef union packet {word_t word;uint16_t value;} packet_t;
Union is plural interpretation of a storage element with different representations. It is basically casting.
Code:
void SPI_writedat (spi_inst_t *spi, int cs_pin, const uint8_t data) {packet_t packet;packet.word.data = 0x100 | data;gpio_put(cs_pin, 0);int bytes = spi_write16_blocking(spi, &packet.value, 1); // Send one 'word' from packet.value;gpio_put(cs_pin, 1);}
Transmit/Receive FIFO: Read Receive FIFO. Write Transmit
FIFO. You must right-justify data when the PrimeCell SSP is
programmed for a data size that is less than 16 bits.
Unused bits at the top are ignored by transmit logic. The
receive logic automatically right-justifies.
The union and struct give a nice software abstraction of your intentions. The compiler can understand your needs. Then a critical piece of code exists to translate it down to hardware. This piece of code understands the hardware's needs and intentions.
Edit:
C does not follow a standard with bit fields. So you should ignore that. Union/overlay is pretty much a sin.
Code:
void SPI_writedat (spi_inst_t *spi, int cs_pin, const uint8_t data) {uint16_t val = 0x100 | data;gpio_put(cs_pin, 0);int bytes = spi_write16_blocking(spi, &val, 1); // Send one 'word' from packet.value;gpio_put(cs_pin, 1);}
Statistics: Posted by dthacher — Sun Nov 17, 2024 1:07 pm