Enabling Wifi on nRF7002

Hello everyone,

I'm trying to set up the Wifi stack on the nRF7002 and use it with MicroPython, but encountering a lot of difficulty with it. I cropped up a network_wlan.c file and added it to the build. I configured the CMakeLists.txt, prj.conf and nrf7002dk_nrf5340_cpuapp_ns.conf to the best of my knowledge (based on the given wifi samples) but I've hit a roadblock.

Since I've got no prior experience with this board or Zephyr, I decided to start by just enabling the Wifi_scan functionality.

Right now I can "choose" which error I'd like to see:
1). Given this "main_scan()" function which I'm calling via REPL to use in microPy:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
STATIC mp_obj_t main_scan(void)
{
scan_result = 0U;
net_mgmt_init_event_callback(&wifi_shell_mgmt_cb,
wifi_mgmt_event_handler,
WIFI_SHELL_MGMT_EVENTS);
net_mgmt_add_event_callback(&wifi_shell_mgmt_cb);
k_sleep(K_SECONDS(1));
printk("Starting %s with CPU frequency: %d MHz\n", CONFIG_BOARD, SystemCoreClock / MHZ(1));
if (net_if_get_default() == NULL) {
printk("Bad!!! \n");
return 0;
}
if (!is_mac_addr_set(net_if_get_default())) {
printk("0000\n");
struct net_if *iface = net_if_get_default();
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


When I try to invoke it via these commands, I get a Secure Fault:
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> [00:00:00.000,000] <dbg> counteMicroPython c44a93be3-dirty on 2025-04-18; zephyr-nrf7002dk with nrf5340
Type "help()" for more information.
>>>
>>>
>>>
>>>
>>> import network
>>>
>>> w = network.WLAN(network.AP_IF)
>>>
>>> w.scanwifi()
r_rtc.0: init_rtc: Initialized
--- 99 messages dropped ---
[00:00:00.473,236] <dbg> os: z_impl_k_mutex_lock: 0x20032370 took mutex 0x20030d64, count: 1, orig prio: 0
[00:00:00.473,266] <dbg> os: z_impl_k_mutex_unlock: mutex 0x20030d64 lock_count: 1
[00:00:00.473,297] <dbg> os: z_impl_k_mutex_unlock: new owner of mutex 0x20030d64: (nil) (prio: -1000)
[00:00:00.473,480] <inf> wifi_supplicant: wpa_supplicant initialized
[00:00:00.474,151] <dbg> os: z_impl_k_mutex_lock: 0x20032370 took mutex 0x20030d64, count: 1, orig prio: 0
[00:00:00.474,182] <dbg> os: z_impl_k_mutex_unlock: mutex 0x20030d64 lock_count: 1
[00:00:00.474,212] <dbg> os: z_impl_k_mutex_unlock: new owner of mutex 0x20030d64: (nil) (prio: -1000)
[00:00:00.474,914] <dbg> os: z_impl_k_mutex_lock: 0x20032370 took mutex 0x20030d64, count: 1, orig prio: 0
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Which lead me to the suspicion that the network interface is not initialized and "net_if_get_default()" (line 19 in the above function) is returning NULL.

2). So I decided to see if I can do a "net_if_set_default()" via the "active" function:
 
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
STATIC mp_obj_t wlan_active(size_t n_args, const mp_obj_t *args) {
wlan_obj_t *self = MP_OBJ_TO_PTR(args[0]);
if (n_args == 1) {
return mp_obj_new_bool(self->is_active);
} else {
self->is_active = mp_obj_is_true(args[1]);
if (self->is_active) {
if (self->iface == NULL) {
// const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(wifi0));
// const struct device *dev = DEVICE_DT_GET_ONE(nordic_nrf700x);
const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_wifi));
struct net_if *wifi_iface = net_if_lookup_by_dev(dev);
/* As both are Ethernet, we need to set specific interface*/
net_if_set_default(wifi_iface);
net_config_init_app(dev, "Initializing network");
/* TODO: Add proper synchronization to wait for WPA supplicant initialization */
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


But this brings its own predicaments since the project won't build with this function; giving the error:
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
[16/20] Linking C executable zephyr/zephyr_pre0.elf
FAILED: zephyr/zephyr_pre0.elf zephyr/zephyr_pre0.map /Users/tudor/Documents/GitHub/micropython_nRF/ports/zephyr/build/zephyr/zephyr/zephyr_pre0.map
: && ccache /opt/nordic/ncs/toolchains/b8efef2ad5/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc -Os -DNDEBUG -gdwarf-4 -gdwarf-4 zephyr/CMakeFiles/zephyr_pre0.dir/misc/empty_file.c.obj -o zephyr/zephyr_pre0.elf zephyr/CMakeFiles/offsets.dir/./arch/arm/core/offsets/offsets.c.obj tfm/api_ns/interface/lib/s_veneers.o -T zephyr/linker_zephyr_pre0.cmd -Wl,-Map=/Users/tudor/Documents/GitHub/micropython_nRF/ports/zephyr/build/zephyr/zephyr/zephyr_pre0.map -Wl,--whole-archive app/libapp.a zephyr/libzephyr.a zephyr/arch/common/libarch__common.a zephyr/arch/arch/arm/core/libarch__arm__core.a zephyr/arch/arch/arm/core/cortex_m/libarch__arm__core__cortex_m.a zephyr/arch/arch/arm/core/cortex_m/cmse/libarch__arm__core__cortex_m__cmse.a zephyr/arch/arch/arm/core/mpu/libarch__arm__core__mpu.a zephyr/lib/libc/newlib/liblib__libc__newlib.a zephyr/lib/libc/common/liblib__libc__common.a zephyr/lib/posix/options/liblib__posix__options.a zephyr/lib/net_buf/liblib__net_buf.a zephyr/lib/os/zvfs/liblib__os__zvfs.a zephyr/soc/soc/nrf5340/libsoc__nordic.a zephyr/subsys/fs/libsubsys__fs.a zephyr/subsys/random/libsubsys__random.a zephyr/subsys/net/libsubsys__net.a zephyr/subsys/net/l2/offloaded_netdev/libsubsys__net__l2__offloaded_netdev.a zephyr/subsys/net/l2/ethernet/libsubsys__net__l2__ethernet.a zephyr/subsys/net/l2/wifi/libsubsys__net__l2__wifi.a zephyr/subsys/net/ip/libsubsys__net__ip.a zephyr/subsys/net/lib/config/libsubsys__net__lib__config.a zephyr/subsys/net/lib/dhcpv4/libsubsys__net__lib__dhcpv4.a zephyr/subsys/net/lib/dns/libsubsys__net__lib__dns.a zephyr/subsys/net/conn_mgr/libsubsys__net__conn_mgr.a zephyr/drivers/clock_control/libdrivers__clock_control.a zephyr/drivers/console/libdrivers__console.a zephyr/drivers/counter/libdrivers__counter.a zephyr/drivers/entropy/libdrivers__entropy.a zephyr/drivers/flash/libdrivers__flash.a zephyr/drivers/gpio/libdrivers__gpio.a zephyr/drivers/mbox/libdrivers__mbox.a zephyr/drivers/pinctrl/libdrivers__pinctrl.a zephyr/drivers/pwm/libdrivers__pwm.a zephyr/drivers/serial/libdrivers__serial.a zephyr/drivers/spi/libdrivers__spi.a zephyr/drivers/timer/libdrivers__timer.a zephyr/drivers/watchdog/libdrivers__watchdog.a modules/nrf/subsys/nrf_security/src/zephyr/libmbedtls_zephyr.a modules/nrf/subsys/net/lib/wifi_ready/libWIFI_READY_LIB.a modules/nrf/subsys/net/l2_wifi_if_conn/lib..__nrf__subsys__net__l2_wifi_if_conn.a modules/nrf/modules/trusted-firmware-m/lib..__nrf__modules__trusted-firmware-m.a modules/nrf/modules/trusted-firmware-m/libtfm_api_nrf.a modules/trusted-firmware-m/libtfm_api.a modules/hal_nordic/nrfx/libmodules__hal_nordic__nrfx.a -Wl,--no-whole-archive zephyr/kernel/libkernel.a -L/Users/tudor/Documents/GitHub/micropython_nRF/ports/zephyr/build/zephyr/zephyr libmicropython.a zephyr/kernel/libkernel.a zephyr/arch/common/libisr_tables.a -mcpu=cortex-m33 -mthumb -mabi=aapcs -mfpu=fpv5-sp-d16 -mfloat-abi=hard -mfp16-format=ieee -fuse-ld=bfd -Wl,--gc-sections -Wl,--build-id=none -Wl,--sort-common=descending -Wl,--sort-section=alignment -Wl,-u,_OffsetAbsSyms -Wl,-u,_ConfigAbsSyms -nostdlib -static -Wl,-X -Wl,-N -Wl,--orphan-handling=warn -Wl,-no-pie -L"/opt/nordic/ncs/toolchains/b8efef2ad5/opt/zephyr-sdk/arm-zephyr-eabi/arm-zephyr-eabi"/lib/thumb/v8-m.main+fp/hard -specs=nano.specs modules/nrf/subsys/nrf_security/src/libmbedtls.a modules/nrf/subsys/nrf_security/src/libmbedx509.a modules/nrf/subsys/nrf_security/src/libmbedcrypto.a modules/nrf/subsys/nrf_security/src/libmbedcrypto_base.a modules/nrf/subsys/nrf_security/src/libnrf_security_utils.a zephyr/kernel/libkernel.a /opt/nordic/ncs/v2.9.1/nrfxlib/crypto/nrf_oberon/lib/cortex-m33/hard-float/liboberon_mbedtls_3.0.15.a /opt/nordic/ncs/v2.9.1/nrfxlib/crypto/nrf_oberon/lib/cortex-m33/hard-float/liboberon_3.0.15.a -lc -L"/opt/nordic/ncs/toolchains/b8efef2ad5/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/thumb/v8-m.main+fp/hard" -lm -lc -lgcc -lc && cd /Users/tudor/Documents/GitHub/micropython_nRF/ports/zephyr/build/zephyr/zephyr && /opt/homebrew/bin/cmake -E true
/opt/nordic/ncs/toolchains/b8efef2ad5/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd: libmicropython.a(modnetwork.c.obj): in function `wlan_active':
/Users/tudor/Documents/GitHub/micropython_nRF/ports/zephyr/../../ports/zephyr/network_wlan.c:165: undefined reference to `__device_dts_ord_135'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
FAILED: _sysbuild/sysbuild/images/zephyr-prefix/src/zephyr-stamp/zephyr-build /Users/tudor/Documents/GitHub/micropython_nRF/ports/zephyr/build/_sysbuild/sysbuild/images/zephyr-prefix/src/zephyr-stamp/zephyr-build
cd /Users/tudor/Documents/GitHub/micropython_nRF/ports/zephyr/build/zephyr && /opt/homebrew/bin/cmake --build .
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /opt/homebrew/bin/cmake --build /Users/tudor/Documents/GitHub/micropython_nRF/ports/zephyr/build
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Where network_wlan.c:169: undefined reference to `__device_dts_ord_135' points to this line of code inside the "wlan_active()" function:
Fullscreen
1
net_if_down(self->iface);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


A bit of digging (github.com/.../41677) leads to the hypothesis that a few lines above, the definition of the Wifi interface doesn't actually work/ isn't instantiated and returns NULL:
Fullscreen
1
2
const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_wifi));
struct net_if *wifi_iface = net_if_lookup_by_dev(dev);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Not sure if it makes a difference but: I'm using an Apple M1 MacBook.
I can provide any of the above files (or others) if needed, to be able to solve this mistery. I'm sure it's something trivial, but can't figure out what. -.-

Does anyone have any idea what might be causing these issues and what's the correct way of moving forward to be able to do a correct "wifi scan"? Thank you in advance and I hope you have a wonderful day!