Reinitialize SD-card

Hi

I am using nRF52840 with SDK 15.3.

I have a project where I uses FatFS to write to a SD-card with SPI interface (SPIM3). This works very well.

I am now trying to shut down the power to SD-card between writes, but I am facing some problems reinitializing the system again. My test project doesn't even turn off power to SD-card, but just tries to initialize -> shutdown -> reinitialize.

Do you have any thoughts about what could be wrong?

I have made a small project where I do the following steps:

Init_SDcard_HW(); //Initializes SPIM3 to be used with SD-card

uint8_t SD_Err = Init_SDcard_FatFS();        //Initializes the SD-card

nrf_delay_ms(100);     //HERE I AM ABLE TO RD/WR FROM SD-CARD

sd_card_shutdown();
nrf_delay_ms(100);

sd_card_remount();     //THIS FUNCTION FAILS WHEN F_MOUNT IS CALLED WITH NRF_DISK_ERR
nrf_delay_ms(100);

SD_Err = Init_SDcard_FatFS(); //Initializes the SD-card
nrf_delay_ms(100);

My functions looks like:

void Init_SDcard_HW(void)
{
      static diskio_blkdev_t drives[] = {
      DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev), NULL)
      };
      diskio_blockdev_register(drives, ARRAY_SIZE(drives));
}

uint8_t Init_SDcard_FatFS(void)
{
     FILINFO fno;

     FRESULT ff_result;
     DSTATUS disk_state = STA_NOINIT;

     for (uint32_t retries = 3; retries && disk_state; --retries)
     {
          disk_state = disk_initialize(0);
     }
     if (disk_state)
     {
          Error_Code = 1;
          return 1;
     }

     uint32_t blocks_per_mb = (1024uL * 1024uL) / m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_size;
     uint32_t capacity = m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_count / blocks_per_mb;


     ff_result = f_mount(&fs, "", 1);
     if (ff_result)
     {
          Error_Code = 2;
          return 2;
     }

}

void sd_card_shutdown(void){
     f_mount(NULL, "", 1);

     m_block_dev_sdc.block_dev.p_ops->uninit(&m_block_dev_sdc.block_dev);
}

FRESULT sd_card_remount(void) {

     ret_code_t rc = m_block_dev_sdc.block_dev.p_ops->init(&m_block_dev_sdc.block_dev, NULL, NULL);
     if (rc != NRF_SUCCESS)
     {
          return rc;
     }

     DSTATUS disk_state = disk_initialize(0);
     if (disk_state)
     {
          return disk_state;
     }

     memset(&fs, 0, sizeof(fs));
     FRESULT res = f_mount(&fs, "", 1);
     if (res != FR_OK)
     {
          return res;
     }
}

Parents
  • Hi

    In your _remount() function, it doesn't seem like you're checking the status of the SD card before trying to initialize it. Also, you should try adding some logging to debug and see what kind of error messages you're getting returned when trying to remount the board here, as troubleshooting without logs will be only guesswork from our side I'm afraid.

    Best regards,

    Simon

  • Hi,

    I will try to implement some more logging.

    In the mean time the remount function returns successfull af both init calls. The problem is that the last f_mount fails.

    If I have the memset line active (setting fs to 0) then f_mount just hangs forever. If I remove the memset call then f_mount returns FR_DISK_ERR.

Reply Children
No Data
Related