Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6796

SDK • Re: How to send 9-bit data words via PICO hardware_spi API

$
0
0
OK, I have tried to implement your suggestion but have a few questions if you wouldn't mind. I got compile errors on your two typedef statements as I gather they needed a 3rd parameter. I have just repeated the name at the end as follows which stopped those errors. Is that right?

Code:

typedef struct word_t {unsigned int data : 9;unsigned int unused : 7;} word_t;typedef union packet_t {word_t word;uint16_t value;} packet_t;
Can I just check my understanding of how this works? Does the union provide a way to access the same 16-bits of data as two different data types/structures?

To call the SPI_writedat function, the parameters have to be of the same type as defined in the function so to make a write function, I need to receive the data parameter as uint8_t.
This is what I currently have ...

Code:

void SPI_writedat ( spi_inst_t *spi, const uint8_t data ) {packet_t packet;packet.value = 0x0100 | data; // Add 9th bit of 1gpio_put(cs_pin, 0);int bytes = spi_write16_blocking(spi1, &packet.value, 1);gpio_put(cs_pin, 1);printf("%d data bytes written.\n", bytes);)SPI_writedat(spi, 0xFF); // Example call
If spi_write16_blocking() can take the data as uint16_t (&packet.value), I don't understand the function of the alternative structure word_t within packet_t? As I have specified the SPI format as 9-bits per transfer, why am I nor passing spi_write16_blocking &packet.word.data ?

If spi_write16_blocking has to (as it's name suggests) receive data as 16-bit but will only send 9-bits (as per the SPI format), can you clarify if it is the first 9 bits from the left or the right (i.e. for a 16-bit word D15-D0, will the SPI API send D15-D7 or D8-D0) and in what order (highest bit to lowest or lowest to highest)?

Many thanks

Statistics: Posted by all2coolnick — Sat Nov 16, 2024 4:38 pm



Viewing all articles
Browse latest Browse all 6796

Trending Articles