esp_loader.h 5.7 KB

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