1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-02-08 RT-Thread the first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_uart.h"
#include "interrupt.h"
#include "drv_gpio.h"
#include "drv_clock.h"
#define readl(addr) (*(volatile unsigned int *)(addr))
#define writel(value,addr) (*(volatile unsigned int *)(addr) = (value))
#ifdef RT_USING_SERIAL
struct device_uart
{
rt_uint32_t hw_base;
rt_uint32_t irqno;
char name[RT_NAME_MAX];
rt_uint32_t gpio_rx_port;
rt_uint32_t gpio_tx_port;
rt_uint32_t gpio_rx_pin;
rt_uint32_t gpio_tx_pin;
rt_uint32_t gpio_rx_fun;
rt_uint32_t gpio_tx_fun;
};
static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg);
static int uart_putc(struct rt_serial_device *serial, char c);
static int uart_getc(struct rt_serial_device *serial);
static rt_ssize_t uart_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
void uart_irq_handler(int irqno, void *param);
const struct rt_uart_ops _uart_ops =
{
uart_configure,
uart_control,
uart_putc,
uart_getc,
uart_dma_transmit
};
/*
* UART Initiation
*/
int rt_hw_uart_init(void)
{
struct rt_serial_device *serial;
struct device_uart *uart;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
#ifdef TINA_USING_UART0
{
static struct rt_serial_device serial0;
static struct device_uart uart0;
serial = &serial0;
uart = &uart0;
serial->ops = &_uart_ops;
serial->config = config;
serial->config.baud_rate = 115200;
uart->hw_base = UART0_BASE_ADDR; // UART0_BASE;
uart->irqno = UART0_INTERRUPT; // IRQ_UART0;
uart->gpio_rx_port = GPIO_PORT_E;
uart->gpio_tx_port = GPIO_PORT_E;
uart->gpio_rx_pin = GPIO_PIN_0;
uart->gpio_tx_pin = GPIO_PIN_1;
uart->gpio_rx_fun = IO_FUN_4;
uart->gpio_tx_fun = IO_FUN_4;
rt_hw_serial_register(serial,
"uart0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
}
#endif
#ifdef TINA_USING_UART1
{
static struct rt_serial_device serial1;
static struct device_uart uart1;
serial = &serial1;
uart = &uart1;
serial->ops = &_uart_ops;
serial->config = config;
serial->config.baud_rate = 115200;
uart->hw_base = UART1_BASE_ADDR; // UART1_BASE;
uart->irqno = UART1_INTERRUPT; // IRQ_UART1;
uart->gpio_rx_port = GPIO_PORT_A;
uart->gpio_tx_port = GPIO_PORT_A;
uart->gpio_rx_pin = GPIO_PIN_2;
uart->gpio_tx_pin = GPIO_PIN_3;
uart->gpio_rx_fun = IO_FUN_4;
uart->gpio_tx_fun = IO_FUN_4;
rt_hw_serial_register(serial,
"uart1",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
}
#endif
#ifdef TINA_USING_UART2
{
static struct rt_serial_device serial2;
static struct device_uart uart2;
serial = &serial2;
uart = &uart2;
serial->ops = &_uart_ops;
serial->config = config;
serial->config.baud_rate = 115200;
uart->hw_base = UART2_BASE_ADDR; // UART1_BASE;
uart->irqno = UART2_INTERRUPT; // IRQ_UART1;
uart->gpio_rx_port = GPIO_PORT_E;
uart->gpio_tx_port = GPIO_PORT_E;
uart->gpio_rx_pin = GPIO_PIN_8;
uart->gpio_tx_pin = GPIO_PIN_7;
uart->gpio_rx_fun = IO_FUN_2;
uart->gpio_tx_fun = IO_FUN_2;
rt_hw_serial_register(serial,
"uart2",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
}
#endif
return 0;
}
/*
* UART interface
*/
static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
rt_uint32_t addr, val;
struct device_uart *uart;
RT_ASSERT(serial != RT_NULL);
serial->config = *cfg;
uart = serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
/* config gpio port */
gpio_set_func(uart->gpio_rx_port, uart->gpio_rx_pin, uart->gpio_rx_fun);
gpio_set_func(uart->gpio_tx_port, uart->gpio_tx_pin, uart->gpio_tx_fun);
/* Enable UART clock */
/* Open the clock gate for uart */
if ((rt_uint32_t)(uart->hw_base) == UART0_BASE_ADDR)
{
bus_gate_clk_enalbe(UART0_GATING);
bus_software_reset_enalbe(UART0_GATING);
bus_software_reset_disalbe(UART0_GATING);
}
else if ((rt_uint32_t)(uart->hw_base) == UART1_BASE_ADDR)
{
bus_gate_clk_enalbe(UART1_GATING);
bus_software_reset_enalbe(UART1_GATING);
bus_software_reset_disalbe(UART1_GATING);
}
else if ((rt_uint32_t)(uart->hw_base) == UART2_BASE_ADDR)
{
bus_gate_clk_enalbe(UART2_GATING);
bus_software_reset_enalbe(UART2_GATING);
bus_software_reset_disalbe(UART2_GATING);
}
else
RT_ASSERT(0);
/* Config uart0 to 115200-8-1-0 */
addr = uart->hw_base;
/* close uart irq */
writel(0x0, addr + UART_IER);
/* config fifo */
writel(0x37, addr + UART_FCR);
/* config modem */
writel(0x0, addr + UART_MCR);
/* config baud */
val = readl(addr + UART_LCR);
val |= (1 << 7);
writel(val, addr + UART_LCR);
val = apb_get_clk() / 16 / serial->config.baud_rate;
writel(val & 0xff, addr + UART_DLL);
writel((val >> 8) & 0xff, addr + UART_DLH);
val = readl(addr + UART_LCR);
val &= ~(1 << 7);
writel(val, addr + UART_LCR);
val = readl(addr + UART_LCR);
val &= ~0x1f;
val |= ((serial->config.data_bits - DATA_BITS_5) << 0) | (0 << 2) | (0x0 << 3);
writel(val, addr + UART_LCR);
writel(0xf, addr + UART_TFL);
writel(0x3F, addr + UART_RFL);
writel(0x1, addr + UART_IER);
return RT_EOK;
}
static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
struct device_uart *uart;
uart = serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
/* Disable the UART Interrupt */
rt_hw_interrupt_mask(uart->irqno);
writel(0x00, uart->hw_base + UART_IER);
break;
case RT_DEVICE_CTRL_SET_INT:
/* install interrupt */
rt_hw_interrupt_install(uart->irqno, uart_irq_handler,
serial, uart->name);
rt_hw_interrupt_umask(uart->irqno);
writel(0x01, uart->hw_base + UART_IER);
/* Enable the UART Interrupt */
break;
}
return (RT_EOK);
}
static int uart_putc(struct rt_serial_device *serial, char c)
{
struct device_uart *uart;
volatile rt_uint32_t *sed_buf;
volatile rt_uint32_t *sta;
uart = serial->parent.user_data;
sed_buf = (rt_uint32_t *)(uart->hw_base + UART_THR);
sta = (rt_uint32_t *)(uart->hw_base + UART_USR);
/* FIFO status, contain valid data */
while (!(*sta & 0x02));
*sed_buf = c;
return (1);
}
static int uart_getc(struct rt_serial_device *serial)
{
int ch = -1;
volatile rt_uint32_t *rec_buf;
volatile rt_uint32_t *sta;
struct device_uart *uart = serial->parent.user_data;
RT_ASSERT(serial != RT_NULL);
rec_buf = (rt_uint32_t *)(uart->hw_base + UART_RHB);
sta = (rt_uint32_t *)(uart->hw_base + UART_USR);
/* Receive Data Available */
if (*sta & 0x08)
{
ch = *rec_buf & 0xff;
}
return ch;
}
static rt_ssize_t uart_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
{
return (0);
}
/* UART ISR */
void uart_irq_handler(int irqno, void *param)
{
rt_uint32_t val;
struct rt_serial_device *serial = (struct rt_serial_device *)param;
struct device_uart *uart = serial->parent.user_data;
val = readl(uart->hw_base + 0x08) & 0x0F;
/* read interrupt status and clear it */
if (val & 0x4) /* rx ind */
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
}
if (0) /* tx done */
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
}
}
#endif
|