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

Parents
  • 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 

Reply
  • 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 

Children
No Data
Related