stm32_port.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <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. // #define SERIAL_DEBUG_ENABLE
  22. static UART_HandleTypeDef *uart;
  23. static GPIO_TypeDef* gpio_port_io0, *gpio_port_rst;
  24. static uint16_t gpio_num_io0, gpio_num_rst;
  25. #ifdef SERIAL_DEBUG_ENABLE
  26. static void dec_to_hex_str(const uint8_t dec, uint8_t hex_str[3])
  27. {
  28. static const uint8_t dec_to_hex[] = {
  29. '0', '1', '2', '3', '4', '5', '6', '7',
  30. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  31. };
  32. hex_str[0] = dec_to_hex[(dec >> 4)];
  33. hex_str[1] = dec_to_hex[(dec & 0xF)];
  34. hex_str[2] = '\0';
  35. }
  36. static void serial_debug_print(const uint8_t *data, uint16_t size, bool write)
  37. {
  38. static bool write_prev = false;
  39. uint8_t hex_str[3];
  40. if(write_prev != write) {
  41. write_prev = write;
  42. printf("\n--- %s ---\n", write ? "WRITE" : "READ");
  43. }
  44. for(uint32_t i = 0; i < size; i++) {
  45. dec_to_hex_str(data[i], hex_str);
  46. printf("%s ", hex_str);
  47. }
  48. }
  49. #else
  50. static void serial_debug_print(const uint8_t *data, uint16_t size, bool write) { }
  51. #endif
  52. static uint32_t s_time_end;
  53. esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size, uint32_t timeout)
  54. {
  55. serial_debug_print(data, size, true);
  56. HAL_StatusTypeDef err = HAL_UART_Transmit(uart, (uint8_t *)data, size, timeout);
  57. if (err == HAL_OK) {
  58. return ESP_LOADER_SUCCESS;
  59. } else if (err == HAL_TIMEOUT) {
  60. return ESP_LOADER_ERROR_TIMEOUT;
  61. } else {
  62. return ESP_LOADER_ERROR_FAIL;
  63. }
  64. }
  65. esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
  66. {
  67. memset(data, 0x22, size);
  68. HAL_StatusTypeDef err = HAL_UART_Receive(uart, data, size, timeout);
  69. serial_debug_print(data, size, false);
  70. if (err == HAL_OK) {
  71. return ESP_LOADER_SUCCESS;
  72. } else if (err == HAL_TIMEOUT) {
  73. return ESP_LOADER_ERROR_TIMEOUT;
  74. } else {
  75. return ESP_LOADER_ERROR_FAIL;
  76. }
  77. }
  78. void loader_port_stm32_init(loader_stm32_config_t *config)
  79. {
  80. uart = config->huart;
  81. gpio_port_io0 = config->port_io0;
  82. gpio_port_rst = config->port_rst;
  83. gpio_num_io0 = config->pin_num_io0;
  84. gpio_num_rst = config->pin_num_rst;
  85. }
  86. // Set GPIO0 LOW, then
  87. // assert reset pin for 100 milliseconds.
  88. void loader_port_enter_bootloader(void)
  89. {
  90. HAL_GPIO_WritePin(gpio_port_rst, gpio_num_io0, GPIO_PIN_RESET);
  91. loader_port_reset_target();
  92. HAL_Delay(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
  93. HAL_GPIO_WritePin(gpio_port_io0, gpio_num_io0, GPIO_PIN_SET);
  94. }
  95. void loader_port_reset_target(void)
  96. {
  97. HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, GPIO_PIN_RESET);
  98. HAL_Delay(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
  99. HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, GPIO_PIN_SET);
  100. }
  101. void loader_port_delay_ms(uint32_t ms)
  102. {
  103. HAL_Delay(ms);
  104. }
  105. void loader_port_start_timer(uint32_t ms)
  106. {
  107. s_time_end = HAL_GetTick() + ms;
  108. }
  109. uint32_t loader_port_remaining_time(void)
  110. {
  111. int32_t remaining = s_time_end - HAL_GetTick();
  112. return (remaining > 0) ? (uint32_t)remaining : 0;
  113. }
  114. void loader_port_debug_print(const char *str)
  115. {
  116. printf("DEBUG: %s", str);
  117. }
  118. esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate)
  119. {
  120. uart->Init.BaudRate = baudrate;
  121. if( HAL_UART_Init(uart) != HAL_OK ) {
  122. return ESP_LOADER_ERROR_FAIL;
  123. }
  124. return ESP_LOADER_SUCCESS;
  125. }