diff --git a/bootloader/soc/uart.c b/bootloader/soc/uart.c index 74f0af9..7b3657f 100644 --- a/bootloader/soc/uart.c +++ b/bootloader/soc/uart.c @@ -65,7 +65,7 @@ void uart_wait_idle(u32 idx, u32 which) } } -void uart_send(u32 idx, u8 *buf, u32 len) +void uart_send(u32 idx, const u8 *buf, u32 len) { uart_t *uart = (uart_t *)(UART_BASE + uart_baseoff[idx]); @@ -77,14 +77,30 @@ void uart_send(u32 idx, u8 *buf, u32 len) }; } -void uart_recv(u32 idx, u8 *buf, u32 len) +u32 uart_recv(u32 idx, u8 *buf, u32 len) { uart_t *uart = (uart_t *)(UART_BASE + uart_baseoff[idx]); + u32 timeout = get_tmr_us() + 1000; + u32 i; - for (u32 i = 0; i != len; i++) + for (i = 0; ; i++) { while (!(uart->UART_LSR & UART_LSR_RDR)) - ; + { + if (!len) + { + if (timeout < get_tmr_us()) + break; + } + else if (len < i) + break; + } + if (timeout < get_tmr_us()) + break; + buf[i] = uart->UART_THR_DLAB; + timeout = get_tmr_us() + 1000; }; + + return i ? (len ? (i - 1) : i) : 0; } diff --git a/bootloader/soc/uart.h b/bootloader/soc/uart.h index 808232d..39a8f23 100644 --- a/bootloader/soc/uart.h +++ b/bootloader/soc/uart.h @@ -76,7 +76,7 @@ typedef struct _uart_t void uart_init(u32 idx, u32 baud); void uart_wait_idle(u32 idx, u32 which); -void uart_send(u32 idx, u8 *buf, u32 len); -void uart_recv(u32 idx, u8 *buf, u32 len); +void uart_send(u32 idx, const u8 *buf, u32 len); +u32 uart_recv(u32 idx, u8 *buf, u32 len); #endif diff --git a/nyx/nyx_gui/soc/uart.c b/nyx/nyx_gui/soc/uart.c index 74f0af9..7b3657f 100644 --- a/nyx/nyx_gui/soc/uart.c +++ b/nyx/nyx_gui/soc/uart.c @@ -65,7 +65,7 @@ void uart_wait_idle(u32 idx, u32 which) } } -void uart_send(u32 idx, u8 *buf, u32 len) +void uart_send(u32 idx, const u8 *buf, u32 len) { uart_t *uart = (uart_t *)(UART_BASE + uart_baseoff[idx]); @@ -77,14 +77,30 @@ void uart_send(u32 idx, u8 *buf, u32 len) }; } -void uart_recv(u32 idx, u8 *buf, u32 len) +u32 uart_recv(u32 idx, u8 *buf, u32 len) { uart_t *uart = (uart_t *)(UART_BASE + uart_baseoff[idx]); + u32 timeout = get_tmr_us() + 1000; + u32 i; - for (u32 i = 0; i != len; i++) + for (i = 0; ; i++) { while (!(uart->UART_LSR & UART_LSR_RDR)) - ; + { + if (!len) + { + if (timeout < get_tmr_us()) + break; + } + else if (len < i) + break; + } + if (timeout < get_tmr_us()) + break; + buf[i] = uart->UART_THR_DLAB; + timeout = get_tmr_us() + 1000; }; + + return i ? (len ? (i - 1) : i) : 0; } diff --git a/nyx/nyx_gui/soc/uart.h b/nyx/nyx_gui/soc/uart.h index 808232d..39a8f23 100644 --- a/nyx/nyx_gui/soc/uart.h +++ b/nyx/nyx_gui/soc/uart.h @@ -76,7 +76,7 @@ typedef struct _uart_t void uart_init(u32 idx, u32 baud); void uart_wait_idle(u32 idx, u32 which); -void uart_send(u32 idx, u8 *buf, u32 len); -void uart_recv(u32 idx, u8 *buf, u32 len); +void uart_send(u32 idx, const u8 *buf, u32 len); +u32 uart_recv(u32 idx, u8 *buf, u32 len); #endif