23
23
#define ARDUINO_SERIAL_EVENT_TASK_RUNNING_CORE -1
24
24
#endif
25
25
26
+ #if (SOC_UART_LP_NUM >= 1)
27
+ #define UART_HW_FIFO_LEN (uart_num ) ((uart_num < SOC_UART_HP_NUM) ? SOC_UART_FIFO_LEN : SOC_LP_UART_FIFO_LEN)
28
+ #else
29
+ #define UART_HW_FIFO_LEN (uart_num ) SOC_UART_FIFO_LEN
30
+ #endif
31
+
26
32
void serialEvent (void ) __attribute__((weak));
27
33
28
- #if SOC_UART_HP_NUM > 1
34
+ #if SOC_UART_NUM > 1
29
35
void serialEvent1 (void ) __attribute__((weak));
30
- #endif /* SOC_UART_HP_NUM > 1 */
36
+ #endif /* SOC_UART_NUM > 1 */
31
37
32
- #if SOC_UART_HP_NUM > 2
38
+ #if SOC_UART_NUM > 2
33
39
void serialEvent2 (void ) __attribute__((weak));
34
- #endif /* SOC_UART_HP_NUM > 2 */
40
+ #endif /* SOC_UART_NUM > 2 */
35
41
36
- #if SOC_UART_HP_NUM > 3
42
+ #if SOC_UART_NUM > 3
37
43
void serialEvent3 (void ) __attribute__((weak));
38
- #endif /* SOC_UART_HP_NUM > 3 */
44
+ #endif /* SOC_UART_NUM > 3 */
39
45
40
- #if SOC_UART_HP_NUM > 4
46
+ #if SOC_UART_NUM > 4
41
47
void serialEvent4 (void ) __attribute__((weak));
42
- #endif /* SOC_UART_HP_NUM > 4 */
48
+ #endif /* SOC_UART_NUM > 4 */
49
+
50
+ #if SOC_UART_NUM > 5
51
+ void serialEvent5 (void ) __attribute__((weak));
52
+ #endif /* SOC_UART_NUM > 5 */
43
53
44
54
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
45
55
// There is always Seria0 for UART0
46
56
HardwareSerial Serial0 (0 );
47
- #if SOC_UART_HP_NUM > 1
57
+ #if SOC_UART_NUM > 1
48
58
HardwareSerial Serial1 (1 );
49
59
#endif
50
- #if SOC_UART_HP_NUM > 2
60
+ #if SOC_UART_NUM > 2
51
61
HardwareSerial Serial2 (2 );
52
62
#endif
53
- #if SOC_UART_HP_NUM > 3
63
+ #if SOC_UART_NUM > 3
54
64
HardwareSerial Serial3 (3 );
55
65
#endif
56
- #if SOC_UART_HP_NUM > 4
66
+ #if SOC_UART_NUM > 4
57
67
HardwareSerial Serial4 (4 );
58
68
#endif
59
-
69
+ #if (SOC_UART_NUM > 5)
70
+ HardwareSerial Serial5 (5 );
71
+ #endif
60
72
#if HWCDC_SERIAL_IS_DEFINED == 1 // Hardware JTAG CDC Event
61
73
extern void HWCDCSerialEvent (void ) __attribute__((weak));
62
74
#endif
@@ -81,26 +93,31 @@ void serialEventRun(void) {
81
93
if (serialEvent && Serial0.available ()) {
82
94
serialEvent ();
83
95
}
84
- #if SOC_UART_HP_NUM > 1
96
+ #if SOC_UART_NUM > 1
85
97
if (serialEvent1 && Serial1.available ()) {
86
98
serialEvent1 ();
87
99
}
88
100
#endif
89
- #if SOC_UART_HP_NUM > 2
101
+ #if SOC_UART_NUM > 2
90
102
if (serialEvent2 && Serial2.available ()) {
91
103
serialEvent2 ();
92
104
}
93
105
#endif
94
- #if SOC_UART_HP_NUM > 3
106
+ #if SOC_UART_NUM > 3
95
107
if (serialEvent3 && Serial3.available ()) {
96
108
serialEvent3 ();
97
109
}
98
110
#endif
99
- #if SOC_UART_HP_NUM > 4
111
+ #if SOC_UART_NUM > 4
100
112
if (serialEvent4 && Serial4.available ()) {
101
113
serialEvent4 ();
102
114
}
103
115
#endif
116
+ #if SOC_UART_NUM > 5
117
+ if (serialEvent5 && Serial5.available ()) {
118
+ serialEvent5 ();
119
+ }
120
+ #endif
104
121
}
105
122
#endif
106
123
@@ -185,7 +202,8 @@ void HardwareSerial::onReceive(OnReceiveCb function, bool onlyOnTimeout) {
185
202
186
203
// in case that onReceive() shall work only with RX Timeout, FIFO shall be high
187
204
// this is a work around for an IDF issue with events and low FIFO Full value (< 3)
188
- if (_onReceiveTimeout) {
205
+ // Not valid for the LP UART
206
+ if (_onReceiveTimeout && _uart_nr < SOC_UART_HP_NUM) {
189
207
uartSetRxFIFOFull (_uart, 120 );
190
208
log_w (" OnReceive is set to Timeout only, thus FIFO Full is now 120 bytes." );
191
209
}
@@ -207,12 +225,13 @@ bool HardwareSerial::setRxFIFOFull(uint8_t fifoBytes) {
207
225
HSERIAL_MUTEX_LOCK ();
208
226
// in case that onReceive() shall work only with RX Timeout, FIFO shall be high
209
227
// this is a work around for an IDF issue with events and low FIFO Full value (< 3)
210
- if (_onReceiveCB != NULL && _onReceiveTimeout) {
228
+ // Not valid for the LP UART
229
+ if (_onReceiveCB != NULL && _onReceiveTimeout && _uart_nr < SOC_UART_HP_NUM) {
211
230
fifoBytes = 120 ;
212
231
log_w (" OnReceive is set to Timeout only, thus FIFO Full is now 120 bytes." );
213
232
}
214
233
bool retCode = uartSetRxFIFOFull (_uart, fifoBytes); // Set new timeout
215
- if (fifoBytes > 0 && fifoBytes < SOC_UART_FIFO_LEN - 1 ) {
234
+ if (fifoBytes > 0 && fifoBytes < UART_HW_FIFO_LEN (_uart_nr) - 1 ) {
216
235
_rxFIFOFull = fifoBytes;
217
236
}
218
237
HSERIAL_MUTEX_UNLOCK ();
@@ -298,8 +317,8 @@ void HardwareSerial::_uartEventTask(void *args) {
298
317
}
299
318
300
319
void HardwareSerial::begin (unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms, uint8_t rxfifo_full_thrhd) {
301
- if (_uart_nr >= SOC_UART_HP_NUM ) {
302
- log_e (" Serial number is invalid, please use a number from 0 to %u" , SOC_UART_HP_NUM - 1 );
320
+ if (_uart_nr >= SOC_UART_NUM ) {
321
+ log_e (" Serial number is invalid, please use a number from 0 to %u" , SOC_UART_NUM - 1 );
303
322
return ;
304
323
}
305
324
@@ -333,16 +352,16 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
333
352
txPin = _txPin < 0 ? (int8_t )SOC_TX0 : _txPin;
334
353
}
335
354
break ;
336
- #if SOC_UART_HP_NUM > 1 // may save some flash bytes...
355
+ #if SOC_UART_HP_NUM > 1
337
356
case UART_NUM_1:
338
357
if (rxPin < 0 && txPin < 0 ) {
339
358
// do not change RX1/TX1 if it has already been set before
340
359
rxPin = _rxPin < 0 ? (int8_t )RX1 : _rxPin;
341
360
txPin = _txPin < 0 ? (int8_t )TX1 : _txPin;
342
361
}
343
362
break ;
344
- #endif
345
- #if SOC_UART_HP_NUM > 2 // may save some flash bytes...
363
+ #endif // UART_NUM_1
364
+ #if SOC_UART_HP_NUM > 2
346
365
case UART_NUM_2:
347
366
if (rxPin < 0 && txPin < 0 ) {
348
367
// do not change RX2/TX2 if it has already been set before
@@ -354,11 +373,11 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
354
373
#endif
355
374
}
356
375
break ;
357
- #endif
358
- #if SOC_UART_HP_NUM > 3 // may save some flash bytes...
376
+ #endif // UART_NUM_2
377
+ #if SOC_UART_HP_NUM > 3
359
378
case UART_NUM_3:
360
379
if (rxPin < 0 && txPin < 0 ) {
361
- // do not change RX2/TX2 if it has already been set before
380
+ // do not change RX3/TX3 if it has already been set before
362
381
#ifdef RX3
363
382
rxPin = _rxPin < 0 ? (int8_t )RX3 : _rxPin;
364
383
#endif
@@ -367,11 +386,11 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
367
386
#endif
368
387
}
369
388
break ;
370
- #endif
371
- #if SOC_UART_HP_NUM > 4 // may save some flash bytes...
389
+ #endif // UART_NUM_3
390
+ #if SOC_UART_HP_NUM > 4
372
391
case UART_NUM_4:
373
392
if (rxPin < 0 && txPin < 0 ) {
374
- // do not change RX2/TX2 if it has already been set before
393
+ // do not change RX4/TX4 if it has already been set before
375
394
#ifdef RX4
376
395
rxPin = _rxPin < 0 ? (int8_t )RX4 : _rxPin;
377
396
#endif
@@ -380,7 +399,20 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
380
399
#endif
381
400
}
382
401
break ;
402
+ #endif // UART_NUM_4
403
+ #if (SOC_UART_LP_NUM >= 1)
404
+ case LP_UART_NUM_0:
405
+ if (rxPin < 0 && txPin < 0 ) {
406
+ // do not change RX0_LP/TX0_LP if it has already been set before
407
+ #ifdef LP_RX0
408
+ rxPin = _rxPin < 0 ? (int8_t )LP_RX0 : _rxPin;
409
+ #endif
410
+ #ifdef LP_TX0
411
+ txPin = _txPin < 0 ? (int8_t )LP_TX0 : _txPin;
383
412
#endif
413
+ }
414
+ break ;
415
+ #endif // LP_UART_NUM_0
384
416
}
385
417
}
386
418
@@ -445,7 +477,8 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
445
477
if (!_rxFIFOFull) { // it has not being changed before calling begin()
446
478
// set a default FIFO Full value for the IDF driver
447
479
uint8_t fifoFull = 1 ;
448
- if (baud > 57600 || (_onReceiveCB != NULL && _onReceiveTimeout)) {
480
+ // if baud rate is higher than 57600 or onReceive() is set, it will set FIFO Full to 120 bytes, except for LP UART
481
+ if (_uart_nr < SOC_UART_HP_NUM && (baud > 57600 || (_onReceiveCB != NULL && _onReceiveTimeout))) {
449
482
fifoFull = 120 ;
450
483
}
451
484
uartSetRxFIFOFull (_uart, fifoFull);
@@ -477,6 +510,12 @@ void HardwareSerial::setDebugOutput(bool en) {
477
510
if (_uart == 0 ) {
478
511
return ;
479
512
}
513
+ #if (SOC_UART_LP_NUM >= 1)
514
+ if (_uart_nr >= SOC_UART_HP_NUM) {
515
+ log_e (" LP UART does not support Debug Output." );
516
+ return ;
517
+ }
518
+ #endif
480
519
if (en) {
481
520
uartSetDebug (_uart);
482
521
} else {
@@ -581,34 +620,37 @@ bool HardwareSerial::setMode(SerialMode mode) {
581
620
}
582
621
583
622
// minimum total RX Buffer size is the UART FIFO space (128 bytes for most SoC) + 1. IDF imposition.
623
+ // LP UART has FIFO of 16 bytes
584
624
size_t HardwareSerial::setRxBufferSize (size_t new_size) {
585
625
586
626
if (_uart) {
587
627
log_e (" RX Buffer can't be resized when Serial is already running. Set it before calling begin()." );
588
628
return 0 ;
589
629
}
590
-
591
- if (new_size <= SOC_UART_FIFO_LEN) {
592
- log_w (" RX Buffer set to minimum value: %d." , SOC_UART_FIFO_LEN + 1 ); // ESP32, S2, S3 and C3 means higher than 128
593
- new_size = SOC_UART_FIFO_LEN + 1 ;
630
+ uint8_t FIFOLen = UART_HW_FIFO_LEN (_uart_nr);
631
+ // Valid value is higher than the FIFO length
632
+ if (new_size <= FIFOLen) {
633
+ new_size = FIFOLen + 1 ;
634
+ log_w (" RX Buffer set to minimum value: %d." , new_size);
594
635
}
595
636
596
637
_rxBufferSize = new_size;
597
638
return _rxBufferSize;
598
639
}
599
640
600
- // minimum total TX Buffer size is the UART FIFO space (128 bytes for most SoC).
641
+ // minimum total TX Buffer size is the UART FIFO space (128 bytes for most SoC) + 1.
642
+ // LP UART has FIFO of 16 bytes
601
643
size_t HardwareSerial::setTxBufferSize (size_t new_size) {
602
644
603
645
if (_uart) {
604
646
log_e (" TX Buffer can't be resized when Serial is already running. Set it before calling begin()." );
605
647
return 0 ;
606
648
}
607
-
608
- if (new_size <= SOC_UART_FIFO_LEN) {
609
- log_w ( " TX Buffer set to minimum value: %d. " , SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
610
- _txBufferSize = 0 ; // it will use just UART FIFO with SOC_UART_FIFO_LEN bytes (128 for most SoC)
611
- return SOC_UART_FIFO_LEN ;
649
+ uint8_t FIFOLen = UART_HW_FIFO_LEN (_uart_nr);
650
+ // Valid values are zero or higher than the FIFO length
651
+ if (new_size > 0 && new_size <= FIFOLen) {
652
+ new_size = FIFOLen + 1 ;
653
+ log_w ( " TX Buffer set to minimum value: %d. " , new_size) ;
612
654
}
613
655
// if new_size is higher than SOC_UART_FIFO_LEN, TX Ringbuffer will be active and it will be used to report back "availableToWrite()"
614
656
_txBufferSize = new_size;
0 commit comments