- Notifications
You must be signed in to change notification settings - Fork1k
feat(uart): support UART Tx Rx pin swap function#2601
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Hi@fpistm, I'm finishing testing at the moment with different STM32 chips and will report back as soon as I'm done and things are ready to be merged :) |
@fpistm I've finished testing on multiple ST families Nucleo-64 boards. Code works as intended and is ready for merging! In case you want to test yourself here's my sketch: #include<Arduino.h>#define__XSTR(a) __STR(a)#define__STR(a) #a// #define USE_HALFDUPLEX// Define serials with normal pins order// RX TXHardwareSerialSerial4(PC11, PC10);#ifdef USE_HALFDUPLEX// TXHardwareSerialSerial5(PC12);#else// RX TXHardwareSerialSerial5(PD2, PC12);#endif// Send from USB to SerialTx, then via wires to SerialRx and then back to USB#defineSerialTx Serial5#defineSerialRx Serial4int lastButtonState;voidsetup() {pinMode(USER_BTN, INPUT); Serial.begin(115200);if (digitalRead(USER_BTN) == LOW) {// Button held on startup - enable SerialTx pinswap#ifdef USE_HALFDUPLEX SerialTx.setTx(PD2); SerialTx.setHalfDuplex();#else SerialTx.setTx(PD2); SerialTx.setRx(PC12);#endif } SerialTx.begin(9600); SerialRx.begin(9600); Serial.printf("%s: pin swap %s, half duplex %s\r\n",__XSTR(SerialTx),READ_BIT(SerialTx.getHandle()->Instance->CR2, USART_CR2_SWAP) ?"enabled" :"disabled", SerialTx.isHalfDuplex() ?"enabled" :"disabled"); Serial.println("How to use:"); Serial.printf("Button released USB->%s->%s->USB\r\n",__XSTR(SerialTx),__XSTR(SerialRx)); Serial.printf("Button pressed USB->%s->%s->USB\r\n",__XSTR(SerialRx),__XSTR(SerialTx)); lastButtonState =digitalRead(USER_BTN);}voidloop() {int buttonState =digitalRead(USER_BTN);if (buttonState) {// Forward transmission SerialTx->SerialRxif (!lastButtonState) {#ifdef USE_HALFDUPLEX// Just switched, enable half-duplex receiver SerialTx.enableHalfDuplexRx();#endif Serial.printf("\r\nButton released: USB->%s->%s->USB\r\n",__XSTR(SerialTx),__XSTR(SerialRx)); }if (Serial.available()) {// If anything comes in Serial (USB), SerialTx.write(Serial.read());// read it and send it out SerialTx }if (SerialRx.available()) {// If anything comes in SerialRx Serial.write(SerialRx.read());// read it and send it out Serial (USB) } }else {// Reverse transmission SerialRx->SerialTxif (lastButtonState) {#ifdef USE_HALFDUPLEX// Just switched, enable half-duplex receiver SerialTx.enableHalfDuplexRx();#endif Serial.printf("\r\nButton pressed USB->%s->%s->USB\r\n",__XSTR(SerialRx),__XSTR(SerialTx)); }if (Serial.available()) {// If anything comes in Serial (USB), SerialRx.write(Serial.read());// read it and send it out SerialTx }if (SerialTx.available()) {// If anything comes in SerialRx Serial.write(SerialTx.read());// read it and send it out Serial (USB) } } lastButtonState = buttonState;} So I would do the following:
|
Hi@fronders, thanks for the PR, will try to review it this week. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM
thanks@fronders and sorry for the delay.
bfdefe2
intostm32duino:mainUh oh!
There was an error while loading.Please reload this page.
This enables UART Tx and Rx pin swap function on STM32 families that support it (F0, L0, L4, G0, G4, H7 etc.)
In order to enable pin swap, user just needs toswap the pins either when declaring
HardwareSerial
object:or by using
setTx()
andsetRx()
methods:If the chip does not support the swap, or the pins are incorrect, the library behaves the same way as before (throws an error for invalid pins).
The cool thing is that it even supports half-duplex mode on thenormally Rx pin - the Rx pin is used for transmission and reception then. User can either use
Rx == Tx
trick as before or just provide a single pin definitionFixes:#2538
See also:#1418