serial_flash_example_main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 APP_START_ADDRESS = 0x10000;
  16. static char payload[1024];
  17. static void flash_binary(FILE *image, size_t image_size)
  18. {
  19. esp_loader_error_t err;
  20. int32_t packet_number = 0;
  21. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  22. err = esp_loader_connect(&connect_config);
  23. if (err != ESP_LOADER_SUCCESS) {
  24. ESP_LOGE(TAG, "Cannot connect to target.");
  25. return;
  26. }
  27. ESP_LOGI(TAG, "Connected to target");
  28. err = esp_loader_flash_start(APP_START_ADDRESS, image_size, sizeof(payload));
  29. if (err != ESP_LOADER_SUCCESS) {
  30. ESP_LOGE(TAG, "Flash start operation failed.");
  31. return;
  32. }
  33. ESP_LOGI(TAG, "Start programming");
  34. while (image_size > 0) {
  35. size_t to_read = MIN(image_size, sizeof(payload));
  36. size_t read = fread(payload, 1, to_read, image);
  37. if (read != to_read) {
  38. ESP_LOGE(TAG, "Error occurred while reading file.");
  39. return;
  40. }
  41. err = esp_loader_flash_write(payload, to_read);
  42. if (err != ESP_LOADER_SUCCESS) {
  43. ESP_LOGE(TAG, "Packet could not be written");
  44. return;
  45. }
  46. ESP_LOGI(TAG, "packet: %d written: %u B", packet_number++, to_read);
  47. image_size -= to_read;
  48. };
  49. ESP_LOGI(TAG, "Finished programming");
  50. }
  51. static FILE *get_image_and_its_size(size_t *image_size)
  52. {
  53. ESP_LOGI(TAG, "Initializing SPIFFS");
  54. esp_vfs_spiffs_conf_t conf = {
  55. .base_path = "/spiffs",
  56. .partition_label = NULL,
  57. .max_files = 5,
  58. .format_if_mount_failed = false
  59. };
  60. // Use settings defined above to initialize and mount SPIFFS filesystem.
  61. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  62. if (ret != ESP_OK) {
  63. if (ret == ESP_FAIL) {
  64. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  65. } else if (ret == ESP_ERR_NOT_FOUND) {
  66. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  67. } else {
  68. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  69. }
  70. return NULL;
  71. }
  72. FILE *image = fopen("/spiffs/hello-world.bin", "r");
  73. if (image == NULL) {
  74. ESP_LOGE(TAG, "Failed to open file");
  75. esp_vfs_spiffs_unregister(NULL);
  76. return NULL;
  77. }
  78. fseek(image, 0L, SEEK_END);
  79. *image_size = ftell(image);
  80. rewind(image);
  81. ESP_LOGI(TAG, "Image size: %u", *image_size);
  82. return image;
  83. }
  84. void app_main(void)
  85. {
  86. // Initialize UART
  87. esp_loader_error_t err = loader_port_serial_init(115200);
  88. if (err != ESP_LOADER_SUCCESS) {
  89. ESP_LOGE(TAG, "serial initialization failed.");
  90. return;
  91. }
  92. size_t image_size;
  93. FILE *image = get_image_and_its_size(&image_size);
  94. if (image == NULL) {
  95. return;
  96. }
  97. flash_binary(image, image_size);
  98. // All done, close file, unmount partition and disable SPIFFS
  99. fclose(image);
  100. esp_vfs_spiffs_unregister(NULL);
  101. }