-
Notifications
You must be signed in to change notification settings - Fork 6
/
Port_ESPEasySerial_HardwareSerial.cpp
343 lines (290 loc) · 8.85 KB
/
Port_ESPEasySerial_HardwareSerial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "Port_ESPEasySerial_HardwareSerial.h"
#include "ESPEasySerialType.h"
#ifdef ESP32
// Temporary work-around for bug in ESP32 code, where the pin matrix is not cleaned up between calling end() and begin()
// Work-around is to keep track of the last used pins for a serial port,
// as it is likely that a node will always use the same pins most of the time.
// If not, a reboot may be OK to fix it.
// Another idea is to swap pins among UART ports.
// e.g. use the same pins on Serial1 if it was used on Serial2 before.
// PR to fix it: https://github.com/espressif/arduino-esp32/pull/5385
// See: https://github.com/espressif/arduino-esp32/issues/3878
static int receivePin0 = -1;
static int transmitPin0 = -1;
static int receivePin1 = -1;
static int transmitPin1 = -1;
static int receivePin2 = -1;
static int transmitPin2 = -1;
bool pinsChanged(ESPEasySerialPort port,
int receivePin,
int transmitPin)
{
switch (port) {
case ESPEasySerialPort::serial0: return receivePin != receivePin0 || transmitPin != transmitPin0;
# if SOC_UART_NUM > 1
case ESPEasySerialPort::serial1: return receivePin != receivePin1 || transmitPin != transmitPin1;
# endif // if SOC_UART_NUM > 1
# if SOC_UART_NUM > 2
case ESPEasySerialPort::serial2: return receivePin != receivePin2 || transmitPin != transmitPin2;
# endif // if SOC_UART_NUM > 2
default:
// No other hardware serial ports
break;
}
return false;
}
void setPinsCache(ESPEasySerialPort port,
int receivePin,
int transmitPin)
{
switch (port) {
case ESPEasySerialPort::serial0:
receivePin0 = receivePin;
transmitPin0 = transmitPin;
break;
# if SOC_UART_NUM > 1
case ESPEasySerialPort::serial1:
receivePin1 = receivePin;
transmitPin1 = transmitPin;
break;
# endif // if SOC_UART_NUM > 1
# if SOC_UART_NUM > 2
case ESPEasySerialPort::serial2:
receivePin2 = receivePin;
transmitPin2 = transmitPin;
break;
# endif // if SOC_UART_NUM > 2
default:
// No other hardware serial ports
break;
}
}
#endif // ifdef ESP32
Port_ESPEasySerial_HardwareSerial_t::Port_ESPEasySerial_HardwareSerial_t() {}
Port_ESPEasySerial_HardwareSerial_t::~Port_ESPEasySerial_HardwareSerial_t() {}
void Port_ESPEasySerial_HardwareSerial_t::resetConfig(const ESPEasySerialConfig& config)
{
if (!isHWserial(config.port)) { return; }
/*
if (_config == config) return;
// First call end()
// Then create new instance.
_config.receivePin = receivePin;
_config.transmitPin = transmitPin;
_config.inverse_logic = inverse_logic;
_config.rxBuffSize = buffSize;
_config.txBuffSize = buffSize;
*/
_config = config;
switch (config.port) {
case ESPEasySerialPort::serial0:
#if SOC_UART_NUM > 1
case ESPEasySerialPort::serial1:
#endif // if SOC_UART_NUM > 1
#if SOC_UART_NUM > 2
case ESPEasySerialPort::serial2:
#endif // if SOC_UART_NUM > 2
_config.port = config.port;
break;
default:
_config.port = ESPeasySerialType::getSerialType(config.port, config.receivePin, config.transmitPin);
}
if (_config.port == ESPEasySerialPort::serial0) {
#if defined(ESP32) && !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) && ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
_serial = &Serial0;
#else // if defined(ESP32) && !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) && ARDUINO_USB_CDC_ON_BOOT
_serial = &Serial;
#endif // if defined(ESP32) && !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) && ARDUINO_USB_CDC_ON_BOOT
#ifdef ESP8266
} else if (_config.port == ESPEasySerialPort::serial0_swap) {
_serial = &Serial;
#endif // ifdef ESP8266
#if SOC_UART_NUM > 1
} else if (_config.port == ESPEasySerialPort::serial1) {
_serial = &Serial1;
#endif // if SOC_UART_NUM > 1
#if SOC_UART_NUM > 2
} else if (_config.port == ESPEasySerialPort::serial2) {
_serial = &Serial2;
#endif // if SOC_UART_NUM > 2
} else {
_config.port = ESPEasySerialPort::not_set;
}
}
#ifdef ESP8266
void Port_ESPEasySerial_HardwareSerial_t::begin(unsigned long baud)
{
_config.baud = 0;
if (_serial == nullptr) {
return;
}
/* Order: TX/RX
* UART 0 possible options are (1, 3), (2, 3) or (15, 13)
* UART 1 allows only TX on 2 if UART 0 is not (2, 3)
*/
if (_config.port == ESPEasySerialPort::serial0_swap) {
_config.transmitPin = 15;
_config.receivePin = 13;
} else if (_config.port == ESPEasySerialPort::serial0) {
_config.transmitPin = 1;
_config.receivePin = 3;
} else if (_config.port == ESPEasySerialPort::serial1) {
_config.transmitPin = 2;
_config.receivePin = -1;
} else {
_config.transmitPin = -1;
_config.receivePin = -1;
return;
}
_config.baud = baud;
_serial->setRxBufferSize(_config.rxBuffSize);
_serial->begin(_config.baud, _config.config, _config.mode, _config.transmitPin, _config.inverse_logic);
_serial->pins(_config.transmitPin, _config.receivePin);
}
#endif // ifdef ESP8266
#ifdef ESP32
void Port_ESPEasySerial_HardwareSerial_t::begin(unsigned long baud)
{
if (_serial == nullptr) {
_config.baud = 0;
return;
}
// Timeout added for 1.0.1
// See: https://github.com/espressif/arduino-esp32/commit/233d31bed22211e8c85f82bcf2492977604bbc78
// getHW()->begin(baud, config, _config.receivePin, _config.transmitPin, invert, timeout_ms);
if (pinsChanged(_config.port, _config.receivePin, _config.transmitPin) || (_config.baud != baud)) {
setPinsCache(_config.port, _config.receivePin, _config.transmitPin);
_config.baud = baud;
// Allow to flush data from the serial buffers
// Otherwise the ESP may hang at boot.
delay(10);
_serial->end();
delay(10);
if (_config.rxBuffSize > 256) {
_config.rxBuffSize = _serial->setRxBufferSize(_config.rxBuffSize);
}
if (_config.txBuffSize > 256) {
_config.txBuffSize = _serial->setRxBufferSize(_config.txBuffSize);
}
_serial->begin(baud, _config.config, _config.receivePin, _config.transmitPin, _config.inverse_logic);
_serial->flush();
}
}
#endif // ifdef ESP32
void Port_ESPEasySerial_HardwareSerial_t::end() {
if (_serial != nullptr) {
_serial->end();
// FIXME TD-er: Should we restore the pins for ESP8266?
}
}
int Port_ESPEasySerial_HardwareSerial_t::available(void)
{
if (_serial != nullptr) {
return _serial->available();
}
return 0;
}
int Port_ESPEasySerial_HardwareSerial_t::availableForWrite(void)
{
if (_serial != nullptr) {
return _serial->availableForWrite();
}
return 0;
}
int Port_ESPEasySerial_HardwareSerial_t::peek(void)
{
if (_serial != nullptr) {
return _serial->peek();
}
return 0;
}
int Port_ESPEasySerial_HardwareSerial_t::read(void)
{
if (_serial != nullptr) {
return _serial->read();
}
return 0;
}
size_t Port_ESPEasySerial_HardwareSerial_t::read(uint8_t *buffer,
size_t size)
{
if (_serial != nullptr) {
#ifdef ESP32
return _serial->read(buffer, size);
#endif // ifdef ESP32
#ifdef ESP8266
return _serial->read((char *)buffer, size);
#endif // ifdef ESP8266
}
return 0;
}
void Port_ESPEasySerial_HardwareSerial_t::flush(void)
{
if (_serial != nullptr) {
_serial->flush();
}
}
void Port_ESPEasySerial_HardwareSerial_t::flush(bool txOnly)
{
if (_serial != nullptr) {
#ifdef ESP32
_serial->flush(txOnly);
#endif // ifdef ESP32
#ifdef ESP8266
_serial->flush();
#endif // ifdef ESP8266
}
}
size_t Port_ESPEasySerial_HardwareSerial_t::write(uint8_t value)
{
if (_serial != nullptr) {
return _serial->write(value);
}
return 0;
}
size_t Port_ESPEasySerial_HardwareSerial_t::write(const uint8_t *buffer,
size_t size)
{
if (_serial != nullptr) {
return _serial->write(buffer, size);
}
return 0;
}
int Port_ESPEasySerial_HardwareSerial_t::getBaudRate() const
{
if (_serial != nullptr) {
return _serial->baudRate();
}
return 0;
}
Port_ESPEasySerial_HardwareSerial_t::operator bool() const
{
if (_serial != nullptr) {
return _serial->operator bool();
}
return false;
}
void Port_ESPEasySerial_HardwareSerial_t::setDebugOutput(bool enabled) {
if (_serial != nullptr) {
return _serial->setDebugOutput(enabled);
}
}
size_t Port_ESPEasySerial_HardwareSerial_t::setRxBufferSize(size_t new_size)
{
if (_serial != nullptr) {
return _serial->setRxBufferSize(new_size);
}
return 0;
}
size_t Port_ESPEasySerial_HardwareSerial_t::setTxBufferSize(size_t new_size)
{
if (_serial != nullptr) {
#ifdef ESP32
return _serial->setTxBufferSize(new_size);
#endif // ifdef ESP32
#ifdef ESP8266
return new_size;
#endif // ifdef ESP8266
}
return 0;
}