Skip to content

Commit

Permalink
Get esp32 examples building again
Browse files Browse the repository at this point in the history
  • Loading branch information
Grazfather committed Dec 6, 2024
1 parent e40db55 commit d662b29
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 56 deletions.
3 changes: 2 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const port_list: []const struct {
name: [:0]const u8,
dep_name: [:0]const u8,
} = &.{
// .{ .name = "esp", .dep_name = "port/espressif/esp" },
.{ .name = "esp", .dep_name = "port/espressif/esp" },
.{ .name = "gd32", .dep_name = "port/gigadevice/gd32" },
.{ .name = "atsam", .dep_name = "port/microchip/atsam" },
.{ .name = "avr", .dep_name = "port/microchip/avr" },
Expand Down Expand Up @@ -681,6 +681,7 @@ inline fn custom_find_import_pkg_hash_or_fatal(comptime dep_name: []const u8) []
if (@hasDecl(pkg, "build_zig") and pkg.build_zig == @This()) break pkg.deps;
} else deps.root_deps;

@setEvalBranchQuota(2000); // DELETEME
comptime for (pkg_deps) |dep| {
if (std.mem.eql(u8, dep[0], dep_name)) return dep[1];
};
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
.@"tools/uf2" = .{ .path = "tools/uf2" },

// ports
// .@"port/espressif/esp" = .{ .path = "port/espressif/esp", .lazy = true },
.@"port/espressif/esp" = .{ .path = "port/espressif/esp", .lazy = true },
.@"port/gigadevice/gd32" = .{ .path = "port/gigadevice/gd32", .lazy = true },
.@"port/microchip/atsam" = .{ .path = "port/microchip/atsam", .lazy = true },
.@"port/microchip/avr" = .{ .path = "port/microchip/avr", .lazy = true },
Expand Down
24 changes: 14 additions & 10 deletions examples/espressif/esp/build.zig
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
const std = @import("std");
const MicroZig = @import("microzig/build");
const esp = @import("microzig/port/espressif/esp");
const microzig = @import("microzig");

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

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

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep) orelse return;
const available_examples = [_]Example{
.{ .target = mb.ports.esp.chips.esp32_c3, .name = "esp32-c3_blinky", .file = "src/blinky.zig" },
};

