serial_flash_example_main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Serial flasher 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 <string.h>
  8. #include "esp_err.h"
  9. #include "esp_log.h"
  10. #include "esp_spiffs.h"
  11. #include "esp_loader.h"
  12. #include "serial_io.h"
  13. #include <sys/param.h>
  14. static const char *TAG = "example";
  15. const uint32_t HIGHER_BAUD_RATE = 921600;
  16. const uint32_t APP_START_ADDRESS = 0x10000;
  17. static uint8_t payload[1024];
  18. esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate);
  19. static void flash_binary(FILE *image, size_t image_size)
  20. {
  21. esp_loader_error_t err;
  22. int32_t packet_number = 0;
  23. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  24. err = esp_loader_connect(&connect_config);
  25. if (err != ESP_LOADER_SUCCESS) {
  26. ESP_LOGE(TAG, "Cannot connect to target.");
  27. return;
  28. }
  29. ESP_LOGI(TAG, "Connected to target");
  30. err = esp_loader_change_baudrate(HIGHER_BAUD_RATE);
  31. if (err != ESP_LOADER_SUCCESS) {
  32. ESP_LOGE(TAG, "Unable to change baud rate on target.");
  33. return;
  34. }
  35. err = loader_port_change_baudrate(HIGHER_BAUD_RATE);
  36. if (err != ESP_LOADER_SUCCESS) {
  37. ESP_LOGE(TAG, "Unable to change baud rate.");
  38. return;
  39. }
  40. err = esp_loader_flash_start(APP_START_ADDRESS, image_size, sizeof(payload));
  41. if (err != ESP_LOADER_SUCCESS) {
  42. ESP_LOGE(TAG, "Flash start operation failed.");
  43. return;
  44. }
  45. ESP_LOGI(TAG, "Start programming");
  46. while (image_size > 0) {
  47. size_t to_read = MIN(image_size, sizeof(payload));
  48. size_t read = fread(payload, 1, to_read, image);
  49. if (read != to_read) {
  50. ESP_LOGE(TAG, "Error occurred while reading file.");
  51. return;
  52. }
  53. err = esp_loader_flash_write(payload, to_read);
  54. if (err != ESP_LOADER_SUCCESS) {
  55. ESP_LOGE(TAG, "Packet could not be written");
  56. return;
  57. }
  58. ESP_LOGI(TAG, "packet: %d written: %u B", packet_number++, to_read);
  59. image_size -= to_read;
  60. };
  61. ESP_LOGI(TAG, "Finished programming");
  62. err = esp_loader_flash_verify();
  63. if (err != ESP_LOADER_SUCCESS) {
  64. ESP_LOGE(TAG, "MD5 does not match. err: %d", err);
  65. return;
  66. }
  67. ESP_LOGI(TAG, "Flash verified");
  68. }
  69. static FILE *get_image_and_its_size(size_t *image_size)
  70. {
  71. ESP_LOGI(TAG, "Initializing SPIFFS");
  72. esp_vfs_spiffs_conf_t conf = {
  73. .base_path = "/spiffs",
  74. .partition_label = NULL,
  75. .max_files = 5,
  76. .format_if_mount_failed = false
  77. };
  78. // Use settings defined above to initialize and mount SPIFFS filesystem.
  79. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  80. if (ret != ESP_OK) {
  81. if (ret == ESP_FAIL) {
  82. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  83. } else if (ret == ESP_ERR_NOT_FOUND) {
  84. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  85. } else {
  86. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  87. }
  88. return NULL;
  89. }
  90. FILE *image = fopen("/spiffs/hello-world.bin", "r");
  91. if (image == NULL) {
  92. ESP_LOGE(TAG, "Failed to open file");
  93. esp_vfs_spiffs_unregister(NULL);
  94. return NULL;
  95. }
  96. fseek(image, 0L, SEEK_END);
  97. *image_size = ftell(image);
  98. rewind(image);
  99. ESP_LOGI(TAG, "Image size: %u", *image_size);
  100. return image;
  101. }
  102. void app_main(void)
  103. {
  104. // Initialize UART
  105. esp_loader_error_t err = loader_port_serial_init(115200);
  106. if (err != ESP_LOADER_SUCCESS) {
  107. ESP_LOGE(TAG, "serial initialization failed.");
  108. return;
  109. }
  110. size_t image_size;
  111. FILE *image = get_image_and_its_size(&image_size);
  112. if (image == NULL) {
  113. return;
  114. }
  115. flash_binary(image, image_size);
  116. // All done, close file, unmount partition and disable SPIFFS
  117. fclose(image);
  118. esp_vfs_spiffs_unregister(NULL);
  119. }