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

SPI clock pin configuration for nRF52840.

Hi,

I am using nRF52840 on custom board. I am trying to configure SPI pins, but I am not getting anything on the clock pin. I can see the chip select pin going low and high which is good. But there is no clock on the pin. Following is the code snippet from my project.

#define MEM_CS                           NRF_GPIO_PIN_MAP(0,4)
#define MEM_SI                           NRF_GPIO_PIN_MAP(0,7)
#define MEM_SO                           NRF_GPIO_PIN_MAP(1,8)
#define MEM_SCK                          NRF_GPIO_PIN_MAP(0,13)

void memInit(void)
{
	    //GPIO configuration
        nrf_gpio_cfg_output(MEM_CS);
        nrf_gpio_cfg_output(MEM_SI);
        nrf_gpio_cfg_output(MEM_SCK);
        nrf_gpio_cfg_input(MEM_SO, GPIO_PIN_CNF_PULL_Pulldown);

                
        //SPI configuration
        nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
        spi_config.ss_pin    = MEM_CS;
        spi_config.miso_pin  = MEM_SO;
        spi_config.mosi_pin  = MEM_SI;
        spi_config.sck_pin   = MEM_SCK;
        spi_config.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST;
        spi_config.frequency = NRF_SPI_FREQ_250K;
        spi_config.mode      = NRF_SPI_MODE_2;
        
        nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL);

}

uint32_t memId()
{
        __disable_irq();
        
        nrf_gpio_pin_clear(MEM_CS);         //Pin goes low now

        err  = nrf_drv_spi_transfer(&spi, p_tx_buffer, sizeof(tx_buffer), NULL, 0);  
        nrf_delay_ms(100);
        
        nrf_gpio_pin_set(MEM_CS);           //Pin goes high now

	    __enable_irq();
        return err;
}

For my custom board configuration I have set Preprocessor definitions to BOARD_CUSTOM. But for simplicity, I have defined the pins in the source code file. I am thinking to test it first and then I can define the pins for SPI in custom_board.h. On the custom board I have checked for the pins they are not connected to any other peripheral. 

For development, I am using SES with nRF SDK15.0.0 and Softdevice S140. 

Any insight would be appreciated.

Thanks!

  • I forgot to add the event handler code so to explain further, here is the code for event handler function:

    void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
                           void *                    p_context)
    {
        printf("\r\n Error : %ld", err);
    }

    Every time I am calling, memId I can see the chip select pin (MEM_CS) goes low and high. The delay in memId funciton is just for testing purpose. So, I can see the Error being printed as "0", that means the nrf_drv_spi_transfer() returns successfully. This is confusing me.

    Looking forward for any help.

  • Hi,

    Are you using the SPI or SPIM peripheral? (do you enable DMA or not in sdk_config.h)?

    While that should not explain this issue, I notice that you control the CS signal yourself using nrf_gpio_pin_clear() before the transaction and nrf_gpio_pin_set() after (using just a delay which is more than long enough). In that case you should use NRF_DRV_SPI_PIN_NOT_USED when you configure the driver. Alternatively, keep your driver configuration and remove the controlling of the CS pin in your memId() function. 

    Regarding the issue itself I dot not immediately see what casues the issue, but you disable interrupts during the transaction, and this will cause problems for the driver which is interrupt driven. So you should at least remove the call to __disable_irq().

  • Hi Einar,

    I am using SPI peripheral. I have enabled DMA using SPI0_USEE_EASY_DMA define in sdk_config.h. Here is the snippet from sdk_config file:

    #ifndef SPI0_ENABLED
    #define SPI0_ENABLED 1
    #endif
    // <q> SPI0_USE_EASY_DMA  - Use EasyDMA
     
    
    #ifndef SPI0_USE_EASY_DMA
    #define SPI0_USE_EASY_DMA 1
    #endif

    Thanks for responding back. I will try what you have told me and let you know.

    Its good to know that we should not be accessing any pins if we have configured for any peripheral.

    I will get back soon after trying.

    Thanks once again.

    Regards,

    Shivek

  • Hi Einar,

    So you should at least remove the call to __disable_irq().

    I tried removing the line __disable_irq(). But it didn't help. I am still not able to see the clock on the pin. I tried the code with the board supplied by Fanstel. I have attached the document from them which has the schematic for the dev kit I am using. I will really appreciate if you can have a look on the document and let me know if there is anything I am doing wrong with the pin selection for the purpose.

    M2_LN60E40F+Product+Specifications.pdf

    The LN60E40F is a module based on nRF9160 and nRF52840. For the schematic you can go to the page no. 25 of this document. The schematic is not having good resolution and that is frustrating for me as well. 

    Thank you for your efforts.

    Regards,

    Shivek

  • Confusing schematic, but it looks like nRF52840 P0.13 is not available on a pin; P013 may instead be the P0.13 pin on the nRF9160.  Edit: oh, maybe it's the other way round which means it is available. Hmm ..

Related