esp_loader.h 6.9 KB

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