@@ -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 )
4869struct 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+
108130static 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
121154static 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}
220253static 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+
233285static 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+
241324int 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