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

Problems to manage SPI pins to communicate with the slave.

Hi to everyone!

I'm trying to use the nRF52832 board as master to use an external device (as slave) through SPI. I take the example peripheral/SPI to start to learn how to work with this. In the example I see the event but I don't found when is triggered. link

And, to comunnicate with the slave, the master has to accomplish with one condition: When the master pulls low the ss pin, it also has to wait until MISO pin goes low before starting to transfer. But I don't know how to manage the pins and indicate the way to comunicate with the slave device.

Any idea?

Thank in advance.

Parents
  • The NRF_DRV_SPI_EVENT_DONE event is passed to the event handler when the transfer is doen. The Slave Select pin is optional in the SPI master driver config struct. If you set this pin to NRF_SPI_PIN_NOT_CONNECTED, you can control the SS/CS pin yourself. You can then check if the MISO pin goes low before starting the SPI transfer. Something like the below code should work:

    void spi_event_handler(nrf_drv_spi_evt_t const * p_event, void * p_context)
    {
    	nrf_gpio_pin_clear(SPI_SS_PIN);
    }
    
    void spi_transfer()
    {
    	nrf_gpio_pin_set(SPI_SS_PIN);
    	while (nrf_gpio_pin_read(SPI_MISO_PIN))
    	{
    	}
    	APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    }
    
    void main()
    {
    	nrf_gpio_cfg_output(SPI_SS_PIN);
    	
    	nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.ss_pin   = NRF_SPI_PIN_NOT_CONNECTED;
        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, NULL));
    	
    	spi_transfer();
    	
    	while(1)
    	{
    		
    	}
    }
    
  • Sorry but I find a mistake in the code. The clear and set functions are in the wrong positions. To fix it you must interchange them.

Reply Children
No Data
Related