@@ -28,7 +28,7 @@ HardwareSerial Serial1(1);
28
28
HardwareSerial Serial2 (2 );
29
29
#endif
30
30
31
- HardwareSerial::HardwareSerial (int uart_nr) : _uart_nr(uart_nr), _uart(NULL ) {}
31
+ HardwareSerial::HardwareSerial (int uart_nr) : _uart_nr(uart_nr), _uart(NULL ), _baud( 0 ), _rx_pin(- 1 ), _tx_pin(- 1 ) {}
32
32
33
33
void HardwareSerial::begin (unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms)
34
34
{
@@ -52,23 +52,29 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
52
52
txPin = TX2;
53
53
}
54
54
55
- _uart = uartBegin (_uart_nr, baud ? baud : 9600 , config, rxPin, txPin, 256 , invert);
56
-
57
- if (!baud) {
58
- time_t startMillis = millis ();
59
- unsigned long detectedBaudRate = 0 ;
60
- while (millis () - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate (_uart))) {
61
- yield ();
62
- }
63
-
64
- end ();
65
-
66
- if (detectedBaudRate) {
67
- delay (100 ); // Give some time...
68
- _uart = uartBegin (_uart_nr, detectedBaudRate, config, rxPin, txPin, 256 , invert);
69
- } else {
70
- log_e (" Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible" );
71
- _uart = NULL ;
55
+ if ((_uart != NULL )&&(rxPin == _rx_pin)&&( txPin == _tx_pin)&&(baud !=0 )){ // just change baud rate,
56
+ uartSetBaudRate (_uart,baud);
57
+ log_i (" begin set baud only" );
58
+ } else { // completely reconfigure
59
+ _uart = uartBegin (_uart_nr, baud ? baud : 9600 , config, rxPin, txPin, 256 , invert);
60
+ // log_i("Init Uart[%d] baud=%d, rx=%d, tx=%d",_uart_nr,baud,rxPin,txPin);
61
+
62
+ if (!baud) {
63
+ time_t startMillis = millis ();
64
+ unsigned long detectedBaudRate = 0 ;
65
+ while (millis () - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate (_uart))) {
66
+ yield ();
67
+ }
68
+
69
+ end ();
70
+
71
+ if (detectedBaudRate) {
72
+ delay (100 ); // Give some time...
73
+ _uart = uartBegin (_uart_nr, detectedBaudRate, config, rxPin, txPin, 256 , invert);
74
+ } else {
75
+ log_e (" Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible" );
76
+ _uart = NULL ;
77
+ }
72
78
}
73
79
}
74
80
}
@@ -79,7 +85,7 @@ void HardwareSerial::end()
79
85
uartSetDebug (0 );
80
86
}
81
87
uartEnd (_uart);
82
- _uart = 0 ;
88
+ _uart = NULL ;
83
89
}
84
90
85
91
size_t HardwareSerial::setRxBufferSize (size_t new_size) {
@@ -111,23 +117,17 @@ int HardwareSerial::availableForWrite(void)
111
117
112
118
int HardwareSerial::peek (void )
113
119
{
114
- if (available ()) {
115
- return uartPeek (_uart);
116
- }
117
- return -1 ;
120
+ return uartPeek (_uart);
118
121
}
119
122
120
123
int HardwareSerial::read (void )
121
124
{
122
- if (available ()) {
123
- return uartRead (_uart);
124
- }
125
- return -1 ;
125
+ return uartRead (_uart);
126
126
}
127
127
128
128
void HardwareSerial::flush ()
129
129
{
130
- uartFlush (_uart);
130
+ uartDrain (_uart);
131
131
}
132
132
133
133
size_t HardwareSerial::write (uint8_t c)
@@ -141,11 +141,12 @@ size_t HardwareSerial::write(const uint8_t *buffer, size_t size)
141
141
uartWriteBuf (_uart, buffer, size);
142
142
return size;
143
143
}
144
- uint32_t HardwareSerial::baudRate ()
145
144
145
+ uint32_t HardwareSerial::baudRate ()
146
146
{
147
147
return uartGetBaudRate (_uart);
148
148
}
149
+
149
150
HardwareSerial::operator bool () const
150
151
{
151
152
return true ;
0 commit comments