Create two sdmmc partition

dmrsim
Posts: 24
Joined: Sat Sep 03, 2022 4:52 pm
Location: Italy

Create two sdmmc partition

Postby dmrsim » Thu Jul 11, 2024 2:09 pm

Hello,

I'm using an eMMC and I need two different partition.
With one partition I have no problem, but if I create a second partition, it fail(vfs_fat_sdmmc: host init failed (0x103)).
For example:

Code: Select all

    esp_err_t ret = esp_vfs_fat_sdmmc_mount("/test0", &host, &slot_config, &mount_config, &card);
    ret = esp_vfs_fat_sdmmc_mount("/test1", &host, &slot_config, &mount_config, &card);
How can I fix it(esp-idf 4.4.2)?

Thanks!

ESP_adokitkat
Posts: 50
Joined: Thu Jun 22, 2023 12:50 pm

Re: Create two sdmmc partition

Postby ESP_adokitkat » Mon Jul 15, 2024 12:51 am

Hello.

With `esp_vfs_fat_sdmmc_mount` helper function you are mounting an entire device and if you use it a second time, the SDMMC host is already initialized and it errors out (this will change in the future as we are currently implementing a semi-concurrent use of 2 SDMMC devices, but it doesn't matter in your case as you're using only one eMMC device with 2 file-system partitions on it).

