serial_io.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "esp_loader.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. typedef struct
  22. {
  23. uint32_t baud_rate;
  24. uint32_t uart_port;
  25. uint32_t uart_rx_pin;
  26. uint32_t uart_tx_pin;
  27. uint32_t reset_trigger_pin;
  28. uint32_t gpio0_trigger_pin;
  29. } loader_serial_config_t;
  30. /**
  31. * @brief Initializes serial interface.
  32. *
  33. * @param baud_rate[in] Communication speed.
  34. *
  35. * @return
  36. * - ESP_LOADER_SUCCESS Success
  37. * - ESP_LOADER_ERROR_FAIL Initialization failure
  38. */
  39. esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config);
  40. /**
  41. * @brief Deinitialize serial interface.
  42. */
  43. void loader_port_serial_deinit(void);
  44. /**
  45. * @brief Changes baud rate of serial peripheral.
  46. */
  47. esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate);
  48. /**
  49. * @brief Writes data to serial interface.
  50. *
  51. * @param data[in] Buffer with data to be written.
  52. * @param size[in] Size of data in bytes.
  53. * @param timeout[in] Timeout in milliseconds.
  54. *
  55. * @return
  56. * - ESP_LOADER_SUCCESS Success
  57. * - ESP_LOADER_ERROR_TIMEOUT Timeout elapsed
  58. */
  59. esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size, uint32_t timeout);
  60. /**
  61. * @brief Reads data from serial interface.
  62. *
  63. * @param data[out] Buffer into which received data will be written.
  64. * @param size[in] Number of bytes to read.
  65. * @param timeout[in] Timeout in milliseconds.
  66. *
  67. * @return
  68. * - ESP_LOADER_SUCCESS Success
  69. * - ESP_LOADER_ERROR_TIMEOUT Timeout elapsed
  70. */
  71. esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout);
  72. /**
  73. * @brief Delay in milliseconds.
  74. *
  75. * @param ms[in] Number of milliseconds.
  76. *
  77. */
  78. void loader_port_delay_ms(uint32_t ms);
  79. /**
  80. * @brief Starts timeout timer.
  81. *
  82. * @param ms[in] Number of milliseconds.
  83. *
  84. */
  85. void loader_port_start_timer(uint32_t ms);
  86. /**
  87. * @brief Returns remaining time since timer was started by calling esp_loader_start_timer.
  88. * 0 if timer has elapsed.
  89. *
  90. * @return Number of milliseconds.
  91. *
  92. */
  93. uint32_t loader_port_remaining_time(void);
  94. /**
  95. * @brief Asserts bootstrap pins to enter boot mode and toggles reset pin.
  96. *
  97. * @note Reset pin should stay asserted for at least 20 milliseconds.
  98. */
  99. void loader_port_enter_bootloader(void);
  100. /**
  101. * @brief Toggles reset pin.
  102. *
  103. * @note Reset pin should stay asserted for at least 20 milliseconds.
  104. */
  105. void loader_port_reset_target(void);
  106. /**
  107. * @brief Function can be defined by user to print debug message.
  108. *
  109. * @note Empty weak function is used, otherwise.
  110. *
  111. */
  112. void loader_port_debug_print(const char *str);
  113. #ifdef __cplusplus
  114. }
  115. #endif