Skip to content

Commit f35ec75

Browse files
matthijskooijmancmaglie
authored andcommitted
Remove duplicate code from HardwareSerial::begin() methods.
There are two begin methods, one which accepts just a baud rate and uses the default bit settings and one which accepts both a baudrate and a bit config. Previously, both of these contained a complete implementation, but now the former just calls the latter, explicitely passing the default 8N1 configuration. Technically, this causes a small change: Before the UCSRC register was untouched when calling begin(baud), now it is explicitely initialized with 8N1. However, since this is the default configuration for at least the Uno and the Mega (didn't check any others), probably for all avrs, this shouldn't effectively change anything. Given that the Arduino documentation also documents this as the default when none is passed, explicitly setting it is probably a good idea in any case.
1 parent c2e9860 commit f35ec75

File tree

1 file changed

+1
-38
lines changed

1 file changed

+1
-38
lines changed

hardware/arduino/avr/cores/arduino/HardwareSerial.cpp

+1-38
Original file line numberDiff line numberDiff line change
@@ -283,44 +283,7 @@ HardwareSerial::HardwareSerial(
283283

284284
void HardwareSerial::begin(unsigned long baud)
285285
{
286-
uint16_t baud_setting;
287-
bool use_u2x = true;
288-
289-
#if F_CPU == 16000000UL
290-
// hardcoded exception for compatibility with the bootloader shipped
291-
// with the Duemilanove and previous boards and the firmware on the 8U2
292-
// on the Uno and Mega 2560.
293-
if (baud == 57600) {
294-
use_u2x = false;
295-
}
296-
#endif
297-
298-
try_again:
299-
300-
if (use_u2x) {
301-
*_ucsra = 1 << _u2x;
302-
baud_setting = (F_CPU / 4 / baud - 1) / 2;
303-
} else {
304-
*_ucsra = 0;
305-
baud_setting = (F_CPU / 8 / baud - 1) / 2;
306-
}
307-
308-
if ((baud_setting > 4095) && use_u2x)
309-
{
310-
use_u2x = false;
311-
goto try_again;
312-
}
313-
314-
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
315-
*_ubrrh = baud_setting >> 8;
316-
*_ubrrl = baud_setting;
317-
318-
transmitting = false;
319-
320-
sbi(*_ucsrb, _rxen);
321-
sbi(*_ucsrb, _txen);
322-
sbi(*_ucsrb, _rxcie);
323-
cbi(*_ucsrb, _udrie);
286+
begin(baud, SERIAL_8N1);
324287
}
325288

326289
void HardwareSerial::begin(unsigned long baud, byte config)

0 commit comments

Comments
 (0)