This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to correct send 2 bytes to SPI with MSB

Hi!

I have AD5611 DAC connected to nRF51822 by SPI.

In DAC datasheet we need to send data by MSB first order.

Data packet is 16 bit length.

I feel variable uint16_t and try to move this data to byte array like this:

uint16_t data;
m_tx_buf[0] = data & 0xFF;
m_tx_buf[1] = (data >> 8) & 0xFF;
nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2);

SPI driver configured like this:

nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.ss_pin   = SPI_SS_PIN;
spi_config.mosi_pin = SPI_MOSI_PIN;
spi_config.sck_pin  = SPI_SCK_PIN;
spi_config.frequency = NRF_DRV_SPI_FREQ_1M;
spi_config.bit_order = NRF_DRV_SPI_BIT_ORDER_LSB_FIRST;
spi_config.mode = NRF_DRV_SPI_MODE_0;
nrf_drv_spi_init(&spi, &spi_config, spi_event_handler);

Default NRF_DRV_SPI_DEFAULT_CONFIG configured bit_order by MSB first order, but in this config DAC do not working.

When I configure bit_order to LSB first (like in code above) - DAC work properly, but I think order of data received in DAC is not correct because whichever signal level I give (0, 0x1FF, 0x7FF), the volume from DAC output remains the same.

My questions is:

  1. how to correct feel byte array from uint16_t for send by SPI?

  2. How spi driver send data by MSB: by byte (i.e. earch byte sends by MSB but arrays send by LSB) or by array ( the array is a long chain of bits that send from left bit as first to right bit last)?

Thanks.

  • In my case when data need to send by MSB fist, the correct init of spi config is set bit_order to default:

    .bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;
    

    and buffer init like this:

    m_tx_buf[1] = data & 0xFF;
    m_tx_buf[0] = (data >> 8) & 0xFF;
    

    i.e. simply swap bytes.

    About questions:

    1. In my case simply swap 2 bytes.
    2. SPI send data in MSB mode by byte. I.e. each byte sends by MSB first. Array of bytes sending by byte: "first byte in array"->"first sent".
Related