Skip to content

Commit f573e73

Browse files
MaureenHelmdpgeorge
authored andcommitted
zephyr: Build MicroPython as a cmake target.
Refactors the zephyr build infrastructure to build MicroPython as a cmake target, using the recently introduced core cmake rules. This change makes it possible to build the zephyr port like most other zephyr applications using west or cmake directly. It simplifies building with extra cmake arguments, such as specifying an alternate conf file or adding an Arduino shield. It also enables building the zephyr port anywhere in the host file system, which will allow regressing across multiple boards with the zephyr twister script. Signed-off-by: Maureen Helm <[email protected]>
1 parent 51fa133 commit f573e73

File tree

8 files changed

+201
-188
lines changed

8 files changed

+201
-188
lines changed

ports/zephyr/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

ports/zephyr/CMakeLists.txt

Lines changed: 114 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,118 @@
1+
# This file is part of the MicroPython project, http://micropython.org/
2+
#
3+
# The MIT License (MIT)
4+
#
5+
# Copyright 2020 NXP
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
125
cmake_minimum_required(VERSION 3.13.1)
226

327
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
4-
project(NONE)
5-
6-
target_sources(app PRIVATE src/zephyr_start.c src/zephyr_getchar.c)
7-
8-
add_library(libmicropython STATIC IMPORTED)
9-
set_target_properties(libmicropython PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libmicropython.a)
10-
target_link_libraries(app PUBLIC libmicropython)
11-
12-
zephyr_get_include_directories_for_lang_as_string(C includes)
13-
zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
14-
zephyr_get_compile_definitions_for_lang_as_string(C definitions)
15-
zephyr_get_compile_options_for_lang_as_string(C options)
16-
17-
add_custom_target(
18-
outputexports
19-
COMMAND echo CC="${CMAKE_C_COMPILER}"
20-
COMMAND echo AR="${CMAKE_AR}"
21-
COMMAND echo Z_CFLAGS=${system_includes} ${includes} ${definitions} ${options}
22-
VERBATIM
23-
USES_TERMINAL
28+
project(micropython)
29+
30+
set(MICROPY_PORT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
31+
set(MICROPY_DIR ${MICROPY_PORT_DIR}/../..)
32+
set(MICROPY_TARGET micropython)
33+
34+
include(${MICROPY_DIR}/py/py.cmake)
35+
include(${MICROPY_DIR}/extmod/extmod.cmake)
36+
37+
set(MICROPY_SOURCE_PORT
38+
main.c
39+
help.c
40+
machine_i2c.c
41+
machine_pin.c
42+
machine_uart.c
43+
modmachine.c
44+
moduos.c
45+
modusocket.c
46+
modutime.c
47+
modzephyr.c
48+
modzsensor.c
49+
uart_core.c
50+
zephyr_storage.c
51+
)
52+
list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/)
53+
54+
set(MICROPY_SOURCE_LIB
55+
timeutils/timeutils.c
56+
utils/mpirq.c
57+
utils/stdout_helpers.c
58+
utils/printf.c
59+
utils/pyexec.c
60+
utils/interrupt_char.c
61+
mp-readline/readline.c
62+
oofatfs/ff.c
63+
oofatfs/ffunicode.c
64+
littlefs/lfs1.c
65+
littlefs/lfs1_util.c
66+
littlefs/lfs2.c
67+
littlefs/lfs2_util.c
2468
)
69+
list(TRANSFORM MICROPY_SOURCE_LIB PREPEND ${MICROPY_DIR}/lib/)
70+
71+
set(MICROPY_SOURCE_QSTR
72+
${MICROPY_SOURCE_PY}
73+
${MICROPY_SOURCE_EXTMOD}
74+
${MICROPY_SOURCE_LIB}
75+
${MICROPY_SOURCE_PORT}
76+
)
77+
78+
zephyr_get_include_directories_for_lang(C includes)
79+
zephyr_get_system_include_directories_for_lang(C system_includes)
80+
zephyr_get_compile_definitions_for_lang(C definitions)
81+
zephyr_get_compile_options_for_lang(C options)
82+
83+
set(MICROPY_CPP_FLAGS_EXTRA ${includes} ${system_includes} ${definitions} ${options})
84+
85+
zephyr_library_named(${MICROPY_TARGET})
86+
87+
zephyr_library_include_directories(
88+
${MICROPY_DIR}
89+
${MICROPY_PORT_DIR}
90+
${CMAKE_CURRENT_BINARY_DIR}
91+
)
92+
93+
zephyr_library_compile_options(
94+
-std=gnu99 -fomit-frame-pointer
95+
)
96+
97+
zephyr_library_compile_definitions(
98+
NDEBUG
99+
MP_CONFIGFILE=<${CONFIG_MICROPY_CONFIGFILE}>
100+
MICROPY_HEAP_SIZE=${CONFIG_MICROPY_HEAP_SIZE}
101+
FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\"
102+
MICROPY_VFS_FAT=$<BOOL:${CONFIG_MICROPY_VFS_FAT}>
103+
MICROPY_VFS_LFS1=$<BOOL:${CONFIG_MICROPY_VFS_LFS1}>
104+
MICROPY_VFS_LFS2=$<BOOL:${CONFIG_MICROPY_VFS_LFS2}>
105+
)
106+
107+
zephyr_library_sources(${MICROPY_SOURCE_QSTR})
108+
109+
add_dependencies(${MICROPY_TARGET} zephyr_generated_headers)
110+
111+
include(${MICROPY_DIR}/py/mkrules.cmake)
112+
113+
target_sources(app PRIVATE
114+
src/zephyr_start.c
115+
src/zephyr_getchar.c
116+
)
117+
118+
target_link_libraries(app PRIVATE ${MICROPY_TARGET})

ports/zephyr/Kconfig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This file is part of the MicroPython project, http://micropython.org/
2+
#
3+
# The MIT License (MIT)
4+
#
5+
# Copyright 2020 NXP
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
25+
menu "MicroPython Options"
26+
27+
config MICROPY_CONFIGFILE
28+
string "Configuration file"
29+
default "mpconfigport_minimal.h"
30+
31+
config MICROPY_HEAP_SIZE
32+
int "Heap size"
33+
default 16384
34+
35+
config MICROPY_VFS_FAT
36+
bool "FatFS file system"
37+
38+
config MICROPY_VFS_LFS1
39+
bool "LittleFs version 1 file system"
40+
41+
config MICROPY_VFS_LFS2
42+
bool "LittleFs version 2 file system"
43+
44+
endmenu # MicroPython Options
45+
46+
source "Kconfig.zephyr"

ports/zephyr/Makefile

Lines changed: 0 additions & 116 deletions
This file was deleted.

ports/zephyr/README.md

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,48 @@ switch from master to a tagged release, you can instead do:
5050
With Zephyr installed you may then need to configure your environment,
5151
for example by sourcing `zephyrproject/zephyr/zephyr-env.sh`.
5252

53-
Once Zephyr is ready to use you can build the MicroPython port.
54-
In the port subdirectory `ports/zephyr/` run:
53+
Once Zephyr is ready to use you can build the MicroPython port just like any
54+
other Zephyr application. You can do this anywhere in your file system, it does
55+
not have to be in the `ports/zephyr` directory. Assuming you have cloned the
56+
MicroPython repository into your home directory, you can build the Zephyr port
57+
for a frdm_k64f board like this:
5558

56-
$ make BOARD=<board>
59+
$ west build -b frdm_k64f ~/micropython/ports/zephyr
5760

58-
If you don't specify BOARD, the default is `qemu_x86` (x86 target running
59-
in QEMU emulator). Consult the Zephyr documentation above for the list of
61+
To build for QEMU instead:
62+
63+
$ west build -b qemu_x86 ~/micropython/ports/zephyr
64+
65+
Consult the Zephyr documentation above for the list of
6066
supported boards. Board configuration files appearing in `ports/zephyr/boards/`
6167
correspond to boards that have been tested with MicroPython and may have
6268
additional options enabled, like filesystem support.
6369

