diff --git a/extras/stdin_uart_interrupt/stdin_uart_interrupt.c b/extras/stdin_uart_interrupt/stdin_uart_interrupt.c index 71d2efc..ba0ce4f 100644 --- a/extras/stdin_uart_interrupt/stdin_uart_interrupt.c +++ b/extras/stdin_uart_interrupt/stdin_uart_interrupt.c @@ -40,32 +40,55 @@ #define UART0_RX_SIZE (128) // ESP8266 UART HW FIFO size -static SemaphoreHandle_t uart0_sem = NULL; +static QueueHandle_t uart0_queue; static bool inited = false; -static void uart0_rx_init(void); +static bool uart0_rx_init(void); static int uart0_nonblock; -static TickType_t uart0_vtime; +static TickType_t uart0_vtime = portMAX_DELAY; + +uint32_t uart0_parity_errors; +uint32_t uart0_framing_errors; +uint32_t uart0_breaks_detected; IRAM void uart0_rx_handler(void *arg) { // TODO: Handle UART1, see reg 0x3ff20020, bit2, bit0 represents uart1 and uart0 respectively - if (!UART(UART0).INT_STATUS & UART_INT_STATUS_RXFIFO_FULL) { - return; - } -// printf(" [%08x (%d)]\n", READ_PERI_REG(UART_INT_ST(UART0)), READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)); - if (UART(UART0).INT_STATUS & UART_INT_STATUS_RXFIFO_FULL) { - UART(UART0).INT_CLEAR = UART_INT_CLEAR_RXFIFO_FULL; - if (UART(UART0).STATUS & (UART_STATUS_RXFIFO_COUNT_M << UART_STATUS_RXFIFO_COUNT_S)) { - long int xHigherPriorityTaskWoken; - _xt_isr_mask(1 << INUM_UART); - _xt_clear_ints(1< 0) { - return i; - } + + for(size_t i = 0; i < len; i++, ptr++) { + if (xQueueReceive(uart0_queue, (void*)ptr, vtime) == pdFALSE) { + if (i > 0) { + return i; + } + if (nonblock) { r->_errno = EAGAIN; return -1; } - if (uart0_vtime) { - if (!xSemaphoreTake(uart0_sem, uart0_vtime)) { - if (i > 0) { - return i; - } - return 0; - } - } else if (!xSemaphoreTake(uart0_sem, portMAX_DELAY)) { - printf("\nFailed to get sem\n"); - } + return 0; } - ptr[i] = UART(UART0).FIFO & (UART_FIFO_DATA_M << UART_FIFO_DATA_S); } return len; } -static void uart0_rx_init(void) +static bool uart0_rx_init(void) { + uart0_queue = xQueueCreate(64, sizeof(char)); + + if (!uart0_queue) { + return false; + } + int trig_lvl = 1; - uart0_sem = xSemaphoreCreateCounting(UART0_RX_SIZE, 0); _xt_isr_attach(INUM_UART, uart0_rx_handler, NULL); _xt_isr_unmask(1 << INUM_UART); @@ -142,7 +167,10 @@ static void uart0_rx_init(void) UART(UART0).INT_CLEAR = 0x1ff; // enable rx_interrupt - UART(UART0).INT_ENABLE = UART_INT_ENABLE_RXFIFO_FULL; + UART(UART0).INT_ENABLE = UART_INT_ENABLE_RXFIFO_FULL | UART_INT_ENABLE_PARITY_ERR | + UART_INT_ENABLE_FRAMING_ERR | UART_INT_ENABLE_BREAK_DETECTED; inited = true; + + return true; }