SPI_WRITE AND SPI_READ FUNCTIONS

Hi everybody,

I would like to make my nrf52840dk communicating with an external ADC. As the ADC communicates only using SPI, I need to use spi_write and spi_read functions (actually spi_transceive). Communication must be performed using byte (8-bit) transactions

The spi_write function is defined in this way:

spi_write ( const struct device *dev, const struct spi_config *config, const struct spi_buf_set *tx_bufs)

Suppose I would like to transmit 1 byte of data, (the address of a register) I would write something like:

uint8_t reg_address;

const struct spi_buf tx_buf = {

.buf = &reg_address,

.len = 1

};

const struct spi_buf_set tx {

.buffers = &tx_buf,

.count = 1

};

ret = spi_write (spi_dev, spi_cfg, &tx);

Does it make sense? I set tx_buf->len to 1 as myData is 1 byte long, and tx->count to 1 as I want to transmit 1 byte only.

As for the spi_transceive function, suppose I would like to read 1 byte of data from a register located at reg_address. Recycling the code snippet written above, I would write something like:

uint8_t adc_data;

const struct spi_buf rx_buf {

.buf = &adc_data,

.len = 1

};

const struct spi_buf_set rx {

.buffers = &rx_buf,

.count = 1

};

ret = spi_transceive (spi_dev, spi_cfg, &tx, &rx);

Is it correct?

Finally, I did not understand why spi_buf can be turned into an array, and spi_buf_set can point to an array of spi_buf... Is it useful in case of multiple consecutive transactions (maybe coiled inside a for loop)? Could you please provide me with an example of multiple-consecutive 1-byte write/read operations?

Thanks in advance.

Gianmarco

Related