diff --git a/build.zig b/build.zig index 889455d..d00c962 100644 --- a/build.zig +++ b/build.zig @@ -3,7 +3,7 @@ const Builder = std.build.Builder; const Pkg = std.build.Pkg; const comptimePrint = std.fmt.comptimePrint; -const microzig = @import("deps/microzig/src/main.zig"); +pub const microzig = @import("deps/microzig/src/main.zig"); const chip_path = comptimePrint("{s}/src/rp2040.zig", .{root()}); const board_path = comptimePrint("{s}/src/raspberry_pi_pico.zig", .{root()}); diff --git a/src/hal/gpio.zig b/src/hal/gpio.zig index 18624ee..90e2141 100644 --- a/src/hal/gpio.zig +++ b/src/hal/gpio.zig @@ -97,6 +97,14 @@ pub inline fn toggle(comptime gpio: u32) void { regs.SIO.GPIO_OUT_XOR.raw = (1 << gpio); } +pub inline fn read(comptime gpio: u32) u1 { + const mask = 1 << gpio; + return if ((regs.SIO.GPIO_IN.raw & mask) != 0) + 1 + else + 0; +} + pub inline fn setFunction(comptime gpio: u32, function: Function) void { const pad_bank_reg = comptime std.fmt.comptimePrint("GPIO{}", .{gpio}); @field(regs.PADS_BANK0, pad_bank_reg).modify(.{ diff --git a/src/hal/pins.zig b/src/hal/pins.zig index baa2479..6980bfc 100644 --- a/src/hal/pins.zig +++ b/src/hal/pins.zig @@ -50,11 +50,15 @@ pub const Pin = enum { // schmitt trigger // hysteresis - pub fn getDirection(config: Configuration) gpio.Direction { + pub fn getDirection(comptime config: Configuration) gpio.Direction { return if (config.direction) |direction| direction - else if (config.function.isPwm()) + else if (comptime config.function.isPwm()) .out + else if (comptime config.function.isUartTx()) + .out + else if (comptime config.function.isUartRx()) + .in else @panic("TODO"); } @@ -157,6 +161,24 @@ pub const Function = enum { }; } + pub fn isUartTx(function: Function) bool { + return switch (function) { + .UART0_TX, + .UART1_TX, + => true, + else => false, + }; + } + + pub fn isUartRx(function: Function) bool { + return switch (function) { + .UART0_RX, + .UART1_RX, + => true, + else => false, + }; + } + pub fn pwmSlice(comptime function: Function) u32 { return switch (function) { .PWM0_A, .PWM0_B => 0, @@ -280,7 +302,7 @@ pub fn GPIO(comptime num: u5, comptime direction: gpio.Direction) type { pub inline fn read(self: @This()) u1 { _ = self; - @compileError("TODO"); + return gpio.read(gpio_num); } }, .out => struct { @@ -424,6 +446,21 @@ pub const GlobalConfiguration = struct { if (output_gpios != 0) regs.SIO.GPIO_OE_SET.raw = output_gpios; + if (input_gpios != 0) { + inline for (@typeInfo(GlobalConfiguration).Struct.fields) |field| + if (@field(config, field.name)) |pin_config| { + const pull = pin_config.pull orelse continue; + if (comptime pin_config.getDirection() != .in) + @compileError("Only input pins can have pull up/down enabled"); + + const gpio_regs = @field(regs.PADS_BANK0, field.name); + gpio_regs.modify(comptime .{ + .PUE = @boolToInt(pull == .up), + .PDE = @boolToInt(pull == .down), + }); + }; + } + // TODO: pwm initialization // fields in the Pins(config) type should be zero sized, so we just