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

Conceptual questions regarding SPI master for nRF51822

I have successfully tested the SPI master loopback example on pca10001 eval board. I have a few questions regarding a real-world scenario when there is a slave SPI present. In the loop-back test the RXD byte is immediately available when TXD is written. In a non-loopback scenario, SPI master needs to wait for a valid RX byte. I don't know how to implement this wait. Should I keep reading RXD and clear EVENTS_READY till I get a valid RX byte or is "spi_master_tx_rx" smart enough to throw away blank RX bytes until it receives a real RX byte and put it in the RXD?
What triggers receiving a new RX byte? Clearing EVENTS_READY or reading the RXD? or both? Could you please point me to a simple example of how this is done?

  • spi_master_tx_rx does both, so for example, to read a register from my sensor over SPI I have the following code that works:

    uint8_t readSpiByte(uint32_t *spi_base_address, uint32_t slaveSelect, uint8_t reg) { // write a byte from tx_data, then read a byte into rx_data. // since these are done sequentially we need a 2 byte buffer to // capture the result. uint8_t tx_data[2]; tx_data[0] = READ_BIT | (reg << 3); tx_data[1] = tx_data[0]; uint8_t rx_data[2];

    // Transmit TX_RX_MSG_LENGTH bytes from tx_data and receive same 
    // number of bytes and data into rx_data
    if(!spi_master_tx_rx(spi_base_address, slaveSelect, 2, tx_data, rx_data) )
    {
        return 0;
    }	
    return rx_data[1];
    

    }

Related