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!

Parents
  • 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 ..

  • Hi Einar, 

    As I mentioned in my response to , I have tried using P0.13 as normal GPIO and tested toggling the pin, I was able to see the pin toggling on scope. But if I am using the same pin for SPI clock, it won't get any clock going on that pin.

    Would you be able to send a simple example(other than which is included in SDK), which I can test on my hardware just by simply changing the pins?

     I think that would make me clear that whether its a hardware issue or software issue.

    Regards,

    Shivek

  • I am not getting anything on the clock pin -- at 250kHz clock I would think even a cheap 'scope would see that, but are you using (say) some low-cost analyser which might have too low a resolution?

    p_tx_buffer,and sizeof(tx_buffer) are inconsistent (different buffers), maybe make the buffer size bigger so easier to see the clocks. Hopefully the buffer is not defined const ..

  • Hi Shivek,

    I see you ask for an example which is not included in the SDK, but did you try the SPI Master Example? That is quite simple so if I was to make another example it would be basically the same. Have you tested that on your HW?

  • Hi Einar,

    I found something not good with the SPI peripheral registers. According to my understanding, SPI driver from Nordic is associated with SPI peripheral interface master (page no. 398 of nrf52840 PS v 1.2) and SPIM driver is associated with SPIM with EasyDMA (page no. 406 of nrf52840 PS v 1.2). 

    So, if we want to use simple SPI peripheral (not using EasyDMA) with SPI0 instance, we need to set SPI0_ENABLEDSPI_ENABLED to 1 and we need to disable SPIM peripheral by setting NRFX_SPIM0_ENABLED, NRFX_SPIM_ENABLED to 0.

    Firstly, I found in the spi_master_using_nrf_spi_mngr that NRFX_SPIM_ENABLED is set to 1. May I ask why is that so?

    Secondly, when I debug the same example on nrf52840, I can see the registers for both SPIM0 and SPI0 being configured with the same pins. Which can be a problem as described in the nrf52840 Product specifications. The document clearly says that only one peripheral can be associated to a particular pin at one point of time. 

    Following is the snapshot from debug mode when I try with the above mentioned example:

    I tried writing directly to the ENABLE register of SPIM0 so to disable it. It didn't make a difference. Here is the configuration function code for SPI0 instance initialization.

            //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;
            
            //disabling the SPIM0.
            NRF_SPIM0->ENABLE    = 0;
            
            nrf_drv_spi_init(&spi, &spi_config, NULL, NULL);

    I will appreciate if you can provide full settings for sdk_config.h or anything else, to use SPI0/SPIM0 with and without EasyDMA as I am not able to find any documentation on that. 

    Regards,

    Shivek

  • To clarify the peripherals, SPI0 and SPIM0 and TWI0 are all the same physical hardware peripheral, so only one can be used at any given time; to distinguish between SPI0 and SPIM0 the Enable bits are different - bit 0 enables the SPI peripheral and bits 1 and 2 additionally enable the Tx and Rx DMA (SPIM peripheral). Usually the only reason to not use DMA (SPIM) is to avoid the increased power drain caused by DMA.

    Einar will be better able to describe the SDK settings; I prefer bare-metal which avoids those.

Reply
  • To clarify the peripherals, SPI0 and SPIM0 and TWI0 are all the same physical hardware peripheral, so only one can be used at any given time; to distinguish between SPI0 and SPIM0 the Enable bits are different - bit 0 enables the SPI peripheral and bits 1 and 2 additionally enable the Tx and Rx DMA (SPIM peripheral). Usually the only reason to not use DMA (SPIM) is to avoid the increased power drain caused by DMA.

    Einar will be better able to describe the SDK settings; I prefer bare-metal which avoids those.

Children
  • Hi hmolesworth,

    Thanks for the clarification. I saw that happening with the bit0 of ENABLE register when only SPI0 is enabled and bits1 and bits2 set to 0 when I disabled the EasyDMA. 

    I can see pin toggling at serial clock pin but once only. Like it will go from high to low and then low to high but no serial clock. Will try to use SPIM0 instead of SPI0.

    Regards,

    Shivek

Related