Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
fpistm merged 1 commit intostm32duino:mainfromatmosphericiq:uart-pinswap
Jan 22, 2025

Conversation

fronders
Copy link
Contributor

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 declaringHardwareSerial object:

//                      RX    TXHardwareSerialSerial1(PA10, PA9);// uses normal pins//                      RX    TXHardwareSerialSerial1(PA9, PA10);// uses swapped pins

or by usingsetTx() andsetRx() methods:

Serial1.setRx(PA9);// normally it is a Tx pinSerial1.setTx(PA10);// normally it is a Rx pinSerial1.begin(9600);

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 useRx == Tx trick as before or just provide a single pin definition

Fixes:#2538
See also:#1418

@fpistmfpistm marked this pull request as ready for reviewDecember 11, 2024 19:58
@fronders
Copy link
ContributorAuthor

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 :)

@fronders
Copy link
ContributorAuthor

@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:

  1. Connect UART4 and UART5 with two wires (Rx to Tx and Tx to Rx)
  2. Send stuff over ST-Link's USB COM port and observe it being received back
  3. Hold the button to reverse the direction and send/observe again
  4. Reset the board while holding the button to enable pin swap
  5. Swap the wires (Rx to Rx and Tx to Tx) and test again
  6. Enable half-duplex by uncommenting the define
  7. Flash and test again, this time it needs more wire swapping to match all 4 cases

@fpistmfpistm self-requested a reviewDecember 17, 2024 09:56
@fpistmfpistm added the enhancementNew feature or request labelDec 17, 2024
@fpistm
Copy link
Member

Hi@fronders,

thanks for the PR, will try to review it this week.

fronders reacted with rocket emoji

@fpistmfpistm linked an issueJan 22, 2025 that may beclosed by this pull request
@fpistmfpistm added this to the2.10.0 milestoneJan 22, 2025
Copy link
Member

@fpistmfpistm left a 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.

fronders reacted with hooray emojifronders reacted with heart emoji
@fpistmfpistm merged commitbfdefe2 intostm32duino:mainJan 22, 2025
24 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@fpistmfpistmfpistm approved these changes

Assignees
No one assigned
Labels
enhancementNew feature or request
Projects
Milestone
2.10.0
Development

Successfully merging this pull request may close these issues.

UART Tx/Rx Pin Swap
2 participants
@fronders@fpistm

[8]ページ先頭

©2009-2025 Movatter.jp