esp_loader.h 7.6 KB

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