Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to transmit 20 bits at once over SPI using nRF5 SDK

Hello,

I have found similar questions on transmitting multiple bytes over SPI, but I am still not sure how to do it. I want to communicate with a driver which needs to be configured by writing 20 bits at a time to the chip. One write value could fx. be 0xD0505 (20 bits containing both the register address and the data).

I am trying to modify the nRF5 SDK (v12.2.0) SPI master example. My modifications are shown below.

So I have chosen a tx buffer size of 3 bytes (closest to 20 bit value) and then when transmitting the 20 bit value I simply use m_tx_buf[0] =  0xD0505.

I am very unsure about the [0] index in 'm_tx_buf[0]'. Is that going to work or will it only take the first 8 bits of the 20 bit value?

And also setting the 3 byte buffer size when I don't have an exact 3 byte value to transmit. Do I need to shift the data to get the correct value?

Any help is appreciated. Thanks.

regards,

Jan

******* Code snippet ********

static uint8_t m_tx_buf[3]; // I need to transmit 20 bits at once, so 3 bytes is required
static uint8_t m_rx_buf[3]; /**< RX buffer. */
static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */

while (1)
{
// Reset rx buffer and transfer done flag
memset(m_rx_buf, 0, m_length);
spi_xfer_done = false;
m_tx_buf[0] =  0xD0505;   // 20 bit value to be transmitted
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));

while (!spi_xfer_done)
{
__WFE(); // Wait For Event
}

NRF_LOG_FLUSH();

bsp_board_led_invert(BSP_BOARD_LED_0);
nrf_delay_ms(5000);
}

Related