esp32_port.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 "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. // #define SERIAL_DEBUG_ENABLE
  23. #ifdef SERIAL_DEBUG_ENABLE
  24. static void dec_to_hex_str(const uint8_t dec, uint8_t hex_str[3])
  25. {
  26. static const uint8_t dec_to_hex[] = {
  27. '0', '1', '2', '3', '4', '5', '6', '7',
  28. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  29. };
  30. hex_str[0] = dec_to_hex[dec >> 4];
  31. hex_str[1] = dec_to_hex[dec & 0xF];
  32. hex_str[2] = '\0';
  33. }
  34. static void serial_debug_print(const uint8_t *data, uint16_t size, bool write)
  35. {
  36. static bool write_prev = false;
  37. uint8_t hex_str[3];
  38. if(write_prev != write) {
  39. write_prev = write;
  40. printf("\n--- %s ---\n", write ? "WRITE" : "READ");
  41. }
  42. for(uint32_t i = 0; i < size; i++) {
  43. dec_to_hex_str(data[i], hex_str);
  44. printf("%s ", hex_str);
  45. }
  46. }
  47. #else
  48. static void serial_debug_print(const uint8_t *data, uint16_t size, bool write) { }
  49. #endif
  50. static int64_t s_time_end;
  51. static int32_t s_uart_port;
  52. static int32_t s_reset_trigger_pin;
  53. static int32_t s_gpio0_trigger_pin;
  54. esp_loader_error_t loader_port_esp32_init(const loader_esp32_config_t *config)
  55. {
  56. s_uart_port = config->uart_port;
  57. s_reset_trigger_pin = config->reset_trigger_pin;
  58. s_gpio0_trigger_pin = config->gpio0_trigger_pin;
  59. // Initialize UART
  60. uart_config_t uart_config = {
  61. .baud_rate = config->baud_rate,
  62. .data_bits = UART_DATA_8_BITS,
  63. .parity = UART_PARITY_DISABLE,
  64. .stop_bits = UART_STOP_BITS_1,
  65. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  66. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  67. .source_clk = UART_SCLK_DEFAULT,
  68. #endif
  69. };
  70. int rx_buffer_size = config->rx_buffer_size ? config->rx_buffer_size : 400;
  71. int tx_buffer_size = config->tx_buffer_size ? config->tx_buffer_size : 400;
  72. QueueHandle_t *uart_queue = config->uart_queue ? config->uart_queue : NULL;
  73. int queue_size = config->queue_size ? config->queue_size : 0;
  74. if ( uart_param_config(s_uart_port, &uart_config) != ESP_OK ) {
  75. return ESP_LOADER_ERROR_FAIL;
  76. }
  77. 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 ) {
  78. return ESP_LOADER_ERROR_FAIL;
  79. }
  80. if ( uart_driver_install(s_uart_port, rx_buffer_size, tx_buffer_size, queue_size, uart_queue, 0) != ESP_OK ) {
  81. return ESP_LOADER_ERROR_FAIL;
  82. }
  83. // Initialize boot pin selection pins
  84. gpio_reset_pin(s_reset_trigger_pin);
  85. gpio_set_pull_mode(s_reset_trigger_pin, GPIO_PULLUP_ONLY);
  86. gpio_set_direction(s_reset_trigger_pin, GPIO_MODE_OUTPUT);
  87. gpio_reset_pin(s_gpio0_trigger_pin);
  88. gpio_set_pull_mode(s_gpio0_trigger_pin, GPIO_PULLUP_ONLY);
  89. gpio_set_direction(s_gpio0_trigger_pin, GPIO_MODE_OUTPUT);
  90. return ESP_LOADER_SUCCESS;
  91. }
  92. void loader_port_esp32_deinit(void)
  93. {
  94. uart_driver_delete(s_uart_port);
  95. }
  96. esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size, uint32_t timeout)
  97. {
  98. serial_debug_print(data, size, true);
  99. uart_write_bytes(s_uart_port, (const char *)data, size);
  100. esp_err_t err = uart_wait_tx_done(s_uart_port, pdMS_TO_TICKS(timeout));
  101. if (err == ESP_OK) {
  102. return ESP_LOADER_SUCCESS;
  103. } else if (err == ESP_ERR_TIMEOUT) {
  104. return ESP_LOADER_ERROR_TIMEOUT;
  105. } else {
  106. return ESP_LOADER_ERROR_FAIL;
  107. }
  108. }
  109. esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
  110. {
  111. int read = uart_read_bytes(s_uart_port, data, size, pdMS_TO_TICKS(timeout));
  112. serial_debug_print(data, read, false);
  113. if (read < 0) {
  114. return ESP_LOADER_ERROR_FAIL;
  115. } else if (read < size) {
  116. return ESP_LOADER_ERROR_TIMEOUT;
  117. } else {
  118. return ESP_LOADER_SUCCESS;
  119. }
  120. }
  121. // Set GPIO0 LOW, then
  122. // assert reset pin for 50 milliseconds.
  123. void loader_port_enter_bootloader(void)
  124. {
  125. gpio_set_level(s_gpio0_trigger_pin, 0);
  126. loader_port_reset_target();
  127. loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
  128. gpio_set_level(s_gpio0_trigger_pin, 1);
  129. }
  130. void loader_port_reset_target(void)
  131. {
  132. gpio_set_level(s_reset_trigger_pin, 0);
  133. loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
  134. gpio_set_level(s_reset_trigger_pin, 1);
  135. }
  136. void loader_port_delay_ms(uint32_t ms)
  137. {
  138. usleep(ms * 1000);
  139. }
  140. void loader_port_start_timer(uint32_t ms)
  141. {
  142. s_time_end = esp_timer_get_time() + ms * 1000;
  143. }
  144. uint32_t loader_port_remaining_time(void)
  145. {
  146. int64_t remaining = (s_time_end - esp_timer_get_time()) / 1000;
  147. return (remaining > 0) ? (uint32_t)remaining : 0;
  148. }
  149. void loader_port_debug_print(const char *str)
  150. {
  151. printf("DEBUG: %s\n", str);
  152. }
  153. esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate)
  154. {
  155. esp_err_t err = uart_set_baudrate(s_uart_port, baudrate);
  156. return (err == ESP_OK) ? ESP_LOADER_SUCCESS : ESP_LOADER_ERROR_FAIL;
  157. }