diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index dbf09fd2..bed4c43f 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: example_dir: [ - #espressif/esp, + espressif/esp, gigadevice/gd32, microchip/avr, microchip/atsam, diff --git a/build.zig b/build.zig index 66d98e09..d12da022 100644 --- a/build.zig +++ b/build.zig @@ -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" }, diff --git a/build.zig.zon b/build.zig.zon index 7af42e59..269ce5ee 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -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 }, diff --git a/examples/espressif/esp/build.zig b/examples/espressif/esp/build.zig index 48139900..fc61f7fd 100644 --- a/examples/espressif/esp/build.zig +++ b/examples/espressif/esp/build.zig @@ -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, @@ -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, }; diff --git a/examples/espressif/esp/build.zig.zon b/examples/espressif/esp/build.zig.zon index cd4c4b68..7b92fe85 100644 --- a/examples/espressif/esp/build.zig.zon +++ b/examples/espressif/esp/build.zig.zon @@ -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", diff --git a/port/espressif/esp/build.zig b/port/espressif/esp/build.zig index b9d2577c..c4c29ac8 100644 --- a/port/espressif/esp/build.zig +++ b/port/espressif/esp/build.zig @@ -1,55 +1,66 @@ 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{ + .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, }, - + .cpu_module_file = b.path("src/cpus/espressif-riscv.zig"), + .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); } diff --git a/port/espressif/esp/build.zig.zon b/port/espressif/esp/build.zig.zon index b2504177..4d28545e 100644 --- a/port/espressif/esp/build.zig.zon +++ b/port/espressif/esp/build.zig.zon @@ -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", },