main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* HTTP File Server Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/unistd.h>
  10. #include <sys/stat.h>
  11. #include <sys/param.h>
  12. #include "esp_wifi.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "esp_system.h"
  16. #include "esp_spiffs.h"
  17. #include "esp_netif.h"
  18. #include "esp_err.h"
  19. #include "esp_vfs_fat.h"
  20. #include "nvs_flash.h"
  21. #include "protocol_examples_common.h"
  22. #include "sdkconfig.h"
  23. #include "driver/sdspi_host.h"
  24. #include "driver/spi_common.h"
  25. #include "sdmmc_cmd.h"
  26. #include "soc/soc_caps.h"
  27. #if SOC_SDMMC_HOST_SUPPORTED
  28. #include "driver/sdmmc_host.h"
  29. #endif
  30. /* This example demonstrates how to create file server
  31. * using esp_http_server. This file has only startup code.
  32. * Look in file_server.c for the implementation */
  33. #define MOUNT_POINT "/sdcard"
  34. static const char *TAG="example";
  35. /* ESP32-S2/C3 doesn't have an SD Host peripheral, always use SPI,
  36. * ESP32 can choose SPI or SDMMC Host, SPI is used by default: */
  37. #ifndef CONFIG_EXAMPLE_USE_SDMMC_HOST
  38. #define USE_SPI_MODE
  39. #endif
  40. // DMA channel to be used by the SPI peripheral
  41. #if CONFIG_IDF_TARGET_ESP32
  42. #define SPI_DMA_CHAN 1
  43. // on ESP32-S2, DMA channel must be the same as host id
  44. #elif CONFIG_IDF_TARGET_ESP32S2
  45. #define SPI_DMA_CHAN host.slot
  46. #elif CONFIG_IDF_TARGET_ESP32C3
  47. // on ESP32-C3, DMA channels are shared with all other peripherals
  48. #define SPI_DMA_CHAN 1
  49. #endif //CONFIG_IDF_TARGET_ESP32
  50. // When testing SD and SPI modes, keep in mind that once the card has been
  51. // initialized in SPI mode, it can not be reinitialized in SD mode without
  52. // toggling power to the card.
  53. #ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
  54. static sdmmc_card_t* mount_card = NULL;
  55. static char * mount_base_path = MOUNT_POINT;
  56. #endif
  57. #ifdef USE_SPI_MODE
  58. // Pin mapping when using SPI mode.
  59. // With this mapping, SD card can be used both in SPI and 1-line SD mode.
  60. // Note that a pull-up on CS line is required in SD mode.
  61. #if CONFIG_IDF_TARGET_ESP32C3
  62. #define PIN_NUM_MISO 2
  63. #define PIN_NUM_MOSI 7
  64. #define PIN_NUM_CLK 6
  65. #define PIN_NUM_CS 10
  66. #else
  67. #define PIN_NUM_MISO 2
  68. #define PIN_NUM_MOSI 15
  69. #define PIN_NUM_CLK 14
  70. #define PIN_NUM_CS 13
  71. #endif // CONFIG_IDF_TARGET_ESP32C3
  72. #endif //USE_SPI_MODE
  73. /* Function to initialize SPIFFS */
  74. static esp_err_t init_spiffs(void)
  75. {
  76. ESP_LOGI(TAG, "Initializing SPIFFS");
  77. esp_vfs_spiffs_conf_t conf = {
  78. .base_path = "/spiffs",
  79. .partition_label = NULL,
  80. .max_files = 5, // This decides the maximum number of files that can be created on the storage
  81. .format_if_mount_failed = true
  82. };
  83. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  84. if (ret != ESP_OK) {
  85. if (ret == ESP_FAIL) {
  86. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  87. } else if (ret == ESP_ERR_NOT_FOUND) {
  88. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  89. } else {
  90. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  91. }
  92. return ESP_FAIL;
  93. }
  94. size_t total = 0, used = 0;
  95. ret = esp_spiffs_info(NULL, &total, &used);
  96. if (ret != ESP_OK) {
  97. ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
  98. return ESP_FAIL;
  99. }
  100. ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
  101. return ESP_OK;
  102. }
  103. /* Declare the function which starts the file server.
  104. * Implementation of this function is to be found in
  105. * file_server.c */
  106. esp_err_t start_file_server(const char *base_path);
  107. #ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
  108. void sdcard_mount(void)
  109. {
  110. /*sd_card part code*/
  111. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  112. #ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
  113. .format_if_mount_failed = true,
  114. #else
  115. .format_if_mount_failed = false,
  116. #endif // EXAMPLE_FORMAT_IF_MOUNT_FAILED
  117. .max_files = 5,
  118. .allocation_unit_size = 16 * 1024
  119. };
  120. sdmmc_card_t* card;
  121. const char mount_point[] = MOUNT_POINT;
  122. ESP_LOGI(TAG, "Initializing SD card");
  123. #ifndef USE_SPI_MODE
  124. ESP_LOGI(TAG, "Using SDMMC peripheral");
  125. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  126. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  127. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  128. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  129. // To use 1-line SD mode, uncomment the following line:
  130. // slot_config.width = 1;
  131. // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
  132. // Internal pull-ups are not sufficient. However, enabling internal pull-ups
  133. // does make a difference some boards, so we do that here.
  134. gpio_set_pull_mode(15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
  135. gpio_set_pull_mode(2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
  136. gpio_set_pull_mode(4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only
  137. gpio_set_pull_mode(12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only
  138. gpio_set_pull_mode(13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes
  139. esp_err_t ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
  140. #else
  141. ESP_LOGI(TAG, "Using SPI peripheral");
  142. sdmmc_host_t host = SDSPI_HOST_DEFAULT();
  143. spi_bus_config_t bus_cfg = {
  144. .mosi_io_num = PIN_NUM_MOSI,
  145. .miso_io_num = PIN_NUM_MISO,
  146. .sclk_io_num = PIN_NUM_CLK,
  147. .quadwp_io_num = -1,
  148. .quadhd_io_num = -1,
  149. .max_transfer_sz = 4000,
  150. };
  151. esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CHAN);
  152. if (ret != ESP_OK) {
  153. ESP_LOGE(TAG, "Failed to initialize bus.");
  154. ESP_ERROR_CHECK(ret);
  155. }
  156. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  157. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  158. sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  159. slot_config.gpio_cs = PIN_NUM_CS;
  160. slot_config.host_id = host.slot;
  161. ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
  162. mount_card = card;
  163. #endif //USE_SPI_MODE
  164. if(ret != ESP_OK){
  165. if (ret == ESP_FAIL) {
  166. ESP_LOGE(TAG, "Failed to mount filesystem. "
  167. "If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  168. } else {
  169. ESP_LOGE(TAG, "Failed to initialize the card (%s). "
  170. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
  171. }
  172. ESP_ERROR_CHECK(ret);
  173. }
  174. sdmmc_card_print_info(stdout, card);
  175. }
  176. static esp_err_t unmount_card(const char* base_path, sdmmc_card_t* card)
  177. {
  178. #ifdef USE_SPI_MODE
  179. esp_err_t err = esp_vfs_fat_sdcard_unmount(base_path, card);
  180. #else
  181. esp_err_t err = esp_vfs_fat_sdmmc_unmount();
  182. #endif
  183. ESP_ERROR_CHECK(err);
  184. #ifdef USE_SPI_MODE
  185. sdmmc_host_t host = SDSPI_HOST_DEFAULT();
  186. err = spi_bus_free(host.slot);
  187. #endif
  188. ESP_ERROR_CHECK(err);
  189. return err;
  190. }
  191. #endif //CONFIG_EXAMPLE_MOUNT_SD_CARD
  192. void app_main(void)
  193. {
  194. /*Mount the SDcard first if needed.*/
  195. #ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
  196. sdcard_mount();
  197. #endif
  198. ESP_ERROR_CHECK(nvs_flash_init());
  199. ESP_ERROR_CHECK(esp_netif_init());
  200. ESP_ERROR_CHECK(esp_event_loop_create_default());
  201. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  202. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  203. * examples/protocols/README.md for more information about this function.
  204. */
  205. ESP_ERROR_CHECK(example_connect());
  206. /* Initialize file storage */
  207. ESP_ERROR_CHECK(init_spiffs());
  208. /* Start the file server */
  209. ESP_ERROR_CHECK(start_file_server("/spiffs"));
  210. #ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
  211. //deinitialize the bus after all devices are removed
  212. ESP_ERROR_CHECK(unmount_card(mount_base_path, mount_card));
  213. #endif
  214. }