Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

126 lignes
4.4 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/unistd.h>
  4. #include <sys/stat.h>
  5. #include "esp_err.h"
  6. #include "esp_log.h"
  7. #include "esp_vfs_fat.h"
  8. #include "driver/sdmmc_host.h"
  9. #include "driver/sdspi_host.h"
  10. #include "sdmmc_cmd.h"
  11. #include "spiffs/spiffs_vfs.h"
  12. #include "storage.h"
  13. static const char *tag = "storage";
  14. // This example can use SDMMC and SPI peripherals to communicate with SD card.
  15. // By default, SDMMC peripheral is used.
  16. // To enable SPI mode, uncomment the following line:
  17. //#define USE_SPI_MODE
  18. // When testing SD and SPI modes, keep in mind that once the card has been
  19. // initialized in SPI mode, it can not be reinitialized in SD mode without
  20. // toggling power to the card.
  21. #ifdef USE_SPI_MODE
  22. // Pin mapping when using SPI mode.
  23. // With this mapping, SD card can be used both in SPI and 1-line SD mode.
  24. // Note that a pull-up on CS line is required in SD mode.
  25. #define PIN_NUM_MISO ((gpio_num_t)2)
  26. #define PIN_NUM_MOSI ((gpio_num_t)15)
  27. #define PIN_NUM_CLK ((gpio_num_t)14)
  28. #define PIN_NUM_CS ((gpio_num_t)13)
  29. #endif //USE_SPI_MODE
  30. int sdcard_init()
  31. {
  32. #ifndef USE_SPI_MODE
  33. ESP_LOGI(tag, "Using SDMMC peripheral");
  34. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  35. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  36. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  37. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  38. // To use 1-line SD mode, uncomment the following line:
  39. slot_config.width = 1;
  40. // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
  41. // Internal pull-ups are not sufficient. However, enabling internal pull-ups
  42. // does make a difference some boards, so we do that here.
  43. gpio_set_pull_mode((gpio_num_t)15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
  44. gpio_set_pull_mode((gpio_num_t)2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
  45. //gpio_set_pull_mode((gpio_num_t)4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only
  46. //gpio_set_pull_mode((gpio_num_t)12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only
  47. gpio_set_pull_mode((gpio_num_t)13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes
  48. #else
  49. ESP_LOGI(TAG, "Using SPI peripheral");
  50. sdmmc_host_t host = SDSPI_HOST_DEFAULT();
  51. sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
  52. slot_config.gpio_miso = PIN_NUM_MISO;
  53. slot_config.gpio_mosi = PIN_NUM_MOSI;
  54. slot_config.gpio_sck = PIN_NUM_CLK;
  55. slot_config.gpio_cs = PIN_NUM_CS;
  56. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  57. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  58. #endif //USE_SPI_MODE
  59. // Options for mounting the filesystem.
  60. // If format_if_mount_failed is set to true, SD card will be partitioned and
  61. // formatted in case when mounting fails.
  62. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  63. .format_if_mount_failed = false,
  64. .max_files = 5,
  65. .allocation_unit_size = 16 * 1024
  66. };
  67. // Use settings defined above to initialize SD card and mount FAT filesystem.
  68. // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
  69. // Please check its source code and implement error recovery when developing
  70. // production applications.
  71. sdmmc_card_t* card;
  72. esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
  73. if (ret != ESP_OK) {
  74. if (ret == ESP_FAIL) {
  75. ESP_LOGE(tag, "Failed to mount filesystem. "
  76. "If you want the card to be formatted, set format_if_mount_failed = true.");
  77. } else {
  78. ESP_LOGE(tag, "Failed to initialize the card (%s). "
  79. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
  80. }
  81. return -1;
  82. }
  83. // Card has been initialized, print its properties
  84. sdmmc_card_print_info(stdout, card);
  85. return 0;
  86. }
  87. int storage_init()
  88. {
  89. int ret = 0;
  90. vfs_spiffs_register();
  91. if (spiffs_is_mounted) {
  92. ESP_LOGI(tag, "File system mounted.");
  93. } else {
  94. ESP_LOGE(tag, "Error mounting file system.");
  95. ret += 1 << 1;
  96. }
  97. if (sdcard_init() == 0) {
  98. ESP_LOGI(tag, "SD card mounted.");
  99. } else {
  100. ESP_LOGE(tag, "Error mounting SD card.");
  101. ret += 1 << 2;
  102. }
  103. return ret;
  104. }