基质喷涂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.6 KiB

  1. //
  2. // Created by iflyt on 2025/2/27.
  3. //
  4. #include "uart_dbg.h"
  5. #include "bsp.h"
  6. #include "main.h"
  7. UART_HandleTypeDef huart_dbg;
  8. // 初始化调试串口
  9. void DBG_UART_Init(void)
  10. {
  11. // 使能 GPIO 和 USART 时钟
  12. __HAL_RCC_GPIOA_CLK_ENABLE();
  13. __HAL_RCC_USART1_CLK_ENABLE();
  14. // 配置 GPIO 为复用功能
  15. GPIO_InitTypeDef GPIO_InitStruct = {0};
  16. GPIO_InitStruct.Pin = DBG_UART_TX_PIN | DBG_UART_RX_PIN;
  17. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  18. GPIO_InitStruct.Pull = GPIO_NOPULL;
  19. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  20. GPIO_InitStruct.Alternate = DBG_UART_GPIO_AF;
  21. HAL_GPIO_Init(DBG_UART_GPIO_PORT, &GPIO_InitStruct);
  22. // 配置 USART
  23. huart_dbg.Instance = DBG_UART;
  24. huart_dbg.Init.BaudRate = DBG_UART_BAUDRATE;
  25. huart_dbg.Init.WordLength = UART_WORDLENGTH_8B;
  26. huart_dbg.Init.StopBits = UART_STOPBITS_1;
  27. huart_dbg.Init.Parity = UART_PARITY_NONE;
  28. huart_dbg.Init.Mode = UART_MODE_TX_RX;
  29. huart_dbg.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  30. huart_dbg.Init.OverSampling = UART_OVERSAMPLING_16;
  31. if (HAL_UART_Init(&huart_dbg) != HAL_OK)
  32. {
  33. Error_Handler();
  34. }
  35. }
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. // 重定向 write 函数,实现 printf 重映射
  40. __attribute__((used)) int _write(int file, char *ptr, int len)
  41. {
  42. #if 1
  43. HAL_UART_Transmit(&huart_dbg, (uint8_t *)ptr, len, HAL_MAX_DELAY);
  44. return len;
  45. #else
  46. int i;
  47. for (i = 0; i < len; i++) {
  48. uint8_t c = *ptr++;
  49. HAL_UART_Transmit(&huart_dbg, &c, 1, 100);
  50. }
  51. return len;
  52. #endif
  53. }
  54. #ifdef __cplusplus
  55. }
  56. #endif