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

two channels spi on nrf52

Hi, i am new to embedded system.I want to use nrf52 DK as master and connected with two slaves by two spi channels? is it possible to achieve on nrf52 DK. 1>How to set up and configure the two spi channels? should i enable both spi1 and spi0? 2>if possible Can anyone offer me some example?

Thank You very much!!

  • There is spi example in SDK. It is possible to use two instances of SPI, but if you don't have to do two different transmissions to each slave simultaneously, you can use one instance only and choose slave with chip select pin.

  • hi wojtek, Thank you very much. since in hardware design, I have designed the chip with two separated spi buses. hence i have to use two instances of spi. i have checked the spi example,but i am still confused about how to configure two spi instances.

     nrf_drv_spi_config_t const config =
    {
        #if (SPI0_ENABLED == 1)
            .sck_pin  = SPIM0_SCK_PIN,
            .mosi_pin = SPIM0_MOSI_PIN,
            .miso_pin = SPIM0_MISO_PIN,
            .ss_pin   = SPIM0_SS_PIN,
        #elif (SPI1_ENABLED == 1)
            .sck_pin  = SPIM1_SCK_PIN,
            .mosi_pin = SPIM1_MOSI_PIN,
            .miso_pin = SPIM1_MISO_PIN,
            .ss_pin   = SPIM1_SS_PIN,
        #elif (SPI2_ENABLED == 1)
            .sck_pin  = SPIM2_SCK_PIN,
            .mosi_pin = SPIM2_MOSI_PIN,
            .miso_pin = SPIM2_MISO_PIN,
            .ss_pin   = SPIM2_SS_PIN,
        #endif
        .irq_priority = APP_IRQ_PRIORITY_LOW,
        .orc          = 0xCC,
        .frequency    = NRF_DRV_SPI_FREQ_1M,
        .mode         = NRF_DRV_SPI_MODE_1,
        .bit_order    = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST,
        .ss_pin       = NRF_DRV_SPI_PIN_NOT_USED, //added by DS - if not specified, the nrf_drv_spi_init() will set this to high, which is wrong, CS for AS3911 is active low
    };
    

    for this function, how to enable two spi instance and configure two sck, mosi,miso,ss pin?

  • Hi

    If you want to use two separate instances of SPI modules:

    In SDK 11 you will need to define what SPI modules you want to use in the nrf_drv_config.h file. In the SDK examples this file is located in the folder called "config" located in the example folder. In the file, scroll down until you find this line:

    #define SPI0_ENABLED 0
    

    and set it to 1. Then do the same on e.g. SPI module #1:

    #define SPI1_ENABLED 1
    

    Now, in your application you will have to crate an instance, configure, and enable both SPI modules. That can be done e.g. like this:

    #define SPI0_INSTANCE  0 /**< First SPI instance index. */
    #define SPI1_INSTANCE  1 /**< Second SPI instance index. */
    static const nrf_drv_spi_t spi_0 = NRF_DRV_SPI_INSTANCE(SPI0_INSTANCE );  /**< First SPI instance. */
    static const nrf_drv_spi_t spi_1 = NRF_DRV_SPI_INSTANCE(SPI1_INSTANCE );  /**< First SPI instance. */
    
    nrf_drv_spi_config_t spi_config_0 = NRF_DRV_SPI_DEFAULT_CONFIG(SPI0_INSTANCE);
    nrf_drv_spi_init(&spi_0, &spi_config_0, NULL);
    
    nrf_drv_spi_config_t spi_config_1 = NRF_DRV_SPI_DEFAULT_CONFIG(SPI1_INSTANCE);
    nrf_drv_spi_init(&spi_1, &spi_config_1, NULL);
    

    Please have a look at the SPI peripheral example in the SDK for more details.


    If you want to use one SPI module, but have two SPI devices that must be connected to the same SPI bus:

    Then you will simply have to implement two Slave Select pins (SS). One for device #1 and one for #2. These SS pins must be controlled in software by your application because slave select is not implemented in the SPI hardware modules.

Related