clean up gpios

wch-ch32v003
Matt Knight 2 years ago
parent c71e73759c
commit ed05258e7e

@ -79,10 +79,10 @@ pub const SlewRate = enum {
}; };
pub const DriveStrength = enum { pub const DriveStrength = enum {
ma_2, @"2mA",
ma_4, @"4mA",
ma_8, @"8mA",
ma_12, @"12mA",
}; };
pub const Enabled = enum { pub const Enabled = enum {
@ -92,18 +92,20 @@ pub const Enabled = enum {
pub inline fn reset() void { pub inline fn reset() void {
regs.RESETS.RESET.modify(.{ .io_bank0 = 1, .pads_bank0 = 1 }); regs.RESETS.RESET.modify(.{ .io_bank0 = 1, .pads_bank0 = 1 });
while (regs.RESETS.RESET_DONE.read().io_bank0 == 1) {} regs.RESETS.RESET.modify(.{ .io_bank0 = 0, .pads_bank0 = 0 });
while (regs.RESETS.RESET_DONE.read().pads_bank0 == 1) {}
}
//const gpio_num = gpio_num: { while (true) {
// // calculate max gpios using comptime parsing const reset_done = regs.RESETS.RESET_DONE.read();
//}; if (reset_done.io_bank0 == 1 and reset_done.pads_bank0 == 1)
break;
}
}
/// Initialize a GPIO, set func to SIO /// Initialize a GPIO, set func to SIO
pub inline fn init(comptime gpio: u32) void { pub inline fn init(comptime gpio: u32) void {
regs.SIO.GPIO_OE_CLR.raw = 1 << gpio; const mask = 1 << gpio;
regs.SIO.GPIO_OUT_CLR.raw = 1 << gpio; regs.SIO.GPIO_OE_CLR.raw = mask;
regs.SIO.GPIO_OUT_CLR.raw = mask;
setFunction(gpio, .sio); setFunction(gpio, .sio);
} }
@ -113,17 +115,19 @@ pub inline fn deinit(comptime gpio: u32) void {
} }
pub inline fn setDir(comptime gpio: u32, direction: Direction) void { pub inline fn setDir(comptime gpio: u32, direction: Direction) void {
const mask = 1 << gpio;
switch (direction) { switch (direction) {
.in => regs.SIO.GPIO_OE_CLR.raw |= (1 << gpio), .in => regs.SIO.GPIO_OE_CLR.raw = mask,
.out => regs.SIO.GPIO_OE_SET.raw &= (1 << gpio), .out => regs.SIO.GPIO_OE_SET.raw = mask,
} }
} }
/// Drive a single GPIO high/low /// Drive a single GPIO high/low
pub inline fn put(comptime gpio: u32, value: u1) void { pub inline fn put(comptime gpio: u32, value: u1) void {
const mask = 1 << gpio;
switch (value) { switch (value) {
0 => regs.SIO.GPIO_OUT.raw &= ~@as(u32, 1 << gpio), 0 => regs.SIO.GPIO_OUT_CLR.raw = mask,
1 => regs.SIO.GPIO_OUT.raw |= (1 << gpio), 1 => regs.SIO.GPIO_OUT_SET.raw = mask,
} }
} }

Loading…
Cancel
Save