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 * 2021-10-02 spaceman first version
9 */
10 #include <rtthread.h>
11 #include <rtdevice.h>
12 #include <board.h>
13 #include "drv_uart.h"
14
15 #ifdef RT_USING_SERIAL
16
17 //#define DRV_DEBUG
18 #define LOG_TAG "drv.uart"
19 #include <drv_log.h>
20
21 #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && \
22 !defined(BSP_USING_UART4)
23 #error "Please define at least one BSP_USING_UARTx"
24 /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
25 #endif
26
27
28 /* UART1 */
29 #define UART1_GPIO_TX GPIO_Pin_9
30 #define UART1_GPIO_RX GPIO_Pin_10
31 #define UART1_GPIO_AF GPIO_AF_UART_1
32 #define UART1_GPIO GPIOA
33
34 /* UART2 */
35 #define UART2_GPIO_TX GPIO_Pin_2
36 #define UART2_GPIO_RX GPIO_Pin_3
37 #define UART2_GPIO_AF GPIO_AF_UART_2345
38 #define UART2_GPIO GPIOA
39
40
tkm32_uart_configure(struct rt_serial_device * serial,struct serial_configure * cfg)41 static rt_err_t tkm32_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
42 {
43 struct tkm32_uart *uart;
44 UART_InitTypeDef UART_InitStructure;
45 RT_ASSERT(serial != RT_NULL);
46 RT_ASSERT(cfg != RT_NULL);
47 uart = (struct tkm32_uart *)serial->parent.user_data;
48 UART_InitStructure.UART_BaudRate = cfg->baud_rate;
49 if (cfg->data_bits == DATA_BITS_8)
50 UART_InitStructure.UART_WordLength = UART_WordLength_8b;
51 if (cfg->stop_bits == STOP_BITS_1)
52 UART_InitStructure.UART_StopBits = UART_StopBits_1;
53 else if (cfg->stop_bits == STOP_BITS_2)
54 UART_InitStructure.UART_StopBits = UART_StopBits_2;
55 UART_InitStructure.UART_Parity = UART_Parity_No;
56 UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
57 UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
58 UART_Init(uart->uart, &UART_InitStructure);
59 /* Enable UART */
60 UART_Cmd(uart->uart, ENABLE);
61 return RT_EOK;
62 }
63
tkm32_uart_control(struct rt_serial_device * serial,int cmd,void * arg)64 static rt_err_t tkm32_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
65 {
66 struct tkm32_uart *uart;
67 RT_ASSERT(serial != RT_NULL);
68 uart = (struct tkm32_uart *)serial->parent.user_data;
69 switch (cmd)
70 {
71 case RT_DEVICE_CTRL_CLR_INT:
72 /* disable rx irq */
73 NVIC_DisableIRQ(uart->irq);
74 UART_ITConfig(uart->uart, UART_IT_RXIEN, DISABLE);
75 break;
76 case RT_DEVICE_CTRL_SET_INT:
77 /* enable rx irq */
78 NVIC_EnableIRQ(uart->irq);
79 /* enable interrupt */
80 UART_ITConfig(uart->uart, UART_IT_RXIEN, ENABLE);
81 break;
82 }
83 return RT_EOK;
84 }
85
tkm32_uart_putc(struct rt_serial_device * serial,char c)86 static int tkm32_uart_putc(struct rt_serial_device *serial, char c)
87 {
88 struct tkm32_uart *uart;
89 RT_ASSERT(serial != RT_NULL);
90 uart = (struct tkm32_uart *)serial->parent.user_data;
91 while ((uart->uart->CSR & UART_IT_TXIEN) == 0);
92 uart->uart->TDR = (c & (uint16_t)0x00FF);
93 return 1;
94 }
95
tkm32_uart_getc(struct rt_serial_device * serial)96 static int tkm32_uart_getc(struct rt_serial_device *serial)
97 {
98 int ch;
99 struct tkm32_uart *uart;
100 RT_ASSERT(serial != RT_NULL);
101 uart = (struct tkm32_uart *)serial->parent.user_data;
102 ch = -1;
103 if (uart->uart->CSR & UART_FLAG_RXAVL)
104 {
105 ch = uart->uart->RDR & 0xff;
106 }
107 return ch;
108 }
109
110 static const struct rt_uart_ops tkm32_uart_ops =
111 {
112 tkm32_uart_configure,
113 tkm32_uart_control,
114 tkm32_uart_putc,
115 tkm32_uart_getc,
116 };
117
118 #if defined(BSP_USING_UART1)
119 /* UART1 device driver structure */
120 static struct tkm32_uart uart1;
121 struct rt_serial_device serial1;
UART1_IRQHandler(void)122 void UART1_IRQHandler(void)
123 {
124 struct tkm32_uart *uart;
125 uart = &uart1;
126 /* enter interrupt */
127 rt_interrupt_enter();
128 if (UART_GetITStatus(uart->uart, UART_IT_RXIEN) != RESET)
129 {
130 UART_ClearITPendingBit(uart->uart, UART_IT_RXIEN);
131 rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
132 }
133 if (UART_GetITStatus(uart->uart, UART_IT_TXIEN) != RESET)
134 {
135 /* clear interrupt */
136 UART_ClearITPendingBit(uart->uart, UART_IT_TXIEN);
137 }
138 /* leave interrupt */
139 rt_interrupt_leave();
140 }
141 #endif /* BSP_USING_UART1 */
142
143 #if defined(BSP_USING_UART2)
144 /* UART2 device driver structure */
145 static struct tkm32_uart uart2;
146 struct rt_serial_device serial2;
UART2_IRQHandler(void)147 void UART2_IRQHandler(void)
148 {
149 struct tkm32_uart *uart;
150 uart = &uart2;
151 /* enter interrupt */
152 rt_interrupt_enter();
153 if (UART_GetITStatus(uart->uart, UART_IT_RXIEN) != RESET)
154 {
155 UART_ClearITPendingBit(uart->uart, UART_IT_RXIEN);
156 rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
157 }
158 if (UART_GetITStatus(uart->uart, UART_IT_TXIEN) != RESET)
159 {
160 /* clear interrupt */
161 UART_ClearITPendingBit(uart->uart, UART_IT_TXIEN);
162 }
163 /* leave interrupt */
164 rt_interrupt_leave();
165 }
166 #endif /* BSP_USING_UART2 */
167
168 #ifdef BSP_USING_UART1
UART1PINconfigStepA(void)169 static void UART1PINconfigStepA(void)
170 {
171 /* Enable UART clock */
172 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
173 RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
174 }
175
UART1PINconfigStepB(void)176 static void UART1PINconfigStepB(void)
177 {
178
179 GPIO_InitTypeDef GPIO_InitStructure;
180 /* Configure USART Rx/tx PIN */
181 GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX;
182 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
183 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
184 GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
185 GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
186 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
187 GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
188 GPIO_PinAFConfig(UART1_GPIO, UART1_GPIO_TX | UART1_GPIO_RX, UART1_GPIO_AF);
189 UART_ClearITPendingBit(UART1, 0xFF);
190 }
191 #endif
192
193 #ifdef BSP_USING_UART2
UART2PINconfigStepA(void)194 static void UART2PINconfigStepA(void)
195 {
196 /* Enable UART clock */
197 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2, ENABLE);
198 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
199 }
200
UART2PINconfigStepB(void)201 static void UART2PINconfigStepB(void)
202 {
203 GPIO_InitTypeDef GPIO_InitStructure;
204 /* Configure USART Rx/tx PIN */
205 GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX;
206 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
207 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
208 GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
209 GPIO_InitStructure.GPIO_Pin = UART2_GPIO_RX;
210 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
211 GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
212 GPIO_PinAFConfig(UART2_GPIO, UART2_GPIO_TX | UART2_GPIO_RX, UART2_GPIO_AF);
213 UART_ClearITPendingBit(UART2, 0xFF);
214 }
215 #endif
216
rt_hw_uart_init(void)217 int rt_hw_uart_init(void)
218 {
219 struct tkm32_uart *uart;
220 struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
221 #ifdef BSP_USING_UART1
222 UART1PINconfigStepA();
223 uart = &uart1;
224 uart->uart = UART1;
225 uart->irq = UART1_IRQn;
226 config.baud_rate = BAUD_RATE_115200;
227 serial1.ops = &tkm32_uart_ops;
228 serial1.config = config;
229 /* register UART1 device */
230 rt_hw_serial_register(&serial1, "uart1",
231 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
232 uart);
233 UART1PINconfigStepB();
234 #endif /* BSP_USING_UART1 */
235
236 #ifdef BSP_USING_UART2
237 UART2PINconfigStepA();
238 uart = &uart2;
239 uart->uart = UART2;
240 uart->irq = UART2_IRQn;
241 config.baud_rate = BAUD_RATE_115200;
242 serial2.ops = &tkm32_uart_ops;
243 serial2.config = config;
244 /* register UART2 device */
245 rt_hw_serial_register(&serial2, "uart2",
246 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
247 uart);
248 UART2PINconfigStepB();
249 #endif /* BSP_USING_UART2 */
250 return 0;
251 }
252 INIT_BOARD_EXPORT(rt_hw_uart_init);
253
254 #endif /* RT_USING_SERIAL */
255