esp32_uart.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include "serial_io.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "driver/uart.h"
  19. #include "driver/gpio.h"
  20. #include "esp_timer.h"
  21. #include "esp_log.h"
  22. #include <unistd.h>
  23. // #define SERIAL_DEBUG_ENABLE
  24. #ifdef SERIAL_DEBUG_ENABLE
  25. void serial_debug_print(const uint8_t *data, uint16_t size)
  26. {
  27. ESP_LOG_BUFFER_HEX("LOADER ESP32 PORT", data, size);
  28. }
  29. #else
  30. void serial_debug_print(const uint8_t *data, uint16_t size) { }
  31. #endif
  32. static int64_t s_time_end;
  33. static int32_t s_uart_port;
  34. static int32_t s_reset_trigger_pin;
  35. static int32_t s_gpio0_trigger_pin;
  36. esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config)
  37. {
  38. s_uart_port = config->uart_port;
  39. s_reset_trigger_pin = config->reset_trigger_pin;
  40. s_gpio0_trigger_pin = config->gpio0_trigger_pin;
  41. // Initialize UART
  42. uart_config_t uart_config = {
  43. .baud_rate = config->baud_rate,
  44. .data_bits = UART_DATA_8_BITS,
  45. .parity = UART_PARITY_DISABLE,
  46. .stop_bits = UART_STOP_BITS_1,
  47. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  48. };
  49. const int rx_buffer_size = 2 * 200;
  50. const int tx_buffer_size = 2 * 200;
  51. if ( uart_param_config(s_uart_port, &uart_config) != ESP_OK ) {
  52. return ESP_LOADER_ERROR_FAIL;
  53. }
  54. 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 ) {
  55. return ESP_LOADER_ERROR_FAIL;
  56. }
  57. if ( uart_driver_install(s_uart_port, rx_buffer_size, tx_buffer_size, 0, NULL, 0) != ESP_OK ) {
  58. return ESP_LOADER_ERROR_FAIL;
  59. }
  60. // Initialize boot pin selection pins
  61. gpio_pad_select_gpio(s_reset_trigger_pin);
  62. gpio_set_pull_mode(s_reset_trigger_pin, GPIO_PULLUP_ONLY);
  63. gpio_set_direction(s_reset_trigger_pin, GPIO_MODE_OUTPUT);
  64. gpio_pad_select_gpio(s_gpio0_trigger_pin);
  65. gpio_set_pull_mode(s_gpio0_trigger_pin, GPIO_PULLUP_ONLY);
  66. gpio_set_direction(s_gpio0_trigger_pin, GPIO_MODE_OUTPUT);
  67. return ESP_LOADER_SUCCESS;
  68. }
  69. esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size, uint32_t timeout)
  70. {
  71. serial_debug_print(data, size);
  72. uart_write_bytes(s_uart_port, (const char *)data, size);
  73. esp_err_t err = uart_wait_tx_done(s_uart_port, pdMS_TO_TICKS(timeout));
  74. if (err == ESP_OK) {
  75. return ESP_LOADER_SUCCESS;
  76. } else if (err == ESP_ERR_TIMEOUT) {
  77. return ESP_LOADER_ERROR_TIMEOUT;
  78. } else {
  79. return ESP_LOADER_ERROR_FAIL;
  80. }
  81. }
  82. esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
  83. {
  84. int read = uart_read_bytes(s_uart_port, data, size, pdMS_TO_TICKS(timeout));
  85. if (read < 0) {
  86. return ESP_LOADER_ERROR_FAIL;
  87. } else if (read < size) {
  88. return ESP_LOADER_ERROR_TIMEOUT;
  89. } else {
  90. return ESP_LOADER_SUCCESS;
  91. }
  92. }
  93. // Set GPIO0 LOW, then
  94. // assert reset pin for 50 milliseconds.
  95. void loader_port_enter_bootloader(void)
  96. {
  97. gpio_set_level(s_gpio0_trigger_pin, 0);
  98. gpio_set_level(s_reset_trigger_pin, 0);
  99. gpio_set_level(s_reset_trigger_pin, 1);
  100. loader_port_delay_ms(50);
  101. gpio_set_level(s_gpio0_trigger_pin, 1);
  102. }
  103. void loader_port_reset_target(void)
  104. {
  105. gpio_set_level(s_reset_trigger_pin, 0);
  106. loader_port_delay_ms(50);
  107. gpio_set_level(s_reset_trigger_pin, 1);
  108. }
  109. void loader_port_delay_ms(uint32_t ms)
  110. {
  111. usleep(ms * 1000);
  112. }
  113. void loader_port_start_timer(uint32_t ms)
  114. {
  115. s_time_end = esp_timer_get_time() + ms * 1000;
  116. }
  117. uint32_t loader_port_remaining_time(void)
  118. {
  119. int64_t remaining = (s_time_end - esp_timer_get_time()) / 1000;
  120. return (remaining > 0) ? (uint32_t)remaining : 0;
  121. }
  122. void loader_port_debug_print(const char *str)
  123. {
  124. printf("DEBUG: %s\n", str);
  125. }
  126. esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate)
  127. {
  128. esp_err_t err = uart_set_baudrate(s_uart_port, baudrate);
  129. return (err == ESP_OK) ? ESP_LOADER_SUCCESS : ESP_LOADER_ERROR_FAIL;
  130. }