diff --git a/apps/microtvm/zephyr/demo_runtime/src/main.c b/apps/microtvm/zephyr/demo_runtime/src/main.c index 4074d18b045ae..2e05b31bb575f 100644 --- a/apps/microtvm/zephyr/demo_runtime/src/main.c +++ b/apps/microtvm/zephyr/demo_runtime/src/main.c @@ -216,35 +216,37 @@ tvm_crt_error_t TVMPlatformTimerStop(double* elapsed_time_seconds) { } // Ring buffer used to store data read from the UART on rx interrupt. -#define RING_BUF_SIZE_BYTES TVM_CRT_MAX_PACKET_SIZE_BYTES -RING_BUF_ITEM_DECLARE_SIZE(uart_rx_rbuf, TVM_CRT_MAX_PACKET_SIZE_BYTES); +#define RING_BUF_SIZE_BYTES (TVM_CRT_MAX_PACKET_SIZE_BYTES + 22) +RING_BUF_ITEM_DECLARE_SIZE(uart_rx_rbuf, RING_BUF_SIZE_BYTES); // UART interrupt callback. void uart_irq_cb(const struct device* dev, void* user_data) { uart_irq_update(dev); - struct ring_buf* rbuf = (struct ring_buf*)user_data; - if (uart_irq_rx_ready(dev) != 0) { - uint8_t* data; - uint32_t size; - size = ring_buf_put_claim(rbuf, &data, RING_BUF_SIZE_BYTES); - int rx_size = uart_fifo_read(dev, data, size); - // Write it into the ring buffer. - g_num_bytes_in_rx_buffer += rx_size; - if (g_num_bytes_in_rx_buffer > RING_BUF_SIZE_BYTES) { - TVMLogf("Ring buffer overflow. (%d > %d)", g_num_bytes_in_rx_buffer, RING_BUF_SIZE_BYTES); - TVMPlatformAbort((tvm_crt_error_t)0xbeef3); - } + if (uart_irq_is_pending(dev)) { + struct ring_buf* rbuf = (struct ring_buf*)user_data; + if (uart_irq_rx_ready(dev) != 0) { + uint8_t* data; + uint32_t size; + size = ring_buf_put_claim(rbuf, &data, RING_BUF_SIZE_BYTES); + int rx_size = uart_fifo_read(dev, data, size); + // Write it into the ring buffer. + g_num_bytes_in_rx_buffer += rx_size; + + if (g_num_bytes_in_rx_buffer > RING_BUF_SIZE_BYTES) { + TVMPlatformAbort((tvm_crt_error_t)0xbeef3); + } - if (rx_size < 0) { - TVMPlatformAbort((tvm_crt_error_t)0xbeef1); - } + if (rx_size < 0) { + TVMPlatformAbort((tvm_crt_error_t)0xbeef1); + } - int err = ring_buf_put_finish(rbuf, rx_size); - if (err != 0) { - TVMPlatformAbort((tvm_crt_error_t)0xbeef2); + int err = ring_buf_put_finish(rbuf, rx_size); + if (err != 0) { + TVMPlatformAbort((tvm_crt_error_t)0xbeef2); + } + // CHECK_EQ(bytes_read, bytes_written, "bytes_read: %d; bytes_written: %d", bytes_read, + // bytes_written); } - // CHECK_EQ(bytes_read, bytes_written, "bytes_read: %d; bytes_written: %d", bytes_read, - // bytes_written); } } @@ -311,7 +313,10 @@ void main(void) { g_num_bytes_requested = 0; } } - ring_buf_get_finish(&uart_rx_rbuf, bytes_read); + int err = ring_buf_get_finish(&uart_rx_rbuf, bytes_read); + if (err != 0) { + TVMPlatformAbort((tvm_crt_error_t)0xbeef6); + } } irq_unlock(key); }