Skip to content

Commit 4f9beb5

Browse files
committed
[bsp][qemu-vexpress-a9] Adapter driver for serial port v2 ([#10176](https://github.com/hydevcode/rt-thread/issues/10176))
1 parent 55428e4 commit 4f9beb5

File tree

2 files changed

+164
-27
lines changed

2 files changed

+164
-27
lines changed

bsp/qemu-vexpress-a9/drivers/Kconfig

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,74 @@ menuconfig BSP_USING_UART
1616
default y
1717
select RT_USING_SERIAL
1818
if BSP_USING_UART
19-
config BSP_USING_UART0
20-
bool "Enable UART0"
21-
select RT_USING_UART0
19+
menuconfig BSP_USING_UART0
20+
bool "Enable UART0 (Debugger)"
2221
default y
23-
config BSP_USING_UART1
22+
if BSP_USING_UART0
23+
config BSP_UART0_RX_BUFSIZE
24+
int "Set UART0 RX buffer size"
25+
range 64 65535
26+
depends on RT_USING_SERIAL_V2
27+
default 10240
28+
29+
config BSP_UART0_TX_BUFSIZE
30+
int "Set UART0 TX buffer size"
31+
range 0 65535
32+
depends on RT_USING_SERIAL_V2
33+
default 0
34+
endif
35+
36+
menuconfig BSP_USING_UART1
2437
bool "Enable UART1"
25-
select RT_USING_UART1
2638
default n
27-
config BSP_USING_UART2
39+
if BSP_USING_UART1
40+
config BSP_UART1_RX_BUFSIZE
41+
int "Set UART1 RX buffer size"
42+
range 64 65535
43+
depends on RT_USING_SERIAL_V2
44+
default 10240
45+
46+
config BSP_UART1_TX_BUFSIZE
47+
int "Set UART1 TX buffer size"
48+
range 0 65535
49+
depends on RT_USING_SERIAL_V2
50+
default 0
51+
endif
52+
53+
menuconfig BSP_USING_UART2
2854
bool "Enable UART2"
29-
select RT_USING_UART2
3055
default n
31-
config BSP_USING_UART3
56+
if BSP_USING_UART2
57+
config BSP_UART2_RX_BUFSIZE
58+
int "Set UART2 RX buffer size"
59+
range 64 65535
60+
depends on RT_USING_SERIAL_V2
61+
default 10240
62+
63+
config BSP_UART2_TX_BUFSIZE
64+
int "Set UART2 TX buffer size"
65+
range 0 65535
66+
depends on RT_USING_SERIAL_V2
67+
default 0
68+
endif
69+
70+
menuconfig BSP_USING_UART3
3271
bool "Enable UART3"
33-
select RT_USING_UART3
3472
default n
73+
if BSP_USING_UART3
74+
config BSP_UART3_RX_BUFSIZE
75+
int "Set UART3 RX buffer size"
76+
range 64 65535
77+
depends on RT_USING_SERIAL_V2
78+
default 10240
79+
80+
config BSP_UART3_TX_BUFSIZE
81+
int "Set UART3 TX buffer size"
82+
range 0 65535
83+
depends on RT_USING_SERIAL_V2
84+
default 0
85+
endif
86+
3587
endif
3688

3789
config BSP_USING_LVGL

bsp/qemu-vexpress-a9/drivers/drv_uart_v2.c

Lines changed: 103 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,35 @@ struct hw_uart_device
3636
#define UART_CR(base) __REG32(base + 0x30)
3737
#define UART_IMSC(base) __REG32(base + 0x38)
3838
#define UART_ICR(base) __REG32(base + 0x44)
39+
#define UART_LCR_H(base) __REG32(base + 0x2C)
40+
#define UART_DMACR(base) __REG32(base + 0x48)
41+
#define UART_DMACR(base) __REG32(base + 0x48)
42+
#define UART_IFLS(base) __REG32(base + 0x34)
43+
44+
/* test */
45+
#define UART_TCR(base) __REG32(base + 0x80)
46+
#define UART_ITOP(base) __REG32(base + 0x88)
47+
48+
#define UARTITOP_RXINTR 0x400
49+
50+
/* fifo */
51+
#define UARTLCR_H_FEN 0x10
3952

4053
#define UARTFR_RXFE 0x10
4154
#define UARTFR_TXFF 0x20
42-
#define UARTIMSC_RXIM 0x10
43-
#define UARTIMSC_TXIM 0x20
55+
#define UARTFR_RXFF 0x40
56+
#define UARTFR_TXFE 0x80
57+
#define UARTFR_BUSY 0x08
58+
59+
4460
#define UARTICR_RXIC 0x10
4561
#define UARTICR_TXIC 0x20
4662

63+
64+
#define UARTIMSC_RXIM 0x10
65+
#define UARTIMSC_RTIM 0x40
66+
#define UARTIMSC_TXIM 0x20
67+
4768
#if defined(BSP_USING_UART0)
4869
struct rt_serial_device serial0;
4970
#endif
@@ -105,17 +126,29 @@ static struct hw_uart_device _uart_device[] = {
105126
*
106127
* @param serial Serial device
107128
*/
129+
108130
static void rt_hw_uart_isr(int irqno, void *param)
109131
{
110132
struct rt_serial_device *serial = (struct rt_serial_device *)param;
111-
struct hw_uart_device *uart;
112133
RT_ASSERT(serial != RT_NULL);
134+
struct hw_uart_device *uart;
113135
uart = (struct hw_uart_device *)serial->parent.user_data;
114-
struct rt_serial_rx_fifo *rx_fifo;
115-
rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
116-
RT_ASSERT(rx_fifo != RT_NULL);
117-
rt_ringbuffer_putchar(&(rx_fifo->rb), UART_DR(uart->hw_base));
118-
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
136+
RT_ASSERT(uart != RT_NULL);
137+
138+
if(!(UART_FR(uart->hw_base) & UARTFR_RXFE) && (UART_IMSC(uart->hw_base) & UARTIMSC_RXIM))
139+
{
140+
struct rt_serial_rx_fifo *rx_fifo;
141+
rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
142+
RT_ASSERT(rx_fifo != RT_NULL);
143+
char rec_ch=UART_DR(uart->hw_base)& 0xff;
144+
rt_ringbuffer_putchar(&(rx_fifo->rb), rec_ch);
145+
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
146+
}
147+
else if((UART_IMSC(uart->hw_base) & UARTIMSC_TXIM))
148+
{
149+
UART_ICR(uart->hw_base)|= UARTICR_TXIC;
150+
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
151+
}
119152
}
120153

121154
static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
@@ -194,7 +227,6 @@ static int uart_putc(struct rt_serial_device *serial, char ch)
194227
RT_ASSERT(serial != RT_NULL);
195228

196229
uart = (struct hw_uart_device *)serial->parent.user_data;
197-
198230
while (UART_FR(uart->hw_base) & UARTFR_TXFF);
199231
UART_DR(uart->hw_base) = ch;
200232

@@ -207,6 +239,7 @@ static int uart_getc(struct rt_serial_device *serial)
207239
struct hw_uart_device *uart;
208240

209241
RT_ASSERT(serial != RT_NULL);
242+
210243
uart = (struct hw_uart_device *)serial->parent.user_data;
211244

212245
ch = -1;
@@ -218,18 +251,37 @@ static int uart_getc(struct rt_serial_device *serial)
218251
return ch;
219252
}
220253
static rt_ssize_t uart_transmit(struct rt_serial_device *serial,
221-
rt_uint8_t *buf,
222-
rt_size_t size,
223-
rt_uint32_t tx_flag)
254+
rt_uint8_t *buf,
255+
rt_size_t size,
256+
rt_uint32_t tx_flag)
224257
{
225258
RT_ASSERT(serial != RT_NULL);
226259
RT_ASSERT(buf != RT_NULL);
227-
228260
RT_ASSERT(size);
261+
uint32_t fifo_size = 0, tx_size = 0;
262+
struct hw_uart_device *uart = (struct hw_uart_device *)serial->parent.user_data;
263+
struct rt_serial_tx_fifo *tx_fifo;
264+
tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
265+
uint8_t ch = 0;
266+
RT_ASSERT(tx_fifo != RT_NULL);
267+
268+
if (size > 0)
269+
{
270+
if (UART_IMSC(uart->hw_base) & UARTIMSC_TXIM)
271+
{
272+
UART_IMSC(uart->hw_base) &= ~UARTIMSC_TXIM;
273+
if(rt_ringbuffer_getchar(&tx_fifo->rb, &ch))
274+
{
275+
while (UART_FR(uart->hw_base) & UARTFR_TXFF);
276+
UART_DR(uart->hw_base) = ch;
277+
}
278+
UART_IMSC(uart->hw_base) |= UARTIMSC_TXIM;
279+
}
280+
}
229281

230-
uart_control(serial, RT_DEVICE_CTRL_SET_INT, (void *)tx_flag);
231282
return size;
232283
}
284+
233285
static const struct rt_uart_ops _uart_ops = {
234286
.configure = uart_configure,
235287
.control = uart_control,
@@ -238,12 +290,43 @@ static const struct rt_uart_ops _uart_ops = {
238290
.transmit = uart_transmit
239291
};
240292

293+
static int uart_config(void)
294+
{
295+
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
296+
297+
#ifdef BSP_USING_UART0
298+
_uart_device[0].serial->config = config;
299+
_uart_device[0].serial->config.rx_bufsz = BSP_UART0_RX_BUFSIZE;
300+
_uart_device[0].serial->config.tx_bufsz = BSP_UART0_TX_BUFSIZE;
301+
#endif /* BSP_USING_UART0 */
302+
303+
#ifdef BSP_USING_UART1
304+
_uart_device[1].serial->config = config;
305+
_uart_device[1].serial->config.rx_bufsz = BSP_UART1_RX_BUFSIZE;
306+
_uart_device[1].serial->config.tx_bufsz = BSP_UART1_TX_BUFSIZE;
307+
#endif /* BSP_USING_UART1 */
308+
309+
#ifdef BSP_USING_UART2
310+
_uart_device[2].serial->config = config;
311+
_uart_device[2].serial->config.rx_bufsz = BSP_UART2_RX_BUFSIZE;
312+
_uart_device[2].serial->config.tx_bufsz = BSP_UART2_TX_BUFSIZE;
313+
#endif /* BSP_USING_UART2 */
314+
315+
#ifdef BSP_USING_UART3
316+
_uart_device[3].serial->config = config;
317+
_uart_device[3].serial->config.rx_bufsz = BSP_UART3_RX_BUFSIZE;
318+
_uart_device[3].serial->config.tx_bufsz = BSP_UART3_TX_BUFSIZE;
319+
#endif /* BSP_USING_UART3 */
320+
321+
return RT_EOK;
322+
}
323+
241324
int rt_hw_uart_init(void)
242325
{
243326

244327
rt_err_t err = RT_EOK;
245328

246-
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
329+
uart_config();
247330

248331
for (uint32_t i = 0; i < sizeof(_uart_device) / sizeof(_uart_device[0]); i++)
249332
{
@@ -253,9 +336,6 @@ int rt_hw_uart_init(void)
253336
#endif
254337

255338
_uart_device[i].serial->ops = &_uart_ops;
256-
_uart_device[i].serial->config = config;
257-
_uart_device[i].serial->config.rx_bufsz = 64;
258-
_uart_device[i].serial->config.tx_bufsz = 0;
259339
/* register UART device */
260340
err = rt_hw_serial_register(_uart_device[i].serial,
261341
_uart_device[i].device_name,
@@ -264,6 +344,11 @@ int rt_hw_uart_init(void)
264344
rt_hw_interrupt_install(_uart_device[i].irqno, rt_hw_uart_isr, _uart_device[i].serial, _uart_device[i].device_name);
265345
/* enable Rx and Tx of UART */
266346
UART_CR(_uart_device[i].hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
347+
348+
/* Fifo */
349+
UART_LCR_H(_uart_device[i].hw_base) =(1 << 4);
350+
/* UART_IFLS(_uart_device[i].hw_base) = (0 <<3 ) | (0 <<4 )| (1 <<5 ); */
351+
/* UART_DMACR(_uart_device[i].hw_base) = (1<<0); */
267352
}
268353

269354
return err;

0 commit comments

Comments
 (0)