Send more than 256 bytes using nrf_drv_spi_transfer() in nRF52832 by manually control the SS pin

Hi,

I need to send more than 256 bytes by SPI using the nrf_drv_spi_transfer() function.In this case is for programming or reading a 256 bytes page in a SPI Flash chip. In the same transmission (I mean the packet of bytes maintaining the CS low) I have to send the 4 bytes of control (1 byte of command+3 bytes of address) followed by the 256 bytes to program or read.

I am using the nRF5_SDK_17.1.0 with the SES IDE environment.

I have understood from the case https://devzone.nordicsemi.com/f/nordic-q-a/16580/send-more-than-255-bytes-by-spi-nrf52 that the DMA is limited to 256 bytes in the nRF52832. And I'm obliged to use this chip.

So, for sending a packet of more than 256 bytes this would mean that I should execute more than one nrf_drv_spi_transfer() function but maintaining the SS pin low. However, what I have seen from the code of the nrfx_spim_xfer() function (called into the nrf_drv_spi_transfer function) I can only select the option NRFX_SPIM_PIN_NOT_USED of using ALL the SPI pins in this function or noone. Is there any mechanism available yet to let the function use the SPI pins except the SS pin, and move this manually like a GPIO?That is, let the nrfx_spim_xfer function manage the CLK, MOSI and MISO pins of the SPI but no the SS pin.

The other option I have is modify the code of the former functions (nrfx_spim_xfer and nrf_drv_spi_transfer) by using a parameter that could let me select the use of the SS pin in a way or in another. Or creating a copy of the nrfx_spim_xfer function with another name (like nrfx_spim_xfer_not_ss) and modifying the code but not using the SS pin, more creating a copy of the nrf_drv_spi_transfer function that would call to the new nrfx_spim_xfer_not_ss().

Thank you very much in advance.

Regards,

Joel

  • Hi,

    I have seen from the code of the nrfx_spim_xfer() function (called into the nrf_drv_spi_transfer function) I can only select the option NRFX_SPIM_PIN_NOT_USED of using ALL the SPI pins in this function or noone.

    I'm not sure if I understand what you mean here. The pins are set when you initialize the driver (nrfx_spim_init()), where you can set each individual pin to either the pin number or to NRFX_SPIM_PIN_NOT_USED. The SS pin is controlled by software in the driver for all SPI instances in nRF52832, but if you set the pin to NRFX_SPIM_PIN_NOT_USED, you can control the pin manually in your application.

    The code would be something like this:

    #define SPIM_SS_PIN 3
    uint8_t spi_xfer_done = 0;
    
    void spim_event_handler(nrfx_spim_evt_t const * p_event,
                           void *                  p_context)
    {
    	if(spi_xfer_done == 0)
    	{
    		// Start second transfer
    		APP_ERROR_CHECK(nrfx_spim_xfer(&spi, &xfer_desc, 0));
    		spi_xfer_done++;
    	}
    	if(spi_xfer_done == 1)
    	{
    		// Second transfer done, deassert SS pin
    		nrf_gpio_pin_set(SPIM_SS_PIN);
    		spi_xfer_done = 2;
    	}
    }
    
    vois spi_init(void)
    {
        nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG;
        spi_config.frequency      = NRF_SPIM_FREQ_1M;
        spi_config.ss_pin         = NRFX_SPIM_PIN_NOT_USED;
        spi_config.miso_pin       = NRFX_SPIM_MISO_PIN;
        spi_config.mosi_pin       = NRFX_SPIM_MOSI_PIN;
        spi_config.sck_pin        = NRFX_SPIM_SCK_PIN;
        APP_ERROR_CHECK(nrfx_spim_init(&spi, &spi_config, spim_event_handler, NULL));
    	
    	nrf_gpio_cfg_output(SPIM_SS_PIN);
    	nrf_gpio_pin_set(SPIM_SS_PIN);
    }
    
    vois spi_transfer(void)
    {
    	spi_xfer_done = 0;
    	// Assert SS pin before transfer
    	nrf_gpio_pin_clear(SPIM_SS_PIN);
    	
    	APP_ERROR_CHECK(nrfx_spim_xfer(&spi, &xfer_desc, 0));
    }

    Best regards,
    Jørgen

Related