From 92d9416b4f51938f56a607e3211cccde6e2ee8ca Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Thu, 14 Oct 2021 14:20:24 -0700 Subject: [PATCH 1/4] restructure --- .../standalone_crt/crt_config/crt_config.h | 55 ----------- .../arduino/template_project/boards.json | 59 +++++++++++ .../crt_config/crt_config.h | 0 .../template_project/microtvm_api_server.py | 98 +++++-------------- .../src/example_project}/model.c | 0 .../src/example_project}/model.h | 0 .../src}/example_project/project.ino | 0 .../src/host_driven}/model_support.c | 0 .../src}/host_driven/project.ino | 0 tests/micro/arduino/conftest.py | 20 ++-- 10 files changed, 91 insertions(+), 141 deletions(-) delete mode 100644 apps/microtvm/arduino/host_driven/src/standalone_crt/crt_config/crt_config.h create mode 100644 apps/microtvm/arduino/template_project/boards.json rename apps/microtvm/arduino/{example_project/src/standalone_crt => template_project}/crt_config/crt_config.h (100%) rename apps/microtvm/arduino/{example_project/src => template_project/src/example_project}/model.c (100%) rename apps/microtvm/arduino/{example_project/src => template_project/src/example_project}/model.h (100%) rename apps/microtvm/arduino/{ => template_project/src}/example_project/project.ino (100%) rename apps/microtvm/arduino/{host_driven/src => template_project/src/host_driven}/model_support.c (100%) rename apps/microtvm/arduino/{ => template_project/src}/host_driven/project.ino (100%) diff --git a/apps/microtvm/arduino/host_driven/src/standalone_crt/crt_config/crt_config.h b/apps/microtvm/arduino/host_driven/src/standalone_crt/crt_config/crt_config.h deleted file mode 100644 index cf73103aff8b..000000000000 --- a/apps/microtvm/arduino/host_driven/src/standalone_crt/crt_config/crt_config.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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. - */ - -/*! - * \brief CRT configuration for the host-linked CRT. - */ -#ifndef TVM_RUNTIME_MICRO_CRT_CONFIG_H_ -#define TVM_RUNTIME_MICRO_CRT_CONFIG_H_ - -/*! Log level of the CRT runtime */ -#define TVM_CRT_LOG_LEVEL TVM_CRT_LOG_LEVEL_DEBUG - -/*! Support low-level debugging in MISRA-C runtime */ -#define TVM_CRT_DEBUG 0 - -/*! Maximum supported dimension in NDArray */ -#define TVM_CRT_MAX_NDIM 6 -/*! Maximum supported arguments in generated functions */ -#define TVM_CRT_MAX_ARGS 10 -/*! Maximum supported string length in dltype, e.g. "int8", "int16", "float32" */ -#define TVM_CRT_MAX_STRLEN_DLTYPE 10 -/*! Maximum supported string length in function names */ -#define TVM_CRT_MAX_STRLEN_FUNCTION_NAME 80 - -/*! Maximum number of registered modules. */ -#define TVM_CRT_MAX_REGISTERED_MODULES 2 - -/*! Size of the global function registry, in bytes. */ -#define TVM_CRT_GLOBAL_FUNC_REGISTRY_SIZE_BYTES 512 - -/*! Maximum packet size, in bytes, including the length header. */ -#define TVM_CRT_MAX_PACKET_SIZE_BYTES 8 * 1024 - -/*! \brief Maximum length of a PackedFunc function name. */ -#define TVM_CRT_MAX_FUNCTION_NAME_LENGTH_BYTES 30 - -// #define TVM_CRT_FRAMER_ENABLE_LOGS - -#endif // TVM_RUNTIME_MICRO_CRT_CONFIG_H_ diff --git a/apps/microtvm/arduino/template_project/boards.json b/apps/microtvm/arduino/template_project/boards.json new file mode 100644 index 000000000000..595d56b5f615 --- /dev/null +++ b/apps/microtvm/arduino/template_project/boards.json @@ -0,0 +1,59 @@ +{ + "due": { + "package": "arduino", + "architecture": "sam", + "board": "arduino_due_x_dbg", + "model": "sam3x8e" + }, + "feathers2": { + "package": "esp32", + "architecture": "esp32", + "board": "feathers2", + "model": "esp32", + "note": "Due to the way the Feather S2 bootloader works, compilation behaves fine but uploads cannot be done automatically." + }, + "metrom4": { + "package": "adafruit", + "architecture": "samd", + "board": "adafruit_metro_m4", + "model": "atsamd51" + }, + "spresense": { + "package": "SPRESENSE", + "architecture": "spresense", + "board": "spresense", + "model": "cxd5602gg", + "note": "Spresense only works as of its v2.3.0 sdk." + }, + "nano33ble": { + "package": "arduino", + "architecture": "mbed_nano", + "board": "nano33ble", + "model": "nrf52840" + }, + "pybadge": { + "package": "adafruit", + "architecture": "samd", + "board": "adafruit_pybadge_m4", + "model": "atsamd51" + }, + "teensy40": { + "package": "teensy", + "architecture": "avr", + "board": "teensy40", + "model": "imxrt1060", + "note": "The Teensy boards are listed here for completeness, but they won't work until https://github.com/arduino/arduino-cli/issues/700 is finished." + }, + "teensy41": { + "package": "teensy", + "architecture": "avr", + "board": "teensy41", + "model": "imxrt1060" + }, + "wioterminal": { + "package": "Seeeduino", + "architecture": "samd", + "board": "seeed_wio_terminal", + "model": "atsamd51" + } +} diff --git a/apps/microtvm/arduino/example_project/src/standalone_crt/crt_config/crt_config.h b/apps/microtvm/arduino/template_project/crt_config/crt_config.h similarity index 100% rename from apps/microtvm/arduino/example_project/src/standalone_crt/crt_config/crt_config.h rename to apps/microtvm/arduino/template_project/crt_config/crt_config.h diff --git a/apps/microtvm/arduino/template_project/microtvm_api_server.py b/apps/microtvm/arduino/template_project/microtvm_api_server.py index 3d25d0bcad8f..e285ecc6e3b0 100644 --- a/apps/microtvm/arduino/template_project/microtvm_api_server.py +++ b/apps/microtvm/arduino/template_project/microtvm_api_server.py @@ -44,77 +44,21 @@ IS_TEMPLATE = not (API_SERVER_DIR / MODEL_LIBRARY_FORMAT_RELPATH).exists() +BOARDS = API_SERVER_DIR / "boards.json" + +# Data structure to hold the information microtvm_api_server.py needs +# to communicate with each of these boards. +try: + with open(BOARDS) as boards: + BOARD_PROPERTIES = json.load(boards) +except FileNotFoundError: + raise FileNotFoundError(f"Board file {{{BOARDS}}} does not exist.") + + class BoardAutodetectFailed(Exception): """Raised when no attached hardware is found matching the requested board""" -# Data structure to hold the information microtvm_api_server.py needs -# to communicate with each of these boards. Currently just holds the -# components of each board's FQBN, but might be extended in the future -# to include the SRAM, PSRAM, flash, etc. on each board. -BOARD_PROPERTIES = { - "due": { - "package": "arduino", - "architecture": "sam", - "board": "arduino_due_x_dbg", - "model": "sam3x8e", - }, - # Due to the way the Feather S2 bootloader works, compilation - # behaves fine but uploads cannot be done automatically - "feathers2": { - "package": "esp32", - "architecture": "esp32", - "board": "feathers2", - "model": "esp32", - }, - "metrom4": { - "package": "adafruit", - "architecture": "samd", - "board": "adafruit_metro_m4", - "model": "atsamd51", - }, - # Spresense only works as of its v2.3.0 sdk - "spresense": { - "package": "SPRESENSE", - "architecture": "spresense", - "board": "spresense", - "model": "cxd5602gg", - }, - "nano33ble": { - "package": "arduino", - "architecture": "mbed_nano", - "board": "nano33ble", - "model": "nrf52840", - }, - "pybadge": { - "package": "adafruit", - "architecture": "samd", - "board": "adafruit_pybadge_m4", - "model": "atsamd51", - }, - # The Teensy boards are listed here for completeness, but they - # won't work until https://github.com/arduino/arduino-cli/issues/700 - # is finished - "teensy40": { - "package": "teensy", - "architecture": "avr", - "board": "teensy40", - "model": "imxrt1060", - }, - "teensy41": { - "package": "teensy", - "architecture": "avr", - "board": "teensy41", - "model": "imxrt1060", - }, - "wioterminal": { - "package": "Seeeduino", - "architecture": "samd", - "board": "seeed_wio_terminal", - "model": "atsamd51", - }, -} - PROJECT_TYPES = ["example_project", "host_driven"] PROJECT_OPTIONS = [ @@ -123,11 +67,6 @@ class BoardAutodetectFailed(Exception): choices=list(BOARD_PROPERTIES), help="Name of the Arduino board to build for", ), - server.ProjectOption( - "arduino_model", - choices=[board["model"] for _, board in BOARD_PROPERTIES.items()], - help="Name of the model for each Arduino board.", - ), server.ProjectOption("arduino_cli_cmd", help="Path to the arduino-cli tool."), server.ProjectOption("port", help="Port to use for connecting to hardware"), server.ProjectOption( @@ -166,8 +105,9 @@ def _copy_project_files(self, api_server_dir, project_dir, project_type): so this file is copied separately in generate_project. """ - project_types_folder = api_server_dir.parents[0] - for item in (project_types_folder / project_type / "src").iterdir(): + for item in (API_SERVER_DIR / "src" / project_type).iterdir(): + if item.name == "project.ino": + continue dest = project_dir / "src" / item.name if item.is_dir(): shutil.copytree(item, dest) @@ -176,7 +116,7 @@ def _copy_project_files(self, api_server_dir, project_dir, project_type): # Arduino requires the .ino file have the same filename as its containing folder shutil.copy2( - project_types_folder / project_type / "project.ino", + API_SERVER_DIR / "src" / project_type / "project.ino", project_dir / f"{project_dir.stem}.ino", ) @@ -344,12 +284,20 @@ def generate_project(self, model_library_format_path, standalone_crt_dir, projec # Copies files from the template folder to project_dir shutil.copy2(API_SERVER_DIR / "microtvm_api_server.py", project_dir) + shutil.copy2(BOARDS, project_dir / BOARDS.name) self._copy_project_files(API_SERVER_DIR, project_dir, options["project_type"]) # Copy standalone_crt into src folder self._copy_standalone_crt(source_dir, standalone_crt_dir) self._remove_unused_components(source_dir, options["project_type"]) + # Populate crt-config.h + crt_config_dir = project_dir / "src" / "standalone_crt" / "crt_config" + crt_config_dir.mkdir() + shutil.copy2( + API_SERVER_DIR / "crt_config" / "crt_config.h", crt_config_dir / "crt_config.h" + ) + # Unpack the MLF and copy the relevant files metadata = self._disassemble_mlf(model_library_format_path, source_dir) shutil.copy2(model_library_format_path, source_dir / "model") diff --git a/apps/microtvm/arduino/example_project/src/model.c b/apps/microtvm/arduino/template_project/src/example_project/model.c similarity index 100% rename from apps/microtvm/arduino/example_project/src/model.c rename to apps/microtvm/arduino/template_project/src/example_project/model.c diff --git a/apps/microtvm/arduino/example_project/src/model.h b/apps/microtvm/arduino/template_project/src/example_project/model.h similarity index 100% rename from apps/microtvm/arduino/example_project/src/model.h rename to apps/microtvm/arduino/template_project/src/example_project/model.h diff --git a/apps/microtvm/arduino/example_project/project.ino b/apps/microtvm/arduino/template_project/src/example_project/project.ino similarity index 100% rename from apps/microtvm/arduino/example_project/project.ino rename to apps/microtvm/arduino/template_project/src/example_project/project.ino diff --git a/apps/microtvm/arduino/host_driven/src/model_support.c b/apps/microtvm/arduino/template_project/src/host_driven/model_support.c similarity index 100% rename from apps/microtvm/arduino/host_driven/src/model_support.c rename to apps/microtvm/arduino/template_project/src/host_driven/model_support.c diff --git a/apps/microtvm/arduino/host_driven/project.ino b/apps/microtvm/arduino/template_project/src/host_driven/project.ino similarity index 100% rename from apps/microtvm/arduino/host_driven/project.ino rename to apps/microtvm/arduino/template_project/src/host_driven/project.ino diff --git a/tests/micro/arduino/conftest.py b/tests/micro/arduino/conftest.py index bb9c69bf4a0e..73361774821b 100644 --- a/tests/micro/arduino/conftest.py +++ b/tests/micro/arduino/conftest.py @@ -17,8 +17,9 @@ import datetime import pathlib - +import json import pytest + import tvm.target.target from tvm.micro import project from tvm import micro, relay @@ -34,19 +35,16 @@ / "template_project" ).resolve() +BOARDS = TEMPLATE_PROJECT_DIR / "boards.json" + def arduino_boards() -> dict: """Returns a dict mapping board to target model""" - template = project.TemplateProject.from_directory(TEMPLATE_PROJECT_DIR) - project_options = template.info()["project_options"] - for option in project_options: - if option["name"] == "arduino_board": - boards = option["choices"] - if option["name"] == "arduino_model": - models = option["choices"] - - arduino_boards = {boards[i]: models[i] for i in range(len(boards))} - return arduino_boards + with open(BOARDS) as f: + board_properties = json.load(f) + + boards_model = {board: info["model"] for board, info in board_properties.items()} + return boards_model ARDUINO_BOARDS = arduino_boards() From 095da3cd641d34c61f676f8ed76ed2c0209ad404 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Thu, 14 Oct 2021 14:31:50 -0700 Subject: [PATCH 2/4] readme --- apps/microtvm/arduino/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 apps/microtvm/arduino/README.md diff --git a/apps/microtvm/arduino/README.md b/apps/microtvm/arduino/README.md new file mode 100644 index 000000000000..587d0642b235 --- /dev/null +++ b/apps/microtvm/arduino/README.md @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + +This directory code to interface microTVM with [Arduino](https://www.arduino.cc/). From 2366885673cc8ad3fd5ebf03f44967d74b35bf84 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Mon, 18 Oct 2021 14:39:46 -0700 Subject: [PATCH 3/4] fix readme --- apps/microtvm/arduino/README.md | 2 +- apps/microtvm/zephyr/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/microtvm/arduino/README.md b/apps/microtvm/arduino/README.md index 587d0642b235..b33557b53239 100644 --- a/apps/microtvm/arduino/README.md +++ b/apps/microtvm/arduino/README.md @@ -15,4 +15,4 @@ -This directory code to interface microTVM with [Arduino](https://www.arduino.cc/). +This directory contains code to interface microTVM with [Arduino](https://www.arduino.cc/). diff --git a/apps/microtvm/zephyr/README.md b/apps/microtvm/zephyr/README.md index ad00393c0805..68e9975d4b1c 100644 --- a/apps/microtvm/zephyr/README.md +++ b/apps/microtvm/zephyr/README.md @@ -15,5 +15,5 @@ -This directory code to interface microTVM with the [Zephyr RTOS](https://zephyrproject.org/). +This directory contains code to interface microTVM with the [Zephyr RTOS](https://zephyrproject.org/). From 0f4bdf89f831e15eabe8a2bc2c7adca0e3e626f9 Mon Sep 17 00:00:00 2001 From: Mehrdad Hessar Date: Tue, 19 Oct 2021 10:48:22 -0700 Subject: [PATCH 4/4] trigger