Skip to content

Commit 4fb708e

Browse files
authored
Merge pull request #10847 from tannewt/zephyr_host_networking
Add networking support to the Zephyr native_sim
2 parents 5ea6e09 + 20d9e18 commit 4fb708e

File tree

42 files changed

+910
-425
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+910
-425
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,7 @@ TAGS
113113

114114
# git-review-web outputs
115115
.review
116+
117+
# Zephyr trace files
118+
**/channel0_0
119+
**/*.perfetto-trace

locale/circuitpython.pot

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,10 +2067,13 @@ msgstr ""
20672067

20682068
#: ports/espressif/common-hal/socketpool/SocketPool.c
20692069
#: ports/raspberrypi/common-hal/socketpool/SocketPool.c
2070-
#: ports/zephyr-cp/common-hal/socketpool/SocketPool.c
20712070
msgid "SocketPool can only be used with wifi.radio"
20722071
msgstr ""
20732072

2073+
#: ports/zephyr-cp/common-hal/socketpool/SocketPool.c
2074+
msgid "SocketPool can only be used with wifi.radio or hostnetwork.HostNetwork"
2075+
msgstr ""
2076+
20742077
#: shared-bindings/aesio/aes.c
20752078
msgid "Source and destination buffers must be the same length"
20762079
msgstr ""

ports/zephyr-cp/Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ BUILD ?= build-$(BOARD)
88

99
TRANSLATION ?= en_US
1010

11-
12-
.PHONY: $(BUILD)/zephyr-cp/zephyr/zephyr.elf flash recover debug run clean menuconfig all clean-all test fetch-port-submodules
11+
.PHONY: $(BUILD)/zephyr-cp/zephyr/zephyr.elf flash recover debug run run-sim clean menuconfig all clean-all test fetch-port-submodules
1312

1413
$(BUILD)/zephyr-cp/zephyr/zephyr.elf:
1514
python cptools/pre_zephyr_build_prep.py $(BOARD)
@@ -36,6 +35,16 @@ debug: $(BUILD)/zephyr-cp/zephyr/zephyr.elf
3635
run: $(BUILD)/firmware.exe
3736
$^
3837

38+
run-sim:
39+
$(MAKE) BOARD=native_native_sim BUILD=build-native_native_sim build-native_native_sim/firmware.exe
40+
truncate -s 2M build-native_native_sim/flash.bin
41+
mformat -i build-native_native_sim/flash.bin ::
42+
@if [ -d CIRCUITPY ] && [ -n "$$(find CIRCUITPY -mindepth 1 -print -quit)" ]; then \
43+
echo "Populating build-native_native_sim/flash.bin from ./CIRCUITPY"; \
44+
mcopy -s -i build-native_native_sim/flash.bin CIRCUITPY/* ::; \
45+
fi
46+
build-native_native_sim/firmware.exe --flash=build-native_native_sim/flash.bin --flash_rm -wait_uart -rt
47+
3948
menuconfig:
4049
west build --sysbuild -d $(BUILD) -t menuconfig
4150

ports/zephyr-cp/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ make BOARD=nordic_nrf7002dk
2828
This uses Zephyr's cmake to generate Makefiles that then delegate to
2929
`tools/cpbuild/build_circuitpython.py` to build the CircuitPython bits in parallel.
3030

31+
## Running the native simulator
32+
33+
From `ports/zephyr-cp`, run:
34+
35+
```sh
36+
make run-sim
37+
```
38+
39+
`run-sim` starts the native simulator in realtime.
40+
It prints the PTY path to connect to the simulator REPL.
41+
If a local `./CIRCUITPY/` folder exists, its files are used as the simulator's CIRCUITPY drive.
42+
43+
Edit files in `./CIRCUITPY` (for example `code.py`) and rerun `make run-sim` to test changes.
44+
3145
## Testing other boards
3246

3347
[Any Zephyr board](https://docs.zephyrproject.org/latest/boards/index.html#) can
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "bindings/hostnetwork/HostNetwork.h"
8+
9+
#include "py/runtime.h"
10+
11+
//| class HostNetwork:
12+
//| """Native networking for the host simulator."""
13+
//|
14+
//| def __init__(self) -> None:
15+
//| """Create a HostNetwork instance."""
16+
//| ...
17+
//|
18+
static mp_obj_t hostnetwork_hostnetwork_make_new(const mp_obj_type_t *type,
19+
size_t n_args, size_t n_kw, const mp_obj_t *args) {
20+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
21+
22+
hostnetwork_hostnetwork_obj_t *self = mp_obj_malloc(hostnetwork_hostnetwork_obj_t, &hostnetwork_hostnetwork_type);
23+
common_hal_hostnetwork_hostnetwork_construct(self);
24+
return MP_OBJ_FROM_PTR(self);
25+
}
26+
27+
static const mp_rom_map_elem_t hostnetwork_hostnetwork_locals_dict_table[] = {
28+
};
29+
static MP_DEFINE_CONST_DICT(hostnetwork_hostnetwork_locals_dict, hostnetwork_hostnetwork_locals_dict_table);
30+
31+
MP_DEFINE_CONST_OBJ_TYPE(
32+
hostnetwork_hostnetwork_type,
33+
MP_QSTR_HostNetwork,
34+
MP_TYPE_FLAG_NONE,
35+
make_new, hostnetwork_hostnetwork_make_new,
36+
locals_dict, &hostnetwork_hostnetwork_locals_dict
37+
);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#pragma once
8+
9+
#include <stdint.h>
10+
11+
#include "py/obj.h"
12+
13+
typedef struct {
14+
mp_obj_base_t base;
15+
} hostnetwork_hostnetwork_obj_t;
16+
17+
extern const mp_obj_type_t hostnetwork_hostnetwork_type;
18+
19+
void common_hal_hostnetwork_hostnetwork_construct(hostnetwork_hostnetwork_obj_t *self);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "py/obj.h"
8+
9+
#include "bindings/hostnetwork/__init__.h"
10+
#include "bindings/hostnetwork/HostNetwork.h"
11+
12+
//| """Host networking support for the native simulator."""
13+
//|
14+
15+
static const mp_rom_map_elem_t hostnetwork_module_globals_table[] = {
16+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hostnetwork) },
17+
{ MP_ROM_QSTR(MP_QSTR_HostNetwork), MP_ROM_PTR(&hostnetwork_hostnetwork_type) },
18+
};
19+
static MP_DEFINE_CONST_DICT(hostnetwork_module_globals, hostnetwork_module_globals_table);
20+
21+
const mp_obj_module_t hostnetwork_module = {
22+
.base = { &mp_type_module },
23+
.globals = (mp_obj_dict_t *)&hostnetwork_module_globals,
24+
};
25+
26+
MP_REGISTER_MODULE(MP_QSTR_hostnetwork, hostnetwork_module);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#pragma once
8+
9+
#include "bindings/hostnetwork/HostNetwork.h"
10+
11+
extern hostnetwork_hostnetwork_obj_t common_hal_hostnetwork_obj;

ports/zephyr-cp/boards/frdm_rw612.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ CONFIG_MBEDTLS_PKCS1_V15=y
2020
CONFIG_MBEDTLS_KEY_EXCHANGE_RSA_ENABLED=y
2121
CONFIG_MBEDTLS_ENTROPY_C=y
2222
CONFIG_MBEDTLS_CTR_DRBG_ENABLED=y
23+
CONFIG_MBEDTLS_SHA1=y
2324
CONFIG_MBEDTLS_USE_PSA_CRYPTO=n
2425

2526
CONFIG_BT=y

ports/zephyr-cp/boards/native/native_sim/autogen_board_info.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ frequencyio = false
4949
getpass = false
5050
gifio = false
5151
gnss = false
52-
hashlib = false
52+
hashlib = true # Zephyr networking enabled
53+
hostnetwork = true # Zephyr board has hostnetwork
5354
i2cdisplaybus = true # Zephyr board has busio
5455
i2cioexpander = false
5556
i2ctarget = false
@@ -87,7 +88,7 @@ rtc = false
8788
sdcardio = true # Zephyr board has busio
8889
sdioio = false
8990
sharpdisplay = true # Zephyr board has busio
90-
socketpool = false
91+
socketpool = true # Zephyr networking enabled
9192
spitarget = false
9293
ssl = false
9394
storage = true # Zephyr board has flash

0 commit comments

Comments
 (0)