drv_console.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-30 Meco Man First version
  9. */
  10. #include <board.h>
  11. #include <rtthread.h>
  12. #include <drv_common.h>
  13. static UART_HandleTypeDef console_uart;
  14. void rt_hw_console_init(void)
  15. {
  16. #ifdef USART1
  17. if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart1") == 0)
  18. {
  19. console_uart.Instance = USART1;
  20. }
  21. #endif /* USART1 */
  22. #ifdef USART2
  23. else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart2") == 0)
  24. {
  25. console_uart.Instance = USART2;
  26. }
  27. #endif /* USART2 */
  28. #ifdef USART3
  29. else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart3") == 0)
  30. {
  31. console_uart.Instance = USART3;
  32. }
  33. #endif /* USART3 */
  34. #ifdef UART4
  35. else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart4") == 0)
  36. {
  37. console_uart.Instance = UART4;
  38. }
  39. #endif /* UART4 */
  40. #ifdef UART5
  41. else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart5") == 0)
  42. {
  43. console_uart.Instance = UART5;
  44. }
  45. #endif /* UART5 */
  46. #ifdef USART6
  47. else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart6") == 0)
  48. {
  49. console_uart.Instance = USART6;
  50. }
  51. #endif /* USART6 */
  52. #ifdef UART7
  53. else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart7") == 0)
  54. {
  55. console_uart.Instance = UART7;
  56. }
  57. #endif /* USART7 */
  58. #ifdef UART8
  59. else if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, "uart8") == 0)
  60. {
  61. console_uart.Instance = UART8;
  62. }
  63. #endif /* USART8 */
  64. else
  65. {
  66. RT_ASSERT(0);
  67. }
  68. console_uart.Init.BaudRate = 115200;
  69. console_uart.Init.WordLength = UART_WORDLENGTH_8B;
  70. console_uart.Init.StopBits = UART_STOPBITS_1;
  71. console_uart.Init.Parity = UART_PARITY_NONE;
  72. console_uart.Init.Mode = UART_MODE_TX_RX;
  73. console_uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  74. if (HAL_UART_Init(&console_uart) != HAL_OK)
  75. {
  76. Error_Handler();
  77. }
  78. }
  79. void rt_hw_console_output(const char *str)
  80. {
  81. rt_size_t i = 0, size = 0;
  82. char a = '\r';
  83. __HAL_UNLOCK(&console_uart);
  84. size = rt_strlen(str);
  85. for (i = 0; i < size; i++)
  86. {
  87. if (*(str + i) == '\n')
  88. {
  89. HAL_UART_Transmit(&console_uart, (uint8_t *)&a, 1, 1);
  90. }
  91. HAL_UART_Transmit(&console_uart, (uint8_t *)(str + i), 1, 1);
  92. }
  93. }
  94. char rt_hw_console_getchar(void)
  95. {
  96. int ch = -1;
  97. if (HAL_UART_Receive(&console_uart, (uint8_t *)&ch, 1, 0) != HAL_OK)
  98. {
  99. ch = -1;
  100. rt_thread_mdelay(10);
  101. }
  102. return ch;
  103. }