Right now you need to manually modify `components/fatfs/diskio/diskio.c` file in esp-idf to achieve what you want (menuconfig/Kconfig option will probably be added for this later) changing this:
  1. const PARTITION VolToPart[FF_VOLUMES] = {
  2.     {0, 0},    /* Logical drive 0 ==> Physical drive 0, auto detection */
  3. #if FF_VOLUMES > 1
  4.     {1, 0},    /* Logical drive 1 ==> Physical drive 1, auto detection */
  5. #endif
  6. #if FF_VOLUMES > 2
  7.     {2, 0},     /* Logical drive 2 ==> Physical drive 2, auto detection */
  8. #endif
To this:
  1. const PARTITION VolToPart[FF_VOLUMES] = {
  2.     {0, 1},    /* Logical drive 0 ==> Physical drive 0, Partition 1 */
  3. #if FF_VOLUMES > 1
  4.     {0, 2},    /* Logical drive 1 ==> Physical drive 0, Partition 2 */
  5. #endif
  6. #if FF_VOLUMES > 2
  7.     {2, 0},     /* Logical drive 2 ==> Physical drive 2, auto detection */
  8. #endif
More info here http://elm-chan.org/fsw/ff/doc/filename.html#vol http://elm-chan.org/fsw/ff/doc/fdisk.html http://elm-chan.org/fsw/ff/doc/mkfs.html

Then it would be the best if you looked around `components/fatfs/vfs/vfs_fat_sdmmc.c` file to see how the things like partitioning, formatting, mounting, etc. are done.

I will try to write an example code for you but you will need to wait a little.

Also it would be the best if you were able to update your IDF version to v5.x (the newer the better) since it's service period ended at the start of 2023 and maintenance period will end very soon (31.7.2024) as well. There are lot of patches regarding SDMMC and FATFS in newer versions too. https://docs.espressif.com/projects/esp ... rt-periods

dmrsim
Posts: 24
Joined: Sat Sep 03, 2022 4:52 pm
Location: Italy

Re: Create two sdmmc partition

Postby dmrsim » Mon Jul 15, 2024 11:49 am

Hello ESP_adokitkat,

thanks a lot for your reply!
I have done the path that you say in: /components/fatfs/diskio/diskio.c, but for the second partition I have this error: "vfs_fat_sdmmc: host init failed (0x103)"

I will wait for your example code.
Note that at the moment we cannot update the esp-idf to version 5.

Thanks

ESP_adokitkat
Posts: 50
Joined: Thu Jun 22, 2023 12:50 pm

Re: Create two sdmmc partition

Postby ESP_adokitkat » Mon Jul 15, 2024 5:11 pm

You cannot simply call `esp_vfs_fat_sdmmc_mount` twice - it is a helper function for easy usage (i.e. mounting 1 SDMMC device with 1 partition). The SDMMC host peripheral has to be only initialized once.

Using this code I was able to mount eMMC, partition it to 2 partitions, format and mount them. Then I open, write to and read from a file on each partition. Didn't write the code for unmounting here. Tested with IDF v4.4.8. You still need to keep the change in IDF code from my earlier comment.
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <sys/unistd.h>
  4. #include <sys/stat.h>
  5. #include "esp_check.h"
  6. #include "esp_log.h"
  7. #include "esp_vfs.h"
  8. #include "esp_vfs_fat.h"
  9. #include "vfs_fat_internal.h"
  10. #include "driver/sdspi_host.h"
  11. #include "sdmmc_cmd.h"
  12. #include "diskio_impl.h"
  13. #include "diskio_sdmmc.h"
  14. #include "soc/soc_caps.h"
  15. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  16. #include "sd_protocol_defs.h"
  17. #else
  18. #include "driver/sdmmc_defs.h"
  19. #endif
  20. #if SOC_SDMMC_HOST_SUPPORTED
  21. #include "driver/sdmmc_host.h"
  22. #endif
  23.  
  24. static const char *TAG = "example";
  25.  
  26. #define SDMMC_BOARD_ESP32_EMMC_TEST 0 // Set to 1 if you're using ESP32 eMMC test board
  27.  
  28. #define EXAMPLE_PIN_CMD 11
  29. #define EXAMPLE_PIN_CLK 6
  30. #define EXAMPLE_PIN_D0 7
  31. #define EXAMPLE_PIN_D1 8
  32. #define EXAMPLE_PIN_D2 9
  33. #define EXAMPLE_PIN_D3 10
  34. #define EXAMPLE_PIN_D4 16
  35. #define EXAMPLE_PIN_D5 17
  36. #define EXAMPLE_PIN_D6 5
  37. #define EXAMPLE_PIN_D7 18
  38.  
  39. #define SDMMC_SLOT SDMMC_HOST_SLOT_0 // or SDMMC_HOST_SLOT_1
  40.  
  41. #define MOUNT_POINT1 "/emmc1"
  42. #define MOUNT_POINT2 "/emmc2"
  43. const char base_path1[] = MOUNT_POINT1;
  44. const char base_path2[] = MOUNT_POINT2;
  45.  
  46. // Here you assign how much space each partition takes up (right now 50% each),
  47. // read the example in http://elm-chan.org/fsw/ff/doc/fdisk.html
  48. DWORD partitions[] = {50, 50, 0, 0}; // 1 physical drive, 2 partitions (max 4)
  49.  
  50. #define CHECK_EXECUTE_RESULT(err, str) do { \
  51.     if ((err) !=ESP_OK) { \
  52.         ESP_LOGE(TAG, str" (0x%x).", err); \
  53.         goto cleanup; \
  54.     } \
  55.     } while(0)
  56.  
  57.  
  58. #if SDMMC_BOARD_ESP32_EMMC_TEST
  59. #define SD_TEST_BOARD_VSEL_GPIO     26
  60. #define SD_TEST_BOARD_VSEL_3V3      1
  61. #define SD_TEST_BOARD_VSEL_1V8      0
  62. #define SD_TEST_BOARD_EN_GPIO  27
  63. #define SD_TEST_BOARD_EN_LEVEL 1
  64. #define SD_TEST_BOARD_PWR_RST_DELAY_MS  5
  65. #define SD_TEST_BOARD_PWR_ON_DELAY_MS   50
  66.  
  67. static void card_power_set_esp32_emmc(bool en)
  68. {
  69.     if (en) {
  70.         /* set voltage */
  71.         gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_OUTPUT);
  72.         gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, SD_TEST_BOARD_VSEL_3V3);
  73.         /* power off to make sure card is reset */
  74.         gpio_set_direction(SD_TEST_BOARD_EN_GPIO, GPIO_MODE_OUTPUT);
  75.         gpio_set_level(SD_TEST_BOARD_EN_GPIO, !SD_TEST_BOARD_EN_LEVEL);
  76.         usleep(SD_TEST_BOARD_PWR_RST_DELAY_MS * 1000);
  77.         /* power on */
  78.         gpio_set_level(SD_TEST_BOARD_EN_GPIO, SD_TEST_BOARD_EN_LEVEL);
  79.         usleep(SD_TEST_BOARD_PWR_ON_DELAY_MS * 1000);
  80.     } else {
  81.         gpio_set_level(SD_TEST_BOARD_EN_GPIO, !SD_TEST_BOARD_EN_LEVEL);
  82.         gpio_set_level(SD_TEST_BOARD_VSEL_GPIO, 0);
  83.         gpio_set_direction(SD_TEST_BOARD_VSEL_GPIO, GPIO_MODE_INPUT);
  84.         gpio_set_direction(SD_TEST_BOARD_EN_GPIO, GPIO_MODE_INPUT);
  85.     }
  86. }
  87. #endif
  88.  
  89. static esp_err_t partition_card_2(const esp_vfs_fat_mount_config_t *mount_config, BYTE pdrv)
  90. {
  91.     FRESULT res = FR_OK;
  92.     esp_err_t err = ESP_OK;
  93.     const size_t workbuf_size = 4096;
  94.     void* workbuf = NULL;
  95.     ESP_LOGW(TAG, "partitioning card");
  96.  
  97.     workbuf = ff_memalloc(workbuf_size);
  98.     if (workbuf == NULL) {
  99.         return ESP_ERR_NO_MEM;
  100.     }
  101.  
  102.     res = f_fdisk(pdrv, partitions, workbuf);
  103.     if (res != FR_OK) {
  104.         err = ESP_FAIL;
  105.         ESP_LOGE(TAG, "f_fdisk failed (%d)", res);
  106.     }
  107.     free(workbuf);
  108.     return err;
  109. }
  110.  
  111. static esp_err_t format_card(const esp_vfs_fat_mount_config_t *mount_config, sdmmc_card_t *card)
  112. {
  113.     FRESULT res = FR_OK;
  114.     esp_err_t err;
  115.     const size_t workbuf_size = 4096;
  116.     void* workbuf = NULL;
  117.     ESP_LOGW(TAG, "partitioning card");
  118.  
  119.     workbuf = ff_memalloc(workbuf_size);
  120.     if (workbuf == NULL) {
  121.         return ESP_ERR_NO_MEM;
  122.     }
  123.     size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(
  124.                 card->csd.sector_size,
  125.                 mount_config->allocation_unit_size);
  126.     ESP_LOGW(TAG, "formatting card, allocation unit size=%d", alloc_unit_size);
  127.  
  128.     // Foramt partition 0
  129.     char drv0[3] = {(char)('0' + 0), ':', 0};
  130.     res = f_mkfs(drv0, FM_ANY, alloc_unit_size, workbuf, workbuf_size);
  131.     if (res != FR_OK) {
  132.         err = ESP_FAIL;
  133.         ESP_LOGE(TAG, "f_mkfs failed (%d)", res);
  134.         f_mount(NULL, drv0, 0);
  135.         goto fail;
  136.     }
  137.  
  138.      // Foramt partition 1
  139.     char drv1[3] = {(char)('1' + 0), ':', 0};
  140.     res = f_mkfs(drv1, FM_ANY, alloc_unit_size, workbuf, workbuf_size);
  141.     if (res != FR_OK) {
  142.         err = ESP_FAIL;
  143.         ESP_LOGE(TAG, "f_mkfs failed (%d)", res);
  144.         f_mount(NULL, drv0, 0);
  145.         f_mount(NULL, drv1, 0);
  146.         goto fail;
  147.     }
  148.  
  149.     free(workbuf);
  150.     return ESP_OK;
  151. fail:
  152.     free(workbuf);
  153.     return err;
  154. }
  155.  
  156. static esp_err_t init_sdmmc_host(int slot, const void *slot_config, int *out_slot)
  157. {
  158.     *out_slot = slot;
  159.     return sdmmc_host_init_slot(slot, (const sdmmc_slot_config_t*) slot_config);
  160. }
  161.  
  162. static esp_err_t register_to_vfs_fat(const esp_vfs_fat_mount_config_t *mount_config, sdmmc_card_t *card, uint8_t pdrv,
  163.                                   const char *base_path, FATFS **out_fs)
  164. {
  165.     FATFS* fs = NULL;
  166.     esp_err_t err;
  167.     ff_diskio_register_sdmmc(pdrv, card);
  168.     ESP_LOGD(TAG, "using pdrv=%i", pdrv);
  169.     char drv[3] = {(char)('0' + pdrv), ':', 0};
  170.  
  171.     // connect FATFS to VFS
  172.     err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
  173.     if (err == ESP_ERR_INVALID_STATE) {
  174.         // it's okay, already registered with VFS
  175.     } else if (err != ESP_OK) {
  176.         ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", err);
  177.         goto fail;
  178.     }
  179.     *out_fs = fs;
  180.     return ESP_OK;
  181. fail:
  182.     if (fs) {
  183.         f_mount(NULL, drv, 0);
  184.     }
  185.     esp_vfs_fat_unregister_path(base_path);
  186.     ff_diskio_unregister(pdrv);
  187.     return err;
  188. }
  189.  
  190. static void call_host_deinit(const sdmmc_host_t *host_config)
  191. {
  192.     if (host_config->flags & SDMMC_HOST_FLAG_DEINIT_ARG) {
  193.         host_config->deinit_p(host_config->slot);
  194.     } else {
  195.         host_config->deinit();
  196.     }
  197. }
  198.  
  199. static esp_err_t custom_mount_emmc_2_partitions(const sdmmc_host_t *host_config, const void *slot_config, const esp_vfs_fat_mount_config_t *mount_config, sdmmc_card_t** out_card, bool partition_and_format)
  200. {
  201.     esp_err_t err;
  202.     int card_handle = -1;   //uninitialized
  203.     bool host_inited = false;
  204.    
  205.     sdmmc_card_t* card = NULL;
  206.    
  207.     BYTE pdrv1 = FF_DRV_NOT_USED;
  208.     BYTE pdrv2 = FF_DRV_NOT_USED;
  209.    
  210.     char* dup_path1 = NULL;
  211.     char* dup_path2 = NULL;
  212.    
  213.     FATFS *fs1 = NULL;
  214.     FATFS *fs2 = NULL;
  215.  
  216.     dup_path1 = strdup(base_path1);
  217.     dup_path2 = strdup(base_path2);
  218.     card = (sdmmc_card_t*) malloc(sizeof(sdmmc_card_t)); // One eMMC storage device
  219.  
  220.     // Initialize SDMMC peripheral (only once!)
  221.     err = (*host_config->init)();
  222.     CHECK_EXECUTE_RESULT(err, "host init failed");
  223.     //deinit() needs to be called to revert the init
  224.     host_inited = true;
  225.     // If this failed (indicated by card_handle != -1), slot deinit needs to called()
  226.     // leave card_handle as is to indicate that (though slot deinit not implemented yet.
  227.     err = init_sdmmc_host(host_config->slot, slot_config, &card_handle);
  228.     CHECK_EXECUTE_RESULT(err, "slot init failed");
  229.  
  230.     // Probe and initialize the SD card/eMMC
  231.     err = sdmmc_card_init(host_config, card);
  232.     CHECK_EXECUTE_RESULT(err, "sdmmc_card_init failed");
  233.  
  234.     // Register partitions to VFS
  235.     // Partition 1 `/emmc1`
  236.     ff_diskio_get_drive(&pdrv1); // Get the next available drive number
  237.     err = register_to_vfs_fat(mount_config, card, pdrv1, dup_path1, &fs1);
  238.     CHECK_EXECUTE_RESULT(err, "register_to_vfs_fat failed");
  239.     // Partition 2 `/emmc2`
  240.     ff_diskio_get_drive(&pdrv2); // Get the next available drive number
  241.     err = register_to_vfs_fat(mount_config, card, pdrv2, dup_path2, &fs2);
  242.     CHECK_EXECUTE_RESULT(err, "register_to_vfs_fat failed");
  243.  
  244.     *out_card = card;
  245.  
  246.     if (partition_and_format) { // Partition and format the SD card/eMMC
  247.         err = partition_card_2(mount_config, pdrv1);
  248.         if (err != ESP_OK) {
  249.             goto fail_partition_format;
  250.         }
  251.  
  252.         err = format_card(mount_config, card);
  253.         if (err != ESP_OK) {
  254.             goto fail_partition_format;
  255.         }
  256.         // Formatting successful, unmount the partitions and disable SDMMC peripheral
  257.         // so this function can be run again with `partition_and_format == false` to mount the partitions
  258.         goto fail_partition_format;
  259.     } else { // Mount the partitions
  260.         char drv1[3] = {(char)('0' + pdrv1), ':', 0};
  261.         FRESULT res = f_mount(fs1, drv1, 1);
  262.         if (res != FR_OK) {
  263.             err = ESP_FAIL;
  264.             ESP_LOGE(TAG, "failed to mount card - partition 1 (%d)", res);
  265.             goto fail_partition_format;
  266.         }
  267.         char drv2[3] = {(char)('0' + pdrv2), ':', 0};
  268.         res = f_mount(fs2, drv2, 1);
  269.         if (res != FR_OK) {
  270.             err = ESP_FAIL;
  271.             ESP_LOGE(TAG, "failed to mount card - partition 2 (%d)", res);
  272.             goto fail_partition_format;
  273.         }
  274.     }
  275.  
  276.     return ESP_OK;
  277.  
  278. fail_partition_format:
  279.     esp_vfs_fat_unregister_path(dup_path1);
  280.     esp_vfs_fat_unregister_path(dup_path2);
  281.     ff_diskio_unregister(pdrv1);
  282.     ff_diskio_unregister(pdrv2);
  283.     goto cleanup;
  284. cleanup:
  285.     if (host_inited) {
  286.         call_host_deinit(host_config);
  287.     }
  288.     free(card);
  289.     free(dup_path1);
  290.     free(dup_path2);
  291.     return err;
  292. }
  293.  
  294. void app_main(void)
  295. {
  296.     esp_err_t ret;
  297.  
  298. #if SDMMC_BOARD_ESP32_EMMC_TEST
  299.     card_power_set_esp32_emmc(true);
  300. #endif
  301.  
  302.     // Options for mounting the filesystem.
  303.     // If format_if_mount_failed is set to true, eMMC will be partitioned and
  304.     // formatted in case when mounting fails.
  305.     esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  306.         .format_if_mount_failed = false,
  307.         .max_files = 5,
  308.         .allocation_unit_size = 16 * 1024
  309.     };
  310.     sdmmc_card_t *card = NULL;
  311.  
  312.     ESP_LOGI(TAG, "Initializing eMMC");
  313.  
  314.     // Use settings defined above to initialize eMMC and mount FAT filesystem.
  315.     // Note: esp_vfs_fat_sdmmc_mount is all-in-one convenience functions.
  316.     // Please check its source code and implement error recovery when developing
  317.     // production applications.
  318.  
  319.     ESP_LOGI(TAG, "Using SDMMC peripheral");
  320.  
  321.     // By default, eMMC frequency is initialized to SDMMC_FREQ_DEFAULT (20MHz)
  322.     // For setting a specific frequency, use host.max_freq_khz (range 400kHz - 52MHz for SDMMC)
  323.     // Example: for fixed frequency of 10MHz, use host.max_freq_khz = 10000;
  324.     sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  325.     host.max_freq_khz = SDMMC_FREQ_52M;
  326.     host.slot = SDMMC_SLOT;
  327.  
  328.     // This initializes the slot without card detect (CD) and write protect (WP) signals.
  329.     // Other fields will be initialized to zero
  330.     sdmmc_slot_config_t slot_config = {
  331.         .cd = SDMMC_SLOT_NO_CD,
  332.         .wp = SDMMC_SLOT_NO_WP,
  333.     };
  334.  
  335.     // Set bus width to use:
  336.     slot_config.width = 8;
  337.  
  338.     // Set bus IOs
  339. #if CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
  340.     slot_config.clk = EXAMPLE_PIN_CLK;
  341.     slot_config.cmd = EXAMPLE_PIN_CMD;
  342.     slot_config.d0 = EXAMPLE_PIN_D0;
  343.     slot_config.d1 = EXAMPLE_PIN_D1;
  344.     slot_config.d2 = EXAMPLE_PIN_D2;
  345.     slot_config.d3 = EXAMPLE_PIN_D3;
  346.     slot_config.d4 = EXAMPLE_PIN_D4;
  347.     slot_config.d5 = EXAMPLE_PIN_D5;
  348.     slot_config.d6 = EXAMPLE_PIN_D6;
  349.     slot_config.d7 = EXAMPLE_PIN_D7;
  350. #endif  // CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
  351.  
  352.     // Enable internal pullups on enabled pins. The internal pullups
  353.     // are insufficient however, please make sure 10k external pullups are
  354.     // connected on the bus. This is for debug / example purpose only.
  355.     slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  356.  
  357.     ESP_LOGI(TAG, "Mounting filesystem");
  358.  
  359.     // Last argument is partition_and_format, set to true to partition and format the eMMC
  360.     ret = custom_mount_emmc_2_partitions(&host, &slot_config, &mount_config, &card, true);
  361.     if (ret != ESP_OK) {
  362.         if (ret == ESP_FAIL) {
  363.             ESP_LOGE(TAG, "Failed partition and format.");
  364.         } else {
  365.             ESP_LOGE(TAG, "Failed to initialize the eMMC (%s). "
  366.                      "Make sure eMMC lines have pull-up resistors in place.", esp_err_to_name(ret));
  367.         }
  368.         return;
  369.     }
  370.  
  371.     // Last argument is partition_and_format, set to false to mount partitions on the eMMC
  372.     ret = custom_mount_emmc_2_partitions(&host, &slot_config, &mount_config, &card, false);
  373.     if (ret != ESP_OK) {
  374.         if (ret == ESP_FAIL) {
  375.             ESP_LOGE(TAG, "Failed to mount filesystem. "
  376.                      "If you want the eMMC to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  377.         } else {
  378.             ESP_LOGE(TAG, "Failed to initialize the eMMC (%s). "
  379.                      "Make sure eMMC lines have pull-up resistors in place.", esp_err_to_name(ret));
  380.         }
  381.         return;
  382.     }
  383.     ESP_LOGI(TAG, "Filesystem mounted");
  384.  
  385.     // Card has been initialized, print its properties
  386.     sdmmc_card_print_info(stdout, card);
  387.  
  388.     // First create a file.
  389.     const char *file_a = MOUNT_POINT2"/a.txt";
  390.  
  391.     ESP_LOGI(TAG, "Opening file %s", file_a);
  392.     FILE *f = fopen(file_a, "w");
  393.     if (f == NULL) {
  394.         ESP_LOGE(TAG, "Failed to open file for writing");
  395.         return;
  396.     }
  397.     fprintf(f, "Hello %s!\n", card->cid.name);
  398.     fclose(f);
  399.     ESP_LOGI(TAG, "File written");
  400.  
  401.     // First create a file.
  402.     const char *file_b = MOUNT_POINT1"/b.txt";
  403.  
  404.     ESP_LOGI(TAG, "Opening file %s", file_b);
  405.     f = fopen(file_b, "w");
  406.     if (f == NULL) {
  407.         ESP_LOGE(TAG, "Failed to open file for writing");
  408.         return;
  409.     }
  410.     fprintf(f, "Hello %d!\n", card->cid.serial);
  411.     fclose(f);
  412.     ESP_LOGI(TAG, "File written");
  413.  
  414.     // Open renamed file for reading
  415.     ESP_LOGI(TAG, "Reading file %s", file_b);
  416.     f = fopen(file_b, "r");
  417.     if (f == NULL) {
  418.         ESP_LOGE(TAG, "Failed to open file for reading");
  419.         return;
  420.     }
  421.  
  422.     // Read a line from file
  423.     char line[64];
  424.     fgets(line, sizeof(line), f);
  425.     fclose(f);
  426.  
  427.     // Strip newline
  428.     char *pos = strchr(line, '\n');
  429.     if (pos) {
  430.         *pos = '\0';
  431.     }
  432.     ESP_LOGI(TAG, "Read from file: '%s'", line);
  433.  
  434.     // Open renamed file for reading
  435.     ESP_LOGI(TAG, "Reading file %s", file_a);
  436.     f = fopen(file_a, "r");
  437.     if (f == NULL) {
  438.         ESP_LOGE(TAG, "Failed to open file for reading");
  439.         return;
  440.     }
  441.  
  442.     // Read a line from file
  443.     fgets(line, sizeof(line), f);
  444.     fclose(f);
  445.  
  446.     // Strip newline
  447.     pos = strchr(line, '\n');
  448.     if (pos) {
  449.         *pos = '\0';
  450.     }
  451.     ESP_LOGI(TAG, "Read from file: '%s'", line);
  452.  
  453.     ESP_LOGI(TAG, "Done");
  454. }
