main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Flash multiple partitions 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 <stdio.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <sys/param.h>
  11. #include "serial_io.h"
  12. #include "esp_loader.h"
  13. #define TARGET_RST_Pin 2
  14. #define TARGET_IO0_Pin 3
  15. #define DEFAULT_BAUD_RATE 115200
  16. #define HIGHER_BAUD_RATE 460800
  17. #define SERIAL_DEVICE "/dev/ttyS0"
  18. esp_loader_error_t loader_port_rpi_init(const char *device,
  19. uint32_t baudrate,
  20. uint32_t reset_trigger_pin,
  21. uint32_t gpio0_trigger_pin);
  22. extern const unsigned char bootloader_bin[];
  23. extern const unsigned bootloader_bin_size;
  24. extern const unsigned char hello_world_bin[];
  25. extern const unsigned hello_world_bin_size;
  26. extern const unsigned char partition_table_bin[];
  27. extern const unsigned partition_table_bin_size;
  28. const uint32_t BOOTLOADER_ADDRESS = 0x1000;
  29. const uint32_t PARTITION_ADDRESS = 0x8000;
  30. const uint32_t APPLICATION_ADDRESS = 0x10000;
  31. esp_loader_error_t flash_binary(const unsigned char *bin, size_t size, size_t address)
  32. {
  33. esp_loader_error_t err;
  34. int32_t packet_number = 0;
  35. static uint8_t payload[1024];
  36. const unsigned char *bin_addr = bin;
  37. printf("Erasing flash...\n");
  38. err = esp_loader_flash_start(address, size, sizeof(payload));
  39. if (err != ESP_LOADER_SUCCESS) {
  40. printf("Erasing flash failed with error %d.\n", err);
  41. return err;
  42. }
  43. printf("Start programming\n");
  44. while (size > 0) {
  45. size_t to_read = MIN(size, sizeof(payload));
  46. memcpy(payload, bin_addr, to_read);
  47. err = esp_loader_flash_write(payload, to_read);
  48. if (err != ESP_LOADER_SUCCESS) {
  49. printf("Packet could not be written\n");
  50. return err;
  51. }
  52. printf("packet: %ld written: %u B\n", packet_number++, to_read);
  53. size -= to_read;
  54. bin_addr += to_read;
  55. };
  56. printf("Finished programming\n");
  57. #if MD5_ENABLED
  58. err = esp_loader_flash_verify();
  59. if (err != ESP_LOADER_SUCCESS) {
  60. printf("MD5 does not match. err: %d\n", err);
  61. return err;
  62. }
  63. printf("Flash verified\n");
  64. #endif
  65. return ESP_LOADER_SUCCESS;
  66. }
  67. esp_loader_error_t connect_to_target()
  68. {
  69. loader_port_rpi_init(SERIAL_DEVICE, DEFAULT_BAUD_RATE, TARGET_RST_Pin, TARGET_IO0_Pin);
  70. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  71. esp_loader_error_t err = esp_loader_connect(&connect_config);
  72. if (err != ESP_LOADER_SUCCESS) {
  73. printf("Cannot connect to target. Error: %u\n", err);
  74. return err;
  75. }
  76. printf("Connected to target\n");
  77. err = esp_loader_change_baudrate(HIGHER_BAUD_RATE);
  78. if (err != ESP_LOADER_SUCCESS) {
  79. printf("Unable to change baud rate on target.\n");
  80. return err;
  81. }
  82. err = loader_port_change_baudrate(HIGHER_BAUD_RATE);
  83. if (err != ESP_LOADER_SUCCESS) {
  84. printf("Unable to change baud rate.\n");
  85. return err;
  86. }
  87. printf("Baudrate changed\n");
  88. return ESP_LOADER_SUCCESS;
  89. }
  90. int main(void)
  91. {
  92. if (connect_to_target() == ESP_LOADER_SUCCESS) {
  93. flash_binary(bootloader_bin, bootloader_bin_size, BOOTLOADER_ADDRESS);
  94. flash_binary(partition_table_bin, partition_table_bin_size, PARTITION_ADDRESS);
  95. flash_binary(hello_world_bin, hello_world_bin_size, APPLICATION_ADDRESS);
  96. }
  97. loader_port_reset_target();
  98. }