for (available_examples) |example| {
// `add_firmware` basically works like addExecutable, but takes a
// `microzig.Target` for target instead of a `std.zig.CrossTarget`.
//
// The target will convey all necessary information on the chip,
// cpu and potentially the board as well.
const firmware = microzig.add_firmware(b, .{
const firmware = mb.add_firmware(.{
.name = example.name,
.target = example.target,
.optimize = optimize,
Expand All @@ -27,15 +31,15 @@ pub fn build(b: *std.Build) void {
// and allows installing the firmware as a typical firmware file.
//
// This will also install into `$prefix/firmware` instead of `$prefix/bin`.
microzig.install_firmware(b, firmware, .{});
mb.install_firmware(firmware, .{});

// For debugging, we also always install the firmware as an ELF file
microzig.install_firmware(b, firmware, .{ .format = .elf });
mb.install_firmware(firmware, .{ .format = .elf });
}
}

const Example = struct {
target: MicroZig.Target,
target: *const microzig.Target,
name: []const u8,
file: []const u8,
};
4 changes: 1 addition & 3 deletions examples/espressif/esp/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
.name = "examples/espressif/esp",
.version = "0.0.0",
.dependencies = .{
.@"microzig/build" = .{ .path = "../../../build" },
.@"microzig/port/espressif/esp" = .{ .path = "../../../port/espressif/esp" },
.microzig = .{ .path = "../../.." },
},

.paths = .{
"LICENSE",
"README.md",
Expand Down
90 changes: 51 additions & 39 deletions port/espressif/esp/build.zig
Original file line number Diff line number Diff line change
@@ -1,55 +1,67 @@
const std = @import("std");
const MicroZig = @import("microzig/build");
const microzig = @import("microzig/build-internals");

fn path(comptime suffix: []const u8) std.Build.LazyPath {
return .{
.cwd_relative = comptime ((std.fs.path.dirname(@src().file) orelse ".") ++ suffix),
};
}
const Self = @This();

chips: struct {
esp32_c3: *const microzig.Target,
},

const esp_riscv = .{
.name = "Espressif RISC-V",
.root_source_file = path("/src/cpus/espressif-riscv.zig"),
.target = std.Target.Query{
.cpu_arch = .riscv32,
.cpu_model = .{ .explicit = &std.Target.riscv.cpu.generic_rv32 },
.cpu_features_add = std.Target.riscv.featureSet(&.{
std.Target.riscv.Feature.c,
std.Target.riscv.Feature.m,
}),
.os_tag = .freestanding,
.abi = .eabi,
},
};

const hal = .{
.root_source_file = path("/src/hals/ESP32_C3.zig"),
};

pub const chips = struct {
pub const esp32_c3 = MicroZig.Target{
.preferred_format = .bin, // TODO: Exchange FLAT format with .esp format
boards: struct {},

pub fn init(dep: *std.Build.Dependency) Self {
const b = dep.builder;

const hal: microzig.HardwareAbstractionLayer = .{
.root_source_file = b.path("src/hals/ESP32_C3.zig"),
};
const chip_esp32c3: microzig.Target = .{
.dep = dep,
// TODO: Exchange FLAT format with .esp format
.preferred_binary_format = .bin,
.chip = .{
.name = "ESP32-C3",
.url = "https://www.espressif.com/en/products/socs/esp32-c3",

.cpu = esp_riscv,

.register_definition = .{
.svd = path("/src/chips/ESP32-C3.svd"),
.cpu = std.Target.Query{
// .name = "Espressif RISC-V",
// .root_source_file = b.path("/src/cpus/espressif-riscv.zig"),
.cpu_arch = .riscv32,
.cpu_model = .{ .explicit = &std.Target.riscv.cpu.generic_rv32 },
.cpu_features_add = std.Target.riscv.featureSet(&.{
std.Target.riscv.Feature.c,
std.Target.riscv.Feature.m,
}),
.os_tag = .freestanding,
.abi = .eabi,
},

.register_definition = .{ .svd = b.path("src/chips/ESP32-C3.svd") },
.memory_regions = &.{
.{ .kind = .flash, .offset = 0x4200_0000, .length = 0x0080_0000 }, // external memory, ibus
.{ .kind = .ram, .offset = 0x3FC8_0000, .length = 0x0006_0000 }, // sram 1, data bus
// external memory, ibus
.{ .kind = .flash, .offset = 0x4200_0000, .length = 0x0080_0000 },
// sram 1, data bus
.{ .kind = .ram, .offset = 0x3FC8_0000, .length = 0x0006_0000 },
},
},
.hal = hal,
};
};

pub const boards = struct {};
return .{
.chips = .{
.esp32_c3 = chip_esp32c3.derive(.{}),
},
.boards = .{},
};
}

pub fn build(b: *std.Build) void {
_ = b.step("test", "Run platform agnostic unit tests");
const optimize = b.standardOptimizeOption(.{});

const unit_tests = b.addTest(.{
.root_source_file = b.path("src/hal.zig"),
.optimize = optimize,
});

const unit_tests_run = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run platform agnostic unit tests");
test_step.dependOn(&unit_tests_run.step);
}
4 changes: 2 additions & 2 deletions port/espressif/esp/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
.name = "port/espressif/esp",
.version = "0.0.0",
.dependencies = .{
.@"microzig/build" = .{ .path = "../../../build" },
.@"microzig/build-internals" = .{ .path = "../../../build-internals" },
},
.paths = .{
"LICENSE",
"README.md",
"build.zig",
"build.zig.zon",
"build.zig",
"src",
"docs",
},
Expand Down

0 comments on commit d662b29

Please sign in to comment.