Multiple SPI Instances

Hi,

I'm working on a project where the NRF52840 chip is connected to multiple peripherals (4 of them) through SPI. The hardware has been designed in a way that each peripheral has its separate SPI pins (separate clock and data pins). I'm trying to define an SPI instance for each device, however, I'm facing the following issues with that approach:

1- I used SPI instance 0 with no issues. I used instance 1 for another device and I got this compilation error:

error:  #20: identifier "NRF_DRV_SPI_INSTANCE_1" is undefined

I tried to enable multiple things in the sdk_config.h but none of them worked. Please let me know how to enable Instances1,2,3 properly.

2- I tried to reuse the same instance for 2 peripherals but I got this runtime error:

NRF_ERROR_INVALID_STATE which occurred when calling  "nrfx_spi_init" inside "nrf_drv_spi_init"

3- Is there a better approach you can recommend to talk to 4 devices at the through different SPI?

Regards

  • Hi,

    The hardware has been designed in a way that each peripheral has its separate SPI pins (separate clock and data pins).

    But not separate CS pins? 

    1- I used SPI instance 0 with no issues. I used instance 1 for another device and I got this compilation error:

    error:  #20: identifier "NRF_DRV_SPI_INSTANCE_1" is undefined

    I tried to enable multiple things in the sdk_config.h but none of them worked. Please let me know how to enable Instances1,2,3 properly.

    You need to enable corresponding nrfx define in sdk_config.h:

    // <e> NRFX_SPIM_ENABLED - nrfx_spim - SPIM peripheral driver
    //==========================================================
    #ifndef NRFX_SPIM_ENABLED
    #define NRFX_SPIM_ENABLED 1
    #endif
    // <q> NRFX_SPIM0_ENABLED  - Enable SPIM0 instance
     
    
    #ifndef NRFX_SPIM0_ENABLED
    #define NRFX_SPIM0_ENABLED 1
    #endif
    
    // <q> NRFX_SPIM1_ENABLED  - Enable SPIM1 instance
     
    
    #ifndef NRFX_SPIM1_ENABLED
    #define NRFX_SPIM1_ENABLED 1
    #endif
    
    // <q> NRFX_SPIM2_ENABLED  - Enable SPIM2 instance
     
    
    #ifndef NRFX_SPIM2_ENABLED
    #define NRFX_SPIM2_ENABLED 1
    #endif
    
    // <q> NRFX_SPIM3_ENABLED  - Enable SPIM3 instance
     
    
    #ifndef NRFX_SPIM3_ENABLED
    #define NRFX_SPIM3_ENABLED 1
    #endif
    
    // <q> NRFX_SPIM_EXTENDED_ENABLED  - Enable extended SPIM features
     
    
    #ifndef NRFX_SPIM_EXTENDED_ENABLED
    #define NRFX_SPIM_EXTENDED_ENABLED 1
    #endif
    
    // <o> NRFX_SPIM_MISO_PULL_CFG  - MISO pin pull configuration.
     
    // <0=> NRF_GPIO_PIN_NOPULL 
    // <1=> NRF_GPIO_PIN_PULLDOWN 
    // <3=> NRF_GPIO_PIN_PULLUP 
    
    #ifndef NRFX_SPIM_MISO_PULL_CFG
    #define NRFX_SPIM_MISO_PULL_CFG 1
    #endif
    
    // <o> NRFX_SPIM_DEFAULT_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef NRFX_SPIM_DEFAULT_CONFIG_IRQ_PRIORITY
    #define NRFX_SPIM_DEFAULT_CONFIG_IRQ_PRIORITY 6
    #endif

    2- I tried to reuse the same instance for 2 peripherals but I got this runtime error:

    NRF_ERROR_INVALID_STATE which occurred when calling  "nrfx_spi_init" inside "nrf_drv_spi_init"

    The driver will return this when it has already been initialized. What do you mean using same instance for 2 peripherals?

    3- Is there a better approach you can recommend to talk to 4 devices at the through different SPI?

    You can have one SPIM for multiple slaves by connecting them on the same bus, but you have to use separate CS for each slave. There is possibility that this would decrease the maximum frequency due to the increased line capacitance, but I think it should be ok if you set the GPIO to high drive. Either way you would have to test and see if it works or not.

    regards

    Jared 

  • Thanks a lot Jared. Here are my responses:

    - On the hardware side, each peripheral has a separate CS pin. I know this is not the ideal design but I need to live with that.

    And for the questions:

    1- Thanks for the info. I'll try that and see if this works.

    2- For this one, I used Instance 0 for example to initialize the spi interface for two peripherals (i.e. I reinitialised the same instance twice for 2 peripherals), which does not work from the description of the error code.

    3- I agree that would be the ideal case. However, I dont think I can change the hardware at the moment. My question was more about how to handle the current hardware from the firmware side. Do you recommend defining a different instance for each peripheral or can I reuse the same instance for multiple peripherals

    Thanks a lot in advance!

  • EAkeila said:

    2- For this one, I used Instance 0 for example to initialize the spi interface for two peripherals (i.e. I reinitialised the same instance twice for 2 peripherals), which does not work from the description of the error code.

    Have you un initialized the driver first by calling nrfx_spim_uninit (nrfx_spim_t const *p_instance)?

    EAkeila said:
    3- I agree that would be the ideal case. However, I dont think I can change the hardware at the moment. My question was more about how to handle the current hardware from the firmware side. Do you recommend defining a different instance for each peripheral or can I reuse the same instance for multiple peripherals

    I don't think it matters much if you don't use them at the same time. The current consumption would effectively be the same. 

  • Have you un initialized the driver first by calling nrfx_spim_uninit (nrfx_spim_t const *p_instance)?

    Thanks Jared. So for using one SPI instance for 2 peripherals, do I need to call "nrfx_spim_uinit" and re-initialise spi every time I need to do an spi transaction on each peripheral?

  • Hi,

    Yes, because you need to re-initialize the instance to re-configure the assigned pins for the SPIM instance. Before you can reinitialize you would have to un initialize the instance. 

    Alternatively, you can use 4 instances for each SPI slave. That way you would only have to configure it once. 

    regards

    Jared

Related