Change chip select pin between nrfx_spim SPI transactions to support multiple SPI bus devices

Hi,

I am using nrf5340 with Zephyr and nrf connect sdk 1.7.0 to drive two SPI bus devices off the same bus. 

I have no problem configuring the SPI bus using nrfx_spim as below, and can communicate with each device individually switching devices at compile time. 

But I am having difficulty understanding how to switch devices at run time i..e changing the chip select pin. I have read that it cannot simply be changed in the config structure.

I assume I could could control the CS pin outside of the SPI api, or that I could reinit the whole driver instance every device switch. Neither of these are great solutions, especially given a DMA based process.

Given SPI is a multi device bus I would assume the api has support for changing CS pin without full reinitialization? 

Is there a best practice way to deal with this for nrfx_spim?

//current init code

#define SPI_INSTANCE 1
static const nrfx_spim_t spim = NRFX_SPIM_INSTANCE(SPI_INSTANCE);

nrfx_spim_config_t spim_config =
NRFX_SPIM_DEFAULT_CONFIG(APP_SPIM_SCK_PIN, APP_SPIM_MOSI_PIN, APP_SPIM_MISO_PIN, APP_SPIM_CS_PIN);

spim_config.frequency=NRF_SPIM_FREQ_4M;
spim_config.mode=NRF_SPIM_MODE_0;
spim_config.bit_order=NRF_SPIM_BIT_ORDER_MSB_FIRST;

if (NRFX_SUCCESS !=nrfx_spim_init(&spim, &spim_config, spim_event_single_byte_handler, NULL)) {
print_jr("Init Failed\n");
   LOG_DBG("Init Failed\n");
return 0;
}
manual_isr_setup();

Thanks in advance

Parents
  • Hi,

    The nrfx_spim driver does not have any API for changing the CS pin. However, if you use HW controlled CS pin, then it should be OK to call nrf_spim_csn_configure() after initializing the nrfx_spim driver to change the CS pin.

    Most SPIM instances does not have HW support for it though, and in that case you would need to modify the driver. A better alternative in that case would probably be to handle CS outside of the driver yourself. This is not fundamentally different than having it in the driver, as in both cases there is software that is involved in setting and clearing a GPIO pin.

Reply
  • Hi,

    The nrfx_spim driver does not have any API for changing the CS pin. However, if you use HW controlled CS pin, then it should be OK to call nrf_spim_csn_configure() after initializing the nrfx_spim driver to change the CS pin.

    Most SPIM instances does not have HW support for it though, and in that case you would need to modify the driver. A better alternative in that case would probably be to handle CS outside of the driver yourself. This is not fundamentally different than having it in the driver, as in both cases there is software that is involved in setting and clearing a GPIO pin.

Children
Related