example_common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <sys/param.h>
  16. #include "esp_err.h"
  17. #include "esp_log.h"
  18. #include "serial_io.h"
  19. #include "esp_loader.h"
  20. #include "driver/uart.h"
  21. #include "driver/gpio.h"
  22. #include "example_common.h"
  23. #define HIGHER_BAUD_RATE 230400
  24. void flash_binary(FILE *image, size_t image_size, size_t address)
  25. {
  26. esp_loader_error_t err;
  27. int32_t packet_number = 0;
  28. static uint8_t payload[1024];
  29. ESP_LOGI(TAG, "Erasing flash...");
  30. err = esp_loader_flash_start(address, image_size, sizeof(payload));
  31. if (err != ESP_LOADER_SUCCESS) {
  32. ESP_LOGE(TAG, "Erasing flash failed with error %d.", err);
  33. return;
  34. }
  35. ESP_LOGI(TAG, "Start programming");
  36. while (image_size > 0) {
  37. size_t to_read = MIN(image_size, sizeof(payload));
  38. size_t read = fread(payload, 1, to_read, image);
  39. if (read != to_read) {
  40. ESP_LOGE(TAG, "Error occurred while reading file. to_read %u, read %u", to_read, read);
  41. return;
  42. }
  43. err = esp_loader_flash_write(payload, to_read);
  44. if (err != ESP_LOADER_SUCCESS) {
  45. ESP_LOGE(TAG, "Packet could not be written");
  46. return;
  47. }
  48. printf("packet: %d written: %u B\n", packet_number++, to_read);
  49. image_size -= to_read;
  50. };
  51. ESP_LOGI(TAG, "Finished programming");
  52. #if MD5_ENABLED
  53. err = esp_loader_flash_verify();
  54. if (err != ESP_LOADER_SUCCESS) {
  55. ESP_LOGE(TAG, "MD5 does not match. err: %d", err);
  56. return;
  57. }
  58. ESP_LOGI(TAG, "Flash verified");
  59. #endif
  60. }
  61. FILE *get_image_and_its_size(const char *path, size_t *image_size)
  62. {
  63. FILE *image = fopen(path, "r");
  64. if (image == NULL) {
  65. ESP_LOGE(TAG, "Failed to open file %s", path);
  66. return NULL;
  67. }
  68. fseek(image, 0L, SEEK_END);
  69. *image_size = ftell(image);
  70. rewind(image);
  71. ESP_LOGW(TAG, "File %s opened. Size: %u bytes", path, *image_size);
  72. return image;
  73. }
  74. void upload_file(const char *path, size_t address)
  75. {
  76. size_t image_size;
  77. FILE *image = get_image_and_its_size(path, &image_size);
  78. if (image != NULL) {
  79. flash_binary(image, image_size, address);
  80. fclose(image);
  81. }
  82. }
  83. esp_err_t connect_to_target()
  84. {
  85. const loader_serial_config_t config = {
  86. .baud_rate = 115200,
  87. .uart_port = UART_NUM_1,
  88. .uart_rx_pin = GPIO_NUM_5,
  89. .uart_tx_pin = GPIO_NUM_4,
  90. .reset_trigger_pin = GPIO_NUM_25,
  91. .gpio0_trigger_pin = GPIO_NUM_26,
  92. };
  93. // Initialize UART
  94. esp_loader_error_t err = loader_port_serial_init(&config);
  95. if (err != ESP_LOADER_SUCCESS) {
  96. ESP_LOGE(TAG, "serial initialization failed.");
  97. return err;
  98. }
  99. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  100. err = esp_loader_connect(&connect_config);
  101. if (err != ESP_LOADER_SUCCESS) {
  102. ESP_LOGE(TAG, "Cannot connect to target. Error: %u", err);
  103. return err;
  104. }
  105. ESP_LOGI(TAG, "Connected to target");
  106. #ifndef TARGET_ESP8266
  107. err = esp_loader_change_baudrate(HIGHER_BAUD_RATE);
  108. if (err != ESP_LOADER_SUCCESS) {
  109. ESP_LOGE(TAG, "Unable to change baud rate on target.");
  110. return err;
  111. }
  112. err = loader_port_change_baudrate(HIGHER_BAUD_RATE);
  113. if (err != ESP_LOADER_SUCCESS) {
  114. ESP_LOGE(TAG, "Unable to change baud rate.");
  115. return err;
  116. }
  117. #endif
  118. return ESP_OK;
  119. }