Skip to content

Commit 074b567

Browse files
committed
Merge branch 'main' into zig-master
2 parents 44f6850 + 7cdba73 commit 074b567

Some content is hidden

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

68 files changed

+6198
-1169
lines changed

core/src/interrupt.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ pub const CriticalSectionMutex = struct {
122122

123123
/// Unlocks the mutex.
124124
pub fn unlock(self: *CriticalSectionMutex) void {
125-
if (self.critical_section) |cs| {
125+
const maybe_cs = self.critical_section;
126+
self.critical_section = null;
127+
if (maybe_cs) |cs| {
126128
cs.leave();
127-
self.critical_section = null;
128129
}
129130
}
130131
};

drivers/build.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ pub fn build(b: *std.Build) void {
88
.root_source_file = b.path("framework.zig"),
99
.target = target,
1010
.optimize = optimize,
11+
.imports = &.{.{
12+
.name = "link",
13+
.module = b.dependency("link", .{}).module("link"),
14+
}},
1115
});
1216

1317
const test_suite = b.addTest(.{

drivers/build.zig.zon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
.name = .mz_drivers,
33
.fingerprint = 0x576453eedc5af46e,
44
.minimum_zig_version = "0.15.1",
5+
.dependencies = .{
6+
.link = .{ .path = "../modules/network/link" },
7+
},
58
.version = "0.0.1",
69
.paths = .{
710
"build.zig",

drivers/wireless/cyw43439.zig

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const std = @import("std");
22
const mem = std.mem;
33
const assert = std.debug.assert;
44

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

61-
pub fn send_zc(ptr: *anyopaque, bytes: []u8) anyerror!void {
62+
pub fn send_zc(ptr: *anyopaque, bytes: []u8) Link.Error!void {
6263
const self: *Self = @ptrCast(@alignCast(ptr));
63-
try self.wifi.send_zc(bytes);
64-
}
65-
66-
pub fn ready(ptr: *anyopaque) bool {
67-
const self: *Self = @ptrCast(@alignCast(ptr));
68-
return self.wifi.has_credit();
64+
self.wifi.send_zc(bytes) catch |err| switch (err) {
65+
error.OutOfMemory => return error.OutOfMemory,
66+
error.LinkDown => return error.LinkDown,
67+
else => return error.InternalError,
68+
};
6969
}
7070

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

89+
pub fn link(self: *Self) Link {
90+
return .{
91+
.ptr = self,
92+
.vtable = .{
93+
.recv = recv_zc,
94+
.send = send_zc,
95+
},
96+
};
97+
}
98+
8999
// References:
90100
// https://github.com/embassy-rs/embassy/blob/abb1d8286e2415686150e2e315ca1c380659c3c3/cyw43/src/consts.rs
91101
// https://github.com/jbentham/picowi/blob/main/lib/picowi_regs.h

drivers/wireless/cyw43439/wifi.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,8 @@ pub fn recv_zc(self: *Self, buffer: []u8) !?struct { usize, usize } {
528528
///
529529
/// Buffer has to be 4 bytes aligned and it will be extended in as_words to the
530530
/// word boundary!
531-
pub fn send_zc(self: *Self, buffer: []u8) !void {
532-
if (!self.has_credit()) return error.Cyw43NoCredit;
531+
pub fn send_zc(self: *Self, buffer: []u8) anyerror!void {
532+
if (!self.has_credit()) return error.OutOfMemory;
533533

534534
const eth_frame_len = buffer.len - 22;
535535
// add bus header

examples/espressif/esp/build.zig

Lines changed: 169 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ pub fn build(b: *std.Build) void {
1212
const mz_dep = b.dependency("microzig", .{});
1313
const mb = MicroBuild.init(b, mz_dep) orelse return;
1414

15-
const targets = [_]TargetDescription{
16-
.{ .prefix = "esp32_c3", .target = mb.ports.esp.chips.esp32_c3 },
17-
// .{ .prefix = "esp32_c3_direct_boot", .target = mb.ports.esp.chips.esp32_c3_direct_boot },
18-
.{ .prefix = "esp32_c3_flashless", .target = mb.ports.esp.chips.esp32_c3_flashless },
19-
};
20-
21-
const available_examples = [_]Example{
15+
const examples: []const Example = &.{
2216
.{ .name = "blinky", .file = "src/blinky.zig" },
2317
.{ .name = "custom_clock_config", .file = "src/custom_clock_config.zig" },
2418
.{ .name = "gpio_input", .file = "src/gpio_input.zig" },
@@ -30,15 +24,25 @@ pub fn build(b: *std.Build) void {
3024
.{ .name = "stepper_driver_dumb", .file = "src/stepper_driver_dumb.zig" },
3125
.{ .name = "systimer", .file = "src/systimer.zig" },
3226
.{ .name = "ws2812_blinky", .file = "src/ws2812_blinky.zig" },
27+
.{ .name = "rtos", .file = "src/rtos.zig" },
28+
.{ .name = "tcp_server", .file = "src/tcp_server.zig", .features = .{
29+
.flashless = false,
30+
.lwip = true,
31+
} },
3332
};
3433

35-
for (available_examples) |example| {
34+
for (examples) |example| {
3635
// If we specify example, only select the ones that match
3736
if (maybe_example) |selected_example|
3837
if (!std.mem.containsAtLeast(u8, example.name, 1, selected_example))
3938
continue;
4039

41-
for (targets) |target_desc| {
40+
for (std.enums.values(TargetEnum)) |target_enum| {
41+
if (!example.features.flashless and std.mem.containsAtLeast(u8, @tagName(target_enum), 1, "flashless"))
42+
continue;
43+
44+
const target_desc = target_enum.get_target_desc(mb);
45+
4246
// `add_firmware` basically works like addExecutable, but takes a
4347
// `microzig.Target` for target instead of a `std.zig.CrossTarget`.
4448
//
@@ -51,6 +55,28 @@ pub fn build(b: *std.Build) void {
5155
.root_source_file = b.path(example.file),
5256
});
5357

58+
if (example.features.lwip) {
59+
const target = b.resolveTargetQuery(firmware.target.zig_target);
60+
61+
const foundation_dep = b.dependency("foundation_libc", .{
62+
.target = target,
63+
.optimize = optimize,
64+
.single_threaded = true,
65+
});
66+
67+
const lwip_dep = b.dependency("lwip", .{
68+
.target = target,
69+
.optimize = optimize,
70+
.include_dir = b.path("src/lwip/include"),
71+
});
72+
73+
const libc_lib = foundation_dep.artifact("foundation");
74+
const lwip_lib = lwip_dep.artifact("lwip");
75+
76+
lwip_lib.root_module.linkLibrary(libc_lib);
77+
firmware.app_mod.linkLibrary(lwip_lib);
78+
}
79+
5480
// `installFirmware()` is the MicroZig pendant to `Build.installArtifact()`
5581
// and allows installing the firmware as a typical firmware file.
5682
//
@@ -63,12 +89,146 @@ pub fn build(b: *std.Build) void {
6389
}
6490
}
6591

92+
const TargetEnum = enum {
93+
esp32_c3,
94+
esp32_c3_direct_boot,
95+
esp32_c3_flashless,
96+
97+
fn get_target_desc(target_enum: TargetEnum, mb: *MicroBuild) TargetDescription {
98+
return switch (target_enum) {
99+
.esp32_c3 => .{
100+
.prefix = "esp32_c3",
101+
.target = mb.ports.esp.chips.esp32_c3,
102+
},
103+
.esp32_c3_direct_boot => .{
104+
.prefix = "esp32_c3_direct_boot",
105+
.target = mb.ports.esp.chips.esp32_c3_direct_boot,
106+
},
107+
.esp32_c3_flashless => .{
108+
.prefix = "esp32_c3_flashless",
109+
.target = mb.ports.esp.chips.esp32_c3_flashless,
110+
},
111+
};
112+
}
113+
};
114+
66115
const TargetDescription = struct {
67116
prefix: []const u8,
68117
target: *const microzig.Target,
69118
};
70119

71120
const Example = struct {
121+
const Features = packed struct {
122+
flashless: bool = true,
123+
lwip: bool = false,
124+
};
125+
72126
name: []const u8,
73127
file: []const u8,
128+
features: Features = .{},
129+
};
130+
131+
const lwip_flags = [_][]const u8{ "-std=c99", "-fno-sanitize=undefined" };
132+
const lwip_files = [_][]const u8{
133+
// Core files
134+
"core/init.c",
135+
"core/def.c",
136+
"core/dns.c",
137+
"core/inet_chksum.c",
138+
"core/ip.c",
139+
"core/mem.c",
140+
"core/memp.c",
141+
"core/netif.c",
142+
"core/pbuf.c",
143+
"core/raw.c",
144+
"core/stats.c",
145+
"core/sys.c",
146+
"core/altcp.c",
147+
"core/altcp_alloc.c",
148+
"core/altcp_tcp.c",
149+
"core/tcp.c",
150+
"core/tcp_in.c",
151+
"core/tcp_out.c",
152+
"core/timeouts.c",
153+
"core/udp.c",
154+
155+
// IPv4 implementation:
156+
"core/ipv4/acd.c",
157+
"core/ipv4/autoip.c",
158+
"core/ipv4/dhcp.c",
159+
"core/ipv4/etharp.c",
160+
"core/ipv4/icmp.c",
161+
"core/ipv4/igmp.c",
162+
"core/ipv4/ip4_frag.c",
163+
"core/ipv4/ip4.c",
164+
"core/ipv4/ip4_addr.c",
165+
166+
// IPv6 implementation:
167+
"core/ipv6/dhcp6.c",
168+
"core/ipv6/ethip6.c",
169+
"core/ipv6/icmp6.c",
170+
"core/ipv6/inet6.c",
171+
"core/ipv6/ip6.c",
172+
"core/ipv6/ip6_addr.c",
173+
"core/ipv6/ip6_frag.c",
174+
"core/ipv6/mld6.c",
175+
"core/ipv6/nd6.c",
176+
177+
// Interfaces
178+
"netif/ethernet.c",
179+
180+
// Interfaces:
181+
// "netif/bridgeif.c",
182+
// "netif/ethernet.c",
183+
// "netif/slipif.c",
184+
// "netif/bridgeif_fdb.c",
185+
186+
// sequential APIs
187+
// "api/err.c",
188+
// "api/api_msg.c",
189+
// "api/netifapi.c",
190+
// "api/sockets.c",
191+
// "api/netbuf.c",
192+
// "api/api_lib.c",
193+
// "api/tcpip.c",
194+
// "api/netdb.c",
195+
// "api/if_api.c",
196+
197+
// 6LoWPAN
198+
// "netif/lowpan6.c",
199+
// "netif/lowpan6_ble.c",
200+
// "netif/lowpan6_common.c",
201+
// "netif/zepif.c",
202+
203+
// PPP
204+
// "netif/ppp/polarssl/arc4.c",
205+
// "netif/ppp/polarssl/des.c",
206+
// "netif/ppp/polarssl/md4.c",
207+
// "netif/ppp/polarssl/sha1.c",
208+
// "netif/ppp/polarssl/md5.c",
209+
// "netif/ppp/ipcp.c",
210+
// "netif/ppp/magic.c",
211+
// "netif/ppp/pppoe.c",
212+
// "netif/ppp/mppe.c",
213+
// "netif/ppp/multilink.c",
214+
// "netif/ppp/chap-new.c",
215+
// "netif/ppp/auth.c",
216+
// "netif/ppp/chap_ms.c",
217+
// "netif/ppp/ipv6cp.c",
218+
// "netif/ppp/chap-md5.c",
219+
// "netif/ppp/upap.c",
220+
// "netif/ppp/pppapi.c",
221+
// "netif/ppp/pppos.c",
222+
// "netif/ppp/eap.c",
223+
// "netif/ppp/pppol2tp.c",
224+
// "netif/ppp/demand.c",
225+
// "netif/ppp/fsm.c",
226+
// "netif/ppp/eui64.c",
227+
// "netif/ppp/ccp.c",
228+
// "netif/ppp/pppcrypt.c",
229+
// "netif/ppp/utils.c",
230+
// "netif/ppp/vj.c",
231+
// "netif/ppp/lcp.c",
232+
// "netif/ppp/ppp.c",
233+
// "netif/ppp/ecp.c",
74234
};

examples/espressif/esp/build.zig.zon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
.version = "0.0.0",
55
.dependencies = .{
66
.microzig = .{ .path = "../../.." },
7+
.lwip = .{ .path = "../../../modules/lwip" },
8+
.foundation_libc = .{ .path = "../../../modules/foundation-libc" },
79
},
810
.paths = .{
911
"README.md",

0 commit comments

Comments
 (0)