Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrdadh committed May 24, 2021
1 parent 669f71c commit e625e64
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions apps/microtvm/zephyr/demo_runtime/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit e625e64

Please sign in to comment.