esp_loader.h 9.4 KB

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