example_common.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef SINGLE_TARGET_SUPPORT
  22. #define BOOTLOADER_ADDRESS_8266 0x1000
  23. #define BOOTLOADER_ADDRESS 0x1000
  24. #define PARTITION_ADDRESS 0x8000
  25. #define APPLICATION_ADDRESS 0x10000
  26. extern const uint8_t ESP32_bootloader_bin[];
  27. extern const uint32_t ESP32_bootloader_bin_size;
  28. extern const uint8_t ESP32_hello_world_bin[];
  29. extern const uint32_t ESP32_hello_world_bin_size;
  30. extern const uint8_t ESP32_partition_table_bin[];
  31. extern const uint32_t ESP32_partition_table_bin_size;
  32. extern const uint8_t ESP32_S2_bootloader_bin[];
  33. extern const uint32_t ESP32_S2_bootloader_bin_size;
  34. extern const uint8_t ESP32_S2_hello_world_bin[];
  35. extern const uint32_t ESP32_S2_hello_world_bin_size;
  36. extern const uint8_t ESP32_S2_partition_table_bin[];
  37. extern const uint32_t ESP32_S2_partition_table_bin_size;
  38. extern const uint8_t ESP8266_bootloader_bin[];
  39. extern const uint32_t ESP8266_bootloader_bin_size;
  40. extern const uint8_t ESP8266_hello_world_bin[];
  41. extern const uint32_t ESP8266_hello_world_bin_size;
  42. extern const uint8_t ESP8266_partition_table_bin[];
  43. extern const uint32_t ESP8266_partition_table_bin_size;
  44. void get_example_binaries(target_chip_t target, example_binaries_t *bins)
  45. {
  46. if (target == ESP8266_CHIP) {
  47. bins->boot.data = ESP8266_bootloader_bin;
  48. bins->boot.size = ESP8266_bootloader_bin_size;
  49. bins->boot.addr = BOOTLOADER_ADDRESS_8266;
  50. bins->part.data = ESP8266_partition_table_bin;
  51. bins->part.size = ESP8266_partition_table_bin_size;
  52. bins->part.addr = PARTITION_ADDRESS;
  53. bins->app.data = ESP8266_hello_world_bin;
  54. bins->app.size = ESP8266_hello_world_bin_size;
  55. bins->app.addr = APPLICATION_ADDRESS;
  56. } else if (target == ESP32_CHIP) {
  57. bins->boot.data = ESP32_bootloader_bin;
  58. bins->boot.size = ESP32_bootloader_bin_size;
  59. bins->boot.addr = BOOTLOADER_ADDRESS;
  60. bins->part.data = ESP32_partition_table_bin;
  61. bins->part.size = ESP32_partition_table_bin_size;
  62. bins->part.addr = PARTITION_ADDRESS;
  63. bins->app.data = ESP32_hello_world_bin;
  64. bins->app.size = ESP32_hello_world_bin_size;
  65. bins->app.addr = APPLICATION_ADDRESS;
  66. } else {
  67. bins->boot.data = ESP32_S2_bootloader_bin;
  68. bins->boot.size = ESP32_S2_bootloader_bin_size;
  69. bins->boot.addr = BOOTLOADER_ADDRESS;
  70. bins->part.data = ESP32_S2_partition_table_bin;
  71. bins->part.size = ESP32_S2_partition_table_bin_size;
  72. bins->part.addr = PARTITION_ADDRESS;
  73. bins->app.data = ESP32_S2_hello_world_bin;
  74. bins->app.size = ESP32_S2_hello_world_bin_size;
  75. bins->app.addr = APPLICATION_ADDRESS;
  76. }
  77. }
  78. #endif
  79. esp_loader_error_t connect_to_target(uint32_t higrer_baudrate)
  80. {
  81. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  82. esp_loader_error_t err = esp_loader_connect(&connect_config);
  83. if (err != ESP_LOADER_SUCCESS) {
  84. printf("Cannot connect to target. Error: %u\n", err);
  85. return err;
  86. }
  87. printf("Connected to target\n");
  88. if (higrer_baudrate && esp_loader_get_target() != ESP8266_CHIP) {
  89. err = esp_loader_change_baudrate(higrer_baudrate);
  90. if (err == ESP_LOADER_ERROR_UNSUPPORTED_FUNC) {
  91. printf("ESP8266 does not support change baudrate command.");
  92. return err;
  93. } else if (err != ESP_LOADER_SUCCESS) {
  94. printf("Unable to change baud rate on target.");
  95. return err;
  96. } else {
  97. err = loader_port_change_baudrate(higrer_baudrate);
  98. if (err != ESP_LOADER_SUCCESS) {
  99. printf("Unable to change baud rate.");
  100. return err;
  101. }
  102. printf("Baudrate changed\n");
  103. }
  104. }
  105. return ESP_LOADER_SUCCESS;
  106. }
  107. esp_loader_error_t flash_binary(const uint8_t *bin, size_t size, size_t address)
  108. {
  109. esp_loader_error_t err;
  110. static uint8_t payload[1024];
  111. const uint8_t *bin_addr = bin;
  112. printf("Erasing flash (this may take a while)...\n");
  113. err = esp_loader_flash_start(address, size, sizeof(payload));
  114. if (err != ESP_LOADER_SUCCESS) {
  115. printf("Erasing flash failed with error %d.\n", err);
  116. return err;
  117. }
  118. printf("Start programming\n");
  119. size_t binary_size = size;
  120. size_t written = 0;
  121. while (size > 0) {
  122. size_t to_read = MIN(size, sizeof(payload));
  123. memcpy(payload, bin_addr, to_read);
  124. err = esp_loader_flash_write(payload, to_read);
  125. if (err != ESP_LOADER_SUCCESS) {
  126. printf("\nPacket could not be written! Error %d.\n", err);
  127. return err;
  128. }
  129. size -= to_read;
  130. bin_addr += to_read;
  131. written += to_read;
  132. int progress = (int)(((float)written / binary_size) * 100);
  133. printf("\rProgress: %d %%", progress);
  134. fflush(stdout);
  135. };
  136. printf("\nFinished programming\n");
  137. #if MD5_ENABLED
  138. err = esp_loader_flash_verify();
  139. if (err == ESP_LOADER_ERROR_UNSUPPORTED_FUNC) {
  140. printf("ESP8266 does not support flash verify command.");
  141. return err;
  142. } else if (err != ESP_LOADER_SUCCESS) {
  143. printf("MD5 does not match. err: %d\n", err);
  144. return err;
  145. }
  146. printf("Flash verified\n");
  147. #endif
  148. return ESP_LOADER_SUCCESS;
  149. }