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

How to disable/uninitialize SPI on nRF9160DK used for interfacing SD card?

Hello,

I interfaced nRF9160 and SD card using SPI. I started with this sample: ncs/zephyr/samples/subsys/fs/fat_fs.

Now once the files from SD card are read, I want to uninitialize this SPI, so it can be used by other peripheral. So how can I disable or uninitialize this SPI?

I found that by using "sdhc_spi_set_cs(data, 1)" function, I can deassert chip select pin. But using this function in main program gives the error. I included the header files, still it shows error.

Code:

	while (1) {

              if(sdcard)
              {
                  sd_card();
                  file_open();
                  sdhc_spi_set_cs(data, 1);     //Error
                  sdcard = 0;
              }
		k_sleep(1000);
	}

Parents Reply Children
  • Hello,

    Thank you for the reply.

    I am not using all the SPI instances. I am aware that the same base address is shared by the peripherals. So I am using UART0, UART1, I2C2 and SPI3.

    I will try to disable SPIM by manually writing NRF_SPIM3 registers. If I encounter any problem, I will ask again. Thank you in advance.

  • Hi,

    Jagruti said:
    I will try to disable SPIM by manually writing NRF_SPIM3 registers. If I encounter any problem, I will ask again. Thank you in advance.

    No worries! contact me if you're stuck.

     

    Cheers,

    Håkon 

  • Hi,

     I am able to uninitialize the SPI3 using nrfx_spim_uninit() function.

    Code:

    	while (1) {
    
                  if(sdcard)
                  {
    
                      sd_card();
                      file_open();      // To open and read the file
                      printk("SPI uninitialized\n");
                      nrfx_spim_uninit(DT_INST_0_ZEPHYR_MMC_SPI_SLOT_BUS_NAME);   // To uninitialize the SPI
                      sdcard = 0;
                  }
    		k_sleep(1000);
    	}

    But how can I initialize same SPI peripheral again?

    Now on button press, the SPI is initializing. After reading the file, I am disabling the SPI. But again if I press the button, SPI should initialize or not?

    In sd_card() function, disk and spi initialization is present.

    void sd_card()
    {
            static const char *disk_pdrv = "SD";
    		u64_t memory_size_mb;
    		u32_t block_count;
    		u32_t block_size;
    
    		if (disk_access_init(disk_pdrv) != 0) {
    			LOG_ERR("Storage init ERROR!");
    		}
    
    		if (disk_access_ioctl(disk_pdrv,
    				DISK_IOCTL_GET_SECTOR_COUNT, &block_count)) {
    			LOG_ERR("Unable to get sector count");
    
    		}
    		LOG_INF("Block count %u", block_count);
    
    		if (disk_access_ioctl(disk_pdrv,
    				DISK_IOCTL_GET_SECTOR_SIZE, &block_size)) {
    			LOG_ERR("Unable to get sector size");
    		}
    		printk("Sector size %u\n", block_size);
    
    		memory_size_mb = (u64_t)block_count * block_size;
    		printk("Memory Size(MB) %u\n", (u32_t)memory_size_mb>>20);
    
    	mp.mnt_point = FATFS_MNTP;
    
    	int res = fs_mount(&mp);
    
    	if (res == FR_OK) {
    		printk("Disk mounted.\n");
    		lsdir(FATFS_MNTP);
    
    	} else {
    		printk("Error mounting disk.\n");
    	}
    }

    So is it possible to initialize spi, disk and repeat the open and read operation on file?

  • Hi,

     

    The _uninit function essentially disables the callback, turns off the SPIM module, and sets all pins to their default configuration. But, what it also does is to disable the callback function and interrupts, which can be a bit hard to re-enable again.

    I would rather recommend either mimic'ing the uninit function by writing directly to the NRF_SPIM3 registers (and re-doing this when you want to re-enable the SPIM), or look into the power saving module (CONFIG_DEVICE_POWER_MANAGEMENT=y), which is supported by the SPIM module:

    https://github.com/nrfconnect/sdk-zephyr/blob/master/drivers/spi/spi_nrfx_spim.c#L322-L367

     

    Kind regards,

    Håkon

  • Hi,

    Thank you for the solutions.

    But I don't understand how to implement it.

    Could you please tell in detail? How to use CONFIG_DEVICE_POWER_MANAGEMENT in the application or use the uninit function to disable and again re-enable the SPI?

    Thank you in advance.

Related