Skip to content
Merged
29 changes: 29 additions & 0 deletions apps/microtvm/arduino/template_project/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@
import serial.tools.list_ports
from tvm.micro.project_api import server

_LOG = logging.getLogger(__name__)

MODEL_LIBRARY_FORMAT_RELPATH = pathlib.Path("src") / "model" / "model.tar"
API_SERVER_DIR = pathlib.Path(os.path.dirname(__file__) or os.path.getcwd())
BUILD_DIR = API_SERVER_DIR / "build"
MODEL_LIBRARY_FORMAT_PATH = API_SERVER_DIR / MODEL_LIBRARY_FORMAT_RELPATH

IS_TEMPLATE = not (API_SERVER_DIR / MODEL_LIBRARY_FORMAT_RELPATH).exists()

# Used to check Arduino CLI version installed on the host.
# We only check two levels of the version.
ARDUINO_CLI_VERSION = 0.18


BOARDS = API_SERVER_DIR / "boards.json"

Expand Down Expand Up @@ -77,6 +83,11 @@ class BoardAutodetectFailed(Exception):
server.ProjectOption(
"verbose", help="True to pass --verbose flag to arduino-cli compile and upload"
),
server.ProjectOption(
"warning_as_error",
choices=(True, False),
help="Treat warnings as errors and raise an Exception.",
),
]


Expand Down Expand Up @@ -275,7 +286,25 @@ def _find_modified_include_path(self, project_dir, file_path, include_path):
# It's probably a standard C/C++ header
return include_path

def _get_platform_version(self, arduino_cli_path: str) -> float:
version_output = subprocess.check_output([arduino_cli_path, "version"], encoding="utf-8")
version_output = (
version_output.replace("\n", "").replace("\r", "").replace(":", "").lower().split(" ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is this line stripping out? Can we use a regex or .strip()? I'd love to see a comment here with the standard output of arduino-cli version so I can better understand what's happening here.

Copy link
Member Author

@mehrdadh mehrdadh Oct 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review. I will add the format as a comment and change it to use regex in the following PR which is waiting for this:
#9309

)
full_version = version_output[version_output.index("version") + 1].split(".")
version = float(f"{full_version[0]}.{full_version[1]}")

return version

def generate_project(self, model_library_format_path, standalone_crt_dir, project_dir, options):
# Check Arduino version
version = self._get_platform_version(options["arduino_cli_cmd"])
if version != ARDUINO_CLI_VERSION:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure we should do an exact match check. cc @guberti do you have a better suggestion as to how loose we should be? i could even just be okay with a warning

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think two levels of version, Major + Minor, is enough. We don't need to check the patch level version since they usually don't change interfaces. I discussed this with @gromero and that seems to be the case for Zephyr. For Arduino I got the same impression based on their release messages: https://github.com/arduino/arduino-cli/releases
@guberti probably has more info here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea the Arduino CLI seems to be relatively stable - this looks fine to me.

message = f"Arduino CLI version found is not supported: found {version}, expected {ARDUINO_CLI_VERSION}."
if options.get("warning_as_error") is not None and options["warning_as_error"]:
raise server.ServerError(message=message)
_LOG.warning(message)

# Reference key directories with pathlib
project_dir = pathlib.Path(project_dir)
project_dir.mkdir()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ cd ~
sudo apt-get install -y ca-certificates

# Install Arduino-CLI (specific version)
# To keep in sync with the version
# defined in apps/microtvm/arduino/template_project/microtvm_api_server.py
ARDUINO_CLI_VERSION="0.18.3"

export PATH="/home/vagrant/bin:$PATH"
wget -O - https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh -s 0.18.3
wget -O - https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh -s ${ARDUINO_CLI_VERSION}

# Arduino (the CLI and GUI) require the dialout permission for uploading
sudo usermod -a -G dialout $USER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ source ~/.profile
# Init Zephyr
cd ~
# Using most recent commit that passes all the tests.
~/ubuntu_init_zephyr_project.sh ~/zephyr v2.5-branch --commit dabf23758417fd041fec2a2a821d8f526afac29d
ZEPHYR_VERSION="v2.5-branch"
~/ubuntu_init_zephyr_project.sh ~/zephyr ${ZEPHYR_VERSION} --commit dabf23758417fd041fec2a2a821d8f526afac29d

# Cleanup
rm -f *.sh
30 changes: 30 additions & 0 deletions apps/microtvm/zephyr/template_project/microtvm_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@

BOARDS = API_SERVER_DIR / "boards.json"

# Used to check Zephyr version installed on the host.
# We only check two levels of the version.
ZEPHYR_VERSION = 2.5


# Data structure to hold the information microtvm_api_server.py needs
# to communicate with each of these boards.
try:
Expand Down Expand Up @@ -265,6 +270,11 @@ def _get_nrf_device_args(options):
"config_main_stack_size",
help="Sets CONFIG_MAIN_STACK_SIZE for Zephyr board.",
),
server.ProjectOption(
"warning_as_error",
choices=(True, False),
help="Treat warnings as errors and raise an Exception.",
),
]


