esp_loader.h 5.6 KB

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