esp32_port.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. static void dec_to_hex_str(const uint8_t dec, uint8_t hex_str[3])
  26. {
  27. static const uint8_t dec_to_hex[] = {
  28. '0', '1', '2', '3', '4', '5', '6', '7',
  29. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  30. };
  31. hex_str[0] = dec_to_hex[(dec >> 4)];
  32. hex_str[1] = dec_to_hex[(dec & 0xF)];
  33. hex_str[2] = '\0';
  34. }
  35. static void serial_debug_print(const uint8_t *data, uint16_t size, bool write)
  36. {
  37. static bool write_prev = false;
  38. uint8_t hex_str[3];
  39. if(write_prev != write) {
  40. write_prev = write;
  41. printf("\n--- %s ---\n", write ? "WRITE" : "READ");
  42. }
  43. for(uint32_t i = 0; i < size; i++) {
  44. dec_to_hex_str(data[i], hex_str);
  45. printf("%s ", hex_str);
  46. }
  47. }
  48. #else
  49. static void serial_debug_print(const uint8_t *data, uint16_t size, bool write) { }
  50. #endif
  51. static int64_t s_time_end;
  52. static int32_t s_uart_port;
  53. static int32_t s_reset_trigger_pin;
  54. static int32_t s_gpio0_trigger_pin;
  55. esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config)
  56. {
  57. s_uart_port = config->uart_port;
  58. s_reset_trigger_pin = config->reset_trigger_pin;
  59. s_gpio0_trigger_pin = config->gpio0_trigger_pin;
  60. // Initialize UART
  61. uart_config_t uart_config = {
  62. .baud_rate = config->baud_rate,
  63. .data_bits = UART_DATA_8_BITS,
  64. .parity = UART_PARITY_DISABLE,
  65. .stop_bits = UART_STOP_BITS_1,
  66. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  67. };
  68. const int rx_buffer_size = 2 * 200;
  69. const int tx_buffer_size = 2 * 200;
  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, 0, NULL, 0) != ESP_OK ) {
  77. return ESP_LOADER_ERROR_FAIL;
  78. }
  79. // Initialize boot pin selection pins
  80. gpio_pad_select_gpio(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_pad_select_gpio(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. esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size, uint32_t timeout)
  89. {
  90. serial_debug_print(data, size, true);
  91. uart_write_bytes(s_uart_port, (const char *)data, size);
  92. esp_err_t err = uart_wait_tx_done(s_uart_port, pdMS_TO_TICKS(timeout));
  93. if (err == ESP_OK) {
  94. return ESP_LOADER_SUCCESS;
  95. } else if (err == ESP_ERR_TIMEOUT) {
  96. return ESP_LOADER_ERROR_TIMEOUT;
  97. } else {
  98. return ESP_LOADER_ERROR_FAIL;
  99. }
  100. }
  101. esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
  102. {
  103. int read = uart_read_bytes(s_uart_port, data, size, pdMS_TO_TICKS(timeout));
  104. serial_debug_print(data, read, false);
  105. if (read < 0) {
  106. return ESP_LOADER_ERROR_FAIL;
  107. } else if (read < size) {
  108. return ESP_LOADER_ERROR_TIMEOUT;
  109. } else {
  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. gpio_set_level(s_reset_trigger_pin, 0);
  119. loader_port_delay_ms(50);
  120. gpio_set_level(s_reset_trigger_pin, 1);
  121. loader_port_delay_ms(50);
  122. gpio_set_level(s_gpio0_trigger_pin, 1);
  123. }
  124. void loader_port_reset_target(void)
  125. {
  126. gpio_set_level(s_reset_trigger_pin, 0);
  127. loader_port_delay_ms(50);
  128. gpio_set_level(s_reset_trigger_pin, 1);
  129. }
  130. void loader_port_delay_ms(uint32_t ms)
  131. {
  132. usleep(ms * 1000);
  133. }
  134. void loader_port_start_timer(uint32_t ms)
  135. {
  136. s_time_end = esp_timer_get_time() + ms * 1000;
  137. }
  138. uint32_t loader_port_remaining_time(void)
  139. {
  140. int64_t remaining = (s_time_end - esp_timer_get_time()) / 1000;
  141. return (remaining > 0) ? (uint32_t)remaining : 0;
  142. }
  143. void loader_port_debug_print(const char *str)
  144. {
  145. printf("DEBUG: %s\n", str);
  146. }
  147. esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate)
  148. {
  149. esp_err_t err = uart_set_baudrate(s_uart_port, baudrate);
  150. return (err == ESP_OK) ? ESP_LOADER_SUCCESS : ESP_LOADER_ERROR_FAIL;
  151. }