Mkdir not working in example storage/sd_card

huybk213
Posts: 13
Joined: Tue Jan 01, 2019 9:13 am

Mkdir not working in example storage/sd_card

Postby huybk213 » Fri Jul 05, 2019 5:59 pm

Hi everyone,

I'm currently using IDF last version.
I'm trying to run example storage/sd_card.

Sdcard example seem to be good. I want to create new folder using command "mkdir", but mkdir alway return error -1.

All line below is my program and log.

Code

Code: Select all

void app_main(void)
{
    ESP_LOGI(TAG, "Initializing SD card");

#if 1
    ESP_LOGI(TAG, "Using SDMMC peripheral");
    sdmmc_host_t host = SDMMC_HOST_DEFAULT();

    // This initializes the slot without card detect (CD) and write protect (WP) signals.
    // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();

    // To use 1-line SD mode, uncomment the following line:
    // slot_config.width = 1;

    // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
    // Internal pull-ups are not sufficient. However, enabling internal pull-ups
    // does make a difference some boards, so we do that here.
    gpio_set_pull_mode(15, GPIO_PULLUP_ONLY);   // CMD, needed in 4- and 1- line modes
    gpio_set_pull_mode(2, GPIO_PULLUP_ONLY);    // D0, needed in 4- and 1-line modes
    gpio_set_pull_mode(4, GPIO_PULLUP_ONLY);    // D1, needed in 4-line mode only
    gpio_set_pull_mode(12, GPIO_PULLUP_ONLY);   // D2, needed in 4-line mode only
    gpio_set_pull_mode(13, GPIO_PULLUP_ONLY);   // D3, needed in 4- and 1-line modes

#else
    ESP_LOGI(TAG, "Using SPI peripheral");

    sdmmc_host_t host = SDSPI_HOST_DEFAULT();
    sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
    slot_config.gpio_miso = PIN_NUM_MISO;
    slot_config.gpio_mosi = PIN_NUM_MOSI;
    slot_config.gpio_sck  = PIN_NUM_CLK;
    slot_config.gpio_cs   = PIN_NUM_CS;
    // This initializes the slot without card detect (CD) and write protect (WP) signals.
    // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
#endif //USE_SPI_MODE

    // Options for mounting the filesystem.
    // If format_if_mount_failed is set to true, SD card will be partitioned and
    // formatted in case when mounting fails.
    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
        .format_if_mount_failed = false,
        .max_files = 5,
        .allocation_unit_size = 16 * 1024
    };

    // Use settings defined above to initialize SD card and mount FAT filesystem.
    // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
    // Please check its source code and implement error recovery when developing
    // production applications.
    sdmmc_card_t* card;
    esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);

    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount filesystem. "
                "If you want the card to be formatted, set format_if_mount_failed = true.");
        } else {
            ESP_LOGE(TAG, "Failed to initialize the card (%s). "
                "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
        }
        return;
    }

    // Card has been initialized, print its properties
    sdmmc_card_print_info(stdout, card);

    // Use POSIX and C standard library functions to work with files.
    // First create a file.
    ESP_LOGI(TAG, "Opening file");
    FILE* f = fopen("/sdcard/hello.txt", "w");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for writing");
        return;
    }
    fprintf(f, "Hello %s!\n", card->cid.name);
    fclose(f);
    ESP_LOGI(TAG, "File written");

    // Check if destination file exists before renaming
    struct stat st;
    if (stat("/sdcard/foo.txt", &st) == 0) {
        // Delete it if it exists
        unlink("/sdcard/foo.txt");
    }

    // Rename original file
    ESP_LOGI(TAG, "Renaming file");
    if (rename("/sdcard/hello.txt", "/sdcard/foo.txt") != 0) {
        ESP_LOGE(TAG, "Rename failed");
        return;
    }

    // Open renamed file for reading
    ESP_LOGI(TAG, "Reading file");
    f = fopen("/sdcard/foo.txt", "r");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for reading");
        return;
    }
    char line[64];
    fgets(line, sizeof(line), f);
    fclose(f);
    // strip newline
    char* pos = strchr(line, '\n');
    if (pos) {
        *pos = '\0';
    }
    ESP_LOGI(TAG, "Read from file: '%s'", line);
	
    int mk_ret = mkdir("/sdcard/new_fd_mkdir", 0775);
    ESP_LOGI(TAG, "mkdir ret %d", mk_ret);
	

    mk_ret = mkdir("/new_fd_mkdir", 0775);
    ESP_LOGI(TAG, "mkdir 2 ret %d", mk_ret);



    mk_ret = mkdir("new_fd_mkdir", 0775);
    ESP_LOGI(TAG, "mkdir 3 ret %d", mk_ret);

     mk_ret = mkdir("sdcard/new_fd_mkdir", 0775);
    ESP_LOGI(TAG, "mkdir ret 4 %d", mk_ret);
	

    // All done, unmount partition and disable SDMMC or SPI peripheral
    esp_vfs_fat_sdmmc_unmount();
    ESP_LOGI(TAG, "Card unmounted");
}
Log

