1010 */
1111
1212#include <rthw.h>
13- #include <rtconfig.h>
1413#include <rtdevice.h>
15- // #include <drivers/drv_serial.h>
14+ #include <ioremap.h>
15+ #include <drivers/misc.h>
1616
1717#include "serial.h"
1818
@@ -44,9 +44,15 @@ static struct hw_uart_device _uart1_device =
4444 .clock_mux = kCLOCK_LPUART1_ClockRoot_MuxOsc24M ,
4545 .clock_ip_name = kCLOCK_Lpuart1 ,
4646};
47- // static struct rt_serial_device _serial1;
4847#endif
4948
49+ static struct hw_uart_device * hw_uart_devices [] =
50+ {
51+ #ifdef BSP_USING_UART1
52+ & _uart1_device ,
53+ #endif
54+ };
55+
5056static void rt_hw_uart_isr (int irqn , void * param )
5157{
5258 struct rt_serial_device * serial = (struct rt_serial_device * )param ;
@@ -89,12 +95,16 @@ static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_co
8995 // uart_set_FIFO_mode(uart->uart_base, RX_FIFO, 1, IRQ_MODE);
9096
9197 struct hw_uart_device * uart = RT_NULL ;
92- lpuart_config_t config ;
98+ static lpuart_config_t config ;
9399
100+ rt_hw_console_output ("uart_configure start ...\n" );
94101 RT_ASSERT (serial != RT_NULL );
95102 uart = (struct hw_uart_device * )serial -> parent .user_data ;
96103
104+ rt_hw_console_output ("uart_configure LPUART_GetDefaultConfig ...\n" );
97105 LPUART_GetDefaultConfig (& config );
106+ rt_hw_console_output ("uart_configure LPUART_GetDefaultConfig done!\n" );
107+
98108 config .baudRate_Bps = cfg -> baud_rate ;
99109
100110 // data bits
@@ -171,17 +181,22 @@ static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_co
171181 config .enableRx = serial -> parent .flag & RT_DEVICE_FLAG_RDONLY ;
172182
173183 // Set UART clock source and clock divider
174- CLOCK_SetRootClockMux (uart -> clock_root , uart -> clock_mux );
175- CLOCK_SetRootClockDiv (uart -> clock_root , 1U );
184+ // CLOCK_SetRootClockMux(uart->clock_root, uart->clock_mux);
185+ // CLOCK_SetRootClockDiv(uart->clock_root, 1U);
176186
177187 // Initialize the LPUART module with the configuration structure and clock source
188+ rt_hw_console_output ("uart_configure LPUART_Init ...\n" );
178189 LPUART_Init (uart -> uart_base , & config , CLOCK_GetIpFreq (uart -> clock_root ));
179190
180- // Enable RX interrupt
191+ // Install interrupt handler
192+ rt_hw_console_output ("uart_configure rt_hw_interrupt_install ...\n" );
181193 rt_hw_interrupt_install (uart -> irqn , rt_hw_uart_isr , serial , "uart" );
182194 rt_hw_interrupt_mask (uart -> irqn );
183195
184- return RT_EOK ;
196+ // Enable RX interrupt
197+ rt_hw_console_output ("uart_configure LPUART_EnableInterrupts ...\n" );
198+ LPUART_EnableInterrupts (uart -> uart_base , kLPUART_RxDataRegFullInterruptEnable );
199+ return RT_EOK ;
185200}
186201
187202static rt_err_t uart_control (struct rt_serial_device * serial , int cmd , void * arg )
@@ -223,24 +238,62 @@ static int uart_putc(struct rt_serial_device *serial, char c)
223238 return 1 ;
224239}
225240
226- void uart1_putc (char c )
241+ void imx_uart1_putc (char c )
242+ {
243+ LPUART_WriteByte (LPUART1 , c );
244+ while (!(LPUART_GetStatusFlags (LPUART1 ) & kLPUART_TxDataRegEmptyFlag ));
245+ }
246+
247+ void imx_uart1_puts (const char * str )
248+ {
249+ int has_cr = 0 ;
250+ while (* str ) {
251+ if (* str == '\r' ) {
252+ has_cr = 1 ;
253+ } else if (* str == '\n' ) {
254+ if (!has_cr ) {
255+ imx_uart1_putc ('\r' );
256+ }
257+ }
258+ imx_uart1_putc (* str ++ );
259+ }
260+ }
261+
262+ void imx_uart1_print_hex (const char * str , rt_base_t hex )
227263{
264+ imx_uart1_puts (str );
265+ imx_uart1_putc ('0' );
266+ imx_uart1_putc ('x' );
267+ for (int i = 60 ; i >= 0 ; i -= 4 ) {
268+ rt_base_t h = (hex >> i ) & 0xF ;
269+ imx_uart1_putc (h < 10 ? '0' + h : 'A' + h - 10 );
270+ }
271+ imx_uart1_putc ('\r' );
272+ imx_uart1_putc ('\n' );
273+ }
274+
275+ void rt_hw_console_putc (char c )
276+ {
277+ #if defined(BSP_USING_UART1 )
228278 uart_putc (& _uart1_device .serial , c );
279+ #endif
229280}
230281
231- void uart1_puts (const char * str )
282+ void rt_hw_console_output (const char * str )
232283{
284+ #if defined(BSP_USING_UART1 )
233285 int has_cr = 0 ;
234286 while (* str ) {
235287 if (* str == '\r' ) {
236288 has_cr = 1 ;
237289 } else if (* str == '\n' ) {
238290 if (!has_cr ) {
239- uart1_putc ('\r' );
291+ rt_hw_console_putc ('\r' );
240292 }
241293 }
242- uart1_putc (* str ++ );
294+ rt_hw_console_putc (* str ++ );
243295 }
296+ #endif
244297}
245298
246299static int uart_getc (struct rt_serial_device * serial )
@@ -271,9 +324,8 @@ static const struct rt_uart_ops _uart_ops =
271324
272325int rt_hw_uart_init (void )
273326{
274- struct hw_uart_device * uart ;
275327 struct serial_configure config ;
276-
328+ /* set serial configure */
277329 config .baud_rate = BAUD_RATE_115200 ;
278330 config .bit_order = BIT_ORDER_LSB ;
279331 config .data_bits = DATA_BITS_8 ;
@@ -282,16 +334,19 @@ int rt_hw_uart_init(void)
282334 config .invert = NRZ_NORMAL ;
283335 config .bufsz = RT_SERIAL_RB_BUFSZ ;
284336
285- #ifdef BSP_USING_UART1
286- uart = & _uart1_device ;
287- _uart1_device .serial .ops = & _uart_ops ;
288- _uart1_device .serial .config = config ;
289-
290- /* register LPUART1 device */
291- rt_hw_serial_register (& _uart1_device .serial , _uart1_device .device_name ,
292- RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX , uart );
293- #endif
337+ for (int i = 0 ; i < RT_ARRAY_SIZE (hw_uart_devices ); i ++ )
338+ {
339+ if (hw_uart_devices [i ] != RT_NULL )
340+ {
341+ hw_uart_devices [i ]-> serial .ops = & _uart_ops ;
342+ hw_uart_devices [i ]-> serial .config = config ;
343+ // hw_uart_devices[i]->uart_base = rt_ioremap((void *)hw_uart_devices[i]->uart_base, sizeof(LPUART_Type));
344+
345+ rt_hw_serial_register (& hw_uart_devices [i ]-> serial , hw_uart_devices [i ]-> device_name ,
346+ RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX , hw_uart_devices [i ]);
347+ }
348+ }
294349
295350 return 0 ;
296351}
297- INIT_BOARD_EXPORT (rt_hw_uart_init );
352+ // INIT_BOARD_EXPORT(rt_hw_uart_init);
0 commit comments