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

a Nrf52 function mapping to nrf51

Hi all,

I am working on interfacing an SD card as well as an OLED Display with my nrf51422 on SDK 12.2.0.

Although, both are on SPI and hence i need to configure them as multiple slaves using multiple master instances instead. I got a way on how to do it which is given here.[link text]:-

(devzone.nordicsemi.com/.../

Although this is done over nrf52, and i m getting a problem in the line:-

"volatile spi_control_block_t * my_setup = mw_spi_instance.drv_inst_idx;"

which says "mw_spi_instance" is undeclared.

Thus i want to know on how i should proceed further. Is there a parallel function in nrf51422 which can be supported replacing it?

Thank you in advance.

  • Hi,

    I looked into this futher, and don't understand exactly how the solution is meant to work. I propose the following alternative, which require less changes to the files:

    Declare the function nrf_drv_spi_switch_chips() in nrf_drv_spi.h, right below nrf_drv_spi_uninit():

    void nrf_drv_spi_switch_chips( nrf_drv_spi_t const * const p_instance,
                                   uint32_t                    chip_select );
    

    Implement the function in nrf_drv_spi.c (below nrf_drv_spi_uninit()):

    void nrf_drv_spi_switch_chips( nrf_drv_spi_t const * const p_instance,
                                   uint32_t                    chip_select )
    {
        spi_control_block_t * p_cb  = &m_cb[p_instance->drv_inst_idx];
        p_cb->ss_pin = chip_select;
    
        //A Slave select must be set as high before setting it as output,
        //because during connect it to the pin it causes glitches.
        nrf_gpio_pin_set(p_cb->ss_pin);
        nrf_gpio_cfg_output(p_cb->ss_pin);
        nrf_gpio_pin_set(p_cb->ss_pin);
    }
    

    You can now call the function with the SPI instance and SS pin as argument from any files including nrf_drv_spi.h (modified SPI master example from SDK 12.3.0):

    static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    #define SPI_SS_PIN_1 29
    #define SPI_SS_PIN_2 30
    
    ...
    
    int main(void)
    {
        NRF_LOG_INFO("SPI example\r\n");
    
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.ss_pin   = SPI_SS_PIN;
        spi_config.miso_pin = SPI_MISO_PIN;
        spi_config.mosi_pin = SPI_MOSI_PIN;
        spi_config.sck_pin  = SPI_SCK_PIN;
        APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler));
    
        while (1)
        {
            // Reset rx buffer and transfer done flag
            memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
    
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    
            while (!spi_xfer_done)
            {
                __WFE();
            }
            nrf_drv_spi_switch_chips(&spi, SPI_SS_PIN_2);
    
            APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    
            while (!spi_xfer_done)
            {
                __WFE();
            }
            nrf_drv_spi_switch_chips(&spi, SPI_SS_PIN_1);
        }
    }
    

    Best regards,

    Jørgen

  • Thank You so much. Sorry to bother you again, i am a beginner, but i am getting an error from here:-

    // Reset rx buffer and transfer done flag memset(m_rx_buf, 0, m_length); spi_xfer_done = false;

        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    
        while (!spi_xfer_done)
        {
            __WFE();
        }
        nrf_drv_spi_switch_chips(&spi, SPI_SS_PIN_2);
    
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    
        while (!spi_xfer_done)
        {
            __WFE();
        }
        nrf_drv_spi_switch_chips(&spi, SPI_SS_PIN_1);
    

    it is saying these are undefined:- m_rx_buffer, rx_length, spi_xfer_done, m_tx_buf, m_length, m_rx_buf, m_length,

    Please help me out.

    Thank you in advance.

  • This was not meant as a full example. You need to merge it into the SPI example from the SDK. m_rx_buffer, rx_length, spi_xfer_done, m_tx_buf, m_length, m_rx_buf, amd m_length is defined there.

  • Please vote and accept the answer by clicking the checkmark next to it if this was helpful!

Related