serial_flash_example_main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "driver/gpio.h"
  14. #include "driver/uart.h"
  15. #include <sys/param.h>
  16. static const char *TAG = "example";
  17. const uint32_t PARTITION_TABLE_ADDRESS = 0x8000;
  18. const uint32_t APPLICATION_ADDRESS = 0x10000;
  19. const uint32_t BOOTLOADER_ADDRESS = 0x1000;
  20. const uint32_t HIGHER_BAUD_RATE = 230400;
  21. static uint8_t payload[1024];
  22. static void flash_binary(FILE *image, size_t image_size, size_t address)
  23. {
  24. esp_loader_error_t err;
  25. int32_t packet_number = 0;
  26. err = esp_loader_flash_start(address, image_size, sizeof(payload));
  27. if (err != ESP_LOADER_SUCCESS) {
  28. ESP_LOGE(TAG, "Flash start operation failed with error %d.", err);
  29. return;
  30. }
  31. ESP_LOGI(TAG, "Start programming");
  32. while (image_size > 0) {
  33. size_t to_read = MIN(image_size, sizeof(payload));
  34. size_t read = fread(payload, 1, to_read, image);
  35. if (read != to_read) {
  36. ESP_LOGE(TAG, "Error occurred while reading file.");
  37. return;
  38. }
  39. err = esp_loader_flash_write(payload, to_read);
  40. if (err != ESP_LOADER_SUCCESS) {
  41. ESP_LOGE(TAG, "Packet could not be written");
  42. return;
  43. }
  44. ESP_LOGI(TAG, "packet: %d written: %u B", packet_number++, to_read);
  45. image_size -= to_read;
  46. };
  47. ESP_LOGI(TAG, "Finished programming");
  48. err = esp_loader_flash_verify();
  49. if (err != ESP_LOADER_SUCCESS) {
  50. ESP_LOGE(TAG, "MD5 does not match. err: %d", err);
  51. return;
  52. }
  53. ESP_LOGI(TAG, "Flash verified");
  54. }
  55. static FILE *get_image_and_its_size(const char *path, size_t *image_size)
  56. {
  57. FILE *image = fopen(path, "r");
  58. if (image == NULL) {
  59. ESP_LOGE(TAG, "Failed to open file %s", path);
  60. esp_vfs_spiffs_unregister(NULL);
  61. return NULL;
  62. }
  63. fseek(image, 0L, SEEK_END);
  64. *image_size = ftell(image);
  65. rewind(image);
  66. ESP_LOGW(TAG, "File %s opened. Size: %u bytes", path, *image_size);
  67. return image;
  68. }
  69. static void upload_file(const char *path, size_t address)
  70. {
  71. size_t image_size;
  72. FILE *image = get_image_and_its_size(path, &image_size);
  73. if (image != NULL) {
  74. flash_binary(image, image_size, address);
  75. fclose(image);
  76. }
  77. }
  78. static esp_err_t connect_to_target()
  79. {
  80. const loader_serial_config_t config = {
  81. .baud_rate = 115200,
  82. .uart_port = UART_NUM_1,
  83. .uart_rx_pin = GPIO_NUM_5,
  84. .uart_tx_pin = GPIO_NUM_4,
  85. .reset_trigger_pin = GPIO_NUM_25,
  86. .gpio0_trigger_pin = GPIO_NUM_26,
  87. };
  88. // Initialize UART
  89. esp_loader_error_t err = loader_port_serial_init(&config);
  90. if(err != ESP_LOADER_SUCCESS) {
  91. ESP_LOGE(TAG, "serial initialization failed.");
  92. return err;
  93. }
  94. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  95. err = esp_loader_connect(&connect_config);
  96. if (err != ESP_LOADER_SUCCESS) {
  97. ESP_LOGE(TAG, "Cannot connect to target.");
  98. return err;
  99. }
  100. ESP_LOGI(TAG, "Connected to target");
  101. // err = esp_loader_change_baudrate(HIGHER_BAUD_RATE);
  102. // if (err != ESP_LOADER_SUCCESS) {
  103. // ESP_LOGE(TAG, "Unable to change baud rate on target.");
  104. // return err;
  105. // }
  106. // err = loader_port_change_baudrate(HIGHER_BAUD_RATE);
  107. // if (err != ESP_LOADER_SUCCESS) {
  108. // ESP_LOGE(TAG, "Unable to change baud rate.");
  109. // return err;
  110. // }
  111. return ESP_OK;
  112. }
  113. static esp_err_t register_vfs()
  114. {
  115. ESP_LOGI(TAG, "Initializing SPIFFS");
  116. esp_vfs_spiffs_conf_t conf = {
  117. .base_path = "/spiffs",
  118. .partition_label = NULL,
  119. .max_files = 5,
  120. .format_if_mount_failed = false
  121. };
  122. // Use settings defined above to initialize and mount SPIFFS filesystem.
  123. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  124. if (ret != ESP_OK) {
  125. if (ret == ESP_FAIL) {
  126. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  127. } else if (ret == ESP_ERR_NOT_FOUND) {
  128. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  129. } else {
  130. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  131. }
  132. }
  133. return ret;
  134. }
  135. void app_main(void)
  136. {
  137. if ( register_vfs() == ESP_OK ) {
  138. if ( connect_to_target() == ESP_OK) {
  139. upload_file("/spiffs/partition-table.bin", PARTITION_TABLE_ADDRESS);
  140. upload_file("/spiffs/bootloader.bin", BOOTLOADER_ADDRESS);
  141. upload_file("/spiffs/hello-world.bin", APPLICATION_ADDRESS);
  142. }
  143. esp_vfs_spiffs_unregister(NULL);
  144. }
  145. }