esp32_port.c 5.3 KB

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