Skip to content

extract network module from pico WiFi examples#846

Merged
mattnite merged 8 commits intoZigEmbeddedGroup:mainfrom
ianic:net_module
Jan 15, 2026
Merged

extract network module from pico WiFi examples#846
mattnite merged 8 commits intoZigEmbeddedGroup:mainfrom
ianic:net_module

Conversation

@ianic
Copy link
Contributor

@ianic ianic commented Jan 10, 2026

Integrating lwip requires adding foundationlibc and lwip dependencies. Putting lwipopts.h and arch/cc.h to the include path, setting options and implementing some functions required by lwip.

To simplify that I extracted everything that is not dependent on the platform into network module. That simplifies using it in applications outside of the rpi/examples/net folder. And opens the path to use lwip on other platforms.

Using WiFi in rpi pico (2)w projects is now simplified to adding dependency to the build.zig.zon:

    .dependencies = .{
        .microzig = .{
            .path = "../../microzig/",
        },
        .net = .{
            .path = "../../microzig/modules/network/",
        },
    },

and adding module to the application in build.zig:

const std = @import("std");
const microzig = @import("microzig");

const MicroBuild = microzig.MicroBuild(.{
    .rp2xxx = true,
});

pub fn build(b: *std.Build) void {
    const optimize = b.standardOptimizeOption(.{});

    const mz_dep = b.dependency("microzig", .{});
    const mb = MicroBuild.init(b, mz_dep) orelse return;
    const target = mb.ports.rp2xxx.boards.raspberrypi.pico2_arm;

    const net_dep = b.dependency("net", .{
        .target = b.resolveTargetQuery(target.zig_target),
        .optimize = optimize,
        .lwip_mem_size = 32 * 1024,
        .lwip_pbuf_pool_size = 32,
    });
    const net_mod = net_dep.module("net");

    const firmware = mb.add_firmware(.{
        .name = app,
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("src/main.zig"),
        .imports = &.{
            .{ .name = "net", .module = net_mod },
        },
    });
    mb.install_firmware(firmware, .{});
}

Lwip has a lot of configurable options. Currently in the build system I opened two of them for configuration: lwip_mem_size and lwip_pbuf_pool_size. That controls memory allocations; static packet buffers pool size and maximum of heap allocation.

There are two platform dependent functions required by lwip: lwip_sys_now and lwip_rand. For rpi they are implemented in hal/drivers.zig.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Lint Results

Found 1 issue on changed lines in 1 file:

  • modules/network/src/root.zig: 1 issue
ℹ️ Additional issues on unchanged lines
The following 14 issue(s) exist but are not on lines changed in this PR:

port/raspberrypi/rp2xxx/src/hal/drivers.zig:478: TODO style comments need to have a linked microzig issue on the same line.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:13: Suggestion: Rename `Datagram_Device` to `DatagramDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:14: Suggestion: Rename `Stream_Device` to `StreamDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:16: Suggestion: Rename `Clock_Device` to `ClockDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:17: Suggestion: Rename `I2CError` to `I2C_Error`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:18: Suggestion: Rename `I2CAddress` to `I2C_Address`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:23: Suggestion: Rename `I2C_Datagram_Device` to `I2C_DatagramDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:468: Suggestion: Rename `Cyw43PioSpi` to `CYW43_PIO_SPI`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:469: Suggestion: Rename `Cyw43_Spi` to `CYW43_SPI`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:470: Suggestion: Rename `Cyw43_Bus` to `CYW43_Bus`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:471: Suggestion: Rename `Cyw43_Runner` to `CYW43_Runner`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:473: Suggestion: Rename `CYW43_Pio_Device_Config` to `CYW43_PIO_DeviceConfig`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:479: Suggestion: Rename `CYW43_Pio_Device` to `CYW43_PIO_Device`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:517: Suggestion: Rename `Spi` to `SPI`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.

⚠️ Could not attach inline comments due to an error.

Copy link
Collaborator

@Grazfather Grazfather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome!

@ianic ianic marked this pull request as draft January 12, 2026 16:11
@ianic ianic marked this pull request as ready for review January 12, 2026 16:55
@github-actions github-actions bot dismissed their stale review January 12, 2026 16:56

Updating with new lint results

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Lint Results