Output log:
  1. I (801) example: Initializing eMMC
  2. I (801) example: Using SDMMC peripheral
  3. I (801) example: Mounting filesystem
  4. I (801) gpio: GPIO[10]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
  5. W (1131) example: partitioning card
  6. W (1131) example: partitioning card
  7. W (1131) example: formatting card, allocation unit size=16384
  8. I (10551) gpio: GPIO[10]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
  9. I (10611) example: Filesystem mounted
  10. Name: DG4032
  11. Type: MMC
  12. Speed: 52 MHz
  13. Size: 29820MB
  14. CSD: ver=3, sector_size=512, capacity=61071360 read_bl_len=9
  15. I (10611) example: Opening file /emmc2/a.txt
  16. I (10641) example: File written
  17. I (10641) example: Opening file /emmc1/b.txt
  18. I (10671) example: File written
  19. I (10671) example: Reading file /emmc1/b.txt
  20. I (10671) example: Read from file: 'Hello 10620941!'
  21. I (10671) example: Reading file /emmc2/a.txt
  22. I (10681) example: Read from file: 'Hello DG4032!'
  23. I (10681) example: Done

dmrsim
Posts: 24
Joined: Sat Sep 03, 2022 4:52 pm
Location: Italy

Re: Create two sdmmc partition

