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

Can I use same handler for two different SPI's interfaces?

HEllo,

I was wondering whether or not is advisable to use the same handler to handle transmission/reception of data over SPI0 and SPI1 interfaces.

I have two sensors connected each one to each SPI, let's say sensorA to SPI0 and sensorB to SPI1, I have configured them like below, where it can be seen that I use different handlers for each one, but I wonder if I can combine both handlers into one, is that possible?

I would like to know as well how can I detect if my transmission is done for each SPI, so far I have something like this, but not sure whether it's efficent or not, am I going in the right way?

Thanks in advance

void spi_write_transfer(enableSPI spiInterfaceToUse, unsigned char slaveDeviceId, unsigned char* data, unsigned char bytesNumber) {
  // Reset transfer done flag
  spi_xfer_done_B = false;
  spi_xfer_done_A = false;

  if (spiInterfaceToUse == spiA) {
    APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi_A, data, bytesNumber, NULL, 0));

    while (!spi_xfer_done_A) {
      __WFE();
    }
  } else if(spiInterfaceToUse == spiB){
    APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi_B, data, bytesNumber, NULL, 0));

    while (!spi_xfer_done_B) {
      __WFE();
    }
  }}

Handlers

void spi0_event_handler(nrf_drv_spi_evt_t const * p_event)
{
    if(p_event->type == NRF_DRV_SPI_EVENT_DONE){
      spi_xfer_done_A = true;
    }
      
}

void spi1_event_handler(nrf_drv_spi_evt_t const * p_event)
{
    if(p_event->type == NRF_DRV_SPI_EVENT_DONE){
      spi_xfer_done_B = true;
    }
      
}

SPIM configuration

uint32_t spis_start(enableSPI sensorToEnable){

    uint32_t err_code;
    // Enable the constant latency sub power mode to minimize the time it takes
    // for the SPIS peripheral to become active after the CSN line is asserted
    // (when the CPU is in sleep mode).
    NRF_POWER->TASKS_CONSTLAT = 1;

    // Configuring SPIM0 for the A
    nrf_drv_spi_config_t spi_config_A = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config_A.ss_pin   = SPIM0_SS_PIN;
    spi_config_A.miso_pin = SPIM0_MISO_PIN;
    spi_config_A.mosi_pin = SPIM0_MOSI_PIN;
    spi_config_A.sck_pin  = SPIM0_SCK_PIN;

    // Configuring SPIM0 for the B
    nrf_drv_spi_config_t spi_config_B = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config_B.ss_pin    = SPIM1_SS_PIN;
    spi_config_B.miso_pin  = SPIM1_MISO_PIN;
    spi_config_B.mosi_pin  = SPIM1_MOSI_PIN;
    spi_config_B.sck_pin   = SPIM1_SCK_PIN;

    //Initialising the sensor
    switch(sensorToEnable){
        case spiA:
            NRF_LOG_INFO("spis_start trying to start SPI0");
            err_code = nrf_drv_spi_init(&spi_A, &spi_config_A, spi0_event_handler);
            APP_ERROR_CHECK(err_code);
            break;
        case spiB:
            NRF_LOG_INFO("spis_start trying to start SPI1");
            err_code = nrf_drv_spi_init(&spi_B, &spi_config_B, spi1_event_handler);
            APP_ERROR_CHECK(err_code);
            break;
        case BOTH:
            NRF_LOG_INFO("spis_start trying to start SPI0 & SPI1");
            err_code = nrf_drv_spi_init(&spi_A, &spi_config_A, spi0_event_handler);
            APP_ERROR_CHECK(err_code);
            err_code = nrf_drv_spi_init(&spi_B, &spi_config_B, spi1_event_handler);
            APP_ERROR_CHECK(err_code);
            break;
        default: 
            break;
    }

    return  err_code;
}
Related