Skip to content

Commit

Permalink
avr: update example to use core.experimental (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverpool authored Mar 8, 2025
1 parent 2e2331d commit 584b791
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/microchip/avr/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {

const available_examples = [_]Example{
.{ .target = mb.ports.avr.boards.arduino.nano, .name = "arduino-nano_blinky", .file = "src/blinky.zig" },
.{ .target = mb.ports.avr.boards.arduino.uno_rev3, .name = "arduino-nano_blinky", .file = "src/blinky.zig" },
.{ .target = mb.ports.avr.boards.arduino.uno_rev3, .name = "arduino-uno_blinky", .file = "src/blinky.zig" },
};

for (available_examples) |example| {
Expand Down
14 changes: 8 additions & 6 deletions examples/microchip/avr/src/blinky.zig
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const std = @import("std");
const microzig = @import("microzig");

// LED is PB5
const port = microzig.chip.peripherals.PORTB;
const led_pin = microzig.core.experimental.Pin("PB5");

pub fn main() void {
port.DDRB |= (1 << 5);
port.PORTB |= 0x00;
const led = microzig.core.experimental.gpio.Gpio(led_pin, .{
.mode = .output,
.initial_state = .low,
});
led.init();

while (true) {
microzig.core.experimental.debug.busy_sleep(1_000);
port.PINB |= (1 << 5);
microzig.core.experimental.debug.busy_sleep(20_000);
led.toggle();
}
}
13 changes: 6 additions & 7 deletions port/microchip/avr/src/hals/ATmega328P.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub const clock = struct {
};
};

pub fn parsePin(comptime spec: []const u8) type {
pub fn parse_pin(comptime spec: []const u8) type {
const invalid_format_msg = "The given pin '" ++ spec ++ "' has an invalid format. Pins must follow the format \"P{Port}{Pin}\" scheme.";

if (spec.len != 3)
Expand Down Expand Up @@ -53,18 +53,17 @@ pub const gpio = struct {
cpu.cbi(regs(pin).dir_addr, pin.pin);
}

pub fn read(comptime pin: type) micro.gpio.State {
pub fn read(comptime pin: type) micro.core.experimental.gpio.State {
return if ((regs(pin).pin.* & (1 << pin.pin)) != 0)
.high
else
.low;
}

pub fn write(comptime pin: type, state: micro.gpio.State) void {
if (state == .high) {
cpu.sbi(regs(pin).port_addr, pin.pin);
} else {
cpu.cbi(regs(pin).port_addr, pin.pin);
pub fn write(comptime pin: type, state: micro.core.experimental.gpio.State) void {
switch (state) {
.high => cpu.sbi(regs(pin).port_addr, pin.pin),
.low => cpu.cbi(regs(pin).port_addr, pin.pin),
}
}

Expand Down
13 changes: 6 additions & 7 deletions port/microchip/avr/src/hals/ATmega32U4.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub const clock = struct {
};
};

pub fn parsePin(comptime spec: []const u8) type {
pub fn parse_pin(comptime spec: []const u8) type {
const invalid_format_msg = "The given pin '" ++ spec ++ "' has an invalid format. Pins must follow the format \"P{Port}{Pin}\" scheme.";

if (spec.len != 3)
Expand Down Expand Up @@ -63,18 +63,17 @@ pub const gpio = struct {
cpu.cbi(regs(pin).dir_addr, pin.pin);
}

pub fn read(comptime pin: type) micro.gpio.State {
pub fn read(comptime pin: type) micro.core.experimental.gpio.State {
return if ((regs(pin).pin.* & (1 << pin.pin)) != 0)
.high
else
.low;
}

pub fn write(comptime pin: type, state: micro.gpio.State) void {
if (state == .high) {
cpu.sbi(regs(pin).port_addr, pin.pin);
} else {
cpu.cbi(regs(pin).port_addr, pin.pin);
pub fn write(comptime pin: type, state: micro.core.experimental.gpio.State) void {
switch (state) {
.high => cpu.sbi(regs(pin).port_addr, pin.pin),
.low => cpu.cbi(regs(pin).port_addr, pin.pin),
}
}

Expand Down

0 comments on commit 584b791

Please sign in to comment.