rust-embedded-community / usbd-serial

Work-in progress minimal CDC-ACM (USB serial port) class for usb-device
MIT License
118 stars 35 forks source link

Should this work with the writeln! macro? #2

Closed bentwire closed 5 years ago

bentwire commented 5 years ago

Was trying to use it with writeln! but it does not seem to implement core::fmt::Write. I'm kinda a rust n00b so maybe I am doing something wrong, so just want to confirm.

Disasm commented 5 years ago

Serial works with bytes, not characters, so in general you need to implement your own wrapper with semantics appropriate to you (maybe even with non-trivial conversion). Also note that fmt::Write::write_str is blocking operation: it's not a good idea to halt USB stack while you are writing to serial (even USB serial). You can check this implementation of logger and example which uses it.

bentwire commented 5 years ago

So basically I need to write a wrapper of sorts that sits on top of the USB serial port that can handle the blocking reads/writes while letting the underlying USB serial driver run without blocking?

I guess I am confused because I see examples where people use writeln! to write to a USART/UART. I assume that the USART/UART implementation of fmt::Write::write_str must implement this wrapping layer in order for this to work, else like you said the USART driver interrupt could fail trying to get a borrowed version of the USART object while writeln! has it.

Disasm commented 5 years ago

I don't know how this USART/UART write_str implemented in your case, but for UART it is not a problem to block until completion. With USB this is a bit problematic: technically, you can block on write there, but with some probability you may get a EP0 packet and fail to process it in time, and this could be a problem.

bentwire commented 5 years ago

Ahh ok I see, this makes sense.

All I really want is to use serde to send json structured data across the bus anyway. I think I can figure this out.