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
what is the best re-init sd-card?
regards,
Tuyen
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
when i re-mount sd-card it can not open file
Any file, or just one particular file?
Is that because the file was not not closed properly before you pulled the card out?
Does everything else work OK after the re-init?
If you check the card on a PC (or similar), is it corrupted?
Note that FatFs is a 3rd-party thing - nothing specifically to do with Nordic. For questions specific to FatFs, check the FatFs documentation & forum:
http://elm-chan.org/fsw/ff/00index_e.html - especially the 'Resources' section at the bottom of the page...
when i re-mount sd-card it can not open file
Any file, or just one particular file?
Is that because the file was not not closed properly before you pulled the card out?
Does everything else work OK after the re-init?
If you check the card on a PC (or similar), is it corrupted?
Note that FatFs is a 3rd-party thing - nothing specifically to do with Nordic. For questions specific to FatFs, check the FatFs documentation & forum:
http://elm-chan.org/fsw/ff/00index_e.html - especially the 'Resources' section at the bottom of the page...