Skip to content

Commit ae46a2f

Browse files
authored
Merge branch 'main' into rp2350_uart
2 parents a87b134 + 24106a9 commit ae46a2f

File tree

6 files changed

+64
-23
lines changed

6 files changed

+64
-23
lines changed

examples/microchip/avr/build.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn build(b: *std.Build) void {
1313

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

1919
for (available_examples) |example| {

examples/microchip/avr/src/blinky.zig

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
const std = @import("std");
22
const microzig = @import("microzig");
33

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

76
pub fn main() void {
8-
port.DDRB |= (1 << 5);
9-
port.PORTB |= 0x00;
7+
const led = microzig.core.experimental.gpio.Gpio(led_pin, .{
8+
.mode = .output,
9+
.initial_state = .low,
10+
});
11+
led.init();
1012

1113
while (true) {
12-
microzig.core.experimental.debug.busy_sleep(1_000);
13-
port.PINB |= (1 << 5);
14+
microzig.core.experimental.debug.busy_sleep(20_000);
15+
led.toggle();
1416
}
1517
}

port/microchip/avr/src/hals/ATmega328P.zig

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub const clock = struct {
1616
};
1717
};
1818

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

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

56-
pub fn read(comptime pin: type) micro.gpio.State {
56+
pub fn read(comptime pin: type) micro.core.experimental.gpio.State {
5757
return if ((regs(pin).pin.* & (1 << pin.pin)) != 0)
5858
.high
5959
else
6060
.low;
6161
}
6262

63-
pub fn write(comptime pin: type, state: micro.gpio.State) void {
64-
if (state == .high) {
65-
cpu.sbi(regs(pin).port_addr, pin.pin);
66-
} else {
67-
cpu.cbi(regs(pin).port_addr, pin.pin);
63+
pub fn write(comptime pin: type, state: micro.core.experimental.gpio.State) void {
64+
switch (state) {
65+
.high => cpu.sbi(regs(pin).port_addr, pin.pin),
66+
.low => cpu.cbi(regs(pin).port_addr, pin.pin),
6867
}
6968
}
7069

port/microchip/avr/src/hals/ATmega32U4.zig

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub const clock = struct {
1919
};
2020
};
2121

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

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

66-
pub fn read(comptime pin: type) micro.gpio.State {
66+
pub fn read(comptime pin: type) micro.core.experimental.gpio.State {
6767
return if ((regs(pin).pin.* & (1 << pin.pin)) != 0)
6868
.high
6969
else
7070
.low;
7171
}
7272

73-
pub fn write(comptime pin: type, state: micro.gpio.State) void {
74-
if (state == .high) {
75-
cpu.sbi(regs(pin).port_addr, pin.pin);
76-
} else {
77-
cpu.cbi(regs(pin).port_addr, pin.pin);
73+
pub fn write(comptime pin: type, state: micro.core.experimental.gpio.State) void {
74+
switch (state) {
75+
.high => cpu.sbi(regs(pin).port_addr, pin.pin),
76+
.low => cpu.cbi(regs(pin).port_addr, pin.pin),
7877
}
7978
}
8079

tools/regz/src/Database.zig

+35
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,42 @@ pub fn get_enum_by_name(
13471347
return db.one_alloc(Enum, allocator, query, .{
13481348
.struct_id = struct_id,
13491349
.name = name,
1350+
}) catch |err| switch (err) {
1351+
error.MissingEntity => {
1352+
// lookup the enum among the parents
1353+
var parent_id = struct_id;
1354+
var i: u8 = 0;
1355+
const max_depth = 10;
1356+
while (i <= max_depth) : (i += 1) {
1357+
parent_id = db.get_parent_struct_id(parent_id) catch {
1358+
return err;
1359+
};
1360+
1361+
log.debug("get_enum_by_name: parent_id={} name='{s}'", .{ parent_id, name });
1362+
return db.one_alloc(Enum, allocator, query, .{
1363+
.struct_id = parent_id,
1364+
.name = name,
1365+
}) catch {
1366+
continue;
1367+
};
1368+
}
1369+
return err;
1370+
},
1371+
else => {
1372+
return err;
1373+
},
1374+
};
1375+
}
1376+
1377+
fn get_parent_struct_id(db: *Database, struct_id: StructID) !StructID {
1378+
var stmt = try db.sql.prepare("SELECT parent_id FROM struct_decls WHERE struct_id = ?");
1379+
defer stmt.deinit();
1380+
1381+
const row = try stmt.one(StructID, .{}, .{
1382+
.struct_id = struct_id,
13501383
});
1384+
1385+
return if (row) |parent_id| parent_id else error.MissingEntity;
13511386
}
13521387

13531388
fn one_alloc(

tools/regz/src/atdf.zig

+8-2
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,10 @@ fn load_field(ctx: *Context, node: xml.Node, peripheral_struct_id: StructID, par
676676
.size_bits = 1,
677677
.offset_bits = i,
678678
.enum_id = if (node.get_attribute("values")) |values| blk: {
679-
const e = try db.get_enum_by_name(arena, peripheral_struct_id, values);
679+
const e = db.get_enum_by_name(arena, peripheral_struct_id, values) catch |err| {
680+
log.warn("{s} failed to get_enum_by_name: {s}", .{ name, values });
681+
return err;
682+
};
680683
break :blk e.id;
681684
} else null,
682685
});
@@ -722,7 +725,10 @@ fn load_field(ctx: *Context, node: xml.Node, peripheral_struct_id: StructID, par
722725
.size_bits = width,
723726
.offset_bits = offset,
724727
.enum_id = if (node.get_attribute("values")) |values| blk: {
725-
const e = try db.get_enum_by_name(arena, peripheral_struct_id, values);
728+
const e = db.get_enum_by_name(arena, peripheral_struct_id, values) catch |err| {
729+
log.warn("{s} failed to get_enum_by_name: {s}", .{ name, values });
730+
return err;
731+
};
726732
break :blk e.id;
727733
} else null,
728734
});

0 commit comments

Comments
 (0)