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