stm32_port.c 4.2 KB

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