Skip to content

Commit 53532de

Browse files
authored
Get esp32 examples building again (#307)
* Get esp32 examples building again * Set up cpu module file * Enable building esp examples in gh workflows
1 parent 2c83c7f commit 53532de

7 files changed

Lines changed: 70 additions & 57 deletions

File tree

.github/workflows/examples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
matrix:
1414
example_dir: [
15-
#espressif/esp,
15+
espressif/esp,
1616
gigadevice/gd32,
1717
microchip/avr,
1818
microchip/atsam,

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const port_list: []const struct {
1818
name: [:0]const u8,
1919
dep_name: [:0]const u8,
2020
} = &.{
21-
// .{ .name = "esp", .dep_name = "port/espressif/esp" },
21+
.{ .name = "esp", .dep_name = "port/espressif/esp" },
2222
.{ .name = "gd32", .dep_name = "port/gigadevice/gd32" },
2323
.{ .name = "atsam", .dep_name = "port/microchip/atsam" },
2424
.{ .name = "avr", .dep_name = "port/microchip/avr" },

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
.@"tools/uf2" = .{ .path = "tools/uf2" },
1313

1414
// ports
15-
// .@"port/espressif/esp" = .{ .path = "port/espressif/esp", .lazy = true },
15+
.@"port/espressif/esp" = .{ .path = "port/espressif/esp", .lazy = true },
1616
.@"port/gigadevice/gd32" = .{ .path = "port/gigadevice/gd32", .lazy = true },
1717
.@"port/microchip/atsam" = .{ .path = "port/microchip/atsam", .lazy = true },
1818
.@"port/microchip/avr" = .{ .path = "port/microchip/avr", .lazy = true },

examples/espressif/esp/build.zig

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
const std = @import("std");
2-
const MicroZig = @import("microzig/build");
3-
const esp = @import("microzig/port/espressif/esp");
2+
const microzig = @import("microzig");
43

5-
const available_examples = [_]Example{
6-
.{ .target = esp.chips.esp32_c3, .name = "esp32-c3_blinky", .file = "src/blinky.zig" },
7-
};
4+
const MicroBuild = microzig.MicroBuild(.{
5+
.esp = true,
6+
});
87

98
pub fn build(b: *std.Build) void {
10-
const microzig = MicroZig.init(b, .{});
119
const optimize = b.standardOptimizeOption(.{});
1210

11+
const mz_dep = b.dependency("microzig", .{});
12+
const mb = MicroBuild.init(b, mz_dep) orelse return;
13+
const available_examples = [_]Example{
14+
.{ .target = mb.ports.esp.chips.esp32_c3, .name = "esp32-c3_blinky", .file = "src/blinky.zig" },
15+
};
16+
1317
for (available_examples) |example| {
1418
// `add_firmware` basically works like addExecutable, but takes a
1519
// `microzig.Target` for target instead of a `std.zig.CrossTarget`.
1620
//
1721
// The target will convey all necessary information on the chip,
1822
// cpu and potentially the board as well.
19-
const firmware = microzig.add_firmware(b, .{
23+
const firmware = mb.add_firmware(.{
2024
.name = example.name,
2125
.target = example.target,
2226
.optimize = optimize,
@@ -27,15 +31,15 @@ pub fn build(b: *std.Build) void {
2731
// and allows installing the firmware as a typical firmware file.
2832
//
2933
// This will also install into `$prefix/firmware` instead of `$prefix/bin`.
30-
microzig.install_firmware(b, firmware, .{});
34+
mb.install_firmware(firmware, .{});
3135

3236
// For debugging, we also always install the firmware as an ELF file
33-
microzig.install_firmware(b, firmware, .{ .format = .elf });
37+
mb.install_firmware(firmware, .{ .format = .elf });
3438
}
3539
}
3640

3741
const Example = struct {
38-
target: MicroZig.Target,
42+
target: *const microzig.Target,
3943
name: []const u8,
4044
file: []const u8,
4145
};

examples/espressif/esp/build.zig.zon

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
.name = "examples/espressif/esp",
33
.version = "0.0.0",
44
.dependencies = .{
5-
.@"microzig/build" = .{ .path = "../../../build" },
6-
.@"microzig/port/espressif/esp" = .{ .path = "../../../port/espressif/esp" },
5+
.microzig = .{ .path = "../../.." },
76
},
8-
97
.paths = .{
108
"LICENSE",
119
"README.md",

port/espressif/esp/build.zig

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,66 @@
11
const std = @import("std");
2-
const MicroZig = @import("microzig/build");
2+
const microzig = @import("microzig/build-internals");
33

4-
fn path(comptime suffix: []const u8) std.Build.LazyPath {
5-
return .{
6-
.cwd_relative = comptime ((std.fs.path.dirname(@src().file) orelse ".") ++ suffix),
7-
};
8-
}
4+
const Self = @This();
5+
6+
chips: struct {
7+
esp32_c3: *const microzig.Target,
8+
},
99

10-
const esp_riscv = .{
11-
.name = "Espressif RISC-V",
12-
.root_source_file = path("/src/cpus/espressif-riscv.zig"),
13-
.target = std.Target.Query{
14-
.cpu_arch = .riscv32,
15-
.cpu_model = .{ .explicit = &std.Target.riscv.cpu.generic_rv32 },
16-
.cpu_features_add = std.Target.riscv.featureSet(&.{
17-
std.Target.riscv.Feature.c,
18-
std.Target.riscv.Feature.m,
19-
}),
20-
.os_tag = .freestanding,
21-
.abi = .eabi,
22-
},
23-
};
24-
25-
const hal = .{
26-
.root_source_file = path("/src/hals/ESP32_C3.zig"),
27-
};
28-
29-
pub const chips = struct {
30-
pub const esp32_c3 = MicroZig.Target{
31-
.preferred_format = .bin, // TODO: Exchange FLAT format with .esp format
10+
boards: struct {},
11+
12+
pub fn init(dep: *std.Build.Dependency) Self {
13+
const b = dep.builder;
14+
15+
const hal: microzig.HardwareAbstractionLayer = .{
16+
.root_source_file = b.path("src/hals/ESP32_C3.zig"),
17+
};
18+
const chip_esp32c3: microzig.Target = .{
19+
.dep = dep,
20+
// TODO: Exchange FLAT format with .esp format
21+
.preferred_binary_format = .bin,
3222
.chip = .{
3323
.name = "ESP32-C3",
3424
.url = "https://www.espressif.com/en/products/socs/esp32-c3",
35-
36-
.cpu = esp_riscv,
37-
38-
.register_definition = .{
39-
.svd = path("/src/chips/ESP32-C3.svd"),
25+
.cpu = std.Target.Query{
26+
.cpu_arch = .riscv32,
27+
.cpu_model = .{ .explicit = &std.Target.riscv.cpu.generic_rv32 },
28+
.cpu_features_add = std.Target.riscv.featureSet(&.{
29+
std.Target.riscv.Feature.c,
30+
std.Target.riscv.Feature.m,
31+
}),
32+
.os_tag = .freestanding,
33+
.abi = .eabi,
4034
},
41-
35+
.cpu_module_file = b.path("src/cpus/espressif-riscv.zig"),
36+
.register_definition = .{ .svd = b.path("src/chips/ESP32-C3.svd") },
4237
.memory_regions = &.{
43-
.{ .kind = .flash, .offset = 0x4200_0000, .length = 0x0080_0000 }, // external memory, ibus
44-
.{ .kind = .ram, .offset = 0x3FC8_0000, .length = 0x0006_0000 }, // sram 1, data bus
38+
// external memory, ibus
39+
.{ .kind = .flash, .offset = 0x4200_0000, .length = 0x0080_0000 },
40+
// sram 1, data bus
41+
.{ .kind = .ram, .offset = 0x3FC8_0000, .length = 0x0006_0000 },
4542
},
4643
},
4744
.hal = hal,
4845
};
49-
};
5046

51-
pub const boards = struct {};
47+
return .{
48+
.chips = .{
49+
.esp32_c3 = chip_esp32c3.derive(.{}),
50+
},
51+
.boards = .{},
52+
};
53+
}
5254

5355
pub fn build(b: *std.Build) void {
54-
_ = b.step("test", "Run platform agnostic unit tests");
56+
const optimize = b.standardOptimizeOption(.{});
57+
58+
const unit_tests = b.addTest(.{
59+
.root_source_file = b.path("src/hal.zig"),
60+
.optimize = optimize,
61+
});
62+
63+
const unit_tests_run = b.addRunArtifact(unit_tests);
64+
const test_step = b.step("test", "Run platform agnostic unit tests");
65+
test_step.dependOn(&unit_tests_run.step);
5566
}

port/espressif/esp/build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
.name = "port/espressif/esp",
33
.version = "0.0.0",
44
.dependencies = .{
5-
.@"microzig/build" = .{ .path = "../../../build" },
5+
.@"microzig/build-internals" = .{ .path = "../../../build-internals" },
66
},
77
.paths = .{
88
"LICENSE",
99
"README.md",
10-
"build.zig",
1110
"build.zig.zon",
11+
"build.zig",
1212
"src",
1313
"docs",
1414
},

0 commit comments

Comments
 (0)