@@ -36,14 +36,34 @@ struct hw_uart_device
36
36
#define UART_CR (base ) __REG32(base + 0x30)
37
37
#define UART_IMSC (base ) __REG32(base + 0x38)
38
38
#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_IFLS (base ) __REG32(base + 0x34)
42
+
43
+ /* test */
44
+ #define UART_TCR (base ) __REG32(base + 0x80)
45
+ #define UART_ITOP (base ) __REG32(base + 0x88)
46
+
47
+ #define UARTITOP_RXINTR 0x400
48
+
49
+ /* fifo */
50
+ #define UARTLCR_H_FEN 0x10
39
51
40
52
#define UARTFR_RXFE 0x10
41
53
#define UARTFR_TXFF 0x20
42
- #define UARTIMSC_RXIM 0x10
43
- #define UARTIMSC_TXIM 0x20
54
+ #define UARTFR_RXFF 0x40
55
+ #define UARTFR_TXFE 0x80
56
+ #define UARTFR_BUSY 0x08
57
+
58
+
44
59
#define UARTICR_RXIC 0x10
45
60
#define UARTICR_TXIC 0x20
46
61
62
+
63
+ #define UARTIMSC_RXIM 0x10
64
+ #define UARTIMSC_RTIM 0x40
65
+ #define UARTIMSC_TXIM 0x20
66
+
47
67
#if defined(BSP_USING_UART0 )
48
68
struct rt_serial_device serial0 ;
49
69
#endif
@@ -105,17 +125,29 @@ static struct hw_uart_device _uart_device[] = {
105
125
*
106
126
* @param serial Serial device
107
127
*/
128
+
108
129
static void rt_hw_uart_isr (int irqno , void * param )
109
130
{
110
131
struct rt_serial_device * serial = (struct rt_serial_device * )param ;
111
- struct hw_uart_device * uart ;
112
132
RT_ASSERT (serial != RT_NULL );
133
+ struct hw_uart_device * uart ;
113
134
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 );
135
+ RT_ASSERT (uart != RT_NULL );
136
+
137
+ if (!(UART_FR (uart -> hw_base ) & UARTFR_RXFE ) && (UART_IMSC (uart -> hw_base ) & UARTIMSC_RXIM ))
138
+ {
139
+ struct rt_serial_rx_fifo * rx_fifo ;
140
+ rx_fifo = (struct rt_serial_rx_fifo * ) serial -> serial_rx ;
141
+ RT_ASSERT (rx_fifo != RT_NULL );
142
+ char rec_ch = UART_DR (uart -> hw_base )& 0xff ;
143
+ rt_ringbuffer_putchar (& (rx_fifo -> rb ), rec_ch );
144
+ rt_hw_serial_isr (serial , RT_SERIAL_EVENT_RX_IND );
145
+ }
146
+ else if ((UART_IMSC (uart -> hw_base ) & UARTIMSC_TXIM ))
147
+ {
148
+ UART_ICR (uart -> hw_base )|= UARTICR_TXIC ;
149
+ rt_hw_serial_isr (serial , RT_SERIAL_EVENT_TX_DONE );
150
+ }
119
151
}
120
152
121
153
static rt_err_t uart_configure (struct rt_serial_device * serial , struct serial_configure * cfg )
@@ -194,7 +226,6 @@ static int uart_putc(struct rt_serial_device *serial, char ch)
194
226
RT_ASSERT (serial != RT_NULL );
195
227
196
228
uart = (struct hw_uart_device * )serial -> parent .user_data ;
197
-
198
229
while (UART_FR (uart -> hw_base ) & UARTFR_TXFF );
199
230
UART_DR (uart -> hw_base ) = ch ;
200
231
@@ -207,6 +238,7 @@ static int uart_getc(struct rt_serial_device *serial)
207
238
struct hw_uart_device * uart ;
208
239
209
240
RT_ASSERT (serial != RT_NULL );
241
+
210
242
uart = (struct hw_uart_device * )serial -> parent .user_data ;
211
243
212
244
ch = -1 ;
@@ -218,18 +250,37 @@ static int uart_getc(struct rt_serial_device *serial)
218
250
return ch ;
219
251
}
220
252
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 )
253
+ rt_uint8_t * buf ,
254
+ rt_size_t size ,
255
+ rt_uint32_t tx_flag )
224
256
{
225
257
RT_ASSERT (serial != RT_NULL );
226
258
RT_ASSERT (buf != RT_NULL );
227
-
228
259
RT_ASSERT (size );
260
+ uint32_t fifo_size = 0 , tx_size = 0 ;
261
+ struct hw_uart_device * uart = (struct hw_uart_device * )serial -> parent .user_data ;
262
+ struct rt_serial_tx_fifo * tx_fifo ;
263
+ tx_fifo = (struct rt_serial_tx_fifo * ) serial -> serial_tx ;
264
+ uint8_t ch = 0 ;
265
+ RT_ASSERT (tx_fifo != RT_NULL );
266
+
267
+ if (size > 0 )
268
+ {
269
+ if (UART_IMSC (uart -> hw_base ) & UARTIMSC_TXIM )
270
+ {
271
+ UART_IMSC (uart -> hw_base ) &= ~UARTIMSC_TXIM ;
272
+ if (rt_ringbuffer_getchar (& tx_fifo -> rb , & ch ))
273
+ {
274
+ while (UART_FR (uart -> hw_base ) & UARTFR_TXFF );
275
+ UART_DR (uart -> hw_base ) = ch ;
276
+ }
277
+ UART_IMSC (uart -> hw_base ) |= UARTIMSC_TXIM ;
278
+ }
279
+ }
229
280
230
- uart_control (serial , RT_DEVICE_CTRL_SET_INT , (void * )tx_flag );
231
281
return size ;
232
282
}
283
+
233
284
static const struct rt_uart_ops _uart_ops = {
234
285
.configure = uart_configure ,
235
286
.control = uart_control ,
@@ -238,12 +289,43 @@ static const struct rt_uart_ops _uart_ops = {
238
289
.transmit = uart_transmit
239
290
};
240
291
292
+ static int uart_config (void )
293
+ {
294
+ struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT ;
295
+
296
+ #ifdef BSP_USING_UART0
297
+ _uart_device [0 ].serial -> config = config ;
298
+ _uart_device [0 ].serial -> config .rx_bufsz = BSP_UART0_RX_BUFSIZE ;
299
+ _uart_device [0 ].serial -> config .tx_bufsz = BSP_UART0_TX_BUFSIZE ;
300
+ #endif /* BSP_USING_UART0 */
301
+
302
+ #ifdef BSP_USING_UART1
303
+ _uart_device [1 ].serial -> config = config ;
304
+ _uart_device [1 ].serial -> config .rx_bufsz = BSP_UART1_RX_BUFSIZE ;
305
+ _uart_device [1 ].serial -> config .tx_bufsz = BSP_UART1_TX_BUFSIZE ;
306
+ #endif /* BSP_USING_UART1 */
307
+
308
+ #ifdef BSP_USING_UART2
309
+ _uart_device [2 ].serial -> config = config ;
310
+ _uart_device [2 ].serial -> config .rx_bufsz = BSP_UART2_RX_BUFSIZE ;
311
+ _uart_device [2 ].serial -> config .tx_bufsz = BSP_UART2_TX_BUFSIZE ;
312
+ #endif /* BSP_USING_UART2 */
313
+
314
+ #ifdef BSP_USING_UART3
315
+ _uart_device [3 ].serial -> config = config ;
316
+ _uart_device [3 ].serial -> config .rx_bufsz = BSP_UART3_RX_BUFSIZE ;
317
+ _uart_device [3 ].serial -> config .tx_bufsz = BSP_UART3_TX_BUFSIZE ;
318
+ #endif /* BSP_USING_UART3 */
319
+
320
+ return RT_EOK ;
321
+ }
322
+
241
323
int rt_hw_uart_init (void )
242
324
{
243
325
244
326
rt_err_t err = RT_EOK ;
245
327
246
- struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT ;
328
+ uart_config () ;
247
329
248
330
for (uint32_t i = 0 ; i < sizeof (_uart_device ) / sizeof (_uart_device [0 ]); i ++ )
249
331
{
@@ -253,9 +335,6 @@ int rt_hw_uart_init(void)
253
335
#endif
254
336
255
337
_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 ;
259
338
/* register UART device */
260
339
err = rt_hw_serial_register (_uart_device [i ].serial ,
261
340
_uart_device [i ].device_name ,
@@ -264,6 +343,11 @@ int rt_hw_uart_init(void)
264
343
rt_hw_interrupt_install (_uart_device [i ].irqno , rt_hw_uart_isr , _uart_device [i ].serial , _uart_device [i ].device_name );
265
344
/* enable Rx and Tx of UART */
266
345
UART_CR (_uart_device [i ].hw_base ) = (1 << 0 ) | (1 << 8 ) | (1 << 9 );
346
+
347
+ /* Fifo */
348
+ UART_LCR_H (_uart_device [i ].hw_base ) = (1 << 4 );
349
+ /* UART_IFLS(_uart_device[i].hw_base) = (0 <<3 ) | (0 <<4 )| (1 <<5 ); */
350
+ /* UART_DMACR(_uart_device[i].hw_base) = (1<<0); */
267
351
}
268
352
269
353
return err ;
0 commit comments