64-
6570
Running
6671
-------
6772

68-
To run the resulting firmware in QEMU (for BOARDs like qemu_x86,
69-
qemu_cortex_m3):
73+
To flash the resulting firmware to your board:
7074

71-
make run
75+
$ west flash
7276

73-
With the default configuration, networking is now enabled, so you need to
74-
follow instructions in https://wiki.zephyrproject.org/view/Networking-with-Qemu
75-
to setup host side of TAP/SLIP networking. If you get error like:
77+
Or, to flash it to your board and start a gdb debug session:
7678

77-
could not connect serial device to character backend 'unix:/tmp/slip.sock'
79+
$ west debug
7880

79-
it's a sign that you didn't followed instructions above. If you would like
80-
to just run it quickly without extra setup, see "minimal" build below.
81+
To run the resulting firmware in QEMU (for BOARDs like qemu_x86,
82+
qemu_cortex_m3):
8183

82-
For deploying/flashing a firmware on a real board, follow Zephyr
83-
documentation for a given board, including known issues for that board
84-
(if any). (Mind again that networking is enabled for the default build,
85-
so you should know if there're any special requirements in that regard,
86-
cf. for example QEMU networking requirements above; real hardware boards
87-
generally should not have any special requirements, unless there're known
88-
issues).
84+
$ west build -t run
8985

90-
For example, to deploy firmware on the FRDM-K64F board run:
86+
Networking is enabled with the default configuration, so you need to follow
87+
instructions in
88+
https://docs.zephyrproject.org/latest/guides/networking/qemu_setup.html#networking-with-qemu
89+
to setup the host side of TAP/SLIP networking. If you get an error like:
9190

92-
$ make BOARD=frdm_k64f flash
91+
could not connect serial device to character backend 'unix:/tmp/slip.sock'
9392

93+
it's a sign that you didn't follow the instructions above. If you would like
94+
to just run it quickly without extra setup, see "minimal" build below.
9495

9596
Quick example
9697
-------------
@@ -151,9 +152,10 @@ enabled over time.
151152

152153
To make a minimal build:
153154

154-
./make-minimal BOARD=<board>
155+
$ west build -b qemu_x86 ~/micropython/ports/zephyr -- -DCONF_FILE=prj_minimal.conf
155156

156157
To run a minimal build in QEMU without requiring TAP networking setup
157-
run the following after you built image with the previous command:
158+
run the following after you built an image with the previous command:
159+
160+
$ west build -t run
158161

159-
./make-minimal BOARD=<qemu_x86_nommu|qemu_x86|qemu_cortex_m3> run

0 commit comments

Comments
 (0)