Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions drivers/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub fn build(b: *std.Build) void {
.root_source_file = b.path("framework.zig"),
.target = target,
.optimize = optimize,
.imports = &.{.{
.name = "link",
.module = b.dependency("link", .{}).module("link"),
}},
});

const test_suite = b.addTest(.{
Expand Down
3 changes: 3 additions & 0 deletions drivers/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
.name = .mz_drivers,
.fingerprint = 0x576453eedc5af46e,
.minimum_zig_version = "0.15.1",
.dependencies = .{
.link = .{ .path = "../modules/network/link" },
},
.version = "0.0.1",
.paths = .{
"build.zig",
Expand Down
24 changes: 17 additions & 7 deletions drivers/wireless/cyw43439.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;

const Link = @import("link");
const Bus = @import("cyw43439/bus.zig");
const WiFi = @import("cyw43439/wifi.zig");
pub const JoinOptions = WiFi.JoinOptions;
Expand Down Expand Up @@ -58,14 +59,13 @@ pub fn recv_zc(ptr: *anyopaque, bytes: []u8) anyerror!?struct { usize, usize } {
return self.wifi.recv_zc(bytes);
}

pub fn send_zc(ptr: *anyopaque, bytes: []u8) anyerror!void {
pub fn send_zc(ptr: *anyopaque, bytes: []u8) Link.Error!void {
const self: *Self = @ptrCast(@alignCast(ptr));
try self.wifi.send_zc(bytes);
}

pub fn ready(ptr: *anyopaque) bool {
const self: *Self = @ptrCast(@alignCast(ptr));
return self.wifi.has_credit();
self.wifi.send_zc(bytes) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.LinkDown => return error.LinkDown,
else => return error.InternalError,
};
}

pub fn gpio(self: *Self, pin: u2) Pin {
Expand All @@ -86,6 +86,16 @@ pub const Pin = struct {
}
};

pub fn link(self: *Self) Link {
return .{
.ptr = self,
.vtable = .{
.recv = recv_zc,
.send = send_zc,
},
};
}

// References:
// https://github.com/embassy-rs/embassy/blob/abb1d8286e2415686150e2e315ca1c380659c3c3/cyw43/src/consts.rs
// https://github.com/jbentham/picowi/blob/main/lib/picowi_regs.h
Expand Down
4 changes: 2 additions & 2 deletions drivers/wireless/cyw43439/wifi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,8 @@ pub fn recv_zc(self: *Self, buffer: []u8) !?struct { usize, usize } {
///
/// Buffer has to be 4 bytes aligned and it will be extended in as_words to the
/// word boundary!
pub fn send_zc(self: *Self, buffer: []u8) !void {
if (!self.has_credit()) return error.Cyw43NoCredit;
pub fn send_zc(self: *Self, buffer: []u8) anyerror!void {
if (!self.has_credit()) return error.OutOfMemory;

const eth_frame_len = buffer.len - 22;
// add bus header
Expand Down
27 changes: 10 additions & 17 deletions examples/raspberrypi/rp2xxx/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,19 @@ pub fn build(b: *std.Build) void {
.imports = example.imports,
});

// Import net module for some examples
if (std.mem.indexOf(u8, example.name, "_net-") != null) {
const target = b.resolveTargetQuery(firmware.target.zig_target);
const foundation_dep = b.dependency("foundationlibc", .{
.target = target,
const net_dep = b.dependency("net", .{
.target = b.resolveTargetQuery(firmware.target.zig_target),
.optimize = optimize,
.mtu = 1500,
// Cyw43 driver requires 22 bytes of header and 4 bytes of footer.
// header + ethernet + mtu + footer = 22 + 14 + 1500 + 4 = 1540
.pbuf_length = 1540,
.pbuf_header_length = 22,
});
const lwip_dep = b.dependency("lwip", .{
.target = target,
.optimize = optimize,
});
const lwip_mod = lwip_dep.module("lwip");
// link libc
lwip_mod.linkLibrary(foundation_dep.artifact("foundation"));
// add path to the configuration, lwipopts.h
lwip_mod.addIncludePath(b.path("src/net/lwip/include"));
// add c import paths
for (lwip_mod.include_dirs.items) |dir| {
firmware.app_mod.include_dirs.append(b.allocator, dir) catch @panic("out of memory");
}
firmware.app_mod.addImport("lwip", lwip_mod);
const net_mod = net_dep.module("net");
firmware.app_mod.addImport("net", net_mod);
}

// `install_firmware()` is the MicroZig pendant to `Build.installArtifact()`
Expand Down
3 changes: 1 addition & 2 deletions examples/raspberrypi/rp2xxx/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
.url = "git+https://github.com/Gnyblast/zig-ssd1306-gddram-fonts#84dd5fab70ddc1198eef1cd1305cc82f08f419dc",
.hash = "ssd1306_font_8x8-0.0.0-oGb_cERcAAA6NJzWDOMepgnY6r4blNEHjpkldDaRAui5",
},
.foundationlibc = .{ .path = "../../../modules/foundation-libc/" },
.lwip = .{ .path = "../../../modules/lwip/" },
.net = .{ .path = "../../../modules/network/" },
},
.paths = .{
"README.md",
Expand Down
45 changes: 0 additions & 45 deletions examples/raspberrypi/rp2xxx/src/net/lwip/exports.zig

This file was deleted.

4 changes: 0 additions & 4 deletions examples/raspberrypi/rp2xxx/src/net/lwip/readme.md

This file was deleted.

27 changes: 27 additions & 0 deletions examples/raspberrypi/rp2xxx/src/net/lwip_exports.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// Exports for the lwip
/// Required from modules/network/src/include/arch.cc#L26
const std = @import("std");
const microzig = @import("microzig");
const hal = microzig.hal;

/// Time since boot in milliseconds.
export fn lwip_sys_now() u32 {
const ts = hal.time.get_time_since_boot();
return @truncate(ts.to_us() / 1000);
}

var rng: ?hal.rand.Ascon = null;

export fn lwip_rand() u32 {
return switch (hal.compatibility.chip) {
.RP2350 => hal.rand.trng.random_blocking(),
.RP2040 => brk: {
if (rng == null) {
rng = .init();
}
var val: u32 = 0;
rng.?.fill(std.mem.asBytes(&val));
break :brk val;
},
};
}
13 changes: 3 additions & 10 deletions examples/raspberrypi/rp2xxx/src/net/pong.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub const microzig_options = microzig.Options{
};
const log = std.log.scoped(.main);

const net = @import("lwip/net.zig");
comptime {
_ = @import("lwip/exports.zig");
_ = @import("lwip_exports.zig");
}
const net = @import("net");
const secrets = @import("secrets.zig");

pub fn main() !void {
Expand All @@ -39,14 +39,7 @@ pub fn main() !void {
log.debug("wifi joined", .{});

// init lwip network interface
var nic: net.Interface = .{
.link = .{
.ptr = wifi,
.recv = drivers.WiFi.recv,
.send = drivers.WiFi.send,
.ready = drivers.WiFi.ready,
},
};
var nic: net.Interface = .{ .link = wifi.link() };
try nic.init(wifi.mac, try secrets.nic_options());

var ts = time.get_time_since_boot();
Expand Down
2 changes: 1 addition & 1 deletion examples/raspberrypi/rp2xxx/src/net/secrets.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const net = @import("lwip/net.zig");
const net = @import("net");
const microzig = @import("microzig");
const JoinOptions = microzig.hal.drivers.WiFi.Chip.JoinOptions;

Expand Down
13 changes: 3 additions & 10 deletions examples/raspberrypi/rp2xxx/src/net/tcp_client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub const microzig_options = microzig.Options{
};
const log = std.log.scoped(.main);

const net = @import("lwip/net.zig");
comptime {
_ = @import("lwip/exports.zig");
_ = @import("lwip_exports.zig");
}
const net = @import("net");
const secrets = @import("secrets.zig");

pub fn main() !void {
Expand All @@ -36,14 +36,7 @@ pub fn main() !void {
try wifi.join(secrets.ssid, secrets.pwd, secrets.join_opt);

// init lwip network interface
var nic: net.Interface = .{
.link = .{
.ptr = wifi,
.recv = drivers.WiFi.recv,
.send = drivers.WiFi.send,
.ready = drivers.WiFi.ready,
},
};
var nic: net.Interface = .{ .link = wifi.link() };
try nic.init(wifi.mac, try secrets.nic_options());

// init handler
Expand Down
13 changes: 3 additions & 10 deletions examples/raspberrypi/rp2xxx/src/net/tcp_server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub const microzig_options = microzig.Options{
};
const log = std.log.scoped(.main);

const net = @import("lwip/net.zig");
comptime {
_ = @import("lwip/exports.zig");
_ = @import("lwip_exports.zig");
}
const net = @import("net");
const secrets = @import("secrets.zig");

pub fn main() !void {
Expand All @@ -36,14 +36,7 @@ pub fn main() !void {
try wifi.join(secrets.ssid, secrets.pwd, secrets.join_opt);

// init lwip network interface
var nic: net.Interface = .{
.link = .{
.ptr = wifi,
.recv = drivers.WiFi.recv,
.send = drivers.WiFi.send,
.ready = drivers.WiFi.ready,
},
};
var nic: net.Interface = .{ .link = wifi.link() };
try nic.init(wifi.mac, try secrets.nic_options());

// init server
Expand Down
14 changes: 4 additions & 10 deletions examples/raspberrypi/rp2xxx/src/net/udp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub const microzig_options = microzig.Options{
};
const log = std.log.scoped(.main);

const net = @import("lwip/net.zig");
comptime {
_ = @import("lwip/exports.zig");
_ = @import("lwip_exports.zig");
}
const net = @import("net");
const secrets = @import("secrets.zig");

pub fn main() !void {
Expand All @@ -36,15 +36,9 @@ pub fn main() !void {
try wifi.join(secrets.ssid, secrets.pwd, secrets.join_opt);

// init lwip network interface
var nic: net.Interface = .{
.link = .{
.ptr = wifi,
.recv = drivers.WiFi.recv,
.send = drivers.WiFi.send,
.ready = drivers.WiFi.ready,
},
};
var nic: net.Interface = .{ .link = wifi.link() };
try nic.init(wifi.mac, try secrets.nic_options());

// udp init
var udp: net.Udp = try .init(&nic);
// listen for udp packets on port 9999 and call on_recv for each received packet
Expand Down
Loading
Loading