Skip to content

Commit bd650e8

Browse files
authored
rp2xxx: Unique board identifier (#910)
1 parent 8f1453b commit bd650e8

4 files changed

Lines changed: 45 additions & 12 deletions

File tree

examples/raspberrypi/rp2xxx/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub fn build(b: *std.Build) void {
1818
// RaspberryPi Boards:
1919
.{ .target = raspberrypi.pico, .name = "pico_board_blinky", .file = "src/board_blinky.zig" },
2020
.{ .target = raspberrypi.pico, .name = "pico_flash-program", .file = "src/rp2040_only/flash_program.zig" },
21-
.{ .target = raspberrypi.pico, .name = "pico_flash-id", .file = "src/rp2040_only/flash_id.zig" },
2221
.{ .target = raspberrypi.pico, .name = "pico_random", .file = "src/rp2040_only/random.zig" },
2322
.{ .target = raspberrypi.pico, .name = "pico_rtc", .file = "src/rp2040_only/rtc.zig" },
2423
.{ .target = raspberrypi.pico, .name = "pico_multicore", .file = "src/blinky_core1.zig" },
@@ -103,6 +102,7 @@ pub fn build(b: *std.Build) void {
103102
.{ .name = "net-udp", .file = "src/net/udp.zig" },
104103
.{ .name = "net-tcp_client", .file = "src/net/tcp_client.zig" },
105104
.{ .name = "net-tcp_server", .file = "src/net/tcp_server.zig" },
105+
.{ .name = "board-id", .file = "src/board_id.zig" },
106106
};
107107

108108
var available_examples: std.array_list.Managed(Example) = .init(b.allocator);

examples/raspberrypi/rp2xxx/src/rp2040_only/flash_id.zig renamed to examples/raspberrypi/rp2xxx/src/board_id.zig

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const microzig = @import("microzig");
44
const rp2xxx = microzig.hal;
55
const time = rp2xxx.time;
66
const gpio = rp2xxx.gpio;
7-
const flash = rp2xxx.flash;
87

98
const uart = rp2xxx.uart.instance.num(0);
109
const uart_tx_pin = gpio.num(0);
@@ -15,10 +14,11 @@ pub fn panic(message: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noretu
1514
while (true) {}
1615
}
1716

18-
pub const std_options = struct {
19-
pub const log_level = .debug;
20-
pub const logFn = rp2xxx.uart.log;
17+
pub const microzig_options = microzig.Options{
18+
.log_level = .debug,
19+
.logFn = rp2xxx.uart.log,
2120
};
21+
const log = std.log.scoped(.main);
2222

2323
pub fn main() !void {
2424
// init uart logging
@@ -29,9 +29,7 @@ pub fn main() !void {
2929
rp2xxx.uart.init_logger(uart);
3030

3131
while (true) {
32-
const serial_number = flash.id();
33-
const hex_serial_number = std.fmt.bytesToHex(&serial_number, .lower);
34-
std.log.info("serial number: {s}", .{hex_serial_number});
32+
log.info("unique board id: {x}", .{rp2xxx.get_board_id()});
3533
time.sleep_ms(1000);
3634
}
3735
}

port/raspberrypi/rp2xxx/src/hal.zig

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const builtin = @import("builtin");
2-
const std = @import("std");
32
const microzig = @import("microzig");
43
const SIO = microzig.chip.peripherals.SIO;
54

@@ -144,6 +143,24 @@ pub fn get_cpu_id() u32 {
144143
return SIO.CPUID.read().CPUID;
145144
}
146145

146+
var board_id: ?[8]u8 = null;
147+
148+
/// Unique board identifier.
149+
/// On an RP2040-based board, the unique identifier is retrieved from the
150+
/// external NOR flash device
151+
/// On an RP2350-based board, the unique identifier is retrieved from OTP
152+
/// memory.
153+
pub fn get_board_id() [8]u8 {
154+
if (board_id) |b| {
155+
return b;
156+
}
157+
board_id = switch (compatibility.chip) {
158+
.RP2040 => flash.id(),
159+
.RP2350 => rom.get_board_id(),
160+
};
161+
return board_id.?;
162+
}
163+
147164
test "hal tests" {
148165
_ = pio;
149166
_ = usb;

port/raspberrypi/rp2xxx/src/hal/rom.zig

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
//! that would otherwise have to take up space in most user binaries.
77

88
const std = @import("std");
9-
const microzig = @import("microzig");
109
const compatibility = @import("compatibility.zig");
11-
const arch = compatibility.arch;
1210
const chip = compatibility.chip;
13-
const options = microzig.options.hal;
1411

1512
/// Returns the ROM version number.
1613
pub inline fn get_version_number() u8 {
@@ -213,3 +210,24 @@ pub fn reset_to_usb_boot() void {
213210
},
214211
}
215212
}
213+
214+
pub fn get_sys_info(out_buffer: [*]u32, out_buffer_word_size: u32, flags: u32) void {
215+
switch (chip) {
216+
.RP2040 => @compileError("not supported on this chip"),
217+
.RP2350 => {
218+
const f: *const signatures.get_sys_info = @ptrCast(@alignCast(lookup_function(.get_sys_info)));
219+
const rc = f(out_buffer, out_buffer_word_size, flags);
220+
std.debug.assert(rc == 4);
221+
},
222+
}
223+
}
224+
225+
pub fn get_board_id() [8]u8 {
226+
var out: [9]u32 = @splat(0);
227+
get_sys_info(&out, out.len, 0x001);
228+
var buf: [8]u8 = undefined;
229+
for (std.mem.asBytes(out[2..])[0..8], 1..) |b, i| {
230+
buf[8 - i] = b;
231+
}
232+
return buf;
233+
}

0 commit comments

Comments
 (0)