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 Reply Children
  • Ok, so nrfx_spim_xfer() will handle the ss_pin for me if set in initialization? Here is my initialization code:

       // SPIM0, NVM, initialization:
        nrfx_spim_config_t spim0_config = NRFX_SPIM_DEFAULT_CONFIG;
    
        spim0_config.frequency      = NRF_SPIM_FREQ_8M;
        spim0_config.ss_pin         = E_NVM_CS_PIN;
        spim0_config.miso_pin       = E_NVM_MISO_PIN;
        spim0_config.mosi_pin       = E_NVM_MOSI_PIN;
        spim0_config.sck_pin        = E_NVM_SCLK_PIN;
        spim0_config.ss_active_high = false; // Chip select is active low.
        APP_ERROR_CHECK( nrfx_spim_init( &spim_nvm, &spim0_config, nvm_spim_event_handler, NULL ) );

    just want to confirm that I don't need to call 

    nrf_gpio_pin_clear(E_NVM_CS_PIN); // to activate
    and
    nrf_gpio_pin_set(E_NVM_CS_PIN); // to de-activate
  • Yes, with that config the driver will toggle the SS pin for you, no need to handle that manually in the application.

Related