Make UARTs adhere to the std.Io.Reader interface - #1008
Conversation
6446f2d to
e7b3e2e
Compare
| 1 => try w.writeByte(buf[0]), | ||
| else => unreachable, | ||
| else => { | ||
| const b = uart.read_word_blocking(uart_reader.deadline) catch |err| switch (err) { |
There was a problem hiding this comment.
Since we're returning one error regardless, we don't need to capture the error here, you can write instead something like:
const b = uart.read_word_blocking(uart_reader.deadline) catch return error.ReadFailed;
There was a problem hiding this comment.
Thanks a ton for the comment! It should be applied on 78b1d61.
Thanks again!
| @@ -177,6 +177,8 @@ pub const UART = enum(u1) { | |||
| .deadline = deadline, | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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! 😎
When trying to leverage an UART through an
std.Io.Readerinterface I was met with compilation errors such asThis PR adds changes so that the UART's HAL adheres to the
std.Io.Readerinterface whilst also updating thestreamfunction.The reader implementation works on my side when driving the UART for performing Modbus requests.
If there's anything that should be changed don't hesitate to do so: these are my first steps with Zig!
Thanks a ton for your time.