@@ -36,14 +36,32 @@ 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_IFLS (base ) __REG32(base + 0x34)
42+
43+ #define UART_TCR (base ) __REG32(base + 0x80)
44+ #define UART_ITOP (base ) __REG32(base + 0x88)
45+
46+ #define UARTITOP_RXINTR 0x400
47+
48+ #define UARTLCR_H_FEN 0x10
3949
4050#define UARTFR_RXFE 0x10
4151#define UARTFR_TXFF 0x20
42- #define UARTIMSC_RXIM 0x10
43- #define UARTIMSC_TXIM 0x20
52+ #define UARTFR_RXFF 0x40
53+ #define UARTFR_TXFE 0x80
54+ #define UARTFR_BUSY 0x08
55+
56+
4457#define UARTICR_RXIC 0x10
4558#define UARTICR_TXIC 0x20
4659
60+
61+ #define UARTIMSC_RXIM 0x10
62+ #define UARTIMSC_RTIM 0x40
63+ #define UARTIMSC_TXIM 0x20
64+
4765#if defined(BSP_USING_UART0 )
4866struct rt_serial_device serial0 ;
4967#endif
@@ -105,17 +123,29 @@ static struct hw_uart_device _uart_device[] = {
105123 *
106124 * @param serial Serial device
107125 */
126+
108127static void rt_hw_uart_isr (int irqno , void * param )
109128{
110129 struct rt_serial_device * serial = (struct rt_serial_device * )param ;
111- struct hw_uart_device * uart ;
112130 RT_ASSERT (serial != RT_NULL );
131+ struct hw_uart_device * uart ;
113132 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 );
133+ RT_ASSERT (uart != RT_NULL );
134+
135+ if (!(UART_FR (uart -> hw_base ) & UARTFR_RXFE ) && (UART_IMSC (uart -> hw_base ) & UARTIMSC_RXIM ))
136+ {
137+ struct rt_serial_rx_fifo * rx_fifo ;
138+ rx_fifo = (struct rt_serial_rx_fifo * ) serial -> serial_rx ;
139+ RT_ASSERT (rx_fifo != RT_NULL );
140+ char rec_ch = UART_DR (uart -> hw_base )& 0xff ;
141+ rt_ringbuffer_putchar (& (rx_fifo -> rb ), rec_ch );
142+ rt_hw_serial_isr (serial , RT_SERIAL_EVENT_RX_IND );
143+ }
144+ else if ((UART_IMSC (uart -> hw_base ) & UARTIMSC_TXIM ))
145+ {
146+ UART_ICR (uart -> hw_base )|= UARTICR_TXIC ;
147+ rt_hw_serial_isr (serial , RT_SERIAL_EVENT_TX_DONE );
148+ }
119149}
120150
121151static rt_err_t uart_configure (struct rt_serial_device * serial , struct serial_configure * cfg )
@@ -194,7 +224,6 @@ static int uart_putc(struct rt_serial_device *serial, char ch)
194224 RT_ASSERT (serial != RT_NULL );
195225
196226 uart = (struct hw_uart_device * )serial -> parent .user_data ;
197-
198227 while (UART_FR (uart -> hw_base ) & UARTFR_TXFF );
199228 UART_DR (uart -> hw_base ) = ch ;
200229
@@ -207,6 +236,7 @@ static int uart_getc(struct rt_serial_device *serial)
207236 struct hw_uart_device * uart ;
208237
209238 RT_ASSERT (serial != RT_NULL );
239+
210240 uart = (struct hw_uart_device * )serial -> parent .user_data ;
211241
212242 ch = -1 ;
@@ -218,18 +248,37 @@ static int uart_getc(struct rt_serial_device *serial)
218248 return ch ;
219249}
220250static 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 )
251+ rt_uint8_t * buf ,
252+ rt_size_t size ,
253+ rt_uint32_t tx_flag )
224254{
225255 RT_ASSERT (serial != RT_NULL );
226256 RT_ASSERT (buf != RT_NULL );
227-
228257 RT_ASSERT (size );
258+ uint32_t fifo_size = 0 , tx_size = 0 ;
259+ struct hw_uart_device * uart = (struct hw_uart_device * )serial -> parent .user_data ;
260+ struct rt_serial_tx_fifo * tx_fifo ;
261+ tx_fifo = (struct rt_serial_tx_fifo * ) serial -> serial_tx ;
262+ uint8_t ch = 0 ;
263+ RT_ASSERT (tx_fifo != RT_NULL );
264+
265+ if (size > 0 )
266+ {
267+ if (UART_IMSC (uart -> hw_base ) & UARTIMSC_TXIM )
268+ {
269+ UART_IMSC (uart -> hw_base ) &= ~UARTIMSC_TXIM ;
270+ if (rt_ringbuffer_getchar (& tx_fifo -> rb , & ch ))
271+ {
272+ while (UART_FR (uart -> hw_base ) & UARTFR_TXFF );
273+ UART_DR (uart -> hw_base ) = ch ;
274+ }
275+ UART_IMSC (uart -> hw_base ) |= UARTIMSC_TXIM ;
276+ }
277+ }
229278
230- uart_control (serial , RT_DEVICE_CTRL_SET_INT , (void * )tx_flag );
231279 return size ;
232280}
281+
233282static const struct rt_uart_ops _uart_ops = {
234283 .configure = uart_configure ,
235284 .control = uart_control ,
@@ -238,12 +287,43 @@ static const struct rt_uart_ops _uart_ops = {
238287 .transmit = uart_transmit
239288};
240289
290+ static int uart_config (void )
291+ {
292+ struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT ;
293+
294+ #ifdef BSP_USING_UART0
295+ _uart_device [0 ].serial -> config = config ;
296+ _uart_device [0 ].serial -> config .rx_bufsz = BSP_UART0_RX_BUFSIZE ;
297+ _uart_device [0 ].serial -> config .tx_bufsz = BSP_UART0_TX_BUFSIZE ;
298+ #endif /* BSP_USING_UART0 */
299+
300+ #ifdef BSP_USING_UART1
301+ _uart_device [1 ].serial -> config = config ;
302+ _uart_device [1 ].serial -> config .rx_bufsz = BSP_UART1_RX_BUFSIZE ;
303+ _uart_device [1 ].serial -> config .tx_bufsz = BSP_UART1_TX_BUFSIZE ;
304+ #endif /* BSP_USING_UART1 */
305+
306+ #ifdef BSP_USING_UART2
307+ _uart_device [2 ].serial -> config = config ;
308+ _uart_device [2 ].serial -> config .rx_bufsz = BSP_UART2_RX_BUFSIZE ;
309+ _uart_device [2 ].serial -> config .tx_bufsz = BSP_UART2_TX_BUFSIZE ;
310+ #endif /* BSP_USING_UART2 */
311+
312+ #ifdef BSP_USING_UART3
313+ _uart_device [3 ].serial -> config = config ;
314+ _uart_device [3 ].serial -> config .rx_bufsz = BSP_UART3_RX_BUFSIZE ;
315+ _uart_device [3 ].serial -> config .tx_bufsz = BSP_UART3_TX_BUFSIZE ;
316+ #endif /* BSP_USING_UART3 */
317+
318+ return RT_EOK ;
319+ }
320+
241321int rt_hw_uart_init (void )
242322{
243323
244324 rt_err_t err = RT_EOK ;
245325
246- struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT ;
326+ uart_config () ;
247327
248328 for (uint32_t i = 0 ; i < sizeof (_uart_device ) / sizeof (_uart_device [0 ]); i ++ )
249329 {
@@ -253,9 +333,6 @@ int rt_hw_uart_init(void)
253333 #endif
254334
255335 _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 ;
259336 /* register UART device */
260337 err = rt_hw_serial_register (_uart_device [i ].serial ,
261338 _uart_device [i ].device_name ,
@@ -264,6 +341,7 @@ int rt_hw_uart_init(void)
264341 rt_hw_interrupt_install (_uart_device [i ].irqno , rt_hw_uart_isr , _uart_device [i ].serial , _uart_device [i ].device_name );
265342 /* enable Rx and Tx of UART */
266343 UART_CR (_uart_device [i ].hw_base ) = (1 << 0 ) | (1 << 8 ) | (1 << 9 );
344+ UART_LCR_H (_uart_device [i ].hw_base ) = (1 << 4 );
267345 }
268346
269347 return err ;
0 commit comments