| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- /* Flash AT firmware 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.
- */
- #include "esp_err.h"
- #include "esp_log.h"
- #include "esp_vfs_fat.h"
- #include "driver/sdspi_host.h"
- #include "driver/spi_common.h"
- #include "example_common.h"
- #define MOUNT_POINT "/sdcard"
- #define SPI_DMA_CHAN 1
- #define PIN_NUM_MISO 2
- #define PIN_NUM_MOSI 15
- #define PIN_NUM_CLK 14
- #define PIN_NUM_CS 13
- static sdmmc_card_t *card;
- static sdmmc_host_t host = SDSPI_HOST_DEFAULT();
- esp_err_t sdcard_init(void)
- {
- ESP_LOGI(TAG, "Initializing SD card");
- // Use settings defined above to initialize SD card and mount FAT filesystem.
- // Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
- // Please check its source code and implement error recovery when developing
- // production applications.
- 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,
- };
-
- if( spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CHAN) != ESP_OK ) {
- ESP_LOGE(TAG, "Failed to initialize bus.");
- return ESP_FAIL;
- }
- // 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;
- esp_vfs_fat_sdmmc_mount_config_t mount_config = {
- .format_if_mount_failed = false,
- .max_files = 5,
- .allocation_unit_size = 16 * 1024
- };
- const char mount_point[] = MOUNT_POINT;
- esp_err_t ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
- 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));
- }
- return ESP_FAIL;
- }
- return ESP_OK;
- }
- void sdcard_deinit()
- {
- esp_vfs_fat_sdcard_unmount(MOUNT_POINT, card);
- ESP_LOGI(TAG, "Card unmounted");
- spi_bus_free(host.slot);
- }
- void app_main(void)
- {
- if ( sdcard_init() == ESP_OK ) {
- if ( connect_to_target() == ESP_OK) {
- upload_file(MOUNT_POINT"/Firmware.bin", 0);
- }
- sdcard_deinit();
- }
- }
|