Idea: Convert APIs that takes addresses in HALs to take `anytype`.
We have a bunch of functions that take addresses in our various HALs, but the type is often u32 or usize.
We should evaluate how they are used, and see if people tend to call them with points (e.g. by providing a slice or array) or an actual int.
In the former case, they'd have to do something like @intFromPtr, which is fine, but a bit annoying.
We can improve the ergonomics of these APIs (if we find that usage common) by having the type be anytype, and make type checks to ensure it is a pointer or appropriate type.
@@ -95,14 +95,16 @@ pub const Channel = enum(u4) {
pub fn trigger_transfer(
chan: Channel,
- write_addr: u32,
- read_addr: u32,
+ write_addr: anytype,
+ read_addr: anytype,
count: u32,
config: TransferConfig,
) void {
+ comptime if (@typeInfo(@TypeOf(write_addr)) != .pointer) @compileError("write_addr must be a pointer");
+ comptime if (@typeInfo(@TypeOf(read_addr)) != .pointer) @compileError("read_addr must be a pointer");
const regs = chan.get_regs();
- regs.read_addr = read_addr;
- regs.write_addr = write_addr;
+ regs.read_addr = @intFromPtr(read_addr);
+ regs.write_addr = @intFromPtr(write_addr);
regs.trans_count = count;
regs.ctrl_trig.modify(.{
.EN = @intFromBool(config.enable),
This allows the client to have a cleaner call:
channel.?.trigger_transfer(
- @intFromPtr(dst[0..dst.len].ptr),
- @intFromPtr(hello[0..src.len].ptr),
+ dst[0..dst.len].ptr,
+ src[0..hello.len].ptr,
src.len,
.{
Of course, we can allow ints of the appropriate size as well, and we can write a helper to verify the type.
I think that's pretty good as long as we don't allow slices to be passed (as one would expect slice.len to be considered).
We could even take *anyopaque and *const anyopaque. The pointer casting to anyopaque is implicit afaik. This way we only allow pointers (which will probably be the most common passed type).
Ouch, i missed this issue, it is partially done by #517