Code: Select all

Name: SA02G
Type: SDSC
Speed: 20 MHz
Size: 1898MB
I (357) example: Opening file
I (377) example: File written
I (397) example: Renaming file
I (407) example: Reading file
I (407) example: Read from file: 'Hello SA02G!'
I (407) example: mkdir ret -1
I (407) example: mkdir 2 ret -1
I (407) example: mkdir 3 ret -1
I (417) example: mkdir ret 4 -1
I (417) example: Card unmounted
I'm sorry about my English.
Please help me

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Mkdir not working in example storage/sd_card

Postby Ritesh » Sat Jul 06, 2019 8:41 am

huybk213 wrote:
Fri Jul 05, 2019 5:59 pm
Hi everyone,

I'm currently using IDF last version.
I'm trying to run example storage/sd_card.

Sdcard example seem to be good. I want to create new folder using command "mkdir", but mkdir alway return error -1.

All line below is my program and log.

Code

Code: Select all

void app_main(void)
{
    ESP_LOGI(TAG, "Initializing SD card");

#if 1
    ESP_LOGI(TAG, "Using SDMMC peripheral");
    sdmmc_host_t host = SDMMC_HOST_DEFAULT();

    // This initializes the slot without card detect (CD) and write protect (WP) signals.
    // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();

    // To use 1-line SD mode, uncomment the following line:
    // slot_config.width = 1;

    // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
    // Internal pull-ups are not sufficient. However, enabling internal pull-ups
    // does make a difference some boards, so we do that here.
    gpio_set_pull_mode(15, GPIO_PULLUP_ONLY);   // CMD, needed in 4- and 1- line modes
    gpio_set_pull_mode(2, GPIO_PULLUP_ONLY);    // D0, needed in 4- and 1-line modes
    gpio_set_pull_mode(4, GPIO_PULLUP_ONLY);    // D1, needed in 4-line mode only
    gpio_set_pull_mode(12, GPIO_PULLUP_ONLY);   // D2, needed in 4-line mode only
    gpio_set_pull_mode(13, GPIO_PULLUP_ONLY);   // D3, needed in 4- and 1-line modes

#else
    ESP_LOGI(TAG, "Using SPI peripheral");

    sdmmc_host_t host = SDSPI_HOST_DEFAULT();
    sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
    slot_config.gpio_miso = PIN_NUM_MISO;
    slot_config.gpio_mosi = PIN_NUM_MOSI;
    slot_config.gpio_sck  = PIN_NUM_CLK;
    slot_config.gpio_cs   = PIN_NUM_CS;
    // This initializes the slot without card detect (CD) and write protect (WP) signals.
    // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
#endif //USE_SPI_MODE

    // Options for mounting the filesystem.
    // If format_if_mount_failed is set to true, SD card will be partitioned and
    // formatted in case when mounting fails.
    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
        .format_if_mount_failed = false,
        .max_files = 5,
        .allocation_unit_size = 16 * 1024
    };

    // Use settings defined above to initialize SD card and mount FAT filesystem.
    // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
    // Please check its source code and implement error recovery when developing
    // production applications.
    sdmmc_card_t* card;
    esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);

    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount filesystem. "
                "If you want the card to be formatted, set format_if_mount_failed = true.");
        } else {
            ESP_LOGE(TAG, "Failed to initialize the card (%s). "
                "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
        }
        return;
    }

    // Card has been initialized, print its properties
    sdmmc_card_print_info(stdout, card);

    // Use POSIX and C standard library functions to work with files.
    // First create a file.
    ESP_LOGI(TAG, "Opening file");
    FILE* f = fopen("/sdcard/hello.txt", "w");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for writing");
        return;
    }
    fprintf(f, "Hello %s!\n", card->cid.name);
    fclose(f);
    ESP_LOGI(TAG, "File written");

    // Check if destination file exists before renaming
    struct stat st;
    if (stat("/sdcard/foo.txt", &st) == 0) {
        // Delete it if it exists
        unlink("/sdcard/foo.txt");
    }

    // Rename original file
    ESP_LOGI(TAG, "Renaming file");
    if (rename("/sdcard/hello.txt", "/sdcard/foo.txt") != 0) {
        ESP_LOGE(TAG, "Rename failed");
        return;
    }

    // Open renamed file for reading
    ESP_LOGI(TAG, "Reading file");
    f = fopen("/sdcard/foo.txt", "r");
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for reading");
        return;
    }
    char line[64];
    fgets(line, sizeof(line), f);
    fclose(f);
    // strip newline
    char* pos = strchr(line, '\n');
    if (pos) {
        *pos = '\0';
    }
    ESP_LOGI(TAG, "Read from file: '%s'", line);
	
    int mk_ret = mkdir("/sdcard/new_fd_mkdir", 0775);
    ESP_LOGI(TAG, "mkdir ret %d", mk_ret);
	

    mk_ret = mkdir("/new_fd_mkdir", 0775);
    ESP_LOGI(TAG, "mkdir 2 ret %d", mk_ret);



    mk_ret = mkdir("new_fd_mkdir", 0775);
    ESP_LOGI(TAG, "mkdir 3 ret %d", mk_ret);

     mk_ret = mkdir("sdcard/new_fd_mkdir", 0775);
    ESP_LOGI(TAG, "mkdir ret 4 %d", mk_ret);
	

    // All done, unmount partition and disable SDMMC or SPI peripheral
    esp_vfs_fat_sdmmc_unmount();
    ESP_LOGI(TAG, "Card unmounted");
}
Log

