1 /*
2 * Copyright (c) 2020, Shenzhen Academy of Aerospace Technology
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2020-10-16 Dystopia the first version
9 */
10
11 #include <rthw.h>
12 #include <rtthread.h>
13 #include <rtdevice.h>
14
15 #include <board.h>
16 #include <interrupt.h>
17 #include "uart.h"
18 #include "uart_reg.h"
19
20 struct bm3803_uart
21 {
22 void *uart_base;
23 int irq;
24 };
25
bm3803_uart_isr(int tt,void * param)26 static void bm3803_uart_isr(int tt, void *param)
27 {
28 struct bm3803_uart *uart;
29 struct rt_serial_device *serial;
30 struct uart_reg *uart_base;
31
32 serial = (struct rt_serial_device *)param;
33 uart = (struct bm3803_uart *)serial->parent.user_data;
34 uart_base = uart->uart_base;
35
36 if (uart_base->uartstatus & 0x1)
37 {
38 rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
39 }
40 }
41
42 #define NOT_IMPLEMENTED() RT_ASSERT(0)
43
bm3803_configure(struct rt_serial_device * serial,struct serial_configure * cfg)44 static rt_err_t bm3803_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
45 {
46 struct bm3803_uart *uart;
47 struct uart_reg *uart_base;
48
49 RT_ASSERT(serial != RT_NULL);
50 uart = (struct bm3803_uart *)serial->parent.user_data;
51 RT_ASSERT(uart);
52 uart_base = uart->uart_base;
53
54 if (cfg->baud_rate == BAUD_RATE_115200)
55 {
56 uart_base->uartscaler = ((((CPU_FREQ * 10) / (8 * 115200)) - 5) / 10);
57 }
58 else if (cfg->baud_rate == BAUD_RATE_9600)
59 {
60 uart_base->uartscaler = ((((CPU_FREQ * 10) / (8 * 9600)) - 5) / 10);
61 }
62 else
63 {
64 NOT_IMPLEMENTED();
65 }
66
67 uart_base->uartctrl |= 0x3;
68
69 return RT_EOK;
70 }
71
bm3803_control(struct rt_serial_device * serial,int cmd,void * arg)72 static rt_err_t bm3803_control(struct rt_serial_device *serial, int cmd, void *arg)
73 {
74 struct bm3803_uart *uart;
75
76 RT_ASSERT(serial != RT_NULL);
77 uart = (struct bm3803_uart *)serial->parent.user_data;
78
79 switch (cmd)
80 {
81 case RT_DEVICE_CTRL_CLR_INT:
82 /* disable rx irq */
83 rt_hw_interrupt_mask(uart->irq);
84 break;
85 case RT_DEVICE_CTRL_SET_INT:
86 /* enable rx irq */
87 rt_hw_interrupt_umask(uart->irq);
88 break;
89 }
90
91 return RT_EOK;
92 }
93
bm3803_putc(struct rt_serial_device * serial,char c)94 static int bm3803_putc(struct rt_serial_device *serial, char c)
95 {
96 struct bm3803_uart *uart;
97 struct uart_reg *uart_base;
98
99 RT_ASSERT(serial != RT_NULL);
100 uart = (struct bm3803_uart *)serial->parent.user_data;
101 uart_base = uart->uart_base;
102
103 while (!(uart_base->uartstatus & 0x4));
104 uart_base->uartdata = c;
105
106 return 1;
107 }
108
bm3803_getc(struct rt_serial_device * serial)109 static int bm3803_getc(struct rt_serial_device *serial)
110 {
111 int ch;
112 struct bm3803_uart *uart;
113 struct uart_reg *uart_base;
114
115 RT_ASSERT(serial != RT_NULL);
116 uart = (struct bm3803_uart *)serial->parent.user_data;
117 uart_base = uart->uart_base;
118
119 ch = -1;
120 if (uart_base->uartstatus & 0x1)
121 {
122 ch = uart_base->uartdata;
123 }
124
125 return ch;
126 }
127
128 static const struct rt_uart_ops bm3803_uart_ops =
129 {
130 bm3803_configure,
131 bm3803_control,
132 bm3803_putc,
133 bm3803_getc,
134 };
135
136 /* UART device driver structure */
137 #ifdef RT_USING_UART1
138 struct bm3803_uart uart1 =
139 {
140 (void *)UART1_BASE,
141 UART1_TT,
142 };
143 struct rt_serial_device serial1;
144 #endif
145
rt_hw_serial_init(void)146 int rt_hw_serial_init(void)
147 {
148 struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
149
150 #ifdef RT_USING_UART1
151 volatile struct lregs *regs = (struct lregs *)PREGS;
152 serial1.ops = &bm3803_uart_ops;
153 serial1.config = config;
154 /* configure gpio direction */
155 regs->piodir |= (1 << 15);
156 regs->piodir &= ~(1 << 14);
157 /* enable RX interrupt */
158 regs->uartctrl1 = 0;
159 regs->uartctrl1 |= 0x4;
160 /* install ISR */
161 rt_hw_interrupt_install(uart1.irq, bm3803_uart_isr, &serial1, "uart1");
162 rt_hw_interrupt_mask(uart1.irq);
163 /* register UART1 device */
164 rt_hw_serial_register(&serial1, "uart1",
165 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
166 &uart1);
167 #endif
168 return 0;
169 }
170 INIT_BOARD_EXPORT(rt_hw_serial_init);
171