Expand Down Expand Up @@ -342,7 +352,27 @@ def _create_prj_conf(self, project_dir, options):
"aot_demo": "memory microtvm_rpc_common common",
}

def _get_platform_version(self) -> float:
with open(pathlib.Path(os.getenv("ZEPHYR_BASE")) / "VERSION", "r") as f:
lines = f.readlines()
for line in lines:
line = line.replace(" ", "").replace("\n", "").replace("\r", "")
if "VERSION_MAJOR" in line:
version_major = line.split("=")[1]
if "VERSION_MINOR" in line:
version_minor = line.split("=")[1]

return float(f"{version_major}.{version_minor}")

def generate_project(self, model_library_format_path, standalone_crt_dir, project_dir, options):
# Check Zephyr version
version = self._get_platform_version()
if version != ZEPHYR_VERSION:
message = f"Zephyr version found is not supported: found {version}, expected {ZEPHYR_VERSION}."
if options.get("warning_as_error") is not None and options["warning_as_error"]:
raise server.ServerError(message=message)
_LOG.warning(message)

project_dir = pathlib.Path(project_dir)
# Make project directory.
project_dir.mkdir()
Expand Down
5 changes: 3 additions & 2 deletions docker/install/ubuntu_install_arduino.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ set -o pipefail
export DEBIAN_FRONTEND=noninteractive
apt-get install -y ca-certificates

# Install arduino-cli latest version
wget -O - https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh -s
ARDUINO_CLI_VERSION="0.18.3"
# Install arduino-cli
wget -O - https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh -s ${ARDUINO_CLI_VERSION}

# Install the cores we want to test on
arduino-cli core install arduino:mbed_nano
Expand Down
6 changes: 5 additions & 1 deletion docker/install/ubuntu_install_zephyr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ sudo apt-get install -y cmake
pip3 install west

# Init ZephyrProject
# To keep in sync with the version
# defined in apps/microtvm/zephyr/template_project/microtvm_api_server.py
# We use `-branch` tag since it tracks the same version with extra patches for bugs.
ZEPHYR_VERSION="v2.5-branch"
ZEPHYR_PROJECT_PATH=/opt/zephyrproject
ZEPHYR_INIT_SCRIPT=$(find -name "ubuntu_init_zephyr_project.sh")
bash ${ZEPHYR_INIT_SCRIPT} ${ZEPHYR_PROJECT_PATH} v2.5-branch
bash ${ZEPHYR_INIT_SCRIPT} ${ZEPHYR_PROJECT_PATH} ${ZEPHYR_VERSION}
cd ${ZEPHYR_PROJECT_PATH}

# As part of the build process, Zephyr needs to touch some symlinks in zephyr/misc/generated/syscalls_links (this path is relative to the
Expand Down