summaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig78
1 files changed, 78 insertions, 0 deletions
diff --git a/build.zig b/build.zig
new file mode 100644
index 0000000..0a65c1f
--- /dev/null
+++ b/build.zig
@@ -0,0 +1,78 @@
+const std = @import("std");
+const Builder = std.Build;
+
+pub fn build(b: *std.Build) !void {
+ const uno = std.zig.CrossTarget{
+ .cpu_arch = .avr,
+ .cpu_model = .{ .explicit = &std.Target.avr.cpu.atmega328p },
+ .os_tag = .freestanding,
+ .abi = .none,
+ };
+
+ const exe = b.addExecutable(.{
+ .name = "blink",
+ .root_source_file = b.path("blink.zig"),
+ .target = b.resolveTargetQuery(uno),
+ .optimize = .ReleaseSafe,
+ });
+
+ const init = b.addObject(.{
+ .name = "startup",
+ .root_source_file = b.path("init.zig"),
+ .target = b.resolveTargetQuery(uno),
+ .optimize = .ReleaseSafe
+ });
+
+ exe.addObject(init);
+ exe.setLinkerScriptPath(b.path("linker.ld"));
+
+ b.installArtifact(exe);
+
+ const tty = b.option(
+ []const u8,
+ "tty",
+ "Specify the port to which the Arduino is connected (defaults to /dev/ttyACM0)",
+ ) orelse "/dev/ttyACM0";
+
+ const bin_path = b.getInstallPath(.{ .custom = exe.installed_path orelse "./bin" }, exe.out_filename);
+
+ const flash_command = blk: {
+ var tmp = std.ArrayList(u8).init(b.allocator);
+ try tmp.appendSlice("-Uflash:w:");
+ try tmp.appendSlice(bin_path);
+ try tmp.appendSlice(":e");
+ break :blk try tmp.toOwnedSlice();
+ };
+
+ const upload = b.step("upload", "Upload the code to an Arduino device using avrdude");
+ const avrdude = b.addSystemCommand(&.{
+ "avrdude",
+ "-carduino",
+ "-patmega328p",
+ "-D",
+ "-P",
+ tty,
+ flash_command,
+ });
+ upload.dependOn(&avrdude.step);
+ avrdude.step.dependOn(&exe.step);
+
+ const objdump = b.step("objdump", "Show dissassembly of the code using avr-objdump");
+ const avr_objdump = b.addSystemCommand(&.{
+ "avr-objdump",
+ "-dh",
+ bin_path,
+ });
+ objdump.dependOn(&avr_objdump.step);
+ avr_objdump.step.dependOn(&exe.step);
+
+ const monitor = b.step("monitor", "Opens a monitor to the serial output");
+ const screen = b.addSystemCommand(&.{
+ "screen",
+ tty,
+ "115200",
+ });
+ monitor.dependOn(&screen.step);
+
+ b.default_step.dependOn(&exe.step);
+}