zephyr_port.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2022 KT-Elektronik, Klaucke und Partner GmbH
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "zephyr_port.h"
  17. #include <zephyr/drivers/uart.h>
  18. #include <zephyr/console/tty.h>
  19. static const struct device *uart_dev;
  20. static struct gpio_dt_spec enable_spec;
  21. static struct gpio_dt_spec boot_spec;
  22. static struct tty_serial tty;
  23. static char tty_rx_buf[CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE];
  24. static char tty_tx_buf[CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE];
  25. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  26. static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write)
  27. {
  28. static bool write_prev = false;
  29. if (write_prev != write) {
  30. write_prev = write;
  31. printk("\n--- %s ---\n", write ? "WRITE" : "READ");
  32. }
  33. for (uint32_t i = 0; i < size; i++) {
  34. printk("%02x ", data[i]);
  35. }
  36. }
  37. #endif
  38. esp_loader_error_t configure_tty()
  39. {
  40. if (tty_init(&tty, uart_dev) < 0 ||
  41. tty_set_rx_buf(&tty, tty_rx_buf, sizeof(tty_rx_buf)) < 0 ||
  42. tty_set_tx_buf(&tty, tty_tx_buf, sizeof(tty_tx_buf)) < 0) {
  43. return ESP_LOADER_ERROR_FAIL;
  44. }
  45. return ESP_LOADER_SUCCESS;
  46. }
  47. esp_loader_error_t loader_port_read(uint8_t *data, const uint16_t size, const uint32_t timeout)
  48. {
  49. if (!device_is_ready(uart_dev) || data == NULL || size == 0) {
  50. return ESP_LOADER_ERROR_FAIL;
  51. }
  52. ssize_t total_read = 0;
  53. ssize_t remaining = size;
  54. tty_set_rx_timeout(&tty, timeout);
  55. while (remaining > 0) {
  56. const uint16_t chunk_size = remaining < CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE ?
  57. remaining : CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE;
  58. ssize_t read = tty_read(&tty, &data[total_read], chunk_size);
  59. if (read < 0) {
  60. return ESP_LOADER_ERROR_TIMEOUT;
  61. }
  62. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  63. transfer_debug_print(data, read, false);
  64. #endif
  65. total_read += read;
  66. remaining -= read;
  67. }
  68. return ESP_LOADER_SUCCESS;
  69. }
  70. esp_loader_error_t loader_port_write(const uint8_t *data, const uint16_t size, const uint32_t timeout)
  71. {
  72. if (!device_is_ready(uart_dev) || data == NULL || size == 0) {
  73. return ESP_LOADER_ERROR_FAIL;
  74. }
  75. ssize_t total_written = 0;
  76. ssize_t remaining = size;
  77. tty_set_tx_timeout(&tty, timeout);
  78. while (remaining > 0) {
  79. const uint16_t chunk_size = remaining < CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE ?
  80. remaining : CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE;
  81. ssize_t written = tty_write(&tty, &data[total_written], chunk_size);
  82. if (written < 0) {
  83. return ESP_LOADER_ERROR_TIMEOUT;
  84. }
  85. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  86. transfer_debug_print(data, written, true);
  87. #endif
  88. total_written += written;
  89. remaining -= written;
  90. }
  91. return ESP_LOADER_SUCCESS;
  92. }
  93. esp_loader_error_t loader_port_zephyr_init(const loader_zephyr_config_t *config)
  94. {
  95. uart_dev = config->uart_dev;
  96. enable_spec = config->enable_spec;
  97. boot_spec = config->boot_spec;
  98. return configure_tty();
  99. }
  100. void loader_port_reset_target(void)
  101. {
  102. gpio_pin_set_dt(&enable_spec, false);
  103. loader_port_delay_ms(CONFIG_SERIAL_FLASHER_RESET_HOLD_TIME_MS);
  104. gpio_pin_set_dt(&enable_spec, true);
  105. }
  106. void loader_port_enter_bootloader(void)
  107. {
  108. gpio_pin_set_dt(&boot_spec, false);
  109. loader_port_reset_target();
  110. loader_port_delay_ms(CONFIG_SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
  111. gpio_pin_set_dt(&boot_spec, true);
  112. }
  113. void loader_port_delay_ms(uint32_t ms)
  114. {
  115. k_msleep(ms);
  116. }
  117. static uint64_t s_time_end;
  118. void loader_port_start_timer(uint32_t ms)
  119. {
  120. s_time_end = sys_timepoint_calc(Z_TIMEOUT_MS(ms)).tick;
  121. }
  122. uint32_t loader_port_remaining_time(void)
  123. {
  124. int64_t remaining = k_ticks_to_ms_floor64(s_time_end - k_uptime_ticks());
  125. return (remaining > 0) ? (uint32_t)remaining : 0;
  126. }
  127. esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
  128. {
  129. struct uart_config uart_config;
  130. if (!device_is_ready(uart_dev)) {
  131. return ESP_LOADER_ERROR_FAIL;
  132. }
  133. if (uart_config_get(uart_dev, &uart_config) != 0) {
  134. return ESP_LOADER_ERROR_FAIL;
  135. }
  136. uart_config.baudrate = baudrate;
  137. if (uart_configure(uart_dev, &uart_config) != 0) {
  138. return ESP_LOADER_ERROR_FAIL;
  139. }
  140. /* bitrate-change can require tty re-configuration */
  141. return configure_tty();
  142. }