esp_loader.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Connection arguments
  39. */
  40. typedef struct
  41. {
  42. uint32_t sync_timeout; /*!< Maximum time to wait for response from serial interface. */
  43. int32_t trials; /*!< Number of trials to connect to target. If greater than 1,
  44. 100 millisecond delay is inserted after each try. */
  45. } esp_loader_connect_args_t;
  46. #define ESP_LOADER_CONNECT_DEFAULT() { \
  47. .sync_timeout = 100, \
  48. .trials = 10 \
  49. }
  50. /**
  51. * @brief Connects to the target
  52. *
  53. * @param connect_args[in] Timing parameters to be used for connecting to target.
  54. *
  55. * @return
  56. * - ESP_LOADER_SUCCESS Success
  57. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  58. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  59. */
  60. esp_loader_error_t esp_loader_connect(esp_loader_connect_args_t *connect_args);
  61. /**
  62. * @brief Initiates flash operation
  63. *
  64. * @param offset[in] Address from which flash operation will be performed.
  65. * @param image_size[in] Size of the whole binary to be loaded into flash.
  66. * @param block_size[in] Size of buffer used in subsequent calls to esp_loader_flash_write.
  67. *
  68. * @note image_size is size of the whole image, whereas, block_size is chunk of data sent
  69. * to the target, each time esp_loader_flash_write function is called.
  70. *
  71. * @return
  72. * - ESP_LOADER_SUCCESS Success
  73. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  74. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  75. */
  76. esp_loader_error_t esp_loader_flash_start(uint32_t offset, uint32_t image_size, uint32_t block_size);
  77. /**
  78. * @brief Writes supplied data to target's flash memory.
  79. *
  80. * @param payload[in] Data to be flashed into target's memory.
  81. * @param size[in] Size of payload in bytes.
  82. *
  83. * @note size must not be greater that block_size supplied to previously called
  84. * esp_loader_flash_start function. If size is less than block_size,
  85. * remaining bytes of payload buffer will be padded with 0xff.
  86. * Therefore, size of payload buffer has to be equal or greater than block_size.
  87. *
  88. * @return
  89. * - ESP_LOADER_SUCCESS Success
  90. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  91. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  92. */
  93. esp_loader_error_t esp_loader_flash_write(void *payload, uint32_t size);
  94. /**
  95. * @brief Ends flash operation.
  96. *
  97. * @param reboot[in] reboot the target if true.
  98. *
  99. * @return
  100. * - ESP_LOADER_SUCCESS Success
  101. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  102. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  103. */
  104. esp_loader_error_t esp_loader_flash_finish(bool reboot);
  105. /**
  106. * @brief Writes register.
  107. *
  108. * @param address[in] Address of register.
  109. * @param reg_value[in] New register value.
  110. *
  111. * @return
  112. * - ESP_LOADER_SUCCESS Success
  113. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  114. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  115. */
  116. esp_loader_error_t esp_loader_write_register(uint32_t address, uint32_t reg_value);
  117. /**
  118. * @brief Reads register.
  119. *
  120. * @param address[in] Address of register.
  121. * @param reg_value[out] Register value.
  122. *
  123. * @return
  124. * - ESP_LOADER_SUCCESS Success
  125. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  126. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  127. */
  128. esp_loader_error_t esp_loader_read_register(uint32_t address, uint32_t *reg_value);
  129. /**
  130. * @brief Change baud rate.
  131. *
  132. * @note Baud rate has to be also adjusted accordingly on host MCU, as
  133. * target's baud rate is changed upon return from this function.
  134. *
  135. * @param baudrate[in] new baud rate to be set.
  136. *
  137. * @return
  138. * - ESP_LOADER_SUCCESS Success
  139. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  140. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  141. */
  142. #ifndef TARGET_ESP8266
  143. esp_loader_error_t esp_loader_change_baudrate(uint32_t baudrate);
  144. #endif
  145. /**
  146. * @brief Verify target's flash integrity by checking MD5.
  147. * MD5 checksum is computed from data pushed to target's memory by calling
  148. * esp_loader_flash_write() function and compared against target's MD5.
  149. * Target computes checksum based on offset and image_size passed to
  150. * esp_loader_flash_start() function.
  151. *
  152. * @note This function is only available if MD5_ENABLED is set.
  153. *
  154. * @return
  155. * - ESP_LOADER_SUCCESS Success
  156. * - ESP_LOADER_ERROR_INVALID_MD5 MD5 does not match
  157. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  158. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  159. */
  160. esp_loader_error_t esp_loader_flash_verify(void);
  161. /**
  162. * @brief Toggles reset pin.
  163. */
  164. void esp_loader_reset_target(void);
  165. #ifdef __cplusplus
  166. }
  167. #endif