1 /*
2 * Copyright (c) 2006-2022, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2018-05-08 zhuangwei the first version
9 * 2021-02-02 michael5hzg@gmail.com adapt to ls1b
10 */
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include <rthw.h>
14 #include "drv_uart.h"
15 #include "ls1b_pin.h"
16 #include "ls1b_uart.h"
17
18 /* STM32 uart driver */
19 struct rt_uart_ls1b
20 {
21 ls1b_uart_t UARTx;
22 rt_uint32_t IRQ;
23 };
24
ls1b_uart_configure(struct rt_serial_device * serial,struct serial_configure * cfg)25 static rt_err_t ls1b_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
26 {
27 struct rt_uart_ls1b *uart_dev = RT_NULL;
28 ls1b_uart_info_t uart_info = {0};
29
30 RT_ASSERT(serial != RT_NULL);
31 RT_ASSERT(cfg != RT_NULL);
32
33 uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
34
35 // 初始化串口
36 uart_info.UARTx = uart_dev->UARTx;
37 uart_info.baudrate = cfg->baud_rate;
38 uart_info.rx_enable = TRUE;
39 uart_init(&uart_info);
40
41 return RT_EOK;
42
43 }
44
ls1b_uart_control(struct rt_serial_device * serial,int cmd,void * arg)45 static rt_err_t ls1b_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
46 {
47 struct rt_uart_ls1b *uart_dev = RT_NULL;
48
49 RT_ASSERT(serial != RT_NULL);
50 uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
51
52 switch (cmd)
53 {
54 case RT_DEVICE_CTRL_CLR_INT: /* disable rx irq */
55 rt_hw_interrupt_mask(uart_dev->IRQ);
56 break;
57
58 case RT_DEVICE_CTRL_SET_INT: /* enable rx irq */
59 rt_hw_interrupt_umask(uart_dev->IRQ);
60 break;
61
62 default:
63 break;
64 }
65
66 return RT_EOK;
67
68 }
69
ls1b_uart_putc(struct rt_serial_device * serial,char c)70 static int ls1b_uart_putc(struct rt_serial_device *serial, char c)
71 {
72 struct rt_uart_ls1b *uart_dev = RT_NULL;
73
74 RT_ASSERT(serial != RT_NULL);
75
76 uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
77 uart_putc(uart_dev->UARTx, c);
78
79 return 1;
80 }
81
ls1b_uart_getc(struct rt_serial_device * serial)82 static int ls1b_uart_getc(struct rt_serial_device *serial)
83 {
84 struct rt_uart_ls1b *uart_dev = RT_NULL;
85
86 RT_ASSERT(serial != RT_NULL);
87
88 uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
89 void *uart_base = uart_get_base(uart_dev->UARTx);
90
91 if (LSR_RXRDY & reg_read_8(uart_base + LS1B_UART_LSR_OFFSET))
92 {
93 return reg_read_8(uart_base + LS1B_UART_DAT_OFFSET);
94 }
95
96 return -1;
97 }
98
99 /* UART interrupt handler */
uart_irq_handler(int vector,void * param)100 static void uart_irq_handler(int vector, void *param)
101 {
102 struct rt_serial_device *serial = (struct rt_serial_device *)param;
103 struct rt_uart_ls1b *uart_dev = RT_NULL;
104
105 RT_ASSERT(serial != RT_NULL);
106
107 uart_dev = (struct rt_uart_ls1b *)serial->parent.user_data;
108 void *uart_base = uart_get_base(uart_dev->UARTx);
109 unsigned char iir = reg_read_8(uart_base + LS1B_UART_IIR_OFFSET);
110
111 // 判断是否为接收超时或接收到有效数据
112 if ((IIR_RXTOUT & iir) || (IIR_RXRDY & iir))
113 {
114 rt_interrupt_enter();
115 rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
116 rt_interrupt_leave();
117 }
118
119 }
120
121 static const struct rt_uart_ops ls1b_uart_ops =
122 {
123 ls1b_uart_configure,
124 ls1b_uart_control,
125 ls1b_uart_putc,
126 ls1b_uart_getc,
127 };
128
129
130 #if defined(RT_USING_UART1)
131 struct rt_uart_ls1b uart1 =
132 {
133 LS1B_UART1,
134 LS1B_UART1_IRQ,
135 };
136 struct rt_serial_device serial1;
137 #endif /* RT_USING_UART1 */
138
139 #if defined(RT_USING_UART2)
140 struct rt_uart_ls1b uart2 =
141 {
142 LS1B_UART2,
143 LS1B_UART2_IRQ,
144 };
145 struct rt_serial_device serial2;
146 #endif /* RT_USING_UART2 */
147
148
149 #if defined(RT_USING_UART3)
150 struct rt_uart_ls1b uart3 =
151 {
152 LS1B_UART3,
153 LS1B_UART3_IRQ,
154 };
155 struct rt_serial_device serial3;
156 #endif /* RT_USING_UART3 */
157
158 #if defined(RT_USING_UART4)
159 struct rt_uart_ls1b uart4 =
160 {
161 LS1B_UART4,
162 LS1B_UART4_IRQ,
163 };
164 struct rt_serial_device serial4;
165 #endif /* RT_USING_UART4 */
166
167 #if defined(RT_USING_UART5)
168 struct rt_uart_ls1b uart5 =
169 {
170 LS1B_UART5,
171 LS1B_UART5_IRQ,
172 };
173 struct rt_serial_device serial5;
174 #endif /* RT_USING_UART5 */
175
176
177
rt_hw_uart_init(void)178 void rt_hw_uart_init(void)
179 {
180 struct rt_uart_ls1b *uart;
181 struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
182
183 #ifdef RT_USING_UART5
184 uart = &uart5;
185
186 serial5.ops = &ls1b_uart_ops;
187 serial5.config = config;
188
189 rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial5, "UART5");
190
191 /* register UART5 device */
192 rt_hw_serial_register(&serial5,
193 "uart5",
194 //RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX,
195 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
196 uart);
197 #endif /* RT_USING_UART5 */
198
199
200 #ifdef RT_USING_UART2
201 uart = &uart2;
202
203 serial2.ops = &ls1b_uart_ops;
204 serial2.config = config;
205
206 pin_set_purpose(36, PIN_PURPOSE_OTHER);
207 pin_set_purpose(37, PIN_PURPOSE_OTHER);
208 pin_set_remap(36, PIN_REMAP_SECOND);
209 pin_set_remap(37, PIN_REMAP_SECOND);
210
211 rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial2, "UART2");
212
213 /* register UART2 device */
214 rt_hw_serial_register(&serial2,
215 "uart2",
216 //RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX,
217 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
218 uart);
219 #endif /* RT_USING_UART2 */
220
221 }
222
223