emmc_example_main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /* eMMC and FAT filesystem example.
  7. This example code is in the Public Domain (or CC0 licensed, at your option.)
  8. Unless required by applicable law or agreed to in writing, this
  9. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  10. CONDITIONS OF ANY KIND, either express or implied.
  11. */
  12. // This example uses SDMMC peripheral to communicate with an eMMC.
  13. #include <string.h>
  14. #include <sys/unistd.h>
  15. #include <sys/stat.h>
  16. #include "esp_vfs_fat.h"
  17. #include "sdmmc_cmd.h"
  18. #include "driver/sdmmc_host.h"
  19. static const char *TAG = "example";
  20. #define MOUNT_POINT "/eMMC"
  21. void app_main(void)
  22. {
  23. esp_err_t ret;
  24. // Options for mounting the filesystem.
  25. // If format_if_mount_failed is set to true, eMMC will be partitioned and
  26. // formatted in case when mounting fails.
  27. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  28. #ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
  29. .format_if_mount_failed = true,
  30. #else
  31. .format_if_mount_failed = false,
  32. #endif // EXAMPLE_FORMAT_IF_MOUNT_FAILED
  33. .max_files = 5,
  34. .allocation_unit_size = 16 * 1024
  35. };
  36. sdmmc_card_t *card = NULL;
  37. const char mount_point[] = MOUNT_POINT;
  38. ESP_LOGI(TAG, "Initializing eMMC");
  39. // Use settings defined above to initialize eMMC and mount FAT filesystem.
  40. // Note: esp_vfs_fat_sdmmc_mount is all-in-one convenience functions.
  41. // Please check its source code and implement error recovery when developing
  42. // production applications.
  43. ESP_LOGI(TAG, "Using SDMMC peripheral");
  44. // By default, eMMC frequency is initialized to SDMMC_FREQ_DEFAULT (20MHz)
  45. // For setting a specific frequency, use host.max_freq_khz (range 400kHz - 52MHz for SDMMC)
  46. // Example: for fixed frequency of 10MHz, use host.max_freq_khz = 10000;
  47. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  48. host.max_freq_khz = SDMMC_FREQ_52M;
  49. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  50. // Other fields will be initialized to zero
  51. sdmmc_slot_config_t slot_config = {
  52. .cd = SDMMC_SLOT_NO_CD,
  53. .wp = SDMMC_SLOT_NO_WP,
  54. };
  55. // Set bus width to use:
  56. #if CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
  57. slot_config.width = 8;
  58. #elif CONFIG_EXAMPLE_EMMC_BUS_WIDTH_4
  59. slot_config.width = 4;
  60. #else
  61. slot_config.width = 1;
  62. #endif
  63. // Set bus IOs
  64. #if CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
  65. slot_config.clk = CONFIG_EXAMPLE_PIN_CLK;
  66. slot_config.cmd = CONFIG_EXAMPLE_PIN_CMD;
  67. slot_config.d0 = CONFIG_EXAMPLE_PIN_D0;
  68. #if CONFIG_EXAMPLE_EMMC_BUS_WIDTH_4 || CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
  69. slot_config.d1 = CONFIG_EXAMPLE_PIN_D1;
  70. slot_config.d2 = CONFIG_EXAMPLE_PIN_D2;
  71. slot_config.d3 = CONFIG_EXAMPLE_PIN_D3;
  72. #endif // CONFIG_EXAMPLE_EMMC_BUS_WIDTH_4 || CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
  73. #if CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
  74. slot_config.d4 = CONFIG_EXAMPLE_PIN_D4;
  75. slot_config.d5 = CONFIG_EXAMPLE_PIN_D5;
  76. slot_config.d6 = CONFIG_EXAMPLE_PIN_D6;
  77. slot_config.d7 = CONFIG_EXAMPLE_PIN_D7;
  78. #endif //CONFIG_EXAMPLE_EMMC_BUS_WIDTH_8
  79. #endif // CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
  80. // Enable internal pullups on enabled pins. The internal pullups
  81. // are insufficient however, please make sure 10k external pullups are
  82. // connected on the bus. This is for debug / example purpose only.
  83. slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  84. ESP_LOGI(TAG, "Mounting filesystem");
  85. ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
  86. if (ret != ESP_OK) {
  87. if (ret == ESP_FAIL) {
  88. ESP_LOGE(TAG, "Failed to mount filesystem. "
  89. "If you want the eMMC to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  90. } else {
  91. ESP_LOGE(TAG, "Failed to initialize the eMMC (%s). "
  92. "Make sure eMMC lines have pull-up resistors in place.", esp_err_to_name(ret));
  93. }
  94. return;
  95. }
  96. ESP_LOGI(TAG, "Filesystem mounted");
  97. // Card has been initialized, print its properties
  98. sdmmc_card_print_info(stdout, card);
  99. // Use POSIX and C standard library functions to work with files:
  100. // First create a file.
  101. const char *file_hello = MOUNT_POINT"/hello.txt";
  102. ESP_LOGI(TAG, "Opening file %s", file_hello);
  103. FILE *f = fopen(file_hello, "w");
  104. if (f == NULL) {
  105. ESP_LOGE(TAG, "Failed to open file for writing");
  106. return;
  107. }
  108. fprintf(f, "Hello %s!\n", card->cid.name);
  109. fclose(f);
  110. ESP_LOGI(TAG, "File written");
  111. const char *file_foo = MOUNT_POINT"/foo.txt";
  112. // Check if destination file exists before renaming
  113. struct stat st;
  114. if (stat(file_foo, &st) == 0) {
  115. // Delete it if it exists
  116. unlink(file_foo);
  117. }
  118. // Rename original file
  119. ESP_LOGI(TAG, "Renaming file %s to %s", file_hello, file_foo);
  120. if (rename(file_hello, file_foo) != 0) {
  121. ESP_LOGE(TAG, "Rename failed");
  122. return;
  123. }
  124. // Open renamed file for reading
  125. ESP_LOGI(TAG, "Reading file %s", file_foo);
  126. f = fopen(file_foo, "r");
  127. if (f == NULL) {
  128. ESP_LOGE(TAG, "Failed to open file for reading");
  129. return;
  130. }
  131. // Read a line from file
  132. char line[64];
  133. fgets(line, sizeof(line), f);
  134. fclose(f);
  135. // Strip newline
  136. char *pos = strchr(line, '\n');
  137. if (pos) {
  138. *pos = '\0';
  139. }
  140. ESP_LOGI(TAG, "Read from file: '%s'", line);
  141. // All done, unmount partition and disable SDMMC peripheral
  142. esp_vfs_fat_sdcard_unmount(mount_point, card);
  143. ESP_LOGI(TAG, "Card unmounted");
  144. }