Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions port/raspberrypi/rp2xxx/src/hal/uart.zig
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ pub const UART = enum(u1) {
.deadline = deadline,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question that popped up when using the interfaces. Is this deadline correct? It's initialised when instantiating the Reader struct, but at least for the RP2040 I'm creating these deadlines with time.deadline_in_ms() which simply adds the argument with the current time to define an absolute timestamp when the deadline elapses.

Now, given the deadline's instantiated at the very beginning and never recreated it basically becomes a no-op as it's always elapsed later down the road.

Would it make sense to instead of passing an instantiated mdf.time.Deadline pass an integer (i.e. deadline_ms) to instantiate the deadline whenever we make a call such as a in stream()?

Maybe I'm just using the API wrong...

Should this be an error I'd be more than happy to provide a separate PR handling this as it affects both the Reader and Writer.

Thanks a ton for your time.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 'correct', but yeah, it would be weird to use, since a deadline is an absolute time (typically since boot). I am not sure the best way to square our use of deadlines (vs. timeouts) with the reader interface. We could have a default timeout which becomes a deadline when the actual read/write functions are called.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should always stick to absolute deadlines, as otherwise you can't compose the code correctly.

imagine you have a function which constructs a local reader, but is called 6 times. But you want to have these 6 calls timeout after a total of 1 second, and not each single one after 1 second.

We can just expose a .deadline as a public interface of the reader/writer and the user can simply update them as they wish.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super! In order not to mix things up and given this 'issue' affects both the Reader and Writer types what do you think about spinning this off into another issue? That way we can merge the changes to the Reader so that it conforms to std.io.Reader and handle this elsewhere.

If you're okay with that I guess this can be merged as soon as you're happy with the changes and I'll prepare a follow-up PR in a couple of days; I'm currently on holiday! 😎

.interface = .{
.buffer = buffer,
.seek = 0,
.end = 0,
.vtable = &.{
.stream = stream,
},
Expand Down Expand Up @@ -206,21 +208,15 @@ pub const UART = enum(u1) {
return n;
}

fn stream(io_reader: *std.Io.Reader, w: *std.Io.Writer, limit: std.Io.Limit) std.Io.Reader.StreamError!usize {
const r: *Reader = @fieldParentPtr("interface", io_reader);
fn stream(r: *std.Io.Reader, w: *std.Io.Writer, limit: std.Io.Limit) std.Io.Reader.StreamError!usize {
const uart_reader: *Reader = @alignCast(@fieldParentPtr("interface", r));
const uart = uart_reader.uart;
return switch (limit) {
.nothing => 0,
else => blk: {
var buf: [1]u8 = undefined;
const n = r.uart.read_blocking(&buf, null) catch |err| switch (err) {
error.ReceiveError => return error.ReadError,
};

break :blk switch (n) {
0 => 0,
1 => try w.writeByte(buf[0]),
else => unreachable,
};
else => {
const b = uart.read_word_blocking(uart_reader.deadline) catch return error.ReadFailed;
try w.writeByte(b);
return 1;
},
};
}
Expand Down
Loading