Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RP2040 I2C Refactor #213

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bsp/raspberrypi/rp2040/src/hal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ pub fn get_cpu_id() u32 {
test "hal tests" {
_ = pio;
_ = usb;
_ = i2c;
}
4 changes: 2 additions & 2 deletions bsp/raspberrypi/rp2040/src/hal/adc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ pub const Input = enum(u3) {
switch (in) {
else => {
const pin = in.get_gpio_pin();
pin.set_function(.null);
pin.set_pull(null);
pin.set_function(.disabled);
pin.set_pull(.disabled);
pin.set_input_enabled(false);
},
.temp_sensor => {},
Expand Down
51 changes: 44 additions & 7 deletions bsp/raspberrypi/rp2040/src/hal/gpio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub const Function = enum(u5) {
pio1,
gpck,
usb,
null = 0x1f,
disabled = 0x1f,
};

pub const Direction = enum(u1) {
Expand Down Expand Up @@ -66,6 +66,7 @@ pub const Enabled = enum {
pub const Pull = enum {
up,
down,
disabled,
};

pub fn num(n: u5) Pin {
Expand Down Expand Up @@ -99,7 +100,7 @@ pub const Mask = enum(u30) {
}
}

pub fn set_pull(self: Mask, pull: ?Pull) void {
pub fn set_pull(self: Mask, pull: Pull) void {
const raw_mask = @intFromEnum(self);
for (0..@bitSizeOf(Mask)) |i| {
const bit = @as(u5, @intCast(i));
Expand All @@ -108,6 +109,24 @@ pub const Mask = enum(u30) {
}
}

pub fn set_slew_rate(self: Mask, slew_rate: SlewRate) void {
const raw_mask = @intFromEnum(self);
for (0..@bitSizeOf(Mask)) |i| {
const bit = @as(u5, @intCast(i));
if (0 != raw_mask & (@as(u32, 1) << bit))
num(bit).set_slew_rate(slew_rate);
}
}

pub fn set_schmitt_trigger(self: Mask, enabled: Enabled) void {
const raw_mask = @intFromEnum(self);
for (0..@bitSizeOf(Mask)) |i| {
const bit = @as(u5, @intCast(i));
if (0 != raw_mask & (@as(u32, 1) << bit))
num(bit).set_schmitt_trigger(enabled);
}
}

pub fn put(self: Mask, value: u32) void {
SIO.GPIO_OUT_XOR.raw = (SIO.GPIO_OUT.raw ^ value) & @intFromEnum(self);
}
Expand Down Expand Up @@ -167,14 +186,12 @@ pub const Pin = enum(u5) {
return @as(u32, 1) << @intFromEnum(gpio);
}

pub inline fn set_pull(gpio: Pin, pull: ?Pull) void {
pub inline fn set_pull(gpio: Pin, pull: Pull) void {
const pads_reg = gpio.get_pads_reg();

if (pull == null) {
pads_reg.modify(.{ .PUE = 0, .PDE = 0 });
} else switch (pull.?) {
switch (pull) {
.up => pads_reg.modify(.{ .PUE = 1, .PDE = 0 }),
.down => pads_reg.modify(.{ .PUE = 0, .PDE = 1 }),
.disabled => pads_reg.modify(.{ .PUE = 0, .PDE = 0 }),
}
}

Expand Down Expand Up @@ -231,4 +248,24 @@ pub const Pin = enum(u5) {
.padding = 0,
});
}

pub fn set_slew_rate(gpio: Pin, slew_rate: SlewRate) void {
const pads_reg = gpio.get_pads_reg();
pads_reg.modify(.{
.SLEWFAST = switch (slew_rate) {
.slow => @as(u1, 0),
.fast => @as(u1, 1),
},
});
}

pub fn set_schmitt_trigger(gpio: Pin, enabled: Enabled) void {
const pads_reg = gpio.get_pads_reg();
pads_reg.modify(.{
.SCHMITT = switch (enabled) {
.enabled => @as(u1, 1),
.disabled => @as(u1, 0),
},
});
}
};
Loading
Loading