main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 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. // on ESP32-S2, DMA channel must be the same as host id
  41. #ifdef CONFIG_IDF_TARGET_ESP32S2
  42. #define SPI_DMA_CHAN host.slot
  43. #endif //CONFIG_IDF_TARGET_ESP32S2
  44. // DMA channel to be used by the SPI peripheral
  45. #ifdef CONFIG_IDF_TARGET_ESP32
  46. #define SPI_DMA_CHAN 1
  47. #endif //SPI_DMA_CHAN
  48. // When testing SD and SPI modes, keep in mind that once the card has been
  49. // initialized in SPI mode, it can not be reinitialized in SD mode without
  50. // toggling power to the card.
  51. #ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
  52. static sdmmc_card_t* mount_card = NULL;
  53. static char * mount_base_path = MOUNT_POINT;
  54. #endif
  55. #ifdef USE_SPI_MODE
  56. // Pin mapping when using SPI mode.
  57. // With this mapping, SD card can be used both in SPI and 1-line SD mode.
  58. // Note that a pull-up on CS line is required in SD mode.
  59. #define PIN_NUM_MISO 2
  60. #define PIN_NUM_MOSI 15
  61. #define PIN_NUM_CLK 14
  62. #define PIN_NUM_CS 13
  63. #endif //USE_SPI_MODE
  64. /* Function to initialize SPIFFS */
  65. static esp_err_t init_spiffs(void)
  66. {
  67. ESP_LOGI(TAG, "Initializing SPIFFS");
  68. esp_vfs_spiffs_conf_t conf = {
  69. .base_path = "/spiffs",
  70. .partition_label = NULL,
  71. .max_files = 5, // This decides the maximum number of files that can be created on the storage
  72. .format_if_mount_failed = true
  73. };
  74. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  75. if (ret != ESP_OK) {
  76. if (ret == ESP_FAIL) {
  77. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  78. } else if (ret == ESP_ERR_NOT_FOUND) {
  79. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  80. } else {
  81. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  82. }
  83. return ESP_FAIL;
  84. }
  85. size_t total = 0, used = 0;
  86. ret = esp_spiffs_info(NULL, &total, &used);
  87. if (ret != ESP_OK) {
  88. ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
  89. return ESP_FAIL;
  90. }
  91. ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
  92. return ESP_OK;
  93. }
  94. /* Declare the function which starts the file server.
  95. * Implementation of this function is to be found in
  96. * file_server.c */
  97. esp_err_t start_file_server(const char *base_path);
  98. #ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
  99. void sdcard_mount(void)
  100. {
  101. /*sd_card part code*/
  102. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  103. #ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
  104. .format_if_mount_failed = true,
  105. #else
  106. .format_if_mount_failed = false,
  107. #endif // EXAMPLE_FORMAT_IF_MOUNT_FAILED
  108. .max_files = 5,
  109. .allocation_unit_size = 16 * 1024
  110. };
  111. sdmmc_card_t* card;
  112. const char mount_point[] = MOUNT_POINT;
  113. ESP_LOGI(TAG, "Initializing SD card");
  114. #ifndef USE_SPI_MODE
  115. ESP_LOGI(TAG, "Using SDMMC peripheral");
  116. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  117. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  118. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  119. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  120. // To use 1-line SD mode, uncomment the following line:
  121. // slot_config.width = 1;
  122. // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
  123. // Internal pull-ups are not sufficient. However, enabling internal pull-ups
  124. // does make a difference some boards, so we do that here.
  125. gpio_set_pull_mode(15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
  126. gpio_set_pull_mode(2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
  127. gpio_set_pull_mode(4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only
  128. gpio_set_pull_mode(12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only
  129. gpio_set_pull_mode(13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes
  130. esp_err_t ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
  131. #else
  132. ESP_LOGI(TAG, "Using SPI peripheral");
  133. sdmmc_host_t host = SDSPI_HOST_DEFAULT();
  134. spi_bus_config_t bus_cfg = {
  135. .mosi_io_num = PIN_NUM_MOSI,
  136. .miso_io_num = PIN_NUM_MISO,
  137. .sclk_io_num = PIN_NUM_CLK,
  138. .quadwp_io_num = -1,
  139. .quadhd_io_num = -1,
  140. .max_transfer_sz = 4000,
  141. };
  142. esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CHAN);
  143. if (ret != ESP_OK) {
  144. ESP_LOGE(TAG, "Failed to initialize bus.");
  145. ESP_ERROR_CHECK(ret);
  146. }
  147. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  148. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  149. sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  150. slot_config.gpio_cs = PIN_NUM_CS;
  151. slot_config.host_id = host.slot;
  152. ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
  153. mount_card = card;
  154. #endif //USE_SPI_MODE
  155. if(ret != ESP_OK){
  156. if (ret == ESP_FAIL) {
  157. ESP_LOGE(TAG, "Failed to mount filesystem. "
  158. "If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  159. } else {
  160. ESP_LOGE(TAG, "Failed to initialize the card (%s). "
  161. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
  162. }
  163. ESP_ERROR_CHECK(ret);
  164. }
  165. sdmmc_card_print_info(stdout, card);
  166. }
  167. #endif
  168. static esp_err_t unmount_card(const char* base_path, sdmmc_card_t* card)
  169. {
  170. #ifdef USE_SPI_MODE
  171. esp_err_t err = esp_vfs_fat_sdcard_unmount(base_path, card);
  172. #else
  173. esp_err_t err = esp_vfs_fat_sdmmc_unmount();
  174. #endif
  175. ESP_ERROR_CHECK(err);
  176. #ifdef USE_SPI_MODE
  177. sdmmc_host_t host = SDSPI_HOST_DEFAULT();
  178. err = spi_bus_free(host.slot);
  179. #endif
  180. ESP_ERROR_CHECK(err);
  181. return err;
  182. }
  183. void app_main(void)
  184. {
  185. /*Mount the SDcard first if needed.*/
  186. #ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
  187. sdcard_mount();
  188. #endif
  189. ESP_ERROR_CHECK(nvs_flash_init());
  190. ESP_ERROR_CHECK(esp_netif_init());
  191. ESP_ERROR_CHECK(esp_event_loop_create_default());
  192. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  193. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  194. * examples/protocols/README.md for more information about this function.
  195. */
  196. ESP_ERROR_CHECK(example_connect());
  197. /* Initialize file storage */
  198. ESP_ERROR_CHECK(init_spiffs());
  199. /* Start the file server */
  200. ESP_ERROR_CHECK(start_file_server("/spiffs"));
  201. #ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
  202. //deinitialize the bus after all devices are removed
  203. ESP_ERROR_CHECK(unmount_card(mount_base_path, mount_card));
  204. #endif
  205. }