stm32_port.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright 2020-2023 Espressif Systems (Shanghai) CO 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 <string.h>
  16. #include <unistd.h>
  17. #include <stdint.h>
  18. #include <sys/param.h>
  19. #include <stdio.h>
  20. #include "stm32_port.h"
  21. static UART_HandleTypeDef *uart;
  22. static GPIO_TypeDef* gpio_port_io0, *gpio_port_rst;
  23. static uint16_t gpio_num_io0, gpio_num_rst;
  24. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  25. static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write)
  26. {
  27. static bool write_prev = false;
  28. if (write_prev != write) {
  29. write_prev = write;
  30. printf("\n--- %s ---\n", write ? "WRITE" : "READ");
  31. }
  32. for (uint32_t i = 0; i < size; i++) {
  33. printf("%02x ", data[i]);
  34. }
  35. }
  36. #endif
  37. static uint32_t s_time_end;
  38. esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout)
  39. {
  40. HAL_StatusTypeDef err = HAL_UART_Transmit(uart, (uint8_t *)data, size, timeout);
  41. if (err == HAL_OK) {
  42. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  43. transfer_debug_print(data, size, true);
  44. #endif
  45. return ESP_LOADER_SUCCESS;
  46. } else if (err == HAL_TIMEOUT) {
  47. return ESP_LOADER_ERROR_TIMEOUT;
  48. } else {
  49. return ESP_LOADER_ERROR_FAIL;
  50. }
  51. }
  52. esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
  53. {
  54. HAL_StatusTypeDef err = HAL_UART_Receive(uart, data, size, timeout);
  55. if (err == HAL_OK) {
  56. #ifdef SERIAL_FLASHER_DEBUG_TRACE
  57. transfer_debug_print(data, size, false);
  58. #endif
  59. return ESP_LOADER_SUCCESS;
  60. } else if (err == HAL_TIMEOUT) {
  61. return ESP_LOADER_ERROR_TIMEOUT;
  62. } else {
  63. return ESP_LOADER_ERROR_FAIL;
  64. }
  65. }
  66. void loader_port_stm32_init(loader_stm32_config_t *config)
  67. {
  68. uart = config->huart;
  69. gpio_port_io0 = config->port_io0;
  70. gpio_port_rst = config->port_rst;
  71. gpio_num_io0 = config->pin_num_io0;
  72. gpio_num_rst = config->pin_num_rst;
  73. }
  74. // Set GPIO0 LOW, then
  75. // assert reset pin for 100 milliseconds.
  76. void loader_port_enter_bootloader(void)
  77. {
  78. HAL_GPIO_WritePin(gpio_port_io0, gpio_num_io0, GPIO_PIN_RESET);
  79. loader_port_reset_target();
  80. HAL_Delay(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
  81. HAL_GPIO_WritePin(gpio_port_io0, gpio_num_io0, GPIO_PIN_SET);
  82. }
  83. void loader_port_reset_target(void)
  84. {
  85. HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, GPIO_PIN_RESET);
  86. HAL_Delay(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
  87. HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, GPIO_PIN_SET);
  88. }
  89. void loader_port_delay_ms(uint32_t ms)
  90. {
  91. HAL_Delay(ms);
  92. }
  93. void loader_port_start_timer(uint32_t ms)
  94. {
  95. s_time_end = HAL_GetTick() + ms;
  96. }
  97. uint32_t loader_port_remaining_time(void)
  98. {
  99. int32_t remaining = s_time_end - HAL_GetTick();
  100. return (remaining > 0) ? (uint32_t)remaining : 0;
  101. }
  102. void loader_port_debug_print(const char *str)
  103. {
  104. printf("DEBUG: %s", str);
  105. }
  106. esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
  107. {
  108. uart->Init.BaudRate = baudrate;
  109. if( HAL_UART_Init(uart) != HAL_OK ) {
  110. return ESP_LOADER_ERROR_FAIL;
  111. }
  112. return ESP_LOADER_SUCCESS;
  113. }