Implements input, pull up/down and uart routing.

wch-ch32v003
Felix "xq" Queißner 2 years ago
parent 5c853fd23a
commit fe14d4f03b

@ -3,7 +3,7 @@ const Builder = std.build.Builder;
const Pkg = std.build.Pkg; const Pkg = std.build.Pkg;
const comptimePrint = std.fmt.comptimePrint; 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 chip_path = comptimePrint("{s}/src/rp2040.zig", .{root()});
const board_path = comptimePrint("{s}/src/raspberry_pi_pico.zig", .{root()}); const board_path = comptimePrint("{s}/src/raspberry_pi_pico.zig", .{root()});

@ -97,6 +97,14 @@ pub inline fn toggle(comptime gpio: u32) void {
regs.SIO.GPIO_OUT_XOR.raw = (1 << gpio); 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 { pub inline fn setFunction(comptime gpio: u32, function: Function) void {
const pad_bank_reg = comptime std.fmt.comptimePrint("GPIO{}", .{gpio}); const pad_bank_reg = comptime std.fmt.comptimePrint("GPIO{}", .{gpio});
@field(regs.PADS_BANK0, pad_bank_reg).modify(.{ @field(regs.PADS_BANK0, pad_bank_reg).modify(.{

@ -50,11 +50,15 @@ pub const Pin = enum {
// schmitt trigger // schmitt trigger
// hysteresis // hysteresis
pub fn getDirection(config: Configuration) gpio.Direction { pub fn getDirection(comptime config: Configuration) gpio.Direction {
return if (config.direction) |direction| return if (config.direction) |direction|
direction direction
else if (config.function.isPwm()) else if (comptime config.function.isPwm())
.out .out
else if (comptime config.function.isUartTx())
.out
else if (comptime config.function.isUartRx())
.in
else else
@panic("TODO"); @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 { pub fn pwmSlice(comptime function: Function) u32 {
return switch (function) { return switch (function) {
.PWM0_A, .PWM0_B => 0, .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 { pub inline fn read(self: @This()) u1 {
_ = self; _ = self;
@compileError("TODO"); return gpio.read(gpio_num);
} }
}, },
.out => struct { .out => struct {
@ -424,6 +446,21 @@ pub const GlobalConfiguration = struct {
if (output_gpios != 0) if (output_gpios != 0)
regs.SIO.GPIO_OE_SET.raw = output_gpios; 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 // TODO: pwm initialization
// fields in the Pins(config) type should be zero sized, so we just // fields in the Pins(config) type should be zero sized, so we just

Loading…
Cancel
Save