2018-12-16 13:10:24 -05:00
|
|
|
/* HTTP File Server Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
2020-07-28 06:38:21 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2018-12-16 13:10:24 -05:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include "esp_wifi.h"
|
2019-07-05 04:58:04 -04:00
|
|
|
#include "esp_event.h"
|
2018-12-16 13:10:24 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_spiffs.h"
|
2019-08-31 10:19:21 -04:00
|
|
|
#include "esp_netif.h"
|
2020-07-28 06:38:21 -04:00
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_vfs_fat.h"
|
|
|
|
#include "nvs_flash.h"
|
2018-11-20 11:42:37 -05:00
|
|
|
#include "protocol_examples_common.h"
|
2020-07-28 06:38:21 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "driver/sdspi_host.h"
|
|
|
|
#include "driver/spi_common.h"
|
|
|
|
#include "sdmmc_cmd.h"
|
|
|
|
#include "soc/soc_caps.h"
|
|
|
|
#if SOC_SDMMC_HOST_SUPPORTED
|
|
|
|
#include "driver/sdmmc_host.h"
|
|
|
|
#endif
|
2018-12-16 13:10:24 -05:00
|
|
|
|
|
|
|
/* This example demonstrates how to create file server
|
|
|
|
* using esp_http_server. This file has only startup code.
|
|
|
|
* Look in file_server.c for the implementation */
|
|
|
|
|
2020-07-28 06:38:21 -04:00
|
|
|
#define MOUNT_POINT "/sdcard"
|
2018-12-16 13:10:24 -05:00
|
|
|
static const char *TAG="example";
|
2020-12-14 22:00:02 -05:00
|
|
|
/* ESP32-S2/C3 doesn't have an SD Host peripheral, always use SPI,
|
2020-07-28 06:38:21 -04:00
|
|
|
* ESP32 can choose SPI or SDMMC Host, SPI is used by default: */
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLE_USE_SDMMC_HOST
|
|
|
|
#define USE_SPI_MODE
|
|
|
|
#endif
|
2020-12-14 22:00:02 -05:00
|
|
|
// DMA channel to be used by the SPI peripheral
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#define SPI_DMA_CHAN 1
|
2020-07-28 06:38:21 -04:00
|
|
|
// on ESP32-S2, DMA channel must be the same as host id
|
2020-12-14 22:00:02 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2020-07-28 06:38:21 -04:00
|
|
|
#define SPI_DMA_CHAN host.slot
|
2020-12-14 22:00:02 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
// on ESP32-C3, DMA channels are shared with all other peripherals
|
2020-07-28 06:38:21 -04:00
|
|
|
#define SPI_DMA_CHAN 1
|
2020-12-14 22:00:02 -05:00
|
|
|
#endif //CONFIG_IDF_TARGET_ESP32
|
2020-07-28 06:38:21 -04:00
|
|
|
|
|
|
|
// When testing SD and SPI modes, keep in mind that once the card has been
|
|
|
|
// initialized in SPI mode, it can not be reinitialized in SD mode without
|
|
|
|
// toggling power to the card.
|
|
|
|
#ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
|
|
|
|
static sdmmc_card_t* mount_card = NULL;
|
|
|
|
static char * mount_base_path = MOUNT_POINT;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_SPI_MODE
|
|
|
|
// Pin mapping when using SPI mode.
|
|
|
|
// With this mapping, SD card can be used both in SPI and 1-line SD mode.
|
|
|
|
// Note that a pull-up on CS line is required in SD mode.
|
2020-12-14 22:00:02 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#define PIN_NUM_MISO 2
|
|
|
|
#define PIN_NUM_MOSI 7
|
|
|
|
#define PIN_NUM_CLK 6
|
|
|
|
#define PIN_NUM_CS 10
|
|
|
|
#else
|
2020-07-28 06:38:21 -04:00
|
|
|
#define PIN_NUM_MISO 2
|
|
|
|
#define PIN_NUM_MOSI 15
|
|
|
|
#define PIN_NUM_CLK 14
|
|
|
|
#define PIN_NUM_CS 13
|
2020-12-14 22:00:02 -05:00
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32C3
|
2020-07-28 06:38:21 -04:00
|
|
|
#endif //USE_SPI_MODE
|
2018-12-16 13:10:24 -05:00
|
|
|
|
|
|
|
/* Function to initialize SPIFFS */
|
|
|
|
static esp_err_t init_spiffs(void)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Initializing SPIFFS");
|
|
|
|
|
|
|
|
esp_vfs_spiffs_conf_t conf = {
|
|
|
|
.base_path = "/spiffs",
|
|
|
|
.partition_label = NULL,
|
|
|
|
.max_files = 5, // This decides the maximum number of files that can be created on the storage
|
|
|
|
.format_if_mount_failed = true
|
|
|
|
};
|
|
|
|
|
|
|
|
esp_err_t ret = esp_vfs_spiffs_register(&conf);
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
if (ret == ESP_FAIL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to mount or format filesystem");
|
|
|
|
} else if (ret == ESP_ERR_NOT_FOUND) {
|
|
|
|
ESP_LOGE(TAG, "Failed to find SPIFFS partition");
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
|
|
|
|
}
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t total = 0, used = 0;
|
|
|
|
ret = esp_spiffs_info(NULL, &total, &used);
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Declare the function which starts the file server.
|
|
|
|
* Implementation of this function is to be found in
|
|
|
|
* file_server.c */
|
|
|
|
esp_err_t start_file_server(const char *base_path);
|
2020-07-28 06:38:21 -04:00
|
|
|
#ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
|
|
|
|
void sdcard_mount(void)
|
|
|
|
{
|
|
|
|
/*sd_card part code*/
|
|
|
|
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
|
|
|
#ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
|
|
|
|
.format_if_mount_failed = true,
|
|
|
|
#else
|
|
|
|
.format_if_mount_failed = false,
|
|
|
|
#endif // EXAMPLE_FORMAT_IF_MOUNT_FAILED
|
|
|
|
.max_files = 5,
|
|
|
|
.allocation_unit_size = 16 * 1024
|
|
|
|
};
|
|
|
|
sdmmc_card_t* card;
|
|
|
|
const char mount_point[] = MOUNT_POINT;
|
|
|
|
ESP_LOGI(TAG, "Initializing SD card");
|
|
|
|
|
|
|
|
#ifndef USE_SPI_MODE
|
|
|
|
ESP_LOGI(TAG, "Using SDMMC peripheral");
|
|
|
|
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
|
|
|
|
|
|
|
// This initializes the slot without card detect (CD) and write protect (WP) signals.
|
|
|
|
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
|
|
|
|
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
|
|
|
|
|
|
|
// To use 1-line SD mode, uncomment the following line:
|
|
|
|
// slot_config.width = 1;
|
|
|
|
|
|
|
|
// GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
|
|
|
|
// Internal pull-ups are not sufficient. However, enabling internal pull-ups
|
|
|
|
// does make a difference some boards, so we do that here.
|
|
|
|
gpio_set_pull_mode(15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
|
|
|
|
gpio_set_pull_mode(2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
|
|
|
|
gpio_set_pull_mode(4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only
|
|
|
|
gpio_set_pull_mode(12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only
|
|
|
|
gpio_set_pull_mode(13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes
|
|
|
|
|
|
|
|
esp_err_t ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
|
|
|
|
#else
|
|
|
|
ESP_LOGI(TAG, "Using SPI peripheral");
|
|
|
|
|
|
|
|
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
|
|
|
|
spi_bus_config_t bus_cfg = {
|
|
|
|
.mosi_io_num = PIN_NUM_MOSI,
|
|
|
|
.miso_io_num = PIN_NUM_MISO,
|
|
|
|
.sclk_io_num = PIN_NUM_CLK,
|
|
|
|
.quadwp_io_num = -1,
|
|
|
|
.quadhd_io_num = -1,
|
|
|
|
.max_transfer_sz = 4000,
|
|
|
|
};
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2020-07-28 06:38:21 -04:00
|
|
|
esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CHAN);
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to initialize bus.");
|
|
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This initializes the slot without card detect (CD) and write protect (WP) signals.
|
|
|
|
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
|
|
|
|
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
|
|
|
|
slot_config.gpio_cs = PIN_NUM_CS;
|
|
|
|
slot_config.host_id = host.slot;
|
|
|
|
ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
|
|
|
|
mount_card = card;
|
|
|
|
#endif //USE_SPI_MODE
|
|
|
|
if(ret != ESP_OK){
|
|
|
|
if (ret == ESP_FAIL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to mount filesystem. "
|
|
|
|
"If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Failed to initialize the card (%s). "
|
|
|
|
"Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
|
|
|
|
}
|
2020-11-10 02:40:01 -05:00
|
|
|
ESP_ERROR_CHECK(ret);
|
2020-07-28 06:38:21 -04:00
|
|
|
}
|
|
|
|
sdmmc_card_print_info(stdout, card);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t unmount_card(const char* base_path, sdmmc_card_t* card)
|
|
|
|
{
|
|
|
|
#ifdef USE_SPI_MODE
|
|
|
|
esp_err_t err = esp_vfs_fat_sdcard_unmount(base_path, card);
|
|
|
|
#else
|
|
|
|
esp_err_t err = esp_vfs_fat_sdmmc_unmount();
|
|
|
|
#endif
|
|
|
|
ESP_ERROR_CHECK(err);
|
|
|
|
#ifdef USE_SPI_MODE
|
|
|
|
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
|
|
|
|
err = spi_bus_free(host.slot);
|
|
|
|
#endif
|
|
|
|
ESP_ERROR_CHECK(err);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2018-12-16 13:10:24 -05:00
|
|
|
|
2020-12-14 22:00:02 -05:00
|
|
|
#endif //CONFIG_EXAMPLE_MOUNT_SD_CARD
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-12-16 13:10:24 -05:00
|
|
|
{
|
2020-07-28 06:38:21 -04:00
|
|
|
/*Mount the SDcard first if needed.*/
|
|
|
|
#ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
|
|
|
|
sdcard_mount();
|
|
|
|
#endif
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2018-11-20 11:42:37 -05:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 11:42:37 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
2018-12-16 13:10:24 -05:00
|
|
|
|
|
|
|
/* Initialize file storage */
|
|
|
|
ESP_ERROR_CHECK(init_spiffs());
|
|
|
|
|
|
|
|
/* Start the file server */
|
|
|
|
ESP_ERROR_CHECK(start_file_server("/spiffs"));
|
2020-07-28 06:38:21 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
|
|
|
|
//deinitialize the bus after all devices are removed
|
|
|
|
ESP_ERROR_CHECK(unmount_card(mount_base_path, mount_card));
|
|
|
|
#endif
|
2018-12-16 13:10:24 -05:00
|
|
|
}
|