Postby dmrsim » Mon Jul 22, 2024 9:05 am

Hello ESP_adokitkat,

Thanks a lot, it works correctly!!
Best regards,

Simeon

random_fmad
Posts: 3
Joined: Tue Oct 25, 2022 10:46 am

Re: Create two sdmmc partition

Postby random_fmad » Tue Aug 06, 2024 4:08 am

This is logs from trying the above code. I'm using ESP IDF 5.1.4, everything seems to be okay, but the partitions aren't forming. Any help would be appreciated ESP_adokitkat.
  1. I (1009) SD Card: Using SDMMC peripheral
  2. I (1010) SD Card: Mounting filesystem
  3. I (1011) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
  4. I (1348) SD Card: Drive number: 0
  5. I (1349) SD Card: Using drive number: 0
  6. I (1349) SD Card: Mount point: /normal1
  7. I (1351) SD Card: Attempting to register FATFS with VFS
  8. I (1357) SD Card: FATFS successfully registered with VFS. Drive: 0:
  9. I (1363) SD Card: Drive number: 1
  10. I (1367) SD Card: Using drive number: 1
  11. I (1372) SD Card: Mount point: /normal2
  12. I (1376) SD Card: Attempting to register FATFS with VFS
  13. I (1382) SD Card: FATFS successfully registered with VFS. Drive: 1:
  14. W (1389) SD Card: partitioning card
  15. I (1396) SD Card: f_fdisk returned: 0
  16. W (1398) SD Card: Partitioning card
  17. W (1402) SD Card: Formatting card, allocation unit size=6144
  18. I (7801) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
  19. I (7857) SD Card: Drive number: 0
  20. I (7857) SD Card: Using drive number: 0
  21. I (7858) SD Card: Mount point: /normal1
  22. I (7859) SD Card: Attempting to register FATFS with VFS
  23. I (7865) SD Card: FATFS successfully registered with VFS. Drive: 0:
  24. I (7872) SD Card: Drive number: 1
  25. I (7876) SD Card: Using drive number: 1
  26. I (7881) SD Card: Mount point: /normal2
  27. I (7885) SD Card: Attempting to register FATFS with VFS
  28. I (7891) SD Card: FATFS successfully registered with VFS. Drive: 1:
  29. I (7900) SD Card: f_mount returned: 0
  30. I (7902) SD Card: Using drive 0:
  31. I (7907) SD Card: f_mount returned: 0
  32. I (7911) SD Card: Using drive: 1:
  33. I (7915) SD Card: Filesystem mounted
  34. Name: APPSD
  35. Type: SDHC/SDXC
  36. Speed: 20.00 MHz (limit: 20.00 MHz)
  37. Size: 7680MB
  38. CSD: ver=2, sector_size=512, capacity=15728640 read_bl_len=9
  39. SSR: bus_width=4
  40. I (7933) SD Card: Opening file /normal2/a.txt
  41. I (7951) SD Card: File written
  42. I (7951) SD Card: Opening file /normal1/b.txt
  43. I (7963) SD Card: File written
  44. I (7963) SD Card: Reading file /normal1/b.txt
  45. I (7965) SD Card: Read from file: 'Hello 310378496!'
  46. I (7966) SD Card: Reading file /normal2/a.txt
  47. I (7973) SD Card: Read from file: 'Hello APPSD!'
  48. I (7977) SD Card: Done

Who is online

Users browsing this forum: ccrause and 215 guests