Skip to content

Commit 9afdc48

Browse files
Add SoftwareSerial wrapper around SerialPIO (#548)
I receive mails weekly asking how to use `SoftwareSerial` on this core. Avoid the issue by including a simple wrapper class around `SerialPIO` which gives the proper class name and constructor parameters. Note that inverted mode is not supported.
1 parent 3ee031a commit 9afdc48

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

cores/rp2040/SoftwareSerial.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
SoftwareSerial wrapper for SerialPIO
3+
4+
Copyright (c) 2022 Earle F. Philhower, III <[email protected]>
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#pragma once
22+
23+
#include "SerialPIO.h"
24+
25+
class SoftwareSerial : public SerialPIO {
26+
public:
27+
// Note the rx/tx pins are swapped in PIO vs SWSerial
28+
SoftwareSerial(pin_size_t rx, pin_size_t tx, bool invert = true) : SerialPIO(tx, rx) {
29+
if (invert) {
30+
panic("SoftwareSerial inverted operation not supported\n");
31+
}
32+
}
33+
void listen() { /* noop */ }
34+
bool isListening() {
35+
return true;
36+
}
37+
};

docs/piouart.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ For example, to make a transmit-only port on GP16
2121
2222
For detailed information about the Serial ports, see the
2323
Arduino `Serial Reference <https://www.arduino.cc/reference/en/language/functions/communication/serial/>`_ .
24+
25+
26+
SoftwareSerial Emulation
27+
========================
28+
A ``SoftwareSerial`` wrapper is included to provide plug-and-play compatibility
29+
with the Arduino `Software Serial <https://docs.arduino.cc/learn/built-in-libraries/software-serial>`_
30+
library. Use the normal ``#include <SoftwareSerial.h>`` to include it. The following
31+
differences from the Arduino standard are present:
32+
* Inverted mode is not supported
33+
* All ports are always listening. The ``listen`` call is a no-op, and ``isListening()`` always returns ``true`` .

0 commit comments

Comments
 (0)