1515#include "drv_uart.h"
1616
1717#include <stdio.h>
18+ #include <sysctl.h>
1819
1920// #include "uart.h"
2021#include "uarths.h"
2122#include "plic.h"
2223
24+ static volatile uarths_t * const _uarths = (volatile uarths_t * )UARTHS_BASE_ADDR ;
25+
2326struct device_uart
2427{
2528 rt_uint32_t hw_base ;
@@ -66,9 +69,6 @@ int rt_hw_uart_init(void)
6669 uart -> hw_base = UARTHS_BASE_ADDR ;
6770 uart -> irqno = IRQN_UARTHS_INTERRUPT ;
6871
69- /* initialize UART HS */
70- uarths_init ();
71-
7272 rt_hw_serial_register (serial ,
7373 "uarths" ,
7474 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX ,
@@ -114,22 +114,31 @@ static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_co
114114{
115115 rt_uint32_t baud_div ;
116116 struct device_uart * uart ;
117+ uint32_t freq = sysctl_clock_get_freq (SYSCTL_CLOCK_CPU );
118+ uint16_t div = freq / cfg -> baud_rate - 1 ;
117119
118120 RT_ASSERT (serial != RT_NULL );
119121 serial -> config = * cfg ;
120122
121123 uart = serial -> parent .user_data ;
122124 RT_ASSERT (uart != RT_NULL );
123125
124- /* Init UART Hardware */
125-
126- /* Enable UART clock */
127-
128- /* Set both receiver and transmitter in UART mode (not SIR) */
129-
130- /* Set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
131-
132- /* set baudrate */
126+ if (uart -> hw_base == UARTHS_BASE_ADDR )
127+ {
128+ _uarths -> div .div = div ;
129+ _uarths -> txctrl .txen = 1 ;
130+ _uarths -> rxctrl .rxen = 1 ;
131+ _uarths -> txctrl .txcnt = 0 ;
132+ _uarths -> rxctrl .rxcnt = 0 ;
133+ _uarths -> ip .txwm = 1 ;
134+ _uarths -> ip .rxwm = 1 ;
135+ _uarths -> ie .txwm = 0 ;
136+ _uarths -> ie .rxwm = 1 ;
137+ }
138+ else
139+ {
140+ /* other uart */
141+ }
133142
134143 return (RT_EOK );
135144}
@@ -167,7 +176,8 @@ static int drv_uart_putc(struct rt_serial_device *serial, char c)
167176 uart = serial -> parent .user_data ;
168177 if (uart -> hw_base == UARTHS_BASE_ADDR )
169178 {
170- uarths_putchar (c );
179+ while (_uarths -> txdata .full );
180+ _uarths -> txdata .data = (uint8_t )c ;
171181 }
172182 else
173183 {
@@ -184,8 +194,11 @@ static int drv_uart_getc(struct rt_serial_device *serial)
184194
185195 if (uart -> hw_base == UARTHS_BASE_ADDR )
186196 {
187- ret = uarths_getc ();
188- if (ret != EOF ) return ret ;
197+ uarths_rxdata_t recv = _uarths -> rxdata ;
198+ if (recv .empty )
199+ return EOF ;
200+ else
201+ return (recv .data & 0xff );
189202 }
190203
191204 /* Receive Data Available */
@@ -203,7 +216,14 @@ static void uart_irq_handler(int irqno, void *param)
203216 /* read interrupt status and clear it */
204217 if (uart -> hw_base == UARTHS_BASE_ADDR )
205218 {
206- if (uarths -> ip .rxwm )
219+ if (_uarths -> ip .rxwm )
207220 rt_hw_serial_isr (serial , RT_SERIAL_EVENT_RX_IND );
208221 }
209222}
223+
224+ /* WEAK for SDK 0.5.6 */
225+
226+ RT_WEAK void uart_debug_init (int uart_channel )
227+ {
228+
229+ }
0 commit comments