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

HardwareSerial::attachRts - new optional parameter to invert signal #765

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion teensy4/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,13 @@ void HardwareSerialIMXRT::begin(uint32_t baud, uint16_t format)

inline void HardwareSerialIMXRT::rts_assert()
{
DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
if (rts_pin_invert_) DIRECT_WRITE_HIGH(rts_pin_baseReg_, rts_pin_bitmask_);
else DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
}

inline void HardwareSerialIMXRT::rts_deassert()
{
if (rts_pin_invert_) DIRECT_WRITE_LOW(rts_pin_baseReg_, rts_pin_bitmask_);
DIRECT_WRITE_HIGH(rts_pin_baseReg_, rts_pin_bitmask_);
}

Expand Down Expand Up @@ -365,6 +367,23 @@ bool HardwareSerialIMXRT::attachRts(uint8_t pin)
if (pin < CORE_NUM_DIGITAL) {
rts_pin_baseReg_ = PIN_TO_BASEREG(pin);
rts_pin_bitmask_ = PIN_TO_BITMASK(pin);
rts_pin_invert_ = false;
pinMode(pin, OUTPUT);
rts_assert();
} else {
rts_pin_baseReg_ = NULL;
return 0;
}
return 1;
}

bool HardwareSerialIMXRT::attachRtsInverted(uint8_t pin)
{
if (!(hardware->ccm_register & hardware->ccm_value)) return 0;
if (pin < CORE_NUM_DIGITAL) {
rts_pin_baseReg_ = PIN_TO_BASEREG(pin);
rts_pin_bitmask_ = PIN_TO_BITMASK(pin);
rts_pin_invert_ = true;
pinMode(pin, OUTPUT);
rts_assert();
} else {
Expand Down
8 changes: 7 additions & 1 deletion teensy4/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,12 @@ class HardwareSerialIMXRT : public HardwareSerial
// receive more data, or HIGH when the serial device should pause transmission.
// All digital pins are supported.
bool attachRts(uint8_t pin);
// Configure CTS flow control. Teensy will transmit when this pin is LOw
// Same as attachRts except the signal levels are reversed.
// Configure RTS flow control. The pin will be HIGH when Teensy is able to
// receive more data, or LOW when the serial device should pause transmission.
// All digital pins are supported.
bool attachRtsInverted(uint8_t pin);
// Configure CTS flow control. Teensy will transmit when this pin is LOW
// and will pause transmission when the pin is HIGH. Only specific pins are
// supported. See https://www.pjrc.com/teensy/td_uart.html
bool attachCts(uint8_t pin);
Expand Down Expand Up @@ -327,6 +332,7 @@ class HardwareSerialIMXRT : public HardwareSerial

volatile uint32_t *rts_pin_baseReg_ = 0;
uint32_t rts_pin_bitmask_ = 0;
bool rts_pin_invert_ = false;

inline void rts_assert();
inline void rts_deassert();
Expand Down