ℹ️ Additional issues on unchanged lines
The following 15 issue(s) exist but are not on lines changed in this PR:

drivers/wireless/cyw43439/wifi.zig:103: Suggestion: Rename `ClmLoadControl` to `CLM_LoadControl`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:478: TODO style comments need to have a linked microzig issue on the same line.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:13: Suggestion: Rename `Datagram_Device` to `DatagramDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:14: Suggestion: Rename `Stream_Device` to `StreamDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:16: Suggestion: Rename `Clock_Device` to `ClockDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:17: Suggestion: Rename `I2CError` to `I2C_Error`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:18: Suggestion: Rename `I2CAddress` to `I2C_Address`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:23: Suggestion: Rename `I2C_Datagram_Device` to `I2C_DatagramDevice`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:468: Suggestion: Rename `Cyw43PioSpi` to `CYW43_PIO_SPI`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:469: Suggestion: Rename `Cyw43_Spi` to `CYW43_SPI`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:470: Suggestion: Rename `Cyw43_Bus` to `CYW43_Bus`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:471: Suggestion: Rename `Cyw43_Runner` to `CYW43_Runner`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:473: Suggestion: Rename `CYW43_Pio_Device_Config` to `CYW43_PIO_DeviceConfig`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:479: Suggestion: Rename `CYW43_Pio_Device` to `CYW43_PIO_Device`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.
port/raspberrypi/rp2xxx/src/hal/drivers.zig:517: Suggestion: Rename `Spi` to `SPI`, it _should_ be more in line with our [style guidelines](https://microzig.tech/docs/contributing/). This automation is not perfect so take it with a grain of salt.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Lint Results

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Lint Results

ianic added 8 commits January 15, 2026 10:23
Integrating lwip requires adding foundationlibc and lwip dependencies.
Putting lwipopts.h and arch/cc.h to the include path, setting
options and implementing some functions required by lwip.

To simplify that I extracted everything that is not dependent on the
platform into network module. That simplifies using it in applications
outside of the rpi/examples/net folder. And opens the path to use lwip
on other platforms.

Using WiFi in rpi pico (2)w projects is now simplified to adding dependency to the `build.zig.zon`:
```zig
    .dependencies = .{
        .microzig = .{
            .path = "../../microzig/",
        },
        .net = .{
            .path = "../../microzig/modules/network/",
        },
    },
```
and adding module to the application in `build.zig`:
```zig
const std = @import("std");
const microzig = @import("microzig");

const MicroBuild = microzig.MicroBuild(.{
    .rp2xxx = true,
});

pub fn build(b: *std.Build) void {
    const optimize = b.standardOptimizeOption(.{});

    const mz_dep = b.dependency("microzig", .{});
    const mb = MicroBuild.init(b, mz_dep) orelse return;
    const target = mb.ports.rp2xxx.boards.raspberrypi.pico2_arm;

    const net_dep = b.dependency("net", .{
        .target = b.resolveTargetQuery(target.zig_target),
        .optimize = optimize,
        .lwip_mem_size = 32 * 1024,
        .lwip_pbuf_pool_size = 32,
    });
    const net_mod = net_dep.module("net");

    const firmware = mb.add_firmware(.{
        .name = app,
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("src/main.zig"),
        .imports = &.{
            .{ .name = "net", .module = net_mod },
        },
    });
    mb.install_firmware(firmware, .{});
}
```

Lwip has a lot of configurable options. Currently in the build system I
opened two of them for configuration: `lwip_mem_size` and
`lwip_pbuf_pool_size`. That controls memory allocations; static packet
buffers pool size and maximum of heap allocation.

There are two platform dependent functions required by lwip:
`lwip_sys_now` and `lwip_rand`. For rpi they are implemented in `hal/drivers.zig`.
Previous one was IP4 specific.
net.Interface depends on underlying link implementation. They are in
different projects so we can't share some struct definition without
including whole module. This adapts one interface to another.

Also removing ready function. If chip can't store network packet it is
now signaled with error.OutOfMemory.
Move cyw43 specific options to the rpi pico examples build.
Drivers project now imports that interface.
@mattnite mattnite merged commit 43e60f5 into ZigEmbeddedGroup:main Jan 15, 2026
56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants