Skip to content

Commit 3680b3c

Browse files
authored
[microTVM][Zephyr] Add 'serial_number' option (#13377)
This commit adds a new option 'serial_number' to allow specifying the serial device number in Zephyr, used to flash and to communicate with target boards attached to specific serial ports, and adjusts Zephyr tests to run with this new option. This is particularly useful in an environment where multiple boards are connected to the host. It also removes 'west_cmd' in test arguments, as it is not used, and refactors Zephyr server API to set the required/optional project options at the beginning of each API call.
1 parent dcea36e commit 3680b3c

File tree

14 files changed

+382
-231
lines changed

14 files changed

+382
-231
lines changed

apps/microtvm/zephyr/template_project/CMakeLists.txt.template

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ set(QEMU_PIPE <QEMU_PIPE> CACHE PATH "Path to QEMU pipe")
2828
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
2929
project(microtvm_autogenerated_project)
3030

31-
if(${ENABLE_CMSIS})
32-
set(CMSIS_PATH <CMSIS_PATH>)
33-
31+
if(DEFINED CMSIS_PATH)
3432
file(GLOB_RECURSE cmsis_lib_srcs
3533
${CMSIS_PATH}/CMSIS/NN/Source/ActivationFunctions/*.c
3634
${CMSIS_PATH}/CMSIS/NN/Source/BasicMathFunctions/*.c

apps/microtvm/zephyr/template_project/microtvm_api_server.py

Lines changed: 116 additions & 76 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
"""Test Project API in different platforms infrastructure."""

tests/micro/project_api/test_project_api.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from tvm.relay.backend import Runtime
2525
from tvm.micro.testing import get_target
2626

27+
from .utils import build_project_api
28+
2729
API_GENERATE_PROJECT = "generate_project"
2830
API_BUILD = "build"
2931
API_FLASH = "flash"
@@ -55,37 +57,7 @@ def test_default_options_exist(platform):
5557
@tvm.testing.requires_micro
5658
def test_project_minimal_options(platform):
5759
"""Test template project with minimum projectOptions"""
58-
shape = (10,)
59-
dtype = "int8"
60-
x = relay.var("x", relay.TensorType(shape=shape, dtype=dtype))
61-
xx = relay.multiply(x, x)
62-
z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype)))
63-
func = relay.Function([x], z)
64-
ir_mod = tvm.IRModule.from_expr(func)
65-
66-
if platform == "arduino":
67-
board = "due"
68-
elif platform == "zephyr":
69-
board = "qemu_x86"
70-
71-
runtime = Runtime("crt", {"system-lib": True})
72-
target = get_target(platform, board)
73-
with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
74-
mod = tvm.relay.build(ir_mod, target=target, runtime=runtime)
75-
76-
project_options = {
77-
"project_type": "host_driven",
78-
"board": board,
79-
}
80-
81-
temp_dir = tvm.contrib.utils.tempdir()
82-
project = tvm.micro.generate_project(
83-
tvm.micro.get_microtvm_template_projects(platform),
84-
mod,
85-
temp_dir / "project",
86-
project_options,
87-
)
88-
project.build()
60+
build_project_api(platform)
8961

9062

9163
if __name__ == "__main__":
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import os
19+
20+
import tvm
21+
22+
from .utils import build_project_api
23+
24+
25+
@tvm.testing.requires_micro
26+
def test_option_cmsis_path():
27+
"""Test project API without CMSIS_PATH environment variable."""
28+
cmsis_path = os.environ.get("CMSIS_PATH", None)
29+
del os.environ["CMSIS_PATH"]
30+
build_project_api("zephyr")
31+
os.environ["CMSIS_PATH"] = cmsis_path

tests/micro/project_api/utils.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import numpy as np
19+
20+
import tvm
21+
from tvm import relay
22+
from tvm.relay.backend import Runtime
23+
from tvm.micro.testing import get_target
24+
25+
26+
def build_project_api(platform: str):
27+
"""Build a relay module with Project API."""
28+
shape = (10,)
29+
dtype = "int8"
30+
x = relay.var("x", relay.TensorType(shape=shape, dtype=dtype))
31+
xx = relay.multiply(x, x)
32+
z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype)))
33+
func = relay.Function([x], z)
34+
ir_mod = tvm.IRModule.from_expr(func)
35+
36+
if platform == "arduino":
37+
board = "due"
38+
elif platform == "zephyr":
39+
board = "qemu_x86"
40+
41+
runtime = Runtime("crt", {"system-lib": True})
42+
target = get_target(platform, board)
43+
with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
44+
mod = tvm.relay.build(ir_mod, target=target, runtime=runtime)
45+
46+
project_options = {
47+
"project_type": "host_driven",
48+
"board": board,
49+
}
50+
51+
temp_dir = tvm.contrib.utils.tempdir()
52+
project = tvm.micro.generate_project(
53+
tvm.micro.get_microtvm_template_projects(platform),
54+
mod,
55+
temp_dir / "project",
56+
project_options,
57+
)
58+
project.build()

tests/micro/zephyr/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ To see the list of supported values for `--board`, run:
4040
```
4141
$ pytest test_zephyr.py --help
4242
```
43+
44+
If you like to test with a real hardware, you have the option to pass the serial number
45+
for your development board.
46+
```
47+
$ pytest test_zephyr.py --board=nrf5340dk_nrf5340_cpuapp --serial="0672FF5"
48+
```

tests/micro/zephyr/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
""" Testing infrastructure for microTVM Zephyr """

tests/micro/zephyr/conftest.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,29 @@
2323

2424

2525
def pytest_addoption(parser):
26-
parser.addoption(
27-
"--west-cmd", default="west", help="Path to `west` command for flashing device."
28-
)
2926
parser.addoption(
3027
"--use-fvp",
3128
action="store_true",
3229
default=False,
3330
help="If set true, use the FVP emulator to run the test",
3431
)
35-
36-
37-
@pytest.fixture(scope="session")
38-
def west_cmd(request):
39-
return request.config.getoption("--west-cmd")
32+
parser.addoption(
33+
"--serial",
34+
default=None,
35+
help="If set true, use the FVP emulator to run the test",
36+
)
4037

4138

4239
@pytest.fixture
4340
def use_fvp(request):
4441
return request.config.getoption("--use-fvp")
4542

4643

44+
@pytest.fixture
45+
def serial_number(request):
46+
return request.config.getoption("--serial")
47+
48+
4749
@pytest.fixture(autouse=True)
4850
def xfail_on_fvp(request, use_fvp):
4951
"""mark the tests as xfail if running on fvp."""

0 commit comments

Comments
 (0)