esp32_port.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Copyright 2020-2023 Espressif Systems (Shanghai) CO 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. #include "esp32_port.h"
  16. #include "driver/uart.h"
  17. #include "driver/gpio.h"
  18. #include "esp_timer.h"
  19. #include "esp_log.h"
  20. #include "esp_idf_version.h"
  21. #include <unistd.h>
  22. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  23. static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write)
  24. {
  25. static bool write_prev = false;
  26. if (write_prev != write) {
  27. write_prev = write;
  28. printf("\n--- %s ---\n", write ? "WRITE" : "READ");
  29. }
  30. for (uint32_t i = 0; i < size; i++) {
  31. printf("%02x ", data[i]);
  32. }
  33. }
  34. #endif
  35. static int64_t s_time_end;
  36. static int32_t s_uart_port;
  37. static int32_t s_reset_trigger_pin;
  38. static int32_t s_gpio0_trigger_pin;
  39. esp_loader_error_t loader_port_esp32_init(const loader_esp32_config_t *config)
  40. {
  41. s_uart_port = config->uart_port;
  42. s_reset_trigger_pin = config->reset_trigger_pin;
  43. s_gpio0_trigger_pin = config->gpio0_trigger_pin;
  44. // Initialize UART
  45. uart_config_t uart_config = {
  46. .baud_rate = config->baud_rate,
  47. .data_bits = UART_DATA_8_BITS,
  48. .parity = UART_PARITY_DISABLE,
  49. .stop_bits = UART_STOP_BITS_1,
  50. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  51. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  52. .source_clk = UART_SCLK_DEFAULT,
  53. #endif
  54. };
  55. int rx_buffer_size = config->rx_buffer_size ? config->rx_buffer_size : 400;
  56. int tx_buffer_size = config->tx_buffer_size ? config->tx_buffer_size : 400;
  57. QueueHandle_t *uart_queue = config->uart_queue ? config->uart_queue : NULL;
  58. int queue_size = config->queue_size ? config->queue_size : 0;
  59. if ( uart_param_config(s_uart_port, &uart_config) != ESP_OK ) {
  60. return ESP_LOADER_ERROR_FAIL;
  61. }
  62. if ( uart_set_pin(s_uart_port, config->uart_tx_pin, config->uart_rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE) != ESP_OK ) {
  63. return ESP_LOADER_ERROR_FAIL;
  64. }
  65. if ( uart_driver_install(s_uart_port, rx_buffer_size, tx_buffer_size, queue_size, uart_queue, 0) != ESP_OK ) {
  66. return ESP_LOADER_ERROR_FAIL;
  67. }
  68. // Initialize boot pin selection pins
  69. gpio_reset_pin(s_reset_trigger_pin);
  70. gpio_set_pull_mode(s_reset_trigger_pin, GPIO_PULLUP_ONLY);
  71. gpio_set_direction(s_reset_trigger_pin, GPIO_MODE_OUTPUT);
  72. gpio_reset_pin(s_gpio0_trigger_pin);
  73. gpio_set_pull_mode(s_gpio0_trigger_pin, GPIO_PULLUP_ONLY);
  74. gpio_set_direction(s_gpio0_trigger_pin, GPIO_MODE_OUTPUT);
  75. return ESP_LOADER_SUCCESS;
  76. }
  77. void loader_port_esp32_deinit(void)
  78. {
  79. uart_driver_delete(s_uart_port);
  80. }
  81. esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout)
  82. {
  83. uart_write_bytes(s_uart_port, (const char *)data, size);
  84. esp_err_t err = uart_wait_tx_done(s_uart_port, timeout);
  85. if (err == ESP_OK) {
  86. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  87. transfer_debug_print(data, size, true);
  88. #endif
  89. return ESP_LOADER_SUCCESS;
  90. } else if (err == ESP_ERR_TIMEOUT) {
  91. return ESP_LOADER_ERROR_TIMEOUT;
  92. } else {
  93. return ESP_LOADER_ERROR_FAIL;
  94. }
  95. }
  96. esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
  97. {
  98. int read = uart_read_bytes(s_uart_port, data, size, timeout);
  99. if (read < 0) {
  100. return ESP_LOADER_ERROR_FAIL;
  101. } else if (read < size) {
  102. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  103. transfer_debug_print(data, read, false);
  104. #endif
  105. return ESP_LOADER_ERROR_TIMEOUT;
  106. } else {
  107. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  108. transfer_debug_print(data, read, false);
  109. #endif
  110. return ESP_LOADER_SUCCESS;
  111. }
  112. }
  113. // Set GPIO0 LOW, then
  114. // assert reset pin for 50 milliseconds.
  115. void loader_port_enter_bootloader(void)
  116. {
  117. gpio_set_level(s_gpio0_trigger_pin, 0);
  118. loader_port_reset_target();
  119. loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
  120. gpio_set_level(s_gpio0_trigger_pin, 1);
  121. }
  122. void loader_port_reset_target(void)
  123. {
  124. gpio_set_level(s_reset_trigger_pin, 0);
  125. loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
  126. gpio_set_level(s_reset_trigger_pin, 1);
  127. }
  128. void loader_port_delay_ms(uint32_t ms)
  129. {
  130. usleep(ms * 1000);
  131. }
  132. void loader_port_start_timer(uint32_t ms)
  133. {
  134. s_time_end = esp_timer_get_time() + ms * 1000;
  135. }
  136. uint32_t loader_port_remaining_time(void)
  137. {
  138. int64_t remaining = (s_time_end - esp_timer_get_time()) / 1000;
  139. return (remaining > 0) ? (uint32_t)remaining : 0;
  140. }
  141. void loader_port_debug_print(const char *str)
  142. {
  143. printf("DEBUG: %s\n", str);
  144. }
  145. esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
  146. {
  147. esp_err_t err = uart_set_baudrate(s_uart_port, baudrate);
  148. return (err == ESP_OK) ? ESP_LOADER_SUCCESS : ESP_LOADER_ERROR_FAIL;
  149. }