esp_loader.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Copyright 2020-2023 Espressif Systems (Shanghai) CO 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. #pragma once
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /* Used for backwards compatibility with the previous API */
  22. #define esp_loader_change_baudrate esp_loader_change_transmission_rate
  23. /**
  24. * Macro which can be used to check the error code,
  25. * and return in case the code is not ESP_LOADER_SUCCESS.
  26. */
  27. #define RETURN_ON_ERROR(x) do { \
  28. esp_loader_error_t _err_ = (x); \
  29. if (_err_ != ESP_LOADER_SUCCESS) { \
  30. return _err_; \
  31. } \
  32. } while(0)
  33. /**
  34. * @brief Error codes
  35. */
  36. typedef enum {
  37. ESP_LOADER_SUCCESS, /*!< Success */
  38. ESP_LOADER_ERROR_FAIL, /*!< Unspecified error */
  39. ESP_LOADER_ERROR_TIMEOUT, /*!< Timeout elapsed */
  40. ESP_LOADER_ERROR_IMAGE_SIZE, /*!< Image size to flash is larger than flash size */
  41. ESP_LOADER_ERROR_INVALID_MD5, /*!< Computed and received MD5 does not match */
  42. ESP_LOADER_ERROR_INVALID_PARAM, /*!< Invalid parameter passed to function */
  43. ESP_LOADER_ERROR_INVALID_TARGET, /*!< Connected target is invalid */
  44. ESP_LOADER_ERROR_UNSUPPORTED_CHIP, /*!< Attached chip is not supported */
  45. ESP_LOADER_ERROR_UNSUPPORTED_FUNC, /*!< Function is not supported on attached target */
  46. ESP_LOADER_ERROR_INVALID_RESPONSE /*!< Internal error */
  47. } esp_loader_error_t;
  48. /**
  49. * @brief Supported targets
  50. */
  51. typedef enum {
  52. ESP8266_CHIP = 0,
  53. ESP32_CHIP = 1,
  54. ESP32S2_CHIP = 2,
  55. ESP32C3_CHIP = 3,
  56. ESP32S3_CHIP = 4,
  57. ESP32C2_CHIP = 5,
  58. ESP32H4_CHIP = 6,
  59. ESP32H2_CHIP = 7,
  60. ESP32C6_CHIP = 8,
  61. ESP_MAX_CHIP = 9,
  62. ESP_UNKNOWN_CHIP = 9
  63. } target_chip_t;
  64. /**
  65. * @brief Application binary header
  66. */
  67. typedef struct {
  68. uint8_t magic;
  69. uint8_t segments;
  70. uint8_t flash_mode;
  71. uint8_t flash_size_freq;
  72. uint32_t entrypoint;
  73. } esp_loader_bin_header_t;
  74. /**
  75. * @brief Segment binary header
  76. */
  77. typedef struct {
  78. uint32_t addr;
  79. uint32_t size;
  80. uint8_t *data;
  81. } esp_loader_bin_segment_t;
  82. /**
  83. * @brief SPI pin configuration arguments
  84. */
  85. typedef union {
  86. struct {
  87. uint32_t pin_clk: 6;
  88. uint32_t pin_q: 6;
  89. uint32_t pin_d: 6;
  90. uint32_t pin_cs: 6;
  91. uint32_t pin_hd: 6;
  92. uint32_t zero: 2;
  93. };
  94. uint32_t val;
  95. } esp_loader_spi_config_t;
  96. /**
  97. * @brief Connection arguments
  98. */
  99. typedef struct {
  100. uint32_t sync_timeout; /*!< Maximum time to wait for response from serial interface. */
  101. int32_t trials; /*!< Number of trials to connect to target. If greater than 1,
  102. 100 millisecond delay is inserted after each try. */
  103. } esp_loader_connect_args_t;
  104. #define ESP_LOADER_CONNECT_DEFAULT() { \
  105. .sync_timeout = 100, \
  106. .trials = 10, \
  107. }
  108. /**
  109. * @brief Connects to the target
  110. *
  111. * @param connect_args[in] Timing parameters to be used for connecting to target.
  112. *
  113. * @return
  114. * - ESP_LOADER_SUCCESS Success
  115. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  116. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  117. */
  118. esp_loader_error_t esp_loader_connect(esp_loader_connect_args_t *connect_args);
  119. /**
  120. * @brief Returns attached target chip.
  121. *
  122. * @warning This function can only be called after connection with target
  123. * has been successfully established by calling esp_loader_connect().
  124. *
  125. * @return One of target_chip_t
  126. */
  127. target_chip_t esp_loader_get_target(void);
  128. #ifdef SERIAL_FLASHER_INTERFACE_UART
  129. /**
  130. * @brief Initiates flash operation
  131. *
  132. * @param offset[in] Address from which flash operation will be performed.
  133. * @param image_size[in] Size of the whole binary to be loaded into flash.
  134. * @param block_size[in] Size of buffer used in subsequent calls to esp_loader_flash_write.
  135. *
  136. * @note image_size is size of the whole image, whereas, block_size is chunk of data sent
  137. * to the target, each time esp_loader_flash_write function is called.
  138. *
  139. * @return
  140. * - ESP_LOADER_SUCCESS Success
  141. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  142. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  143. */
  144. esp_loader_error_t esp_loader_flash_start(uint32_t offset, uint32_t image_size, uint32_t block_size);
  145. /**
  146. * @brief Writes supplied data to target's flash memory.
  147. *
  148. * @param payload[in] Data to be flashed into target's memory.
  149. * @param size[in] Size of payload in bytes.
  150. *
  151. * @note size must not be greater that block_size supplied to previously called
  152. * esp_loader_flash_start function. If size is less than block_size,
  153. * remaining bytes of payload buffer will be padded with 0xff.
  154. * Therefore, size of payload buffer has to be equal or greater than block_size.
  155. *
  156. * @return
  157. * - ESP_LOADER_SUCCESS Success
  158. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  159. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  160. */
  161. esp_loader_error_t esp_loader_flash_write(void *payload, uint32_t size);
  162. /**
  163. * @brief Ends flash operation.
  164. *
  165. * @param reboot[in] reboot the target if true.
  166. *
  167. * @return
  168. * - ESP_LOADER_SUCCESS Success
  169. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  170. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  171. */
  172. esp_loader_error_t esp_loader_flash_finish(bool reboot);
  173. #endif /* SERIAL_FLASHER_INTERFACE_UART */
  174. /**
  175. * @brief Initiates mem operation, initiates loading for program into target RAM
  176. *
  177. * @param offset[in] Address from which mem operation will be performed.
  178. * @param size[in] Size of the whole binary to be loaded into mem.
  179. * @param block_size[in] Size of buffer used in subsequent calls to esp_loader_mem_write.
  180. *
  181. * @note image_size is size of the whole image, whereas, block_size is chunk of data sent
  182. * to the target, each time esp_mem_flash_write function is called.
  183. *
  184. * @return
  185. * - ESP_LOADER_SUCCESS Success
  186. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  187. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  188. */
  189. esp_loader_error_t esp_loader_mem_start(uint32_t offset, uint32_t size, uint32_t block_size);
  190. /**
  191. * @brief Writes supplied data to target's mem memory.
  192. *
  193. * @param payload[in] Data to be loaded into target's memory.
  194. * @param size[in] Size of data in bytes.
  195. *
  196. * @note size must not be greater that block_size supplied to previously called
  197. * esp_loader_mem_start function.
  198. * Therefore, size of data buffer has to be equal or greater than block_size.
  199. *
  200. * @return
  201. * - ESP_LOADER_SUCCESS Success
  202. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  203. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  204. */
  205. esp_loader_error_t esp_loader_mem_write(const void *payload, uint32_t size);
  206. /**
  207. * @brief Ends mem operation, finish loading for program into target RAM
  208. * and send the entrypoint of ram_loadable app
  209. *
  210. * @param entrypoint[in] entrypoint of ram program.
  211. *
  212. * @return
  213. * - ESP_LOADER_SUCCESS Success
  214. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  215. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  216. */
  217. esp_loader_error_t esp_loader_mem_finish(uint32_t entrypoint);
  218. /**
  219. * @brief Writes register.
  220. *
  221. * @param address[in] Address of register.
  222. * @param reg_value[in] New register value.
  223. *
  224. * @return
  225. * - ESP_LOADER_SUCCESS Success
  226. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  227. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  228. */
  229. esp_loader_error_t esp_loader_write_register(uint32_t address, uint32_t reg_value);
  230. /**
  231. * @brief Reads register.
  232. *
  233. * @param address[in] Address of register.
  234. * @param reg_value[out] Register value.
  235. *
  236. * @return
  237. * - ESP_LOADER_SUCCESS Success
  238. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  239. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  240. */
  241. esp_loader_error_t esp_loader_read_register(uint32_t address, uint32_t *reg_value);
  242. /**
  243. * @brief Change baud rate.
  244. *
  245. * @note Baud rate has to be also adjusted accordingly on host MCU, as
  246. * target's baud rate is changed upon return from this function.
  247. *
  248. * @param transmission_rate[in] new baud rate to be set.
  249. *
  250. * @return
  251. * - ESP_LOADER_SUCCESS Success
  252. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  253. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  254. * - ESP_LOADER_ERROR_UNSUPPORTED_FUNC Unsupported on the target
  255. */
  256. esp_loader_error_t esp_loader_change_transmission_rate(uint32_t transmission_rate);
  257. /**
  258. * @brief Verify target's flash integrity by checking MD5.
  259. * MD5 checksum is computed from data pushed to target's memory by calling
  260. * esp_loader_flash_write() function and compared against target's MD5.
  261. * Target computes checksum based on offset and image_size passed to
  262. * esp_loader_flash_start() function.
  263. *
  264. * @note This function is only available if MD5_ENABLED is set.
  265. *
  266. * @return
  267. * - ESP_LOADER_SUCCESS Success
  268. * - ESP_LOADER_ERROR_INVALID_MD5 MD5 does not match
  269. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  270. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  271. * - ESP_LOADER_ERROR_UNSUPPORTED_FUNC Unsupported on the target
  272. */
  273. #if MD5_ENABLED
  274. esp_loader_error_t esp_loader_flash_verify(void);
  275. #endif
  276. /**
  277. * @brief Toggles reset pin.
  278. */
  279. void esp_loader_reset_target(void);
  280. #ifdef __cplusplus
  281. }
  282. #endif