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

NRFX SPIM write and read

Hi team,

I want to make simple communication between NRF52840 dongle and mcp2517 CAN FD contoller using SPI. I do not understand, how to send and read values to and from registers. Same SPI communication works well between dongles.

I set the register value

// Defined register values for MCP
#define cINSTRUCTION_WRITE	     0x02

Set the SPI instance index and flag 

/**< SPI instance index. */
static const nrfx_spim_t spi = NRFX_SPIM_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */

/**< Flag used to indicate that SPI instance completed the transfer. */
static volatile bool spi_xfer_done; 

Created two buffers for transfer and receive and set transfer length

static uint8_t          m_tx_buf[5];
//static uint8_t        cINSTRUCTION_WRITE_m_rx_buf[5];  /**< RX buffer. */
static uint8_t          m_rx_buf[5];  /**< RX buffer. */
static const uint8_t    m_length = sizeof(m_tx_buf);        /**< Transfer length. */

SPIM event handler function 

void spim_event_handler(nrfx_spim_evt_t const * p_event,
                       void *                  p_context)
{
    spi_xfer_done = true;
    /*
    NRF_LOG_INFO("Transfer completed.");
    if (m_rx_buf[0] != 0)
    {
        NRF_LOG_INFO(" Received:");
        NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
    }
    */
}

In the main function I set function for starting the SPI data transfer with DCX control with SPI config

 nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(m_tx_buf, m_length, m_rx_buf, m_length);

nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG;
    spi_config.frequency      = NRF_SPIM_FREQ_1M;
    spi_config.ss_pin         = NRFX_SPIM_SS_PIN;
    spi_config.miso_pin       = NRFX_SPIM_MISO_PIN;
    spi_config.mosi_pin       = NRFX_SPIM_MOSI_PIN;
    spi_config.sck_pin        = NRFX_SPIM_SCK_PIN;
    spi_config.dcx_pin        = NRFX_SPIM_DCX_PIN;
    spi_config.use_hw_ss      = true;
    spi_config.ss_active_high = false;
    APP_ERROR_CHECK(nrfx_spim_init(&spi, &spi_config, spim_event_handler, NULL));

And in the while loop I should start writing and reading registers.  Here I start to face with the problems. How I can  prepare tx_buf with values and how to start send values and read it from register? 

Sorry about huge misunderstanding, but I need to make things clear. 

Parents
  • Hi,

    I'm not sure I understand your questions. You can assign values to tx_buf as normal, for instance like this:

    m_tx_buf[0] = 0x01;
    m_tx_buf[1] = 0x02;
    m_tx_buf[2] = 0x03;
    m_tx_buf[3] = 0x04;
    m_tx_buf[4] = 0x05;

    In your code, you have already created a transfer description struck, xfer_desc, which points to the TX/RX buffers and their length. You can pass this to the function nrfx_spim_xfer/nrfx_spim_xfer_dcx to start the transfer. 

    Best regards,
    Jørgen

  • Typically, the first TX byte is the register address, yes. You seem to have set both tx length and rx length parameters to 1 byte. Note that the RX buffer will be filled when the TX byte is clocked out, not just afterwards. Since the SPI device typically does not know what to respond with before after it has received the entire register address in the TX-buffer, you need to set the RX length to the length of the TX-buffer + the expected response. You can ignore the first byte in the RX-buffer that was clocked out during the TX-part of the transfer.

Reply
  • Typically, the first TX byte is the register address, yes. You seem to have set both tx length and rx length parameters to 1 byte. Note that the RX buffer will be filled when the TX byte is clocked out, not just afterwards. Since the SPI device typically does not know what to respond with before after it has received the entire register address in the TX-buffer, you need to set the RX length to the length of the TX-buffer + the expected response. You can ignore the first byte in the RX-buffer that was clocked out during the TX-part of the transfer.

Children
No Data
Related