Serial (CDC) module for USB Host #10283
Open
+517
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a module wrapping TinyUSB's
class/cdc/cdc_host
library into a pyserial-like API so that a CircuitPython board with a USB Host can talk to other devices with CDC interfaces over USB.It is possible to talk CDC using only the
usb.core.Device.read
/.write
methods. However, this only works in blocking mode. I tried setting up asynchronous communication by supplying a timeout to.read()
, but when I tested my code I missed data.As I understand, CDC reads are performed by sending an
IN
packet, then waiting for the device to eventually send aDATA
packet back. This transaction cannot be cancelled halfway through, so even if CircuitPython times out and moves onto other things, the data transfer is going to happen regardless.I think the only way to build an asynchronous API around sending data through the
usb.core
library is to use FIFOs so that even if CircuitPython times out and moves on, the data is still appended to the FIFO and can be read back later. This is how TinyUSB's Endpoint Stream API works. Theclass/cdc/cdc_host
library I am wrapping internally uses Endpoint Streams for the rx and tx endpoints.I was debating building a module around the cdc host or endpoint streams and decided against endpoint streams since they are only exposed in TinyUSB's private headers. They don't require a significant amount of code to implement though, so if you think that it is better to build a lower-level library than a higher-level library I could submit that as a PR instead. I have most of the code written, but it does not work yet.