flash_at_firmware.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Flash AT firmware 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 "esp_err.h"
  8. #include "esp_log.h"
  9. #include "esp_vfs_fat.h"
  10. #include "driver/sdspi_host.h"
  11. #include "driver/spi_common.h"
  12. #include "example_common.h"
  13. #define MOUNT_POINT "/sdcard"
  14. #define SPI_DMA_CHAN 1
  15. #define PIN_NUM_MISO 2
  16. #define PIN_NUM_MOSI 15
  17. #define PIN_NUM_CLK 14
  18. #define PIN_NUM_CS 13
  19. static sdmmc_card_t *card;
  20. static sdmmc_host_t host = SDSPI_HOST_DEFAULT();
  21. esp_err_t sdcard_init(void)
  22. {
  23. ESP_LOGI(TAG, "Initializing SD card");
  24. // Use settings defined above to initialize SD card and mount FAT filesystem.
  25. // Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
  26. // Please check its source code and implement error recovery when developing
  27. // production applications.
  28. spi_bus_config_t bus_cfg = {
  29. .mosi_io_num = PIN_NUM_MOSI,
  30. .miso_io_num = PIN_NUM_MISO,
  31. .sclk_io_num = PIN_NUM_CLK,
  32. .quadwp_io_num = -1,
  33. .quadhd_io_num = -1,
  34. .max_transfer_sz = 4000,
  35. };
  36. if( spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CHAN) != ESP_OK ) {
  37. ESP_LOGE(TAG, "Failed to initialize bus.");
  38. return ESP_FAIL;
  39. }
  40. // This initializes the slot without card detect (CD) and write protect (WP) signals.
  41. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  42. sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  43. slot_config.gpio_cs = PIN_NUM_CS;
  44. slot_config.host_id = host.slot;
  45. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  46. .format_if_mount_failed = false,
  47. .max_files = 5,
  48. .allocation_unit_size = 16 * 1024
  49. };
  50. const char mount_point[] = MOUNT_POINT;
  51. esp_err_t ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
  52. if (ret != ESP_OK) {
  53. if (ret == ESP_FAIL) {
  54. ESP_LOGE(TAG, "Failed to mount filesystem. "
  55. "If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  56. } else {
  57. ESP_LOGE(TAG, "Failed to initialize the card (%s). "
  58. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
  59. }
  60. return ESP_FAIL;
  61. }
  62. return ESP_OK;
  63. }
  64. void sdcard_deinit()
  65. {
  66. esp_vfs_fat_sdcard_unmount(MOUNT_POINT, card);
  67. ESP_LOGI(TAG, "Card unmounted");
  68. spi_bus_free(host.slot);
  69. }
  70. void app_main(void)
  71. {
  72. if ( sdcard_init() == ESP_OK ) {
  73. if ( connect_to_target() == ESP_OK) {
  74. upload_file(MOUNT_POINT"/Firmware.bin", 0);
  75. }
  76. sdcard_deinit();
  77. }
  78. }