Skip to content

Commit ae17ba7

Browse files
committed
Extend swsertest for swapping HW and SW serial on USB to PC
1 parent b4f1efd commit ae17ba7

File tree

1 file changed

+50
-29
lines changed

1 file changed

+50
-29
lines changed

examples/swsertest/swsertest.ino

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
// On ESP8266:
22
// At 80MHz runs up 57600ps, and at 160MHz CPU frequency up to 115200bps with only negligible errors.
3-
// Connect pin 12 to 14.
3+
// Connect pin 13 to 15.
4+
// For verification and as a example for how to use SW serial on the USB to PC connection,
5+
// which allows the use of HW Serial on GPIO13 and GPIO15 instead, #define SWAPSERIAL below.
6+
// Notice how the bitrates are also swapped then between RX/TX and GPIO13/GPIO15.
7+
// Builtin debug output etc. must be stopped on HW Serial in this case, as it would interfere with the
8+
// external communication on GPIO13/GPIO15.
49

510
#include <SoftwareSerial.h>
611

712
#ifndef D5
813
#if defined(ESP8266)
14+
#define D8 (15)
915
#define D5 (14)
10-
#define D6 (12)
1116
#define D7 (13)
12-
#define D8 (15)
17+
#define D6 (12)
18+
#define RX (3)
1319
#define TX (1)
1420
#elif defined(ESP32)
21+
#define D8 (5)
1522
#define D5 (18)
16-
#define D6 (19)
1723
#define D7 (23)
18-
#define D8 (5)
24+
#define D6 (19)
25+
#define RX (3)
1926
#define TX (1)
2027
#endif
2128
#endif
@@ -26,33 +33,47 @@
2633
#define BAUD_RATE 57600
2734
#endif
2835

29-
SoftwareSerial swSer;
36+
#undef SWAPSERIAL
37+
38+
#ifndef SWAPSERIAL
39+
auto& usbSerial = Serial;
40+
SoftwareSerial testSerial;
41+
#else
42+
SoftwareSerial usbSerial;
43+
auto& testSerial = Serial;
44+
#endif
3045

3146
void setup() {
32-
Serial.begin(115200);
33-
// Important: the buffer size optimizations here, in particular the isrBufSize (11) that is only sufficiently
34-
// large to hold a single word (up to start - 8 data - parity - stop), are on the basis that any char written
35-
// to the loopback SoftwareSerial adapter gets read before another write is performed.
36-
// Block writes with a size greater than 1 would usually fail. Do not copy this into your own project without
37-
// reading the documentation.
38-
swSer.begin(BAUD_RATE, SWSERIAL_8N1, D5, D6, false, 95, 11);
39-
40-
Serial.println(PSTR("\nSoftware serial test started"));
41-
42-
for (char ch = ' '; ch <= 'z'; ch++) {
43-
swSer.write(ch);
44-
}
45-
swSer.println();
47+
#ifndef SWAPSERIAL
48+
usbSerial.begin(115200);
49+
// Important: the buffer size optimizations here, in particular the isrBufSize (11) that is only sufficiently
50+
// large to hold a single word (up to start - 8 data - parity - stop), are on the basis that any char written
51+
// to the loopback SoftwareSerial adapter gets read before another write is performed.
52+
// Block writes with a size greater than 1 would usually fail. Do not copy this into your own project without
53+
// reading the documentation.
54+
testSerial.begin(BAUD_RATE, SWSERIAL_8N1, D7, D8, false, 95, 11);
55+
#else
56+
testSerial.begin(115200);
57+
testSerial.setDebugOutput(false);
58+
testSerial.swap();
59+
usbSerial.begin(BAUD_RATE, SWSERIAL_8N1, RX, TX, false, 95);
60+
#endif
61+
62+
usbSerial.println(PSTR("\nSoftware serial test started"));
63+
64+
for (char ch = ' '; ch <= 'z'; ch++) {
65+
testSerial.write(ch);
66+
}
67+
testSerial.println();
4668
}
4769

4870
void loop() {
49-
while (swSer.available() > 0) {
50-
Serial.write(swSer.read());
51-
yield();
52-
}
53-
while (Serial.available() > 0) {
54-
swSer.write(Serial.read());
55-
yield();
56-
}
57-
71+
while (testSerial.available() > 0) {
72+
usbSerial.write(testSerial.read());
73+
yield();
74+
}
75+
while (usbSerial.available() > 0) {
76+
testSerial.write(usbSerial.read());
77+
yield();
78+
}
5879
}

0 commit comments

Comments
 (0)