guberti commented on a change in pull request #8708:
URL: https://github.com/apache/tvm/pull/8708#discussion_r687241877
##########
File path: apps/microtvm/arduino/template_project/microtvm_api_server.py
##########
@@ -0,0 +1,423 @@
+import collections
+import functools
+import json
+import logging
+import os
+import os.path
+import pathlib
+import re
+import shlex
+import shutil
+import subprocess
+import sys
+import tarfile
+from string import Template
+import tempfile
+import time
+
+import serial
+import serial.tools.list_ports
+
+from tvm.micro.project_api import server
+
+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()
+
+
+class InvalidPortException(Exception):
+ """Raised when the given port could not be opened"""
+
+
+class SketchUploadException(Exception):
+ """Raised when a sketch cannot be uploaded for an unknown reason."""
+
+
+class BoardAutodetectFailed(Exception):
+ """Raised when no attached hardware is found matching the requested
board"""
+
+
+BOARD_PROPERTIES = {
+ "due": {"package": "arduino", "architecture": "sam", "board":
"arduino_due_x"},
+ # 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",
+ },
+ # Spresense only works as of its v2.3.0 sdk
+ "spresense": {
+ "package": "SPRESENSE",
+ "architecture": "spresense",
+ "board": "spresense",
+ },
+ "nano33ble": {
+ "package": "arduino",
+ "architecture": "mbed_nano",
+ "board": "nano33ble",
+ },
+ "pybadge": {
+ "package": "adafruit",
+ "architecture": "samd",
+ "board": "adafruit_pybadge_m4",
+ },
+ # 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",
+ },
+ "teensy41": {
+ "package": "teensy",
+ "architecture": "avr",
+ "board": "teensy41",
+ },
+}
+
+PROJECT_TYPES = ["example_project", "host_driven"]
+
+PROJECT_OPTIONS = [
+ server.ProjectOption(
+ "arduino_board",
+ choices=list(BOARD_PROPERTIES),
+ help="Name of the Arduino board to build for",
+ ),
+ 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(
+ "example_project",
+ help="Type of project to generate.",
+ choices=tuple(PROJECT_TYPES),
+ ),
+ server.ProjectOption(
+ "verbose", help="True to pass --verbose flag to arduino-cli compile
and upload"
+ ),
+]
+
+
+class Handler(server.ProjectAPIHandler):
+ def __init__(self):
+ super(Handler, self).__init__()
+ self._proc = None
+ self._port = None
+ self._serial = None
+
+ def server_info_query(self, tvm_version):
+ return server.ServerInfo(
+ platform_name="arduino",
+ is_template=IS_TEMPLATE,
+ model_library_format_path=MODEL_LIBRARY_FORMAT_PATH,
+ project_options=PROJECT_OPTIONS,
+ )
+
+ def _copy_project_files(self, api_server_dir, project_dir, project_type):
+ project_types_folder = api_server_dir.parents[0]
+ shutil.copytree(
+ project_types_folder / project_type / "src", project_dir / "src",
dirs_exist_ok=True
+ )
+ # Arduino requires the .ino file have the same filename as its
containing folder
+ shutil.copy2(
+ project_types_folder / project_type / "project.ino",
+ project_dir / f"{project_dir.stem}.ino",
+ )
+
+ CRT_COPY_ITEMS = ("include", "src")
+
+ def _copy_standalone_crt(self, source_dir, standalone_crt_dir):
+ # Copy over the standalone_crt directory
+ output_crt_dir = source_dir / "standalone_crt"
+ for item in self.CRT_COPY_ITEMS:
+ src_path = os.path.join(standalone_crt_dir, item)
+ dst_path = output_crt_dir / item
+ if os.path.isdir(src_path):
+ shutil.copytree(src_path, dst_path)
+ else:
+ shutil.copy2(src_path, dst_path)
+
+ # Example project is the "minimum viable project",
+ # and doesn't need a fancy RPC server
+ EXAMPLE_PROJECT_UNUSED_COMPONENTS = [
+ "include/dmlc",
+ "src/support",
+ "src/runtime/minrpc",
+ "src/runtime/crt/graph_executor",
+ "src/runtime/crt/microtvm_rpc_common",
+ "src/runtime/crt/microtvm_rpc_server",
+ "src/runtime/crt/tab",
+ ]
+
+ def _remove_unused_components(self, source_dir, project_type):
+ unused_components = []
+ if project_type == "example_project":
+ unused_components = self.EXAMPLE_PROJECT_UNUSED_COMPONENTS
+
+ for component in unused_components:
+ shutil.rmtree(source_dir / "standalone_crt" / component)
+
+ def _disassemble_mlf(self, mlf_tar_path, source_dir):
+ with tempfile.TemporaryDirectory() as mlf_unpacking_dir_str:
+ mlf_unpacking_dir = pathlib.Path(mlf_unpacking_dir_str)
+ with tarfile.open(mlf_tar_path, "r:") as tar:
+ tar.extractall(mlf_unpacking_dir)
+
+ model_dir = source_dir / "model"
+ model_dir.mkdir()
+
+ # Copy C files from model. The filesnames and quantity
+ # depend on the target string, so we just copy all c files
+ source_dir = mlf_unpacking_dir / "codegen" / "host" / "src"
+ for file in source_dir.rglob(f"*.c"):
+ shutil.copy(file, model_dir)
+
+ # Return metadata.json for use in templating
+ with open(os.path.join(mlf_unpacking_dir, "metadata.json")) as f:
+ metadata = json.load(f)
+ return metadata
+
+ def _template_model_header(self, source_dir, metadata):
+ with open(source_dir / "model.h", "r") as f:
+ model_h_template = Template(f.read())
+
+ template_values = {
+ "workspace_size_bytes": metadata["memory"]["functions"]["main"][0][
Review comment:
Added an assert and a comment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]