example_common.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <stdio.h>
  16. #include <string.h>
  17. #include <sys/param.h>
  18. #include "serial_io.h"
  19. #include "esp_loader.h"
  20. #include "example_common.h"
  21. esp_loader_error_t connect_to_target(uint32_t higrer_baudrate)
  22. {
  23. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  24. esp_loader_error_t err = esp_loader_connect(&connect_config);
  25. if (err != ESP_LOADER_SUCCESS) {
  26. printf("Cannot connect to target. Error: %u\n", err);
  27. return err;
  28. }
  29. printf("Connected to target\n");
  30. #ifndef TARGET_ESP8266
  31. if (higrer_baudrate) {
  32. err = esp_loader_change_baudrate(higrer_baudrate);
  33. if (err != ESP_LOADER_SUCCESS) {
  34. printf("Unable to change baud rate on target.");
  35. return err;
  36. }
  37. err = loader_port_change_baudrate(higrer_baudrate);
  38. if (err != ESP_LOADER_SUCCESS) {
  39. printf("Unable to change baud rate.");
  40. return err;
  41. }
  42. printf("Baudrate changed\n");
  43. }
  44. #endif
  45. return ESP_LOADER_SUCCESS;
  46. }
  47. esp_loader_error_t flash_binary(const unsigned char *bin, size_t size, size_t address)
  48. {
  49. esp_loader_error_t err;
  50. static uint8_t payload[1024];
  51. const unsigned char *bin_addr = bin;
  52. printf("Erasing flash (this may take a while)...\n");
  53. err = esp_loader_flash_start(address, size, sizeof(payload));
  54. if (err != ESP_LOADER_SUCCESS) {
  55. printf("Erasing flash failed with error %d.\n", err);
  56. return err;
  57. }
  58. printf("Start programming\n");
  59. size_t binary_size = size;
  60. size_t written = 0;
  61. while (size > 0) {
  62. size_t to_read = MIN(size, sizeof(payload));
  63. memcpy(payload, bin_addr, to_read);
  64. err = esp_loader_flash_write(payload, to_read);
  65. if (err != ESP_LOADER_SUCCESS) {
  66. printf("\nPacket could not be written! Error %d.\n", err);
  67. return err;
  68. }
  69. size -= to_read;
  70. bin_addr += to_read;
  71. written += to_read;
  72. int progress = (int)(((float)written / binary_size) * 100);
  73. printf("\rProgress: %d %%", progress);
  74. fflush(stdout);
  75. };
  76. printf("\nFinished programming\n");
  77. #if MD5_ENABLED
  78. err = esp_loader_flash_verify();
  79. if (err != ESP_LOADER_SUCCESS) {
  80. printf("MD5 does not match. err: %d\n", err);
  81. return err;
  82. }
  83. printf("Flash verified\n");
  84. #endif
  85. return ESP_LOADER_SUCCESS;
  86. }