From af7aa777f9bc17b18f80615010896ba4f1562799 Mon Sep 17 00:00:00 2001 From: Matthew Knight Date: Mon, 18 Oct 2021 18:22:13 -0700 Subject: [PATCH] Freshen things up (#6) * catch up to some of master's changes, getting prepped to anchor to 0.9.0 * found fix for lpc board --- .github/workflows/build.yml | 28 ++++++++++++ build.zig | 66 ++++++++++++++------------- src/core/debug.zig | 6 +-- src/core/gpio.zig | 1 + src/core/interrupts.zig | 3 ++ src/modules/chips/lpc1768/lpc1768.zig | 2 + tests/uart-sync.zig | 3 +- 7 files changed, 73 insertions(+), 36 deletions(-) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..35a13f4 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,28 @@ +name: Build +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + schedule: + - cron: "0 7 * * *" + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + submodules: recursive + fetch-depth: 0 + + # only using master until 0.9.0 is released + - name: Setup Zig + uses: goto-bus-stop/setup-zig@v1.3.0 + with: + version: master + + - name: Build tests + run: zig build -Drelease-small diff --git a/build.zig b/build.zig index 93fa0a0..6543c41 100644 --- a/build.zig +++ b/build.zig @@ -4,17 +4,17 @@ const std = @import("std"); -pub fn build(b: *std.build.Builder) void { +pub fn build(b: *std.build.Builder) !void { const mode = b.standardReleaseOptions(); const test_step = b.step("test", "Builds and runs the library test suite"); const BuildConfig = struct { name: []const u8, backing: Backing }; const all_backings = [_]BuildConfig{ - BuildConfig{ .name = "boards.arduino_nano", .backing = Backing{ .board = pkgs.boards.arduino_nano } }, + //BuildConfig{ .name = "boards.arduino_nano", .backing = Backing{ .board = pkgs.boards.arduino_nano } }, BuildConfig{ .name = "boards.mbed_lpc1768", .backing = Backing{ .board = pkgs.boards.mbed_lpc1768 } }, - BuildConfig{ .name = "chips.atmega328p", .backing = Backing{ .chip = pkgs.chips.atmega328p } }, - BuildConfig{ .name = "chips.lpc1768", .backing = Backing{ .chip = pkgs.chips.lpc1768 } }, + //BuildConfig{ .name = "chips.atmega328p", .backing = Backing{ .chip = pkgs.chips.atmega328p } }, + //BuildConfig{ .name = "chips.lpc1768", .backing = Backing{ .chip = pkgs.chips.lpc1768 } }, }; const Test = struct { name: []const u8, source: []const u8 }; @@ -28,7 +28,7 @@ pub fn build(b: *std.build.Builder) void { inline for (all_backings) |cfg| { inline for (all_tests) |tst| { - const exe = addEmbeddedExecutable( + const exe = try addEmbeddedExecutable( b, "test-" ++ tst.name ++ "-" ++ cfg.name, tst.source, @@ -45,12 +45,12 @@ pub fn build(b: *std.build.Builder) void { } } -fn addEmbeddedExecutable(builder: *std.build.Builder, name: []const u8, source: []const u8, backing: Backing) *std.build.LibExeObjStep { +fn addEmbeddedExecutable(builder: *std.build.Builder, name: []const u8, source: []const u8, backing: Backing) !*std.build.LibExeObjStep { const Pkg = std.build.Pkg; const microzig_base = Pkg{ .name = "microzig", - .path = "src/core/microzig.zig", + .path = .{ .path = "src/core/microzig.zig" }, }; const chip = switch (backing) { @@ -62,18 +62,18 @@ fn addEmbeddedExecutable(builder: *std.build.Builder, name: []const u8, source: const chip_package = Pkg{ .name = "chip", - .path = chip.path, + .path = .{ .path = chip.path }, .dependencies = &[_]Pkg{ microzig_base, pkgs.mmio, Pkg{ .name = "cpu", - .path = chip.cpu.path, + .path = .{ .path = chip.cpu.path }, .dependencies = &[_]Pkg{ microzig_base, pkgs.mmio }, }, Pkg{ .name = "microzig-linker", - .path = "src/modules/linker/linker.zig", + .path = .{ .path = "src/modules/linker/linker.zig" }, }, }, }; @@ -96,11 +96,11 @@ fn addEmbeddedExecutable(builder: *std.build.Builder, name: []const u8, source: const file_suffix = ".ld"; var ld_file_name: [file_prefix.len + 2 * hash.len + file_suffix.len]u8 = undefined; - const filename = std.fmt.bufPrint(&ld_file_name, "{s}{}{s}", .{ + const filename = try std.fmt.bufPrint(&ld_file_name, "{s}{}{s}", .{ file_prefix, std.fmt.fmtSliceHexLower(&hash), file_suffix, - }) catch unreachable; + }); break :blk builder.dupe(filename); }; @@ -128,44 +128,46 @@ fn addEmbeddedExecutable(builder: *std.build.Builder, name: []const u8, source: const file_suffix = ".zig"; var ld_file_name: [file_prefix.len + 2 * hash.len + file_suffix.len]u8 = undefined; - const filename = std.fmt.bufPrint(&ld_file_name, "{s}{}{s}", .{ + const filename = try std.fmt.bufPrint(&ld_file_name, "{s}{}{s}", .{ file_prefix, std.fmt.fmtSliceHexLower(&hash), file_suffix, - }) catch unreachable; + }); break :blk builder.dupe(filename); }; { - var config_file = std.fs.cwd().createFile(config_file_name, .{}) catch unreachable; + std.fs.cwd().makeDir(std.fs.path.dirname(config_file_name).?) catch {}; + var config_file = try std.fs.cwd().createFile(config_file_name, .{}); defer config_file.close(); var writer = config_file.writer(); + try writer.print("pub const has_board = {};\n", .{has_board}); + if (has_board) + try writer.print("pub const board_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(backing.board.name)}); - writer.print("pub const has_board = {};\n", .{has_board}) catch unreachable; - if (has_board) { - writer.print("pub const board_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(backing.board.name)}) catch unreachable; - } - - writer.print("pub const chip_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.name)}) catch unreachable; - writer.print("pub const cpu_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.cpu.name)}) catch unreachable; + try writer.print("pub const chip_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.name)}); + try writer.print("pub const cpu_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.cpu.name)}); } const config_pkg = Pkg{ .name = "microzig-config", - .path = config_file_name, + .path = .{ .path = config_file_name }, }; + const build_options = builder.addOptions(); + build_options.addOption([]const u8, "microzig_chip_name", chip.name); + build_options.addOption([]const u8, "microzig_cpu_name", chip.cpu.name); + build_options.addOption([]const u8, "microzig_target_triple", try chip.cpu.target.zigTriple(builder.allocator)); + const linkerscript_gen = builder.addExecutable("linkerscript-gen", "src/tools/linkerscript-gen.zig"); linkerscript_gen.addPackage(chip_package); linkerscript_gen.addPackage(Pkg{ .name = "microzig-linker", - .path = "src/modules/linker/linker.zig", + .path = .{ .path = "src/modules/linker/linker.zig" }, }); - linkerscript_gen.addBuildOption([]const u8, "microzig_chip_name", chip.name); - linkerscript_gen.addBuildOption([]const u8, "microzig_cpu_name", chip.cpu.name); - linkerscript_gen.addBuildOption([]const u8, "microzig_target_triple", chip.cpu.target.zigTriple(builder.allocator) catch unreachable); + linkerscript_gen.addOptions("build_options", build_options); const linkerscript_invocation = linkerscript_gen.run(); linkerscript_invocation.addArg(linker_script_name); @@ -177,7 +179,7 @@ fn addEmbeddedExecutable(builder: *std.build.Builder, name: []const u8, source: exe.single_threaded = true; exe.setTarget(chip.cpu.target); - exe.setLinkerScriptPath(linker_script_name); + exe.setLinkerScriptPath(.{ .path = linker_script_name }); exe.step.dependOn(&linkerscript_invocation.step); // TODO: @@ -185,7 +187,7 @@ fn addEmbeddedExecutable(builder: *std.build.Builder, name: []const u8, source: // - This requires building another tool that runs on the host that compiles those files and emits the linker script. // - src/tools/linkerscript-gen.zig is the source file for this - // exe.bundle_compiler_rt = false; + exe.bundle_compiler_rt = false; switch (backing) { .chip => { @@ -204,7 +206,7 @@ fn addEmbeddedExecutable(builder: *std.build.Builder, name: []const u8, source: chip_package, Pkg{ .name = "board", - .path = board.path, + .path = .{ .path = board.path }, .dependencies = &[_]Pkg{ microzig_base, chip_package, pkgs.mmio }, }, }, @@ -241,7 +243,7 @@ pub const Backing = union(enum) { const pkgs = struct { const mmio = std.build.Pkg{ .name = "microzig-mmio", - .path = "src/core/mmio.zig", + .path = .{ .path = "src/core/mmio.zig" }, }; const cpus = struct { @@ -264,7 +266,7 @@ const pkgs = struct { .cpu_arch = .arm, .cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m3 }, .os_tag = .freestanding, - .abi = .eabi, + .abi = .none, }, }; }; diff --git a/src/core/debug.zig b/src/core/debug.zig index eb2474c..88d0f7a 100644 --- a/src/core/debug.zig +++ b/src/core/debug.zig @@ -19,6 +19,7 @@ pub fn busySleep(comptime limit: comptime_int) void { const DebugErr = error{}; fn writerWrite(ctx: void, string: []const u8) DebugErr!usize { + _ = ctx; write(string); return string.len; } @@ -26,10 +27,9 @@ fn writerWrite(ctx: void, string: []const u8) DebugErr!usize { const DebugWriter = std.io.Writer(void, DebugErr, writerWrite); pub fn write(string: []const u8) void { - if (!micro.config.has_board) - return; - if (!@hasDecl(micro.board, "debugWrite")) + if (!micro.config.has_board and !@hasDecl(micro.board, "debugWrite")) return; + micro.board.debugWrite(string); } diff --git a/src/core/gpio.zig b/src/core/gpio.zig index 325cfed..4008b89 100644 --- a/src/core/gpio.zig +++ b/src/core/gpio.zig @@ -102,6 +102,7 @@ pub fn Gpio(comptime pin: type, config: anytype) type { // open drain fn setDrive(drive: Drive) void { + _ = drive; @compileError("open drain not implemented yet!"); } fn getDrive() Drive { diff --git a/src/core/interrupts.zig b/src/core/interrupts.zig index 10013c4..79f297f 100644 --- a/src/core/interrupts.zig +++ b/src/core/interrupts.zig @@ -4,16 +4,19 @@ const micro = @import("microzig.zig"); /// Unmasks the given interrupt and enables its execution. /// Note that interrupts must be globally enabled with `sei()` as well. pub fn enable(comptime interrupt: anytype) void { + _ = interrupt; @compileError("not implemented yet!"); } /// Masks the given interrupt and disables its execution. pub fn disable(comptime interrupt: anytype) void { + _ = interrupt; @compileError("not implemented yet!"); } /// Returns true when the given interrupt is unmasked. pub fn isEnabled(comptime interrupt: anytype) bool { + _ = interrupt; @compileError("not implemented yet!"); } diff --git a/src/modules/chips/lpc1768/lpc1768.zig b/src/modules/chips/lpc1768/lpc1768.zig index 4f39fa4..b4a8e05 100644 --- a/src/modules/chips/lpc1768/lpc1768.zig +++ b/src/modules/chips/lpc1768/lpc1768.zig @@ -174,6 +174,7 @@ pub fn Uart(comptime index: usize) type { } pub fn canWrite(self: Self) bool { + _ = self; return switch (UARTn.LSR.read().THRE) { .VALID => true, .THR_IS_EMPTY_ => false, @@ -185,6 +186,7 @@ pub fn Uart(comptime index: usize) type { } pub fn canRead(self: Self) bool { + _ = self; return switch (UARTn.LSR.read().RDR) { .EMPTY => false, .NOTEMPTY => true, diff --git a/tests/uart-sync.zig b/tests/uart-sync.zig index 335fda2..8c97d4f 100644 --- a/tests/uart-sync.zig +++ b/tests/uart-sync.zig @@ -31,7 +31,7 @@ const PLL = struct { .FLASHTIM = @intToEnum(@TypeOf(micro.chip.registers.SYSCON.FLASHCFG.read().FLASHTIM), @intCast(u4, timing - 1)), }); } - fn feed_pll() callconv(.Inline) void { + inline fn feed_pll() void { micro.chip.registers.SYSCON.PLL0FEED.write(.{ .PLL0FEED = 0xAA }); micro.chip.registers.SYSCON.PLL0FEED.write(.{ .PLL0FEED = 0x55 }); } @@ -109,6 +109,7 @@ pub fn main() !void { var out = debug_port.writer(); var in = debug_port.reader(); + _ = in; try out.writeAll("Please enter a sentence:\r\n");