Skip to content

Commit 230758b

Browse files
Add invert option to SWSerial (#563)
Thanks to @StefanKellerAC !
1 parent d7e02c6 commit 230758b

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

cores/rp2040/SerialPIO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SerialPIO : public HardwareSerial {
5454
// Not to be called by users, only from the IRQ handler. In public so that the C-language IQR callback can access it
5555
void _handleIRQ();
5656

57-
private:
57+
protected:
5858
bool _running = false;
5959
pin_size_t _tx, _rx;
6060
int _baud;

cores/rp2040/SoftwareSerial.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,34 @@ class SoftwareSerial : public SerialPIO {
2626
public:
2727
// Note the rx/tx pins are swapped in PIO vs SWSerial
2828
SoftwareSerial(pin_size_t rx, pin_size_t tx, bool invert = false) : SerialPIO(tx, rx) {
29-
if (invert) {
30-
panic("SoftwareSerial inverted operation not supported\n");
29+
_invert = invert;
30+
}
31+
32+
~SoftwareSerial() {
33+
if (_invert) {
34+
gpio_set_outover(_tx, 0);
35+
gpio_set_outover(_rx, 0);
3136
}
3237
}
38+
39+
virtual void begin(unsigned long baud = 115200) override {
40+
begin(baud, SERIAL_8N1);
41+
};
42+
43+
void begin(unsigned long baud, uint16_t config) override {
44+
SerialPIO::begin(baud, config);
45+
if (_invert) {
46+
gpio_set_outover(_tx, GPIO_OVERRIDE_INVERT);
47+
gpio_set_inover(_rx, GPIO_OVERRIDE_INVERT);
48+
}
49+
}
50+
3351
void listen() { /* noop */ }
52+
3453
bool isListening() {
3554
return true;
3655
}
56+
57+
private:
58+
bool _invert;
3759
};

0 commit comments

Comments
 (0)