Skip to content

Commit 5093bba

Browse files
committed
generate multiple files with regz (#533)
generate embassy on the fly now (#536)
1 parent 4b8bec7 commit 5093bba

File tree

17 files changed

+3434
-576135
lines changed

17 files changed

+3434
-576135
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989
with:
9090
version: ${{ env.ZIG_VERSION }}
9191
- name: Generate Code
92-
run: zig build -Dgenerate
92+
run: zig build generate
9393
working-directory: port/stmicro/stm32
9494
- name: Check for code diffs
9595
run: |

build-internals/build.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ pub const Chip = struct {
124124

125125
/// Use the provided file directly as the chip file.
126126
zig: LazyPath,
127+
128+
/// Path to embassy stm32-data directory
129+
embassy: LazyPath,
127130
},
128131

129132
/// The memory regions that are present in this chip.

build.zig

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ pub fn MicroBuild(port_select: PortSelect) type {
422422
regz_run.addArg(@tagName(target.chip.register_definition));
423423

424424
regz_run.addArg("--output_path"); // Write to a file
425-
const zig_file = regz_run.addOutputFileArg("chip.zig");
426425

426+
const chips_dir = regz_run.addOutputDirectoryArg("chips");
427427
var patches = std.ArrayList(regz.patch.Patch).init(b.allocator);
428428

429429
// From chip definition
@@ -443,8 +443,38 @@ pub fn MicroBuild(port_select: PortSelect) type {
443443
}
444444

445445
regz_run.addFileArg(file);
446+
break :blk chips_dir.path(b, b.fmt("{s}.zig", .{target.chip.name}));
447+
},
448+
.embassy => |path| blk: {
449+
const regz_run = b.addRunArtifact(regz_exe);
450+
451+
regz_run.addArg("--microzig");
452+
regz_run.addArg("--format");
453+
regz_run.addArg(@tagName(target.chip.register_definition));
454+
455+
regz_run.addArg("--output_path"); // Write to a file
456+
457+
const chips_dir = regz_run.addOutputDirectoryArg("chips");
458+
var patches = std.ArrayList(regz.patch.Patch).init(b.allocator);
459+
460+
// From chip definition
461+
patches.appendSlice(target.chip.patches) catch @panic("OOM");
462+
463+
// From user invoking `add_firmware`
464+
patches.appendSlice(options.patches) catch @panic("OOM");
465+
466+
if (patches.items.len > 0) {
467+
// write patches to file
468+
const patch_ndjson = serialize_patches(b, patches.items);
469+
const write_file_step = b.addWriteFiles();
470+
const patch_file = write_file_step.add("patch.ndjson", patch_ndjson);
471+
472+
regz_run.addArg("--patch_path");
473+
regz_run.addFileArg(patch_file);
474+
}
446475

447-
break :blk zig_file;
476+
regz_run.addDirectoryArg(path);
477+
break :blk chips_dir.path(b, b.fmt("{s}.zig", .{target.chip.name}));
448478
},
449479

450480
.zig => |src| src,

core/src/microzig.zig

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ pub const config = @import("config");
1919
pub const cpu = @import("cpu");
2020

2121
/// Provides access to the low level features of the current microchip.
22-
pub const chip = struct {
23-
const inner = @import("chip");
24-
pub const types = inner.types;
25-
pub usingnamespace @field(inner.devices, config.chip_name);
26-
};
22+
pub const chip = @import("chip");
2723

2824
/// Provides higher level APIs for interacting with hardware
2925
pub const hal = if (config.has_hal) @import("hal") else void;

port/stmicro/stm32/build.zig

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,28 @@ pub fn init(dep: *std.Build.Dependency) Self {
5151
}
5252

5353
pub fn build(b: *std.Build) !void {
54-
const generate = b.option(bool, "generate", "Generate chip definitions from embassy-rs/stm32-data-generated") orelse false;
54+
const stm32_data_generated = b.lazyDependency("stm32-data-generated", .{}) orelse return;
5555

56-
if (generate) {
57-
const stm32_data_generated = b.lazyDependency("stm32-data-generated", .{}) orelse return;
56+
const generate_optimize = .ReleaseSafe;
57+
const regz_dep = b.dependency("microzig/tools/regz", .{
58+
.optimize = generate_optimize,
59+
});
60+
const regz = regz_dep.module("regz");
5861

59-
const generate_optimize = .ReleaseSafe;
60-
const regz_dep = b.dependency("microzig/tools/regz", .{
61-
.optimize = generate_optimize,
62-
});
63-
const regz = regz_dep.module("regz");
62+
const generate_exe = b.addExecutable(.{
63+
.name = "generate",
64+
.root_source_file = b.path("src/generate.zig"),
65+
.target = b.graph.host,
66+
.optimize = generate_optimize,
67+
});
68+
generate_exe.root_module.addImport("regz", regz);
6469

65-
const generate_exe = b.addExecutable(.{
66-
.name = "generate",
67-
.root_source_file = b.path("src/generate.zig"),
68-
.target = b.graph.host,
69-
.optimize = generate_optimize,
70-
});
71-
generate_exe.root_module.addImport("regz", regz);
70+
const generate_run = b.addRunArtifact(generate_exe);
71+
generate_run.max_stdio_size = std.math.maxInt(usize);
72+
generate_run.addFileArg(stm32_data_generated.path("."));
7273

73-
const generate_run = b.addRunArtifact(generate_exe);
74-
generate_run.max_stdio_size = std.math.maxInt(usize);
75-
generate_run.addFileArg(stm32_data_generated.path("."));
76-
b.getInstallStep().dependOn(&generate_run.step);
77-
} else {
78-
_ = b.step("test", "Run platform agnostic unit tests");
79-
}
74+
const generate_step = b.step("generate", "Generate chips file 'src/Chips.zig'");
75+
generate_step.dependOn(&generate_run.step);
76+
77+
_ = b.step("test", "Run platform agnostic unit tests");
8078
}

port/stmicro/stm32/build.zig.zon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
.@"stm32-data-generated" = .{
99
.url = "git+https://github.com/embassy-rs/stm32-data-generated.git#5198a6e36b24f6d79c4ac91af5e61e8d54667d88",
1010
.hash = "N-V-__8AAFi8WBlOh-NikHFVBjzQE0F1KixgKjVWYnlijPNm",
11-
.lazy = true,
1211
},
1312
},
1413
.paths = .{

0 commit comments

Comments
 (0)