esp_loader.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. ESP_MAX_CHIP = 5,
  56. ESP_UNKNOWN_CHIP = 5
  57. } target_chip_t;
  58. /**
  59. * @brief SPI pin configuration arguments
  60. */
  61. typedef union {
  62. struct {
  63. uint32_t pin_clk: 6;
  64. uint32_t pin_q: 6;
  65. uint32_t pin_d: 6;
  66. uint32_t pin_cs: 6;
  67. uint32_t pin_hd: 6;
  68. uint32_t zero: 2;
  69. };
  70. uint32_t val;
  71. } esp_loader_spi_config_t;
  72. /**
  73. * @brief Connection arguments
  74. */
  75. typedef struct {
  76. uint32_t sync_timeout; /*!< Maximum time to wait for response from serial interface. */
  77. int32_t trials; /*!< Number of trials to connect to target. If greater than 1,
  78. 100 millisecond delay is inserted after each try. */
  79. } esp_loader_connect_args_t;
  80. #define ESP_LOADER_CONNECT_DEFAULT() { \
  81. .sync_timeout = 100, \
  82. .trials = 10, \
  83. }
  84. /**
  85. * @brief Connects to the target
  86. *
  87. * @param connect_args[in] Timing parameters to be used for connecting to target.
  88. *
  89. * @return
  90. * - ESP_LOADER_SUCCESS Success
  91. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  92. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  93. */
  94. esp_loader_error_t esp_loader_connect(esp_loader_connect_args_t *connect_args);
  95. /**
  96. * @brief Returns attached target chip.
  97. *
  98. * @warning This function can only be called after connection with target
  99. * has been successfully established by calling esp_loader_connect().
  100. *
  101. * @return One of target_chip_t
  102. */
  103. target_chip_t esp_loader_get_target(void);
  104. /**
  105. * @brief Initiates flash operation
  106. *
  107. * @param offset[in] Address from which flash operation will be performed.
  108. * @param image_size[in] Size of the whole binary to be loaded into flash.
  109. * @param block_size[in] Size of buffer used in subsequent calls to esp_loader_flash_write.
  110. *
  111. * @note image_size is size of the whole image, whereas, block_size is chunk of data sent
  112. * to the target, each time esp_loader_flash_write function is called.
  113. *
  114. * @return
  115. * - ESP_LOADER_SUCCESS Success
  116. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  117. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  118. */
  119. esp_loader_error_t esp_loader_flash_start(uint32_t offset, uint32_t image_size, uint32_t block_size);
  120. /**
  121. * @brief Writes supplied data to target's flash memory.
  122. *
  123. * @param payload[in] Data to be flashed into target's memory.
  124. * @param size[in] Size of payload in bytes.
  125. *
  126. * @note size must not be greater that block_size supplied to previously called
  127. * esp_loader_flash_start function. If size is less than block_size,
  128. * remaining bytes of payload buffer will be padded with 0xff.
  129. * Therefore, size of payload buffer has to be equal or greater than block_size.
  130. *
  131. * @return
  132. * - ESP_LOADER_SUCCESS Success
  133. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  134. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  135. */
  136. esp_loader_error_t esp_loader_flash_write(void *payload, uint32_t size);
  137. /**
  138. * @brief Ends flash operation.
  139. *
  140. * @param reboot[in] reboot the target if true.
  141. *
  142. * @return
  143. * - ESP_LOADER_SUCCESS Success
  144. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  145. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  146. */
  147. esp_loader_error_t esp_loader_flash_finish(bool reboot);
  148. /**
  149. * @brief Writes register.
  150. *
  151. * @param address[in] Address of register.
  152. * @param reg_value[in] New register value.
  153. *
  154. * @return
  155. * - ESP_LOADER_SUCCESS Success
  156. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  157. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  158. */
  159. esp_loader_error_t esp_loader_write_register(uint32_t address, uint32_t reg_value);
  160. /**
  161. * @brief Reads register.
  162. *
  163. * @param address[in] Address of register.
  164. * @param reg_value[out] Register value.
  165. *
  166. * @return
  167. * - ESP_LOADER_SUCCESS Success
  168. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  169. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  170. */
  171. esp_loader_error_t esp_loader_read_register(uint32_t address, uint32_t *reg_value);
  172. /**
  173. * @brief Change baud rate.
  174. *
  175. * @note Baud rate has to be also adjusted accordingly on host MCU, as
  176. * target's baud rate is changed upon return from this function.
  177. *
  178. * @param baudrate[in] new baud rate to be set.
  179. *
  180. * @return
  181. * - ESP_LOADER_SUCCESS Success
  182. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  183. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  184. * - ESP_LOADER_ERROR_UNSUPPORTED_FUNC Unsupported on the target
  185. */
  186. esp_loader_error_t esp_loader_change_baudrate(uint32_t baudrate);
  187. /**
  188. * @brief Verify target's flash integrity by checking MD5.
  189. * MD5 checksum is computed from data pushed to target's memory by calling
  190. * esp_loader_flash_write() function and compared against target's MD5.
  191. * Target computes checksum based on offset and image_size passed to
  192. * esp_loader_flash_start() function.
  193. *
  194. * @note This function is only available if MD5_ENABLED is set.
  195. *
  196. * @return
  197. * - ESP_LOADER_SUCCESS Success
  198. * - ESP_LOADER_ERROR_INVALID_MD5 MD5 does not match
  199. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  200. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  201. * - ESP_LOADER_ERROR_UNSUPPORTED_FUNC Unsupported on the target
  202. */
  203. #if MD5_ENABLED
  204. esp_loader_error_t esp_loader_flash_verify(void);
  205. #endif
  206. /**
  207. * @brief Toggles reset pin.
  208. */
  209. void esp_loader_reset_target(void);
  210. #ifdef __cplusplus
  211. }
  212. #endif