s32_t UART_get_buf(uart *u, u8_t* dst, u16_t len) { UART_SET_RX_IRQ_OFF(u); u16_t avail = UART_rx_available(u); s32_t len_to_read = MIN(avail, len); if (len_to_read == 0) { UART_SET_RX_IRQ_ON(u); return 0; } u32_t remaining = len_to_read; u32_t len_to_end = UART_RX_BUFFER - u->rx.rix; if (remaining > len_to_end) { if (dst) { memcpy(dst, &u->rx.buf[u->rx.rix], len_to_end); dst += len_to_end; } remaining -= len_to_end; u->rx.rix = 0; } if (dst) { memcpy(dst, &u->rx.buf[u->rx.rix], remaining); } u->rx.rix += remaining; if (u->rx.rix >= UART_RX_BUFFER) { u->rx.rix = 0; } UART_SET_RX_IRQ_ON(u); return len_to_read; }
static int f_uread(int uart, int numchars) { if (_argc < 1 || _argc > 2) { return -1; } if (uart < 0 || uart >= CONFIG_UART_CNT) { return -1; } if (_argc == 1) { numchars = 0x7fffffff; } int l = UART_rx_available(_UART(uart)); l = MIN(l, numchars); int ix = 0; while (ix++ < l) { print("%c", UART_get_char(_UART(uart))); } print("\n%i bytes read\n", l); return 0; }