Code: Select all

Name: SA02G
Type: SDSC
Speed: 20 MHz
Size: 1898MB
I (357) example: Opening file
I (377) example: File written
I (397) example: Renaming file
I (407) example: Reading file
I (407) example: Read from file: 'Hello SA02G!'
I (407) example: mkdir ret -1
I (407) example: mkdir 2 ret -1
I (407) example: mkdir 3 ret -1
I (417) example: mkdir ret 4 -1
I (417) example: Card unmounted
I'm sorry about my English.
Please help me
Hello,

Which File System you have used for SD card? Because earlier into SPIFFS there was same issue of directory level support was messing.

So, There might be chances of file system issue or something like that.
Regards,
Ritesh Prajapati

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Mkdir not working in example storage/sd_card

Postby ESP_igrr » Sun Jul 07, 2019 7:32 am

huybk213: as common with other POSIX functions, you can check the exact error cause using 'errno' variable, if a function has returned -1. You can also use strerror function to convert errno value into a human-readable message.

In this specific case, I would guess that the directory has already been created once (after you did 'make flash' and before 'make monitor') and the code doesn't check for it's existence and tries to create it again.

The version with absolute paths ("/mountpoint/directory") is the correct one, by the way.

huybk213
Posts: 13
Joined: Tue Jan 01, 2019 9:13 am

