esp_loader.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_RESPONSE /*!< Internal error */
  30. } esp_loader_error_t;
  31. /**
  32. * @brief Connection arguments
  33. */
  34. typedef struct
  35. {
  36. uint32_t sync_timeout; /*!< Maximum time to wait for response from serial interface. */
  37. int32_t trials; /*!< Number of trials to connect to target. If greater than 1,
  38. 100 millisecond delay is inserted after each try. */
  39. } esp_loader_connect_args_t;
  40. #define ESP_LOADER_CONNECT_DEFAULT() { \
  41. .sync_timeout = 100, \
  42. .trials = 10 \
  43. }
  44. /**
  45. * @brief Connects to the target
  46. *
  47. * @param connect_args[in] Timing parameters to be used for connecting to target.
  48. *
  49. * @return
  50. * - ESP_LOADER_SUCCESS Success
  51. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  52. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  53. */
  54. esp_loader_error_t esp_loader_connect(esp_loader_connect_args_t *connect_args);
  55. /**
  56. * @brief Initiates flash operation
  57. *
  58. * @param offset[in] Address from which flash operation will be performed.
  59. * @param image_size[in] Size of the whole binary to be loaded into flash.
  60. * @param block_size[in] Size of buffer used in subsequent calls to esp_loader_flash_write.
  61. *
  62. * @note image_size is size of the whole image, whereas, block_size is chunk of data sent
  63. * to the target, each time esp_loader_flash_write function is called.
  64. *
  65. * @return
  66. * - ESP_LOADER_SUCCESS Success
  67. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  68. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  69. */
  70. esp_loader_error_t esp_loader_flash_start(uint32_t offset, uint32_t image_size, uint32_t block_size);
  71. /**
  72. * @brief Writes supplied data to target's flash memory.
  73. *
  74. * @param payload[in] Data to be flashed into target's memory.
  75. * @param size[in] Size of payload in bytes.
  76. *
  77. * @note size must not be greater that block_size supplied to previously called
  78. * esp_loader_flash_start function. If size is less than block_size,
  79. * remaining bytes of payload buffer will be padded with 0xff.
  80. * Therefore, size of payload buffer has to be equal or greater than block_size.
  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_flash_write(void *payload, uint32_t size);
  88. /**
  89. * @brief Ends flash operation.
  90. *
  91. * @param reboot[in] reboot the target if true.
  92. *
  93. * @return
  94. * - ESP_LOADER_SUCCESS Success
  95. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  96. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  97. */
  98. esp_loader_error_t esp_loader_flash_finish(bool reboot);
  99. /**
  100. * @brief Writes register.
  101. *
  102. * @param address[in] Address of register.
  103. * @param reg_value[in] New register value.
  104. *
  105. * @return
  106. * - ESP_LOADER_SUCCESS Success
  107. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  108. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  109. */
  110. esp_loader_error_t esp_loader_write_register(uint32_t address, uint32_t reg_value);
  111. /**
  112. * @brief Reads register.
  113. *
  114. * @param address[in] Address of register.
  115. * @param reg_value[out] Register value.
  116. *
  117. * @return
  118. * - ESP_LOADER_SUCCESS Success
  119. * - ESP_LOADER_ERROR_TIMEOUT Timeout
  120. * - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
  121. */
  122. esp_loader_error_t esp_loader_read_register(uint32_t address, uint32_t *reg_value);
  123. /**
  124. * @brief Toggles reset pin.
  125. */
  126. void esp_loader_reset_target(void);
  127. #ifdef __cplusplus
  128. }
  129. #endif