Dear all,
I'd like to re-init sd-card when detect it plug/un-plug. But i face a issue when i re-mount sd-card it can not open file....My flow as below
bool init_sd_card(void) { static DIR dir; static FILINFO fno; static FIL file; static FATFS fs; uint32_t bytes_written; FRESULT ff_result; DSTATUS disk_state = STA_NOINIT; char filename[50] = "new_testing_file.txt"; // Initialize FATFS disk I/O interface by providing the block device. 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)); printf("Initializing disk 0 (SDC)...\r\n"); for (uint32_t retries = 3; retries && disk_state; --retries) { disk_state = disk_initialize(0); } if (disk_state) { printf("Disk initialization failed %d.\r\n", disk_state); return false; } 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; printf("Capacity: %d MB\r\n", capacity); printf("Mounting volume...\r\n"); ff_result = f_mount(&fs, "", 0); if (ff_result) { printf("Mount failed %d.\r\n", ff_result); return false; } printf("Sucessed.\r\n"); ff_result = f_open(&file, filename, FA_READ); //<============= HANG HERE if (ff_result != FR_OK) { printf("Unable to open or create file: %s", filename); return false; } printf("Open file %s ok, size %d\r\n", filename, f_size(&file)); (void) f_close(&file); return true; } void sd_detect_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { DSTATUS disk_state = STA_NOINIT; if(is_sd_present() == true) { printf("Init sd-card\r\n"); init_sd_card(); } else { printf("DeInit sd-card\r\n"); if (f_mount(0, "", 0)) { printf("UnMount failed\r\n"); } } }
what is the best re-init sd-card?
regards,
Tuyen