Updated parts of gpio and pin to reflect code changes from regz rewrite (#120)

wch-ch32v003
Philipp Wendel 1 year ago committed by GitHub
parent ceaa9ddcb0
commit 23482a6986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -51,7 +51,7 @@ pub fn build(b: *std.build.Builder) !void {
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
var exe = microzig.addEmbeddedExecutable( b, .{ var exe = microzig.addEmbeddedExecutable( b, .{
.name = "my-executable", .name = "my-executable",
.root_source_file = .{ .source_file = .{
.path = "src/main.zig", .path = "src/main.zig",
}, },
.backing = .{ .backing = .{

@ -1,6 +1,6 @@
const std = @import("std"); const std = @import("std");
const micro = @import("microzig"); const micro = @import("microzig");
const chip = @import("chip"); const hal = @import("hal");
pub const Mode = enum { pub const Mode = enum {
input, input,
@ -59,8 +59,8 @@ pub fn Gpio(comptime pin: type, comptime config: anytype) type {
}); });
}, },
.alternate_function => { .alternate_function => {
if (comptime @hasDecl(chip.gpio, "AlternateFunction")) { if (comptime @hasDecl(hal.gpio, "AlternateFunction")) {
const alternate_function = @as(chip.gpio.AlternateFunction, config.alternate_function); const alternate_function = @as(hal.gpio.AlternateFunction, config.alternate_function);
set_alternate_function(alternate_function); set_alternate_function(alternate_function);
} else { } else {
@compileError("Alternate Function not supported yet"); @compileError("Alternate Function not supported yet");
@ -70,12 +70,12 @@ pub fn Gpio(comptime pin: type, comptime config: anytype) type {
} }
fn read() State { fn read() State {
return chip.gpio.read(pin.source_pin); return hal.gpio.read(pin.source_pin);
} }
// outputs: // outputs:
fn write(state: State) void { fn write(state: State) void {
chip.gpio.write(pin.source_pin, state); hal.gpio.write(pin.source_pin, state);
} }
fn set_to_high() void { fn set_to_high() void {
@ -85,8 +85,8 @@ pub fn Gpio(comptime pin: type, comptime config: anytype) type {
write(.low); write(.low);
} }
fn toggle() void { fn toggle() void {
if (comptime @hasDecl(chip.gpio, "toggle")) { if (comptime @hasDecl(hal.gpio, "toggle")) {
chip.gpio.toggle(pin.source_pin); hal.gpio.toggle(pin.source_pin);
} else { } else {
write(switch (read()) { write(switch (read()) {
.low => State.high, .low => State.high,
@ -99,14 +99,14 @@ pub fn Gpio(comptime pin: type, comptime config: anytype) type {
fn set_direction(dir: Direction, output_state: State) void { fn set_direction(dir: Direction, output_state: State) void {
switch (dir) { switch (dir) {
.output => { .output => {
chip.gpio.setOutput(pin.source_pin); hal.gpio.setOutput(pin.source_pin);
write(output_state); write(output_state);
}, },
.input => chip.gpio.setInput(pin.source_pin), .input => hal.gpio.setInput(pin.source_pin),
} }
} }
fn get_direction() Direction { fn get_direction() Direction {
if (chip.gpio.isOutput(pin.source_pin)) { if (hal.gpio.isOutput(pin.source_pin)) {
return .output; return .output;
} else { } else {
return .input; return .input;
@ -123,8 +123,8 @@ pub fn Gpio(comptime pin: type, comptime config: anytype) type {
} }
// alternate function // alternate function
fn set_alternate_function(af: chip.gpio.AlternateFunction) void { fn set_alternate_function(af: hal.gpio.AlternateFunction) void {
chip.gpio.setAlternateFunction(pin.source_pin, af); hal.gpio.setAlternateFunction(pin.source_pin, af);
} }
}; };
// return only a subset of Generic for the requested pin. // return only a subset of Generic for the requested pin.

@ -1,5 +1,5 @@
const std = @import("std"); const std = @import("std");
const micro = @import("microzig"); const config = @import("config");
const chip = @import("chip"); const chip = @import("chip");
const board = @import("board"); const board = @import("board");
const hal = @import("hal"); const hal = @import("hal");
@ -23,7 +23,7 @@ pub fn Pin(comptime spec: []const u8) type {
hal.parse_pin(@field(board.pin_map, spec[board_namespace.len..])) hal.parse_pin(@field(board.pin_map, spec[board_namespace.len..]))
else if (std.mem.startsWith(u8, spec, chip_namespace)) else if (std.mem.startsWith(u8, spec, chip_namespace))
hal.parse_pin(spec[chip_namespace.len..]) hal.parse_pin(spec[chip_namespace.len..])
else if (micro.config.has_board and @hasField(@TypeOf(board.pin_map), spec)) else if (config.has_board and @hasField(@TypeOf(board.pin_map), spec))
hal.parse_pin(@field(board.pin_map, spec)) hal.parse_pin(@field(board.pin_map, spec))
else else
hal.parse_pin(spec); hal.parse_pin(spec);

Loading…
Cancel
Save