|
1 | 1 | // On ESP8266:
|
2 | 2 | // 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. |
4 | 9 |
|
5 | 10 | #include <SoftwareSerial.h>
|
6 | 11 |
|
7 | 12 | #ifndef D5
|
8 | 13 | #if defined(ESP8266)
|
| 14 | +#define D8 (15) |
9 | 15 | #define D5 (14)
|
10 |
| -#define D6 (12) |
11 | 16 | #define D7 (13)
|
12 |
| -#define D8 (15) |
| 17 | +#define D6 (12) |
| 18 | +#define RX (3) |
13 | 19 | #define TX (1)
|
14 | 20 | #elif defined(ESP32)
|
| 21 | +#define D8 (5) |
15 | 22 | #define D5 (18)
|
16 |
| -#define D6 (19) |
17 | 23 | #define D7 (23)
|
18 |
| -#define D8 (5) |
| 24 | +#define D6 (19) |
| 25 | +#define RX (3) |
19 | 26 | #define TX (1)
|
20 | 27 | #endif
|
21 | 28 | #endif
|
|
26 | 33 | #define BAUD_RATE 57600
|
27 | 34 | #endif
|
28 | 35 |
|
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 |
30 | 45 |
|
31 | 46 | 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(); |
46 | 68 | }
|
47 | 69 |
|
48 | 70 | 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 | + } |
58 | 79 | }
|
0 commit comments