Skip to content

Fixes UART pin setting + adds CTS/RTS HW Flow Control #6272

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

Merged
merged 9 commits into from
Feb 17, 2022

Conversation

SuGlider
Copy link
Collaborator

@SuGlider SuGlider commented Feb 12, 2022

Summary

This PR has two components:
1- Fixes Serial.begin() in order to keep RX/TX pins the same, as previouly set, when those parameter are not used.
2 - Adds Hardware Flow Control and adds RTS/CTS pins setting

Impact

Serial will keep RX/TX pins unmodified in case of several begin() calls:

void setup() {
    Serial1.begin(115200, SERIAL_8N1, 18, 19);
    Serial1.println("HELLO WORLD!");
  
    Serial1.begin(115200);
    Serial1.begin(115200);
    Serial1.println("HELLO WORLD!  2nd Time!");
}

Modified and New functions - from HardwareSerial class:

    // Negative Pin Number will keep it unmodified, thus this function can set individual pins
    // SetPins shall be called after Serial begin()
    void setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin = -1, int8_t rtsPin = -1);
    // Enables or disables Hardware Flow Control using RTS and/or CTS pins (must use setAllPins() before)
    void setHwFlowCtrlMode(uint8_t mode = HW_FLOWCTRL_CTS_RTS, uint8_t threshold = 64); 

Note: setPins() accepts negative pin number that will keep it unmodified.

Related links

This replaces PR #6176
Fixes #6185

@SuGlider SuGlider changed the title Uart pins Fixes UART pin setting + adds CTS/RTS HW Flow Control Feb 12, 2022
Copy link
Member

@P-R-O-C-H-Y P-R-O-C-H-Y left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mrengineer7777
Copy link
Collaborator

Possible regression from #6176 . _rxPin(-1), _txPin(-1) were removed. How are the previous pin states tracked?

@SuGlider
Copy link
Collaborator Author

SuGlider commented Feb 14, 2022

@mrengineer7777
No need to track.
It is all done by IDF instead.

Please test the PR to check that it works as intended and let me know.

The "pin tracking" will get lost only when Serial.end() is called, but it sounds to be the correct expected behavior.

@SuGlider SuGlider added Area: Peripherals API Relates to peripheral's APIs. Type: Feature request Feature request for Arduino ESP32 labels Feb 14, 2022
@mrengineer7777
Copy link
Collaborator

mrengineer7777 commented Feb 15, 2022

Please test the PR to check that it works as intended and let me know.

@SuGlider Apologies, as this isn't my issue and I'm not an Espressif employee I won't be able to test this.

The "pin tracking" will get lost only when Serial.end() is called, but it sounds to be the correct expected behavior.

Seems reasonable.

rxPin = rxPin < 0 ? RX2 : rxPin;
txPin = txPin < 0 ? TX2 : txPin;
break;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always add default: for switch statements. ESP-IDF will complain

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256) {}

enum { UART_NUM_0, UART_NUM_1, UART_NUM_2 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this enum will be used just for the switch statement, let's use int instead

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is solved now by including "driver/uart.h" that has those defines.

@me-no-dev me-no-dev merged commit 50e9772 into espressif:master Feb 17, 2022
Jason2866 added a commit to tasmota/arduino-esp32 that referenced this pull request Feb 17, 2022
Fixes UART pin setting + adds CTS/RTS HW Flow Control (espressif#6272)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Peripherals API Relates to peripheral's APIs. Type: Feature request Feature request for Arduino ESP32
Projects
Development

Successfully merging this pull request may close these issues.

Enable UART Flow Control
4 participants