Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[microTVM] AOT Demo #8075

Merged
merged 20 commits into from May 28, 2021
Merged
Expand Up @@ -10,8 +10,8 @@ find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
project(microtvm_zephyr_runtime)

set(CMAKE_VERBOSE_MAKEFILE ON)
file(GLOB TVM_SOURCES ${CMAKE_SOURCE_DIR}/__tvm*.c)
target_sources(app PRIVATE src/main.c ${TVM_SOURCES})

target_sources(app PRIVATE src/main.c)

foreach(tvm_lib ${TVM_LIBS})
string(LENGTH ${tvm_lib} tvm_lib_length)
Expand Down
20 changes: 20 additions & 0 deletions apps/microtvm/zephyr/aot_demo/README.md
@@ -0,0 +1,20 @@
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
<!--- or more contributor license agreements. See the NOTICE file -->
<!--- distributed with this work for additional information -->
<!--- regarding copyright ownership. The ASF licenses this file -->
<!--- to you under the Apache License, Version 2.0 (the -->
<!--- "License"); you may not use this file except in compliance -->
<!--- with the License. You may obtain a copy of the License at -->

<!--- http://www.apache.org/licenses/LICENSE-2.0 -->

<!--- Unless required by applicable law or agreed to in writing, -->
<!--- software distributed under the License is distributed on an -->
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
<!--- KIND, either express or implied. See the License for the -->
<!--- specific language governing permissions and limitations -->
<!--- under the License. -->

This directory contains a Zephyr-based ahead of time (AOT) "demo" runtime environment that
pulls together the microTVM runtime dependencies into a single application
that can run TVM on a microTVM device without the need to a host.
31 changes: 31 additions & 0 deletions apps/microtvm/zephyr/aot_demo/boards/nrf5340dk_nrf5340_cpuapp.conf
@@ -0,0 +1,31 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# This file is specific to the nRF5340 DK board.

# For intrinsics used by generated optimized operators.
CONFIG_CMSIS_DSP=y

# For AOT runtime which requires lots of function call.
CONFIG_MAIN_STACK_SIZE=18000

# For random number generation.
CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y

# For debugging.
CONFIG_LED=y
25 changes: 25 additions & 0 deletions apps/microtvm/zephyr/aot_demo/boards/qemu_x86.conf
@@ -0,0 +1,25 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# This file is specific to the QEMU-emulated microTVM board.

# For TVMPlatformGenerateRandom(). Remember, these values do not need to be truly random.
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_TIMER_RANDOM_GENERATOR=y

# Default stack size is 1k, this is required for debug mode.
CONFIG_MAIN_STACK_SIZE=2000
mehrdadh marked this conversation as resolved.
Show resolved Hide resolved
84 changes: 84 additions & 0 deletions apps/microtvm/zephyr/aot_demo/include/zephyr_uart.h
@@ -0,0 +1,84 @@
/*
mehrdadh marked this conversation as resolved.
Show resolved Hide resolved
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#ifndef TVM_APPS_MICROTVM_ZEPHYR_AOT_DEMO_INCLUDE_ZEPHYR_UART_H_
#define TVM_APPS_MICROTVM_ZEPHYR_AOT_DEMO_INCLUDE_ZEPHYR_UART_H_

#include <drivers/uart.h>
#include <sys/ring_buffer.h>

static const struct device* g_utvm_uart;
#define RING_BUF_SIZE_BYTES (TVM_CRT_MAX_PACKET_SIZE_BYTES + 100)

// Ring buffer used to store data read from the UART on rx interrupt.
RING_BUF_DECLARE(uart_rx_rbuf, RING_BUF_SIZE_BYTES);

size_t write_serial(const char* data, size_t size) {
for (size_t i = 0; i < size; i++) {
uart_poll_out(g_utvm_uart, data[i]);
}
return size;
}

static uint8_t uart_data[8];
// UART interrupt callback.
void uart_irq_cb(const struct device* dev, void* user_data) {
while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
struct ring_buf* rbuf = (struct ring_buf*)user_data;
if (uart_irq_rx_ready(dev) != 0) {
for (;;) {
// Read a small chunk of data from the UART.
int bytes_read = uart_fifo_read(dev, uart_data, sizeof(uart_data));
if (bytes_read < 0) {
TVMPlatformAbort((tvm_crt_error_t)(0xbeef1));
} else if (bytes_read == 0) {
break;
}
// Write it into the ring buffer.
int bytes_written = ring_buf_put(rbuf, uart_data, bytes_read);
if (bytes_read != bytes_written) {
TVMPlatformAbort((tvm_crt_error_t)(0xbeef2));
}
}
}
}
}

// Used to initialize the UART receiver.
void uart_rx_init(struct ring_buf* rbuf, const struct device* dev) {
uart_irq_callback_user_data_set(dev, uart_irq_cb, (void*)rbuf);
uart_irq_rx_enable(dev);
}

// Used to read data from the UART.
int uart_rx_buf_read(uint8_t* data, size_t data_size_bytes) {
unsigned int key = irq_lock();
int bytes_read = ring_buf_get(&uart_rx_rbuf, data, data_size_bytes);
irq_unlock(key);
return bytes_read;
}

// Initialize UART
void TVMPlatformUARTInit() {
// Claim console device.
g_utvm_uart = device_get_binding(DT_LABEL(DT_CHOSEN(zephyr_console)));
uart_rx_init(&uart_rx_rbuf, g_utvm_uart);
}

#endif /* TVM_APPS_MICROTVM_ZEPHYR_AOT_DEMO_INCLUDE_ZEPHYR_UART_H_ */
1 change: 1 addition & 0 deletions apps/microtvm/zephyr/aot_demo/qemu-hack