Re: Mkdir not working in example storage/sd_card

Postby huybk213 » Tue Jul 09, 2019 9:46 am

Ritesh wrote:
Sat Jul 06, 2019 8:41 am

Hello,

Which File System you have used for SD card? Because earlier into SPIFFS there was same issue of directory level support was messing.

So, There might be chances of file system issue or something like that.
Dear Ritesh,
My sdcard was formated in FAT32 MODE

huybk213
Posts: 13
Joined: Tue Jan 01, 2019 9:13 am

Re: Mkdir not working in example storage/sd_card

Postby huybk213 » Tue Jul 09, 2019 9:50 am

ESP_igrr wrote:
Sun Jul 07, 2019 7:32 am
huybk213: as common with other POSIX functions, you can check the exact error cause using 'errno' variable, if a function has returned -1. You can also use strerror function to convert errno value into a human-readable message.

In this specific case, I would guess that the directory has already been created once (after you did 'make flash' and before 'make monitor') and the code doesn't check for it's existence and tries to create it again.

The version with absolute paths ("/mountpoint/directory") is the correct one, by the way.
Dear ESP_igrr,

I just followed your instructions,

Mkdir return error code Invalid argument, and my code following

Code: Select all

    sdmmc_card_t* card;
    esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);

    if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount filesystem. "
                "If you want the card to be formatted, set format_if_mount_failed = true.");
        } else {
            ESP_LOGE(TAG, "Failed to initialize the card (%s). "
                "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
        }
        return;
    }

    // Card has been initialized, print its properties
    sdmmc_card_print_info(stdout, card);

    int mk_ret = mkdir("/sdcard/new_fd_mkdir.txt");
    ESP_LOGI(TAG, "mkdir ret %d, err code %s", mk_ret, strerror(errno));
And i don't know why mkdir return invalid argument, please help me and give me some advices.

Thank you so much

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Mkdir not working in example storage/sd_card

Postby ESP_igrr » Tue Jul 09, 2019 10:05 am

Have you enabled LFN (long file names) in menuconfig, Component config, FATFS? If not, you will have to use an 8.3 name for the directory.

huybk213
Posts: 13
Joined: Tue Jan 01, 2019 9:13 am

Re: Mkdir not working in example storage/sd_card

Postby huybk213 » Tue Jul 09, 2019 10:36 am

ESP_igrr wrote:
Tue Jul 09, 2019 10:05 am
Have you enabled LFN (long file names) in menuconfig, Component config, FATFS? If not, you will have to use an 8.3 name for the directory.
Great!!!!
Thank you so much, i did not enable long file name support before.
Problem now solved, thank you.

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Mkdir not working in example storage/sd_card

Postby Ritesh » Tue Jul 09, 2019 12:01 pm

huybk213 wrote:
Tue Jul 09, 2019 10:36 am
ESP_igrr wrote:
Tue Jul 09, 2019 10:05 am
Have you enabled LFN (long file names) in menuconfig, Component config, FATFS? If not, you will have to use an 8.3 name for the directory.
Great!!!!
Thank you so much, i did not enable long file name support before.
Problem now solved, thank you.
Great. So, It is working now..
Regards,
Ritesh Prajapati

asd123zxc
Posts: 2
Joined: Wed Dec 28, 2022 10:01 pm

Re: Mkdir not working in example storage/sd_card

Postby asd123zxc » Sun Jan 15, 2023 11:05 pm

huybk213 wrote:
Tue Jul 09, 2019 10:36 am
ESP_igrr wrote:
Tue Jul 09, 2019 10:05 am
Have you enabled LFN (long file names) in menuconfig, Component config, FATFS? If not, you will have to use an 8.3 name for the directory.
Great!!!!
Thank you so much, i did not enable long file name support before.
Problem now solved, thank you.
great, works for me too

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Mkdir not working in example storage/sd_card

Postby Ritesh » Fri Feb 03, 2023 5:44 pm

Thanks to all who provided support and great to know that it is working now.
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: No registered users and 288 guests