Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple example: replugging SD-Card while unmounted results in low level disk error #94

Open
nasrudin2468 opened this issue May 31, 2024 · 1 comment

Comments

@nasrudin2468
Copy link

nasrudin2468 commented May 31, 2024

Dear Carl,

I am trying to extend your simple example for an application where sd cards get removed and replugged while the program continues to run.

I've modified the code the code to run within a loop and added delays between unmount and remount for replugging the sd card.
During testing testing i've noticed the following behavior:

As soon as i remove and insert the sd-card again, the app "panics" with the following output:

No response CMD:17
No response CMD:17
No response CMD:17
No response CMD:17 response: 0xff

*** PANIC ***

f_mount error: A hard error occurred in the low level disk I

As long as the card stays plugged-in the first time since reboot - access works fine. Also if I replug the card without ever accessing it (since reboot) it works just fine.

Would you please point me in the right direction regarding the additional things that need to be considered for this usecase?

The modified sample code:

#include <stdio.h>
//
#include "f_util.h"
#include "ff.h"
#include "pico/stdlib.h"
#include "rtc.h"
//
#include "hw_config.h"

int main() {
    stdio_init_all();
    // Set display chip select to high (connected to same SPI)
    gpio_init(5);
    gpio_set_dir(5, GPIO_OUT);
    gpio_put(5, 1);

    // set and hold i2c portexpander in reset (high impedance outputs)
    gpio_init(17);
    gpio_set_dir(17, GPIO_OUT);
    gpio_put(17, 0);
    time_init();
    sleep_ms(2000);
    puts("------------");
    puts("Hello, world!");
    puts("------------");

    while (true) {
    	puts("Insert Card...");
    	sleep_ms(8000);
	// See FatFs - Generic FAT Filesystem Module, "Application Interface",
	// http://elm-chan.org/fsw/ff/00index_e.html
	FATFS fs;
	puts("mount...");
	FRESULT fr = f_mount(&fs, "", 1);
	if (FR_OK != fr) panic("f_mount error: %s (%d)\n", FRESULT_str(fr), fr);
	FIL fil;
	const char* const filename = "filename_long.txt";
	puts("fr_open file.txt...");
	fr = f_open(&fil, filename, FA_OPEN_APPEND | FA_WRITE);
	if (FR_OK != fr && FR_EXIST != fr) {
		panic("f_open(%s) error: %s (%d)\n", filename, FRESULT_str(fr), fr);
        }
	puts("write hello world into file...");
	if (f_printf(&fil, "Hello, world!\n") < 0) {
		printf("f_printf failed\n");
	}
	puts("close file");
	fr = f_close(&fil);
	if (FR_OK != fr) {
		printf("f_close error: %s (%d)\n", FRESULT_str(fr), fr);
	}
	puts("unmount...");
	f_unmount("");

	puts("Done!");
	puts("Remove Card now!");
	sleep_ms(8000);
    }

}

Kind Regards
Nasrudin

@carlk3
Copy link
Owner

carlk3 commented Jun 2, 2024

Does your SD card socket support Card Detection (Insertion/Removal)? I recommend that you set up an interrupt on the Card Detect line to detect removal or insertion of the card when it happens. For example, see main.cpp. Also, see Notes about Card Detect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants