diff --git a/.gitignore b/.gitignore index e73c965..b9279c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ zig-cache/ zig-out/ +.DS_Store diff --git a/README.md b/README.md index d315d6b..6714f93 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,27 @@ -# target extended-remote /dev/ttyACM{first} +# SYCL 2024 PCB Badge -# monitor swd_scan # searches for device -# attach 1 -# file zig-out/firmware/pybadge-io.elf -# +## Getting Started -# $ sudo dfu-util -a 0 --dfuse-address 0x08000000:leave -R -D ~/Downloads/blackmagic-firmware-v1.10.0-rc1/blackpill-f401cc/blackmagic-blackpill-f401cc.bin ^C \ No newline at end of file +Temporarily this repo targets the `zig-master` branch of +[MicroZig](https://github.com/ZigEmbeddedGroup/microzig). In order to build this +repo, you must have it checked out, and `microzig` next to `sycl-badge-2024` in +your filesystem. Once this branch is merged it won't be as janky to build this +firmware. + + +## Uploading firmware using a debugger + +``` +target extended-remote /dev/ttyACM{first} + +monitor swd_scan # searches for device +attach 1 +file zig-out/firmware/pybadge-io.elf + +``` + +## Updating Black Magic Probe firmware + +``` +sudo dfu-util -a 0 --dfuse-address 0x08000000:leave -R -D ~/Downloads/blackmagic-firmware-v1.10.0-rc1/blackpill-f401cc/blackmagic-blackpill-f401cc.bin ^C +``` diff --git a/build.zig b/build.zig index 072cc7d..4244f40 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,8 @@ -const atsam = @import("atsam"); -const MicroZig = @import("microzig"); const std = @import("std"); +const Build = std.Build; + +const MicroZig = @import("microzig/build"); +const atsam = @import("microzig/bsp/microchip/atsam"); pub const py_badge: MicroZig.Target = .{ .preferred_format = .elf, @@ -8,8 +10,8 @@ pub const py_badge: MicroZig.Target = .{ .hal = null, }; -pub fn build(b: *std.Build) void { - const mz = MicroZig.init(b, "microzig"); +pub fn build(b: *Build) void { + const mz = MicroZig.init(b, .{}); const optimize = b.standardOptimizeOption(.{}); const fw_options = b.addOptions(); @@ -25,16 +27,16 @@ pub fn build(b: *std.Build) void { var modified_py_badge = py_badge; modified_py_badge.chip.memory_regions = modified_memory_regions; - const fw = mz.addFirmware(b, .{ + const fw = mz.add_firmware(b, .{ .name = "pybadge-io", .target = modified_py_badge, .optimize = optimize, .source_file = .{ .path = "src/main.zig" }, }); fw.artifact.step.dependOn(&fw_options.step); - fw.modules.app.dependencies.put("options", fw_options.createModule()) catch @panic("out of memory"); - mz.installFirmware(b, fw, .{}); - mz.installFirmware(b, fw, .{ .format = .{ .uf2 = .SAMD51 } }); + fw.modules.app.addImport("options", fw_options.createModule()); + mz.install_firmware(b, fw, .{}); + mz.install_firmware(b, fw, .{ .format = .{ .uf2 = .SAMD51 } }); } pub const Cart = struct { @@ -45,12 +47,12 @@ pub const Cart = struct { pub const CartOptions = struct { name: []const u8, optimize: std.builtin.OptimizeMode, - source_file: std.Build.LazyPath, + source_file: Build.LazyPath, }; -pub fn addCart( - d: *std.Build.Dependency, - b: *std.Build, +pub fn add_cart( + d: *Build.Dependency, + b: *Build, options: CartOptions, ) *Cart { const cart_lib = b.addStaticLibrary(.{ @@ -85,7 +87,7 @@ pub fn addCart( return cart; } -pub fn installCart(b: *std.Build, cart: *Cart) void { - cart.mz.installFirmware(b, cart.fw, .{}); - cart.mz.installFirmware(b, cart.fw, .{ .format = .{ .uf2 = .SAMD51 } }); +pub fn install_cart(b: *Build, cart: *Cart) void { + cart.mz.install_firmware(b, cart.fw, .{}); + cart.mz.install_firmware(b, cart.fw, .{ .format = .{ .uf2 = .SAMD51 } }); } diff --git a/build.zig.zon b/build.zig.zon index d9a77ba..8202c4c 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -9,13 +9,11 @@ }, .dependencies = .{ - .microzig = .{ - .url = "https://github.com/ZigEmbeddedGroup/microzig/archive/57e4379062e396b354d70d5b924d5da2e8905bd4.tar.gz", - .hash = "1220b8b04be6b97020eb19fca51b3cb44c33f5a619c3e36fc48faab8b15261dfff2a", + .@"microzig/build" = .{ + .path = "../microzig/build", }, - .atsam = .{ - .url = "https://github.com/ZigEmbeddedGroup/microchip-atsam/archive/93ddffbcbe7a0b849cca610ee933210280d731b4.tar.gz", - .hash = "12209a965a8aaf3a80b224fc3b796b7a21af86e3550373227db86615b6fa09b642cf", + .@"microzig/bsp/microchip/atsam" = .{ + .path = "../microzig/bsp/microchip/atsam", }, }, } diff --git a/src/Port.zig b/src/Port.zig index 7208df0..2ec1ba8 100644 --- a/src/Port.zig +++ b/src/Port.zig @@ -64,27 +64,27 @@ pub const Group = enum { A, B }; pub const Direction = enum { in, out }; pub const Level = enum { low, high }; -pub inline fn setDir(port: Port, dir: Direction) void { +pub inline fn set_dir(port: Port, dir: Direction) void { switch (dir) { - .in => port.groupPtr().DIRCLR.write(.{ .DIRCLR = @as(u32, 1) << port.pin }), - .out => port.groupPtr().DIRSET.write(.{ .DIRSET = @as(u32, 1) << port.pin }), + .in => port.group_ptr().DIRCLR.write(.{ .DIRCLR = @as(u32, 1) << port.pin }), + .out => port.group_ptr().DIRSET.write(.{ .DIRSET = @as(u32, 1) << port.pin }), } } pub inline fn write(port: Port, level: Level) void { switch (level) { - .low => port.groupPtr().OUTCLR.write(.{ .OUTCLR = @as(u32, 1) << port.pin }), - .high => port.groupPtr().OUTSET.write(.{ .OUTSET = @as(u32, 1) << port.pin }), + .low => port.group_ptr().OUTCLR.write(.{ .OUTCLR = @as(u32, 1) << port.pin }), + .high => port.group_ptr().OUTSET.write(.{ .OUTSET = @as(u32, 1) << port.pin }), } } pub inline fn read(port: Port) Level { - return @enumFromInt(port.groupPtr().IN.read().IN >> port.pin & 1); + return @enumFromInt(port.group_ptr().IN.read().IN >> port.pin & 1); } pub const Mux = enum(u4) { A, B, C, D, E, F, G, H, I, J, K, L, M, N }; -pub inline fn setMux(port: Port, mux: Mux) void { - const pmux = &port.groupPtr().PMUX[port.pin / 2]; +pub inline fn set_mux(port: Port, mux: Mux) void { + const pmux = &port.group_ptr().PMUX[port.pin / 2]; switch (@as(u1, @truncate(port.pin))) { 0 => pmux.modify(.{ .PMUXE = .{ .value = @as(io_types.PORT.PORT_PMUX__PMUXE, @enumFromInt(@intFromEnum(mux))), @@ -93,15 +93,15 @@ pub inline fn setMux(port: Port, mux: Mux) void { .value = @as(io_types.PORT.PORT_PMUX__PMUXO, @enumFromInt(@intFromEnum(mux))), } }), } - port.configPtr().modify(.{ .PMUXEN = 1 }); + port.config_ptr().modify(.{ .PMUXEN = 1 }); } const PinCfg = @typeInfo(std.meta.FieldType(io_types.PORT.GROUP, .PINCFG)).Array.child; -pub inline fn configPtr(port: Port) *volatile PinCfg { - return &port.groupPtr().PINCFG[port.pin]; +pub inline fn config_ptr(port: Port) *volatile PinCfg { + return &port.group_ptr().PINCFG[port.pin]; } -fn groupPtr(port: Port) *volatile io_types.PORT.GROUP { +fn group_ptr(port: Port) *volatile io_types.PORT.GROUP { return &io.PORT.GROUP[@intFromEnum(port.group)]; } diff --git a/src/audio.zig b/src/audio.zig index bebe218..f746476 100644 --- a/src/audio.zig +++ b/src/audio.zig @@ -24,9 +24,9 @@ pub const Channel = struct { pub fn init() void { @setCold(true); - Port.A0.setDir(.out); - Port.AVCC.setDir(.in); - Port.SPKR_EN.setDir(.out); + Port.A0.set_dir(.out); + Port.AVCC.set_dir(.in); + Port.SPKR_EN.set_dir(.out); Port.SPKR_EN.write(.low); io.MCLK.APBDMASK.modify(.{ .DAC_ = 1 }); @@ -39,8 +39,8 @@ pub fn init() void { }); io.DAC.CTRLA.write(.{ .SWRST = 1, .ENABLE = 0, .padding = 0 }); while (io.DAC.SYNCBUSY.read().SWRST != 0) {} - Port.A0.setMux(.B); - Port.AVCC.setMux(.B); + Port.A0.set_mux(.B); + Port.AVCC.set_mux(.B); io.DAC.CTRLB.write(.{ .DIFF = 0, .REFSEL = .{ .value = .VREFPU }, .padding = 0 }); io.DAC.EVCTRL.write(.{ .STARTEI0 = 1, @@ -208,16 +208,16 @@ pub fn init() void { }); io.EVSYS.USER[EVSYS.USER.DAC_START0].write(.{ .CHANNEL = EVSYS.CHANNEL.AUDIO + 1, .padding = 0 }); - dma.initAudio(); + dma.init_audio(); while (io.DAC.STATUS.read().READY0 != 1) {} Port.SPKR_EN.write(.high); io.NVIC.ISER[32 / 32].write(.{ .SETENA = 1 << 32 % 32 }); } -pub fn mix() void { +pub fn mix() callconv(.C) void { var local_channels = channels.*; for (&sample_buffer[ - (dma.getAudioPart() + sample_buffer.len - 1) % sample_buffer.len + (dma.get_audio_part() + sample_buffer.len - 1) % sample_buffer.len ]) |*out_sample| { var sample: i32 = 0; inline for (&local_channels) |*channel| { @@ -262,18 +262,18 @@ pub fn mix() void { out_sample.* = @intCast((sample >> 16) - std.math.minInt(i16)); } channels.* = local_channels; - dma.ackAudio(); + dma.ack_audio(); } -pub fn setChannel(channel: usize, state: Channel) void { +pub fn set_channel(channel: usize, state: Channel) void { io.NVIC.ICER[32 / 32].write(.{ .CLRENA = 1 << 32 % 32 }); channels[channel] = state; io.NVIC.ISER[32 / 32].write(.{ .SETENA = 1 << 32 % 32 }); } -pub fn playNote(channel: usize, note: Note) void { +pub fn play_note(channel: usize, note: Note) void { const sample_rate: f32 = 44100.0; - setChannel(channel, .{ + set_channel(channel, .{ .duty = 1 << 31, .phase = 0, .phase_step = @intFromFloat(0x1p32 / sample_rate * note.frequency + 0.5), @@ -295,7 +295,7 @@ pub fn playNote(channel: usize, note: Note) void { }); } -pub fn playSong(channels_notes: []const []const Note) void { +pub fn play_song(channels_notes: []const []const Note) void { var time: f32 = 0.0; var channels_note_index = [1]usize{0} ** channels.len; var channels_note_start = [1]f32{0.0} ** channels.len; @@ -317,7 +317,7 @@ pub fn playSong(channels_notes: []const []const Note) void { if (note_index.* > notes.len) continue; const note = notes[note_index.* - 1]; note_start.* = next_note_time; - playNote(channel_index, .{ + play_note(channel_index, .{ .duration = @max(note.duration - 0.04, 0.0), .frequency = note.frequency, }); diff --git a/src/cart.zig b/src/cart.zig index 57adc51..aafc545 100644 --- a/src/cart.zig +++ b/src/cart.zig @@ -1,8 +1,8 @@ pub fn init() void { @setCold(true); - Port.BUTTON_OUT.setDir(.in); - Port.BUTTON_OUT.configPtr().write(.{ + Port.BUTTON_OUT.set_dir(.in); + Port.BUTTON_OUT.config_ptr().write(.{ .PMUXEN = 0, .INEN = 1, .PULLEN = 0, @@ -10,12 +10,12 @@ pub fn init() void { .DRVSTR = 0, .padding = 0, }); - Port.BUTTON_CLK.setDir(.out); + Port.BUTTON_CLK.set_dir(.out); Port.BUTTON_CLK.write(.high); - Port.BUTTON_LATCH.setDir(.out); + Port.BUTTON_LATCH.set_dir(.out); Port.BUTTON_LATCH.write(.high); - timer.initFrameSync(); + timer.init_frame_sync(); @memset(@as(*[0x19A0]u8, @ptrFromInt(0x20000000)), 0); @@ -51,7 +51,7 @@ pub fn start() void { } pub fn tick() void { - if (!timer.checkFrameReady()) return; + if (!timer.check_frame_ready()) return; { var gamepad: u8 = 0; @@ -162,7 +162,7 @@ pub fn blit(sprite: [*]const User(u8), x: i32, y: i32, rest: *const extern struc const sprite_index = sprite_x + width * sprite_y; const draw_color_index: u1 = @truncate(sprite[sprite_index >> 3].load() >> (7 - @as(u3, @truncate(sprite_index)))); - clipDraw( + clip_draw( x +| @min(sprite_x, std.math.maxInt(i32)), y +| @min(sprite_y, std.math.maxInt(i32)), draw_color_index, @@ -174,7 +174,7 @@ pub fn blit(sprite: [*]const User(u8), x: i32, y: i32, rest: *const extern struc const sprite_index = sprite_x + width * sprite_y; const draw_color_index: u2 = @truncate(sprite[sprite_index >> 2].load() >> (6 - (@as(u3, @as(u2, @truncate(sprite_index))) << 1))); - clipDraw( + clip_draw( x +| @min(sprite_x, std.math.maxInt(i32)), y +| @min(sprite_y, std.math.maxInt(i32)), draw_color_index, @@ -212,18 +212,18 @@ pub fn oval(x: i32, y: i32, width: u32, height: u32) callconv(.C) void { hline(mid_x -| cur_y, mid_y -| cur_x, cur_y << 1); hline(mid_x -| cur_y, mid_y +| cur_x, cur_y << 1); if (stroke_palette_index) |palette_index| { - clipDrawPalette(mid_x -| cur_x, mid_y -| cur_y, palette_index); - clipDrawPalette(mid_x +| cur_x, mid_y -| cur_y, palette_index); - clipDrawPalette(mid_x -| cur_y, mid_y -| cur_x, palette_index); - clipDrawPalette(mid_x +| cur_y, mid_y -| cur_x, palette_index); - clipDrawPalette(mid_x -| cur_y, mid_y +| cur_x, palette_index); - clipDrawPalette(mid_x +| cur_y, mid_y +| cur_x, palette_index); - clipDrawPalette(mid_x -| cur_x, mid_y +| cur_y, palette_index); - clipDrawPalette(mid_x +| cur_x, mid_y +| cur_y, palette_index); + clip_draw_palette(mid_x -| cur_x, mid_y -| cur_y, palette_index); + clip_draw_palette(mid_x +| cur_x, mid_y -| cur_y, palette_index); + clip_draw_palette(mid_x -| cur_y, mid_y -| cur_x, palette_index); + clip_draw_palette(mid_x +| cur_y, mid_y -| cur_x, palette_index); + clip_draw_palette(mid_x -| cur_y, mid_y +| cur_x, palette_index); + clip_draw_palette(mid_x +| cur_y, mid_y +| cur_x, palette_index); + clip_draw_palette(mid_x -| cur_x, mid_y +| cur_y, palette_index); + clip_draw_palette(mid_x +| cur_x, mid_y +| cur_y, palette_index); } cur_x += 1; err += cur_x; - var temp = err - cur_y; + const temp = err - cur_y; if (temp >= 0) { err = temp; cur_y -= 1; @@ -255,7 +255,7 @@ pub fn rect(x: i32, y: i32, width: u32, height: u32) callconv(.C) void { if (stroke_palette_index) |palette_index| { if (y >= 0 and y < SCREEN_SIZE) { for (@max(x, 0)..@intCast(@min(end_x, SCREEN_SIZE))) |cur_x| { - drawPalette(@intCast(cur_x), @intCast(y), palette_index); + draw_palette(@intCast(cur_x), @intCast(y), palette_index); } } } @@ -263,19 +263,19 @@ pub fn rect(x: i32, y: i32, width: u32, height: u32) callconv(.C) void { for (@max(y + 1, 0)..@intCast(@min(end_y - 1, SCREEN_SIZE))) |cur_y| { if (stroke_palette_index) |palette_index| { if (x >= 0 and x < SCREEN_SIZE) { - drawPalette(@intCast(x), @intCast(cur_y), palette_index); + draw_palette(@intCast(x), @intCast(cur_y), palette_index); } } if (fill_palette_index) |palette_index| { if (width > 2) { for (@max(x + 1, 0)..@intCast(@min(end_x - 1, SCREEN_SIZE))) |cur_x| { - drawPalette(@intCast(cur_x), @intCast(cur_y), palette_index); + draw_palette(@intCast(cur_x), @intCast(cur_y), palette_index); } } } if (stroke_palette_index) |palette_index| { if (width > 1 and end_x - 1 >= 0 and end_x - 1 < SCREEN_SIZE) { - drawPalette(@intCast(end_x - 1), @intCast(cur_y), palette_index); + draw_palette(@intCast(end_x - 1), @intCast(cur_y), palette_index); } } } @@ -283,7 +283,7 @@ pub fn rect(x: i32, y: i32, width: u32, height: u32) callconv(.C) void { if (stroke_palette_index) |palette_index| { if (height > 1 and end_y - 1 >= 0 and end_y - 1 < SCREEN_SIZE) { for (@max(x, 0)..@intCast(@min(end_x, SCREEN_SIZE))) |cur_x| { - drawPalette(@intCast(cur_x), @intCast(end_y - 1), palette_index); + draw_palette(@intCast(cur_x), @intCast(end_y - 1), palette_index); } } } @@ -300,7 +300,7 @@ pub fn text(str: [*]const User(u8), len: usize, x: i32, y: i32) callconv(.C) voi }, ' '...0xFF => |char| { const glyph = &font[char - ' ']; - blitUnsafe(glyph, cur_x, cur_y, 8, 8, BLIT_1BPP); + blit_unsafe(glyph, cur_x, cur_y, 8, 8, BLIT_1BPP); cur_x +|= 8; }, }; @@ -316,7 +316,7 @@ pub fn vline(x: i32, y: i32, len: u32) callconv(.C) void { const palette_index: u2 = @truncate(draw_color - 1); for (@max(y, 0)..@intCast(@min(end_y, SCREEN_SIZE))) |cur_y| { - drawPalette(@intCast(x), @intCast(cur_y), palette_index); + draw_palette(@intCast(x), @intCast(cur_y), palette_index); } } @@ -330,7 +330,7 @@ pub fn hline(x: i32, y: i32, len: u32) callconv(.C) void { const palette_index: u2 = @truncate(draw_color - 1); for (@max(x, 0)..@intCast(@min(end_x, SCREEN_SIZE))) |cur_x| { - drawPalette(@intCast(cur_x), @intCast(y), palette_index); + draw_palette(@intCast(cur_x), @intCast(y), palette_index); } } @@ -414,11 +414,11 @@ pub fn tone(frequency: u32, duration: u32, volume: u32, flags: u32) callconv(.C) }, } - audio.setChannel(@intFromEnum(channel), state); + audio.set_channel(@intFromEnum(channel), state); } pub fn trace(str: [*]const User(u8), len: usize) callconv(.C) void { - std.log.scoped(.trace).info("{}", .{fmtUserString(str[0..len])}); + std.log.scoped(.trace).info("{}", .{fmt_user_string(str[0..len])}); } fn call(func: *const fn () callconv(.C) void) void { @@ -440,14 +440,14 @@ fn call(func: *const fn () callconv(.C) void) void { ); } -fn blitUnsafe(sprite: [*]const u8, x: i32, y: i32, width: u32, height: u32, flags: u32) void { +fn blit_unsafe(sprite: [*]const u8, x: i32, y: i32, width: u32, height: u32, flags: u32) void { switch (flags) { BLIT_1BPP => for (0..height) |sprite_y| { for (0..width) |sprite_x| { const sprite_index = sprite_x + width * sprite_y; const draw_color_index: u1 = @truncate(sprite[sprite_index >> 3] >> (7 - @as(u3, @truncate(sprite_index)))); - clipDraw( + clip_draw( x +| @min(sprite_x, std.math.maxInt(i32)), y +| @min(sprite_y, std.math.maxInt(i32)), draw_color_index, @@ -459,7 +459,7 @@ fn blitUnsafe(sprite: [*]const u8, x: i32, y: i32, width: u32, height: u32, flag const sprite_index = sprite_x + width * sprite_y; const draw_color_index: u2 = @truncate(sprite[sprite_index >> 2] >> (6 - (@as(u3, @as(u2, @truncate(sprite_index))) << 1))); - clipDraw( + clip_draw( x +| @min(sprite_x, std.math.maxInt(i32)), y +| @min(sprite_y, std.math.maxInt(i32)), draw_color_index, @@ -470,24 +470,24 @@ fn blitUnsafe(sprite: [*]const u8, x: i32, y: i32, width: u32, height: u32, flag } } -inline fn clipDraw(x: i32, y: i32, draw_color_index: u2) void { +inline fn clip_draw(x: i32, y: i32, draw_color_index: u2) void { if (x < 0 or x >= SCREEN_SIZE or y < 0 or y >= SCREEN_SIZE) return; draw(@intCast(x), @intCast(y), draw_color_index); } -inline fn clipDrawPalette(x: i32, y: i32, palette_index: u2) void { +inline fn clip_draw_palette(x: i32, y: i32, palette_index: u2) void { if (x < 0 or x >= SCREEN_SIZE or y < 0 or y >= SCREEN_SIZE) return; - drawPalette(@intCast(x), @intCast(y), palette_index); + draw_palette(@intCast(x), @intCast(y), palette_index); } inline fn draw(x: u8, y: u8, draw_color_index: u2) void { const draw_color: u4 = @truncate(DRAW_COLORS.* >> (@as(u4, draw_color_index) << 2)); if (draw_color == 0) return; const palette_index: u2 = @truncate(draw_color - 1); - drawPalette(x, y, palette_index); + draw_palette(x, y, palette_index); } -inline fn drawPalette(x: u8, y: u8, palette_index: u2) void { +inline fn draw_palette(x: u8, y: u8, palette_index: u2) void { std.debug.assert(x < SCREEN_SIZE and y < SCREEN_SIZE); const buffer_index = x + SCREEN_SIZE * y; const shift = @as(u3, @as(u2, @truncate(buffer_index))) << 1; @@ -495,10 +495,10 @@ inline fn drawPalette(x: u8, y: u8, palette_index: u2) void { byte.* = (byte.* & ~(@as(u8, 0b11) << shift)) | @as(u8, palette_index) << shift; } -fn formatUserString(bytes: []const User(u8), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { +fn format_user_string(bytes: []const User(u8), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void { for (bytes) |*byte| try writer.writeByte(byte.load()); } -inline fn fmtUserString(bytes: []const User(u8)) std.fmt.Formatter(formatUserString) { +inline fn fmt_user_string(bytes: []const User(u8)) std.fmt.Formatter(format_user_string) { return .{ .data = bytes }; } diff --git a/src/dma.zig b/src/dma.zig index ca68379..93f0101 100644 --- a/src/dma.zig +++ b/src/dma.zig @@ -1,6 +1,6 @@ pub const enable = true; -pub fn initLcd(bpp: lcd.Bpp) void { +pub fn init_lcd(bpp: lcd.Bpp) void { @setCold(true); init(); @@ -71,17 +71,17 @@ pub fn initLcd(bpp: lcd.Bpp) void { }); } -pub fn startLcd() void { +pub fn start_lcd() void { io.DMAC.CHANNEL[CHANNEL.LCD].CHCTRLA.modify(.{ .ENABLE = 1 }); while (io.DMAC.CHANNEL[CHANNEL.LCD].CHCTRLA.read().ENABLE != 1) {} } -pub fn stopLcd() void { +pub fn stop_lcd() void { io.DMAC.CHANNEL[CHANNEL.LCD].CHCTRLA.modify(.{ .ENABLE = 0 }); while (io.DMAC.CHANNEL[CHANNEL.LCD].CHCTRLA.read().ENABLE != 0) {} } -pub fn initAudio() void { +pub fn init_audio() void { @setCold(true); init(); @@ -170,12 +170,12 @@ pub fn initAudio() void { }); } -pub fn getAudioPart() usize { +pub fn get_audio_part() usize { return (desc_wb[DESC.AUDIO0].SRCADDR.read().SRCADDR - @intFromPtr(audio.sample_buffer) - 1) / @sizeOf(@TypeOf(audio.sample_buffer[0])); } -pub fn ackAudio() void { +pub fn ack_audio() void { io.DMAC.CHANNEL[CHANNEL.AUDIO].CHINTFLAG.write(.{ .TERR = 0, .TCMPL = 1, @@ -184,7 +184,7 @@ pub fn ackAudio() void { }); } -pub fn waitAudio(i: usize) void { +pub fn wait_audio(i: usize) void { while (@intFromBool(desc_wb[DESC.AUDIO0].SRCADDR.read().SRCADDR > @intFromPtr(&audio.buffer[1])) == i) {} } diff --git a/src/lcd.zig b/src/lcd.zig index a86c02d..682277d 100644 --- a/src/lcd.zig +++ b/src/lcd.zig @@ -33,12 +33,12 @@ pub const white24: Color24 = .{ .r = 0xff, .g = 0xff, .b = 0xff }; pub fn init(bpp: Bpp) void { @setCold(true); - Port.TFT_RST.setDir(.out); - Port.TFT_LITE.setDir(.out); - Port.TFT_DC.setDir(.out); - Port.TFT_CS.setDir(.out); - Port.TFT_SCK.setDir(.out); - Port.TFT_MOSI.setDir(.out); + Port.TFT_RST.set_dir(.out); + Port.TFT_LITE.set_dir(.out); + Port.TFT_DC.set_dir(.out); + Port.TFT_CS.set_dir(.out); + Port.TFT_SCK.set_dir(.out); + Port.TFT_MOSI.set_dir(.out); Port.TFT_CS.write(.high); Port.TFT_DC.write(.high); @@ -71,8 +71,8 @@ pub fn init(bpp: Bpp) void { .padding = 0, }); while (io.SERCOM4.SPIM.SYNCBUSY.read().SWRST != 0) {} - Port.TFT_SCK.setMux(.C); - Port.TFT_MOSI.setMux(.C); + Port.TFT_SCK.set_mux(.C); + Port.TFT_MOSI.set_mux(.C); io.GCLK.PCHCTRL[GCLK.PCH.SERCOM4_CORE].write(.{ .GEN = .{ .value = GCLK.GEN.@"120MHz".PCHCTRL_GEN }, .reserved6 = 0, @@ -101,15 +101,15 @@ pub fn init(bpp: Bpp) void { }); while (io.SERCOM4.SPIM.SYNCBUSY.read().ENABLE != 0) {} - sendCmd(ST7735.SWRESET, &.{}, 120 * std.time.us_per_ms); - sendCmd(ST7735.SLPOUT, &.{}, 120 * std.time.us_per_ms); - sendCmd(ST7735.INVOFF, &.{}, 1); - sendCmd(ST7735.COLMOD, &.{@intFromEnum(@as(ST7735.COLMOD_PARAM0, switch (bpp) { + send_cmd(ST7735.SWRESET, &.{}, 120 * std.time.us_per_ms); + send_cmd(ST7735.SLPOUT, &.{}, 120 * std.time.us_per_ms); + send_cmd(ST7735.INVOFF, &.{}, 1); + send_cmd(ST7735.COLMOD, &.{@intFromEnum(@as(ST7735.COLMOD_PARAM0, switch (bpp) { .bpp12 => .@"12BPP", .bpp16 => .@"16BPP", .bpp24 => .@"24BPP", }))}, 1); - sendCmd(ST7735.MADCTL, &.{@as(u8, @bitCast(ST7735.MADCTL_PARAM0{ + send_cmd(ST7735.MADCTL, &.{@as(u8, @bitCast(ST7735.MADCTL_PARAM0{ .MH = .LEFT_TO_RIGHT, .RGB = .BGR, .ML = .TOP_TO_BOTTOM, @@ -117,27 +117,27 @@ pub fn init(bpp: Bpp) void { .MX = false, .MY = true, }))}, 1); - sendCmd(ST7735.GMCTRP1, &.{ + send_cmd(ST7735.GMCTRP1, &.{ 0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10, }, 1); - sendCmd(ST7735.NORON, &.{}, 10 * std.time.us_per_ms); - sendCmd(ST7735.DISPON, &.{}, 10 * std.time.us_per_ms); - sendCmd(ST7735.RAMWR, &.{}, 1); + send_cmd(ST7735.NORON, &.{}, 10 * std.time.us_per_ms); + send_cmd(ST7735.DISPON, &.{}, 10 * std.time.us_per_ms); + send_cmd(ST7735.RAMWR, &.{}, 1); if (dma.enable) { Port.TFT_CS.write(.low); timer.delay(1); - dma.initLcd(bpp); + dma.init_lcd(bpp); } } pub fn invert() void { stop(); inverted = !inverted; - sendCmd(switch (inverted) { + send_cmd(switch (inverted) { false => ST7735.INVOFF, true => ST7735.INVON, }, &.{}, 1); @@ -181,18 +181,18 @@ pub fn rect24(rect: Rect, fill: Color24, line: Color24) void { } pub fn blit12() void { - if (!dma.enable) sendCmd(ST7735.RAMWR, std.mem.asBytes(&fb.bpp12), 1); + if (!dma.enable) send_cmd(ST7735.RAMWR, std.mem.asBytes(&fb.bpp12), 1); } pub fn blit16() void { - if (!dma.enable) sendCmd(ST7735.RAMWR, std.mem.asBytes(&fb.bpp16), 1); + if (!dma.enable) send_cmd(ST7735.RAMWR, std.mem.asBytes(&fb.bpp16), 1); } pub fn blit24() void { - if (!dma.enable) sendCmd(ST7735.RAMWR, std.mem.asBytes(&fb.bpp24), 1); + if (!dma.enable) send_cmd(ST7735.RAMWR, std.mem.asBytes(&fb.bpp24), 1); } -fn sendCmd(cmd: u8, params: []const u8, delay_us: u32) void { +fn send_cmd(cmd: u8, params: []const u8, delay_us: u32) void { timer.delay(1); Port.TFT_CS.write(.low); Port.TFT_DC.write(.low); @@ -213,16 +213,16 @@ fn sendCmd(cmd: u8, params: []const u8, delay_us: u32) void { fn start() void { if (dma.enable) { - sendCmd(ST7735.RAMWR, &.{}, 1); + send_cmd(ST7735.RAMWR, &.{}, 1); Port.TFT_CS.write(.low); timer.delay(1); - dma.startLcd(); + dma.start_lcd(); } } fn stop() void { if (dma.enable) { - dma.stopLcd(); + dma.stop_lcd(); timer.delay(1); Port.TFT_CS.write(.high); timer.delay(1); diff --git a/src/main.zig b/src/main.zig index ba7d368..2db408c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -14,185 +14,192 @@ const std = @import("std"); const timer = @import("timer.zig"); const utils = @import("utils.zig"); -pub const std_options = struct { - pub const log_level = switch (builtin.mode) { - .Debug => .debug, - .ReleaseSafe, .ReleaseFast, .ReleaseSmall => .info, - }; - pub const logFn = log; -}; +const interrupt = struct { + fn unhandled(comptime name: []const u8) microzig.interrupt.Handler { + return .{ + .C = struct { + fn handler() callconv(.C) void { + interrupt.log.info(name, .{}); + microzig.hang(); + } + }.handler, + }; + } -pub const microzig_options = struct { - pub const interrupts = struct { - const interrupt_log = std.log.scoped(.interrupt); - const Context = extern struct { - R0: u32, - R1: u32, - R2: u32, - R3: u32, - R12: u32, - LR: u32, - ReturnAddress: u32, - xPSR: u32, + fn with_context(comptime handler: fn (*Context) void) microzig.interrupt.Handler { + return microzig.interrupt.Handler{ + .Naked = struct { + fn interrupt() callconv(.Naked) void { + asm volatile ( + \\ tst lr, #1 << 2 + \\ ite eq + \\ moveq r0, sp + \\ mrsne r0, psp + \\ b %[handler:P] + : + : [handler] "X" (&handler), + ); + } + }.interrupt, }; + } + const log = std.log.scoped(.interrupt); + const Context = extern struct { + R0: u32, + R1: u32, + R2: u32, + R3: u32, + R12: u32, + LR: u32, + ReturnAddress: u32, + xPSR: u32, + }; +}; - pub const NonMaskableInt = unhandled("NonMaskableInt"); - pub const HardFault = withContext(struct { - fn handler(ctx: *Context) void { - interrupt_log.info("[HardFault] HFSR = 0x{x}, ReturnAddress = 0x{x}", .{ +pub const microzig_options = .{ + .log_level = switch (builtin.mode) { + .Debug => .debug, + .ReleaseSafe, .ReleaseFast, .ReleaseSmall => .info, + }, + .logFn = log, + .interrupts = .{ + .NonMaskableInt = interrupt.unhandled("NonMaskableInt"), + .HardFault = interrupt.with_context(struct { + fn handler(ctx: *interrupt.Context) void { + interrupt.log.info("[HardFault] HFSR = 0x{x}, ReturnAddress = 0x{x}", .{ io.SystemControl.HFSR.raw, ctx.ReturnAddress, }); microzig.hang(); } - }.handler); - pub const MemoryManagement = withContext(struct { - fn handler(ctx: *Context) void { - interrupt_log.info("[MemoryManagement] CFSR = 0x{x}, MMFAR = 0x{x}, ReturnAddress = 0x{x}", .{ + }.handler), + .MemoryManagement = interrupt.with_context(struct { + fn handler(ctx: *interrupt.Context) void { + interrupt.log.info("[MemoryManagement] CFSR = 0x{x}, MMFAR = 0x{x}, ReturnAddress = 0x{x}", .{ io.SystemControl.CFSR.raw, io.SystemControl.MMFAR.raw, ctx.ReturnAddress, }); microzig.hang(); } - }.handler); - pub const BusFault = withContext(struct { - fn handler(ctx: *Context) void { - interrupt_log.info("[BusFault] CFSR = 0x{x}, BFAR = 0x{x}, ReturnAddress = 0x{x}", .{ + }.handler), + .BusFault = interrupt.with_context(struct { + fn handler(ctx: *interrupt.Context) void { + interrupt.log.info("[BusFault] CFSR = 0x{x}, BFAR = 0x{x}, ReturnAddress = 0x{x}", .{ io.SystemControl.CFSR.raw, io.SystemControl.BFAR.raw, ctx.ReturnAddress, }); microzig.hang(); } - }.handler); - pub const UsageFault = withContext(struct { - fn handler(ctx: *Context) void { - interrupt_log.info("[UsageFault] CFSR = 0x{x}, ReturnAddress = 0x{x}", .{ + }.handler), + .UsageFault = interrupt.with_context(struct { + fn handler(ctx: *interrupt.Context) void { + interrupt.log.info("[UsageFault] CFSR = 0x{x}, ReturnAddress = 0x{x}", .{ io.SystemControl.CFSR.raw, ctx.ReturnAddress, }); microzig.hang(); } - }.handler); - pub fn SVCall() callconv(.Naked) void { - asm volatile ( - \\ mvns r0, lr, lsl #31 - 2 - \\ bcc 1f - \\ ite mi - \\ movmi r1, sp - \\ mrspl r1, psp - \\ ldr r2, [r1, #6 * 4] - \\ subs r2, #2 - \\ ldrb r3, [r2, #1 * 1] - \\ cmp r3, #0xDF - \\ bne 1f - \\ ldrb r3, [r2, #0 * 1] - \\ cmp r3, #12 - \\ bhi 1f - \\ tbb [pc, r3] - \\0: - \\ .byte (0f - 0b) / 2 - \\ .byte (9f - 0b) / 2 - \\ .byte (9f - 0b) / 2 - \\ .byte (2f - 0b) / 2 - \\ .byte (3f - 0b) / 2 - \\ .byte (4f - 0b) / 2 - \\ .byte (5f - 0b) / 2 - \\ .byte (6f - 0b) / 2 - \\ .byte (7f - 0b) / 2 - \\ .byte (8f - 0b) / 2 - \\ .byte (8f - 0b) / 2 - \\ .byte (10f - 0b) / 2 - \\1: - \\ .byte (11f - 0b) / 2 - \\ .byte 0xDE - \\ .align 1 - \\0: - \\ ldm r1, {r0-r3} - \\ b %[blit:P] - \\2: - \\ ldm r1, {r0-r3} - \\ b %[oval:P] - \\3: - \\ ldm r1, {r0-r3} - \\ b %[rect:P] - \\4: - \\ ldm r1, {r0-r3} - \\ b %[text:P] - \\5: - \\ ldm r1, {r0-r2} - \\ b %[vline:P] - \\6: - \\ ldm r1, {r0-r2} - \\ b %[hline:P] - \\7: - \\ ldm r1, {r0-r3} - \\ b %[tone:P] - \\8: - \\ movs r0, #0 - \\ str r0, [r1, #0 * 4] - \\9: - \\ bx lr - \\10: - \\ ldm r1, {r0-r1} - \\ b %[trace:P] - \\11: - \\ lsrs r0, #31 - \\ msr control, r0 - \\ it eq - \\ popeq {r3, r5-r11, pc} - \\ subs r0, #1 - 0xFFFFFFFD - \\ push {r4-r11, lr} - \\ movs r4, #0 - \\ movs r5, #0 - \\ movs r6, #0 - \\ movs r7, #0 - \\ mov r8, r4 - \\ mov r9, r5 - \\ mov r10, r6 - \\ mov r11, r7 - \\ bx r0 - : - : [blit] "X" (&cart.blit), - [oval] "X" (&cart.oval), - [rect] "X" (&cart.rect), - [text] "X" (&cart.text), - [vline] "X" (&cart.vline), - [hline] "X" (&cart.hline), - [tone] "X" (&cart.tone), - [trace] "X" (&cart.trace), - ); - } - pub const DebugMonitor = unhandled("DebugMonitor"); - pub const PendSV = unhandled("PendSV"); - pub const SysTick = unhandled("SysTick"); - pub const DMAC_DMAC_1 = audio.mix; - - fn unhandled(comptime name: []const u8) fn () callconv(.C) void { - return struct { - fn handler() callconv(.C) void { - interrupt_log.info(name, .{}); - microzig.hang(); - } - }.handler; - } - fn withContext(comptime handler: fn (*Context) void) fn () callconv(.Naked) void { - return struct { - fn interrupt() callconv(.Naked) void { + }.handler), + .DebugMonitor = interrupt.unhandled("DebugMonitor"), + .PendSV = interrupt.unhandled("PendSV"), + .SysTick = interrupt.unhandled("SysTick"), + .DMAC_DMAC_1 = .{ .C = audio.mix }, + .SVCall = .{ + .Naked = struct { + fn SVCall() callconv(.Naked) void { asm volatile ( - \\ tst lr, #1 << 2 - \\ ite eq - \\ moveq r0, sp - \\ mrsne r0, psp - \\ b %[handler:P] + \\ mvns r0, lr, lsl #31 - 2 + \\ bcc 1f + \\ ite mi + \\ movmi r1, sp + \\ mrspl r1, psp + \\ ldr r2, [r1, #6 * 4] + \\ subs r2, #2 + \\ ldrb r3, [r2, #1 * 1] + \\ cmp r3, #0xDF + \\ bne 1f + \\ ldrb r3, [r2, #0 * 1] + \\ cmp r3, #12 + \\ bhi 1f + \\ tbb [pc, r3] + \\0: + \\ .byte (0f - 0b) / 2 + \\ .byte (9f - 0b) / 2 + \\ .byte (9f - 0b) / 2 + \\ .byte (2f - 0b) / 2 + \\ .byte (3f - 0b) / 2 + \\ .byte (4f - 0b) / 2 + \\ .byte (5f - 0b) / 2 + \\ .byte (6f - 0b) / 2 + \\ .byte (7f - 0b) / 2 + \\ .byte (8f - 0b) / 2 + \\ .byte (8f - 0b) / 2 + \\ .byte (10f - 0b) / 2 + \\1: + \\ .byte (11f - 0b) / 2 + \\ .byte 0xDE + \\ .align 1 + \\0: + \\ ldm r1, {r0-r3} + \\ b %[blit:P] + \\2: + \\ ldm r1, {r0-r3} + \\ b %[oval:P] + \\3: + \\ ldm r1, {r0-r3} + \\ b %[rect:P] + \\4: + \\ ldm r1, {r0-r3} + \\ b %[text:P] + \\5: + \\ ldm r1, {r0-r2} + \\ b %[vline:P] + \\6: + \\ ldm r1, {r0-r2} + \\ b %[hline:P] + \\7: + \\ ldm r1, {r0-r3} + \\ b %[tone:P] + \\8: + \\ movs r0, #0 + \\ str r0, [r1, #0 * 4] + \\9: + \\ bx lr + \\10: + \\ ldm r1, {r0-r1} + \\ b %[trace:P] + \\11: + \\ lsrs r0, #31 + \\ msr control, r0 + \\ it eq + \\ popeq {r3, r5-r11, pc} + \\ subs r0, #1 - 0xFFFFFFFD + \\ push {r4-r11, lr} + \\ movs r4, #0 + \\ movs r5, #0 + \\ movs r6, #0 + \\ movs r7, #0 + \\ mov r8, r4 + \\ mov r9, r5 + \\ mov r10, r6 + \\ mov r11, r7 + \\ bx r0 : - : [handler] "X" (&handler), + : [blit] "X" (&cart.blit), + [oval] "X" (&cart.oval), + [rect] "X" (&cart.rect), + [text] "X" (&cart.text), + [vline] "X" (&cart.vline), + [hline] "X" (&cart.hline), + [tone] "X" (&cart.tone), + [trace] "X" (&cart.trace), ); } - }.interrupt; - } - }; + }.SVCall, + }, + }, }; const InOutError = error{NoConnection}; @@ -227,7 +234,7 @@ pub fn log( out.print("[" ++ level.asText() ++ "] (" ++ @tagName(scope) ++ "): " ++ format ++ "\n", args) catch return; } -pub fn dumpPeripheral(logger: anytype, comptime prefix: []const u8, pointer: anytype) void { +pub fn dump_peripheral(logger: anytype, comptime prefix: []const u8, pointer: anytype) void { switch (@typeInfo(@typeInfo(@TypeOf(pointer)).Pointer.child)) { .Int => { logger.info("[0x{x}] " ++ prefix ++ " = 0x{x}", .{ @intFromPtr(pointer), pointer.* }); @@ -235,10 +242,10 @@ pub fn dumpPeripheral(logger: anytype, comptime prefix: []const u8, pointer: any .Struct => |*info| inline for (info.fields) |field| { if (comptime std.mem.startsWith(u8, field.name, "reserved")) continue; if (comptime std.mem.eql(u8, field.name, "padding")) continue; - dumpPeripheral(logger, prefix ++ "." ++ field.name, &@field(pointer, field.name)); + dump_peripheral(logger, prefix ++ "." ++ field.name, &@field(pointer, field.name)); }, .Array => inline for (0.., pointer) |index, *elem| { - dumpPeripheral(logger, std.fmt.comptimePrint("{s}[{d}]", .{ prefix, index }), elem); + dump_peripheral(logger, std.fmt.comptimePrint("{s}[{d}]", .{ prefix, index }), elem); }, else => @compileError("Unhandled type: " ++ @typeName(@TypeOf(pointer))), } @@ -284,9 +291,9 @@ pub fn main() !void { usb.init(); // ID detection const id_port = Port.D13; - id_port.setDir(.out); + id_port.set_dir(.out); id_port.write(.high); - id_port.configPtr().write(.{ + id_port.config_ptr().write(.{ .PMUXEN = 0, .INEN = 1, .PULLEN = 1, @@ -294,7 +301,7 @@ pub fn main() !void { .DRVSTR = 0, .padding = 0, }); - usb.reinitMode(.DEVICE); + usb.reinit_mode(.DEVICE); timer.init(); lcd.init(.bpp24); @@ -394,7 +401,7 @@ pub fn main() !void { 'A' - '@' => { timer.delay(std.time.us_per_s); const tempo = 0.78; - audio.playSong(&.{ + audio.play_song(&.{ &.{ .{ .duration = tempo * 0.75, .frequency = audio.Note.F5 }, .{ .duration = tempo * 0.25, .frequency = audio.Note.Db5 }, @@ -523,7 +530,7 @@ pub fn main() !void { }, }); }, - 'B' - '@' => utils.resetIntoBootloader(), + 'B' - '@' => utils.reset_into_bootloader(), 'C' - '@' => { // Debug clock frequencies const clock_log = std.log.scoped(.clock); @@ -678,7 +685,7 @@ pub fn main() !void { 'I' - '@' => if (true) lcd.invert(), '\r' => out.writeByte('\n') catch break :input, 'P' - '@' => @panic("user"), - 'R' - '@' => utils.resetIntoApp(), + 'R' - '@' => utils.reset_into_app(), 'S' - '@' => for (0.., &io.PORT.GROUP) |group_i, *group| std.log.info("IN{d} = 0x{X:0>8}", .{ group_i, group.IN.read().IN }), 'T' - '@' => { // Debug timer delay @@ -857,14 +864,14 @@ pub const usb = struct { fn init() void { @setCold(true); - Port.@"D-".setMux(.H); - Port.@"D+".setMux(.H); + Port.@"D-".set_mux(.H); + Port.@"D+".set_mux(.H); io.MCLK.AHBMASK.modify(.{ .USB_ = 1 }); io.MCLK.APBBMASK.modify(.{ .USB_ = 1 }); } /// Reinitialize into the specified mode - fn reinitMode(mode: io_types.USB.USB_CTRLA__MODE) void { + fn reinit_mode(mode: io_types.USB.USB_CTRLA__MODE) void { @setCold(true); // Tear down clocks @@ -1399,14 +1406,14 @@ pub const usb = struct { .out => switch (@as(Setup.standard.Request, @enumFromInt(setup.bRequest))) { .SET_ADDRESS => if (setup.wIndex == 0 and setup.wLength == 0) { if (std.math.cast(u7, setup.wValue)) |addr| { - writeControl(&[0]u8{}); + write_control(&[0]u8{}); io.USB.DEVICE.DADD.write(.{ .DADD = addr, .ADDEN = 1 }); break :setup; } }, .SET_CONFIGURATION => { if (std.math.cast(u8, setup.wValue)) |config| { - writeControl(&[0]u8{}); + write_control(&[0]u8{}); current_configuration = config; cdc.current_connection = 0; switch (config) { @@ -1497,7 +1504,7 @@ pub const usb = struct { .GET_DESCRIPTOR => { switch (@as(Setup.standard.DescriptorType, @enumFromInt(setup.wValue >> 8))) { .DEVICE => if (@as(u8, @truncate(setup.wValue)) == 0 and setup.wIndex == 0) { - writeControl(&[0x12]u8{ + write_control(&[0x12]u8{ 0x12, @intFromEnum(Setup.standard.DescriptorType.DEVICE), // 0x00, 0x02, // 0xef, 0x02, 0x01, // @@ -1513,7 +1520,7 @@ pub const usb = struct { .CONFIGURATION => if (setup.wIndex == 0) { switch (@as(u8, @truncate(setup.wValue))) { 0 => { - writeControl(&[0x003e]u8{ + write_control(&[0x003e]u8{ 0x09, @intFromEnum(Setup.standard.DescriptorType.CONFIGURATION), // 0x3e, 0x00, // 0x02, 0x01, 0x00, // @@ -1558,7 +1565,7 @@ pub const usb = struct { .STRING => switch (@as(u8, @truncate(setup.wValue))) { 0 => switch (setup.wIndex) { 0 => { - writeControl(&[4]u8{ + write_control(&[4]u8{ 4, @intFromEnum(Setup.standard.DescriptorType.STRING), // 0x09, 0x04, // English (United States) }); @@ -1568,7 +1575,7 @@ pub const usb = struct { }, 1 => switch (setup.wIndex) { 0x0409 => { // English (United States) - writeControl(&[38]u8{ + write_control(&[38]u8{ 38, @intFromEnum(Setup.standard.DescriptorType.STRING), // 'Z', 0x00, // 'i', 0x00, // @@ -1595,7 +1602,7 @@ pub const usb = struct { }, 2 => switch (setup.wIndex) { 0x0409 => { // English (United States) - writeControl(&[16]u8{ + write_control(&[16]u8{ 16, @intFromEnum(Setup.standard.DescriptorType.STRING), // 'B', 0x00, // 'a', 0x00, // @@ -1628,13 +1635,13 @@ pub const usb = struct { 0 => switch (setup.bmRequestType.dir) { .out => switch (@as(Setup.cdc.Request, @enumFromInt(setup.bRequest))) { .SET_LINE_CODING => if (setup.wValue == 0) { - writeControl(&[0]u8{}); + write_control(&[0]u8{}); break :setup; }, .SET_CONTROL_LINE_STATE => if (setup.wLength == 0) { if (std.math.cast(u8, setup.wValue)) |conn| { cdc.current_connection = conn; - writeControl(&[0]u8{}); + write_control(&[0]u8{}); break :setup; } }, @@ -1697,7 +1704,7 @@ pub const usb = struct { return data[0..0]; } - fn writeControl(data: []const u8) void { + fn write_control(data: []const u8) void { write(0, data[0..@min(data.len, setup.wLength)]); } diff --git a/src/timer.zig b/src/timer.zig index 4525a38..eca3b28 100644 --- a/src/timer.zig +++ b/src/timer.zig @@ -136,7 +136,7 @@ pub fn init() void { while (io.TC0.COUNT32.SYNCBUSY.read().CTRLB != 0) {} } -pub fn startDelay(us: u32) void { +pub fn start_delay(us: u32) void { io.TC0.COUNT32.COUNT.write(.{ .COUNT = us }); while (io.TC0.COUNT32.SYNCBUSY.read().COUNT != 0) {} io.TC0.COUNT32.CTRLBSET.write(.{ @@ -149,16 +149,16 @@ pub fn startDelay(us: u32) void { while (io.TC0.COUNT32.SYNCBUSY.read().CTRLB != 0) {} } -pub fn finishDelay() void { +pub fn finish_delay() void { while (io.TC0.COUNT32.STATUS.read().STOP != 1) {} } pub fn delay(us: u32) void { - startDelay(us); - finishDelay(); + start_delay(us); + finish_delay(); } -pub fn initFrameSync() void { +pub fn init_frame_sync() void { io.MCLK.APBCMASK.modify(.{ .TC4_ = 1 }); io.TC4.COUNT16.CTRLA.write(.{ .SWRST = 0, @@ -267,7 +267,7 @@ pub fn initFrameSync() void { while (io.TC4.COUNT16.SYNCBUSY.read().CTRLB != 0) {} } -pub fn checkFrameReady() bool { +pub fn check_frame_ready() bool { if (io.TC4.COUNT16.INTFLAG.read().OVF != 1) return false; io.TC4.COUNT16.INTFLAG.write(.{ .OVF = 1, diff --git a/src/utils.zig b/src/utils.zig index 14da2da..c5e9b41 100644 --- a/src/utils.zig +++ b/src/utils.zig @@ -32,12 +32,12 @@ const DBL_TAP_MAGIC = 0xf01669ef; const DBL_TAP_MAGIC_QUICK_BOOT = 0xf02669ef; //adafruit/uf2-samdx1:src/utils.c -pub fn resetIntoApp() noreturn { +pub fn reset_into_app() noreturn { DBL_TAP_PTR.* = DBL_TAP_MAGIC_QUICK_BOOT; NVIC_SystemReset(); } -pub fn resetIntoBootloader() noreturn { +pub fn reset_into_bootloader() noreturn { DBL_TAP_PTR.* = DBL_TAP_MAGIC; NVIC_SystemReset(); } diff --git a/src/wasm4.zig b/src/wasm4.zig index 3c9c4ff..01fb919 100644 --- a/src/wasm4.zig +++ b/src/wasm4.zig @@ -70,7 +70,7 @@ pub inline fn blit(sprite: [*]const u8, x: i32, y: i32, width: u32, height: u32, } /// Copies a subregion within a larger sprite atlas to the framebuffer. -pub inline fn blitSub(sprite: [*]const u8, x: i32, y: i32, width: u32, height: u32, src_x: u32, src_y: u32, stride: u32, flags: u32) void { +pub inline fn blit_sub(sprite: [*]const u8, x: i32, y: i32, width: u32, height: u32, src_x: u32, src_y: u32, stride: u32, flags: u32) void { const rest: extern struct { width: u32, height: u32,