stm32f1xx-hal icon indicating copy to clipboard operation
stm32f1xx-hal copied to clipboard

Implement Rx/Tx reunification for Serial

Open horazont opened this issue 3 years ago • 6 comments

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.

horazont avatar Jan 09 '22 11:01 horazont

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.

horazont avatar Jan 09 '22 11:01 horazont

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

burrbull avatar Jan 09 '22 17:01 burrbull

See #392

burrbull avatar Jan 09 '22 18:01 burrbull

cc @TheZoq2

burrbull avatar Jan 10 '22 14:01 burrbull

Is reconfigure the main goal of reunification for you?

burrbull avatar Jan 14 '22 03:01 burrbull

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.

burrbull avatar Jan 14 '22 04:01 burrbull