Skip to content

Commit

Permalink
Extend swsertest for swapping HW and SW serial on USB to PC
Browse files Browse the repository at this point in the history
  • Loading branch information
dok-net committed Feb 20, 2021
1 parent b4f1efd commit ae17ba7
Showing 1 changed file with 50 additions and 29 deletions.
79 changes: 50 additions & 29 deletions examples/swsertest/swsertest.ino
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
// On ESP8266:
// At 80MHz runs up 57600ps, and at 160MHz CPU frequency up to 115200bps with only negligible errors.
// Connect pin 12 to 14.
// Connect pin 13 to 15.
// For verification and as a example for how to use SW serial on the USB to PC connection,
// which allows the use of HW Serial on GPIO13 and GPIO15 instead, #define SWAPSERIAL below.
// Notice how the bitrates are also swapped then between RX/TX and GPIO13/GPIO15.
// Builtin debug output etc. must be stopped on HW Serial in this case, as it would interfere with the
// external communication on GPIO13/GPIO15.

#include <SoftwareSerial.h>

#ifndef D5
#if defined(ESP8266)
#define D8 (15)
#define D5 (14)
#define D6 (12)
#define D7 (13)
#define D8 (15)
#define D6 (12)
#define RX (3)
#define TX (1)
#elif defined(ESP32)
#define D8 (5)
#define D5 (18)
#define D6 (19)
#define D7 (23)
#define D8 (5)
#define D6 (19)
#define RX (3)
#define TX (1)
#endif
#endif
Expand All @@ -26,33 +33,47 @@
#define BAUD_RATE 57600
#endif

SoftwareSerial swSer;
#undef SWAPSERIAL

#ifndef SWAPSERIAL
auto& usbSerial = Serial;
SoftwareSerial testSerial;
#else
SoftwareSerial usbSerial;
auto& testSerial = Serial;
#endif

void setup() {
Serial.begin(115200);
// Important: the buffer size optimizations here, in particular the isrBufSize (11) that is only sufficiently
// large to hold a single word (up to start - 8 data - parity - stop), are on the basis that any char written
// to the loopback SoftwareSerial adapter gets read before another write is performed.
// Block writes with a size greater than 1 would usually fail. Do not copy this into your own project without
// reading the documentation.
swSer.begin(BAUD_RATE, SWSERIAL_8N1, D5, D6, false, 95, 11);

Serial.println(PSTR("\nSoftware serial test started"));

for (char ch = ' '; ch <= 'z'; ch++) {
swSer.write(ch);
}
swSer.println();
#ifndef SWAPSERIAL
usbSerial.begin(115200);
// Important: the buffer size optimizations here, in particular the isrBufSize (11) that is only sufficiently
// large to hold a single word (up to start - 8 data - parity - stop), are on the basis that any char written
// to the loopback SoftwareSerial adapter gets read before another write is performed.
// Block writes with a size greater than 1 would usually fail. Do not copy this into your own project without
// reading the documentation.
testSerial.begin(BAUD_RATE, SWSERIAL_8N1, D7, D8, false, 95, 11);
#else
testSerial.begin(115200);
testSerial.setDebugOutput(false);
testSerial.swap();
usbSerial.begin(BAUD_RATE, SWSERIAL_8N1, RX, TX, false, 95);
#endif

usbSerial.println(PSTR("\nSoftware serial test started"));

for (char ch = ' '; ch <= 'z'; ch++) {
testSerial.write(ch);
}
testSerial.println();
}

void loop() {
while (swSer.available() > 0) {
Serial.write(swSer.read());
yield();
}
while (Serial.available() > 0) {
swSer.write(Serial.read());
yield();
}

while (testSerial.available() > 0) {
usbSerial.write(testSerial.read());
yield();
}
while (usbSerial.available() > 0) {
testSerial.write(usbSerial.read());
yield();
}
}

0 comments on commit ae17ba7

Please sign in to comment.