forked from TD-er/ESPEasySerial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESPEasySerial_ESP32.cpp
408 lines (357 loc) · 8.9 KB
/
ESPEasySerial_ESP32.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <ESPeasySerial.h>
// ****************************************
// ESP32 implementation wrapper
// Only support HW serial on Serial 0 .. 2
// ****************************************
#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;
case ESPEasySerialPort::serial1: return receivePin != receivePin1 || transmitPin != transmitPin1;
#ifndef ESP32S2
case ESPEasySerialPort::serial2: return receivePin != receivePin2 || transmitPin != transmitPin2;
#endif
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;
case ESPEasySerialPort::serial1:
receivePin1 = receivePin;
transmitPin1 = transmitPin;
break;
#ifndef ESP32S2
case ESPEasySerialPort::serial2:
receivePin2 = receivePin;
transmitPin2 = transmitPin;
break;
#endif
default:
// No other hardware serial ports
break;
}
}
// End of messy work-around.
ESPeasySerial::ESPeasySerial(
ESPEasySerialPort port,
int receivePin,
int transmitPin,
bool inverse_logic,
unsigned int buffSize)
:
#ifndef DISABLE_SC16IS752_Serial
_i2cserial(nullptr),
#endif
_receivePin(receivePin),
_transmitPin(transmitPin),
_inverse_logic(inverse_logic)
{
resetConfig(port, receivePin, transmitPin, inverse_logic, buffSize);
}
ESPeasySerial::~ESPeasySerial() {
flush();
end();
#ifndef DISABLE_SC16IS752_Serial
if (_i2cserial != nullptr) {
delete _i2cserial;
}
#endif
}
void ESPeasySerial::resetConfig(
ESPEasySerialPort port,
int receivePin,
int transmitPin,
bool inverse_logic,
unsigned int buffSize)
{
#ifndef DISABLE_SC16IS752_Serial
if (_i2cserial != nullptr) {
_i2cserial->end();
delete _i2cserial;
}
#endif
#ifndef DISABLE_SC16IS752_Serial
_i2cserial = nullptr;
#endif
_receivePin = receivePin;
_transmitPin = transmitPin;
_inverse_logic = inverse_logic;
_buffSize = buffSize;
switch (port) {
case ESPEasySerialPort::serial0:
case ESPEasySerialPort::serial1:
#ifndef ESP32S2
case ESPEasySerialPort::serial2:
#endif
_serialtype = port;
break;
default:
_serialtype = ESPeasySerialType::getSerialType(port, receivePin, transmitPin);
}
#ifndef DISABLE_SC16IS752_Serial
switch (_serialtype) {
case ESPEasySerialPort::sc16is752:
{
ESPEasySC16IS752_Serial::I2C_address addr = static_cast<ESPEasySC16IS752_Serial::I2C_address>(receivePin);
ESPEasySC16IS752_Serial::SC16IS752_channel ch = static_cast<ESPEasySC16IS752_Serial::SC16IS752_channel>(transmitPin);
_i2cserial = new ESPEasySC16IS752_Serial(addr, ch);
break;
}
default:
break;
}
#endif
}
void ESPeasySerial::begin(unsigned long baud, uint32_t config
, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms) {
_baud = baud;
if (rxPin != -1) { _receivePin = rxPin; }
if (txPin != -1) { _transmitPin = txPin; }
_inverse_logic = invert;
if (!isValid()) {
_baud = 0;
return;
}
if (isI2Cserial()) {
#ifndef DISABLE_SC16IS752_Serial
if (_i2cserial != nullptr) {
_i2cserial->begin(baud);
}
#endif
} else {
// Make sure the extra bit is set for the config. The config differs between ESP32 and ESP82xx
config = config | 0x8000000;
if (isValid()) {
// Timeout added for 1.0.1
// See: https://github.com/espressif/arduino-esp32/commit/233d31bed22211e8c85f82bcf2492977604bbc78
// getHW()->begin(baud, config, _receivePin, _transmitPin, invert, timeout_ms);
if (pinsChanged(_serialtype, _receivePin, _transmitPin)) {
setPinsCache(_serialtype, _receivePin, _transmitPin);
getHW()->begin(baud, config, _receivePin, _transmitPin, _inverse_logic);
}
}
}
}
void ESPeasySerial::end() {
if (!isValid()) {
return;
}
flush();
if (isI2Cserial()) {
#ifndef DISABLE_SC16IS752_Serial
_i2cserial->end();
#endif
} else {
// Work-around to fix proper detach RX pin for older ESP32 core versions
// For now do not call end()
//getHW()->end();
}
}
HardwareSerial * ESPeasySerial::getHW() {
switch (_serialtype) {
case ESPEasySerialPort::serial0: return &Serial;
case ESPEasySerialPort::serial1: return &Serial1;
case ESPEasySerialPort::serial2:
#ifndef ESP32S2
return &Serial2;
#endif
default: break;
}
return nullptr;
}
const HardwareSerial * ESPeasySerial::getHW() const {
switch (_serialtype) {
case ESPEasySerialPort::serial0: return &Serial;
case ESPEasySerialPort::serial1: return &Serial1;
case ESPEasySerialPort::serial2:
#ifndef ESP32S2
return &Serial2;
#endif
default: break;
}
return nullptr;
}
bool ESPeasySerial::isValid() const {
switch (_serialtype) {
case ESPEasySerialPort::serial0:
case ESPEasySerialPort::serial1:
return true;
case ESPEasySerialPort::serial2:
#ifdef ESP32S2
return false;
#else
return true;
#endif
case ESPEasySerialPort::sc16is752:
#ifndef DISABLE_SC16IS752_Serial
return _i2cserial != nullptr;
#else
return false;
#endif
// FIXME TD-er: Must perform proper check for GPIO pins here.
default: break;
}
return false;
}
int ESPeasySerial::peek(void) {
if (!isValid()) {
return -1;
}
if (isI2Cserial()) {
#ifndef DISABLE_SC16IS752_Serial
return _i2cserial->peek();
#else
return false;
#endif
}
return getHW()->peek();
}
size_t ESPeasySerial::write(uint8_t val) {
if (!isValid()) {
return 0;
}
if (isI2Cserial()) {
#ifndef DISABLE_SC16IS752_Serial
return _i2cserial->write(val);
#else
return 0;
#endif
}
return getHW()->write(val);
}
size_t ESPeasySerial::write(const uint8_t *buffer, size_t size) {
if (!isValid() || !buffer) {
return 0;
}
if (isI2Cserial()) {
#ifndef DISABLE_SC16IS752_Serial
return _i2cserial->write(buffer, size);
#else
return 0;
#endif
}
return getHW()->write(buffer, size);
}
size_t ESPeasySerial::write(const char *buffer) {
if (!buffer) { return 0; }
return write(buffer, strlen_P(buffer));
}
int ESPeasySerial::read(void) {
if (!isValid()) {
return -1;
}
if (isI2Cserial()) {
#ifndef DISABLE_SC16IS752_Serial
return _i2cserial->read();
#else
return -1;
#endif
}
return getHW()->read();
}
int ESPeasySerial::available(void) {
if (!isValid()) {
return 0;
}
if (isI2Cserial()) {
#ifndef DISABLE_SC16IS752_Serial
return _i2cserial->available();
#else
return 0;
#endif
}
return getHW()->available();
}
int ESPeasySerial::availableForWrite(void) {
if (!isValid()) {
return 0;
}
if (isI2Cserial()) {
#ifndef DISABLE_SC16IS752_Serial
// FIXME TD-er: Implement availableForWrite
return 64; // _i2cserial->availableForWrite();
#else
return 0;
#endif
} else {
return getHW()->availableForWrite();
}
}
void ESPeasySerial::flush(void) {
if (!isValid()) {
return;
}
if (isI2Cserial()) {
#ifndef DISABLE_SC16IS752_Serial
_i2cserial->flush();
#endif
} else {
getHW()->flush();
}
}
int ESPeasySerial::baudRate(void) {
if (!isValid()) {
return 0;
}
if (isI2Cserial()) {
return _baud;
}
return getHW()->baudRate();
}
void ESPeasySerial::setDebugOutput(bool enable) {
if (!isValid() || isI2Cserial()) {
return;
}
getHW()->setDebugOutput(enable);
}
bool ESPeasySerial::isTxEnabled(void) {
if (!isValid()) {
return false;
}
if (isI2Cserial()) {
return true;
}
return _transmitPin != -1;
}
bool ESPeasySerial::isRxEnabled(void) {
if (!isValid()) {
return false;
}
if (isI2Cserial()) {
return true;
}
return _receivePin != -1;
}
// Not supported in ESP32, since only HW serial is used.
// Function included since it is used in some libraries.
bool ESPeasySerial::listen() {
return true;
}
#endif // ESP32