esp_loader.h 5.9 KB

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