Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Rx/Tx reunification for Serial #390

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

horazont
Copy link
Contributor

@horazont horazont commented Jan 9, 2022

There are many possible pin combinations for a Serial peripheral. Many support at least one alternative pin group to be used, and in addition to that, each pin can also be configured differently. For instance, instead of the common Output<PushPull> with an Input<Floating>, one could also use Output<OpenDrain> and Input<PullUp>, e.g. when
interfacing with a 1-Wire bus.

This pin information is already lost when splitting the Serial object into Rx/Tx. In order to allow a reunification into a Serial object which represents ownership of both Tx and Rx, we need a Serial variant which has that pin information erased.

This PR introduces ErasedSerial, which serves exactly that purpose. In addition, Tx and Rx both gain a consuming reunite function which restores the original Serial. To facilitate that and provide the original contents of the ErasedSerial struct, we use the Tx to stash away the USART instance -- this choice is arbitrary. As the USART is zero-sized anyway and we get both parts (Rx and Tx) when reuniting, it shouldn't matter where we stash it away.

Fixes #386.

There are many possible pin combinations for a Serial peripheral. Many
support at least one alternative pin group to be used, and in addition
to that, each pin can also be configured differently. For instance,
instead of the common Output<PushPull> with an Input<Floating>, one
could also use Output<OpenDrain> and Input<PullUp>, e.g. when
interfacing with a 1-Wire bus.

This pin information is already lost when splitting the Serial object
into Rx/Tx. In order to allow a reunification into a Serial object which
represents ownership of both Tx and Rx, we need a Serial variant which
has that pin information erased.
This becomes relevant as more functions like reconfigure are added to the
Serial structs. We use the Tx to stash away the usart instance used by
the (Erased-)Serial struct, but this choice is arbitrary and irrelevant
because it is zero-sized anyway.

Fixes stm32-rs#386.
@horazont
Copy link
Contributor Author

horazont commented Jan 9, 2022

Note that this answers some of the design questions in #386 in code. I am using this successfully in my async/await experiments on the STM32VLDISCOVERY board, but I'm open for changing things if you disagree about anything.

@burrbull
Copy link
Contributor

burrbull commented Jan 9, 2022

Possibly if Tx contain tx_pin and Rx contain rx_pin we will not need ErasedSerial

@burrbull burrbull mentioned this pull request Jan 9, 2022
@burrbull
Copy link
Contributor

burrbull commented Jan 9, 2022

See #392

@burrbull
Copy link
Contributor

cc @TheZoq2

@burrbull
Copy link
Contributor

Is reconfigure the main goal of reunification for you?

@burrbull
Copy link
Contributor

Is reconfigure the main goal of reunification for you?

In this case I can propose easier solution: #393
The advantage of external function that it takes mutable references instead of owning Tx and Rx and can be used in RTIC, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add reunite for Serial Tx/Rx
2 participants