diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 9257302..b25b4f1 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -194,22 +194,16 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, void HardwareSerial::begin(long baud) { uint16_t baud_setting; - bool use_u2x; + bool use_u2x = true; - // U2X mode is needed for baud rates higher than (CPU Hz / 16) - if (baud > F_CPU / 16) { - use_u2x = true; - } else { - // figure out if U2X mode would allow for a better connection - - // calculate the percent difference between the baud-rate specified and - // the real baud rate for both U2X and non-U2X mode (0-255 error percent) - uint8_t nonu2x_baud_error = abs((int)(255-((F_CPU/(16*(((F_CPU/8/baud-1)/2)+1))*255)/baud))); - uint8_t u2x_baud_error = abs((int)(255-((F_CPU/(8*(((F_CPU/4/baud-1)/2)+1))*255)/baud))); - - // prefer non-U2X mode because it handles clock skew better - use_u2x = (nonu2x_baud_error > u2x_baud_error); +#if F_CPU == 16000000UL + // hardcoded exception for compatibility with the bootloader shipped + // with the Duemilanove and previous boards and the firmware on the 8U2 + // on the Uno and Mega 2560. + if (baud == 57600) { + use_u2x = false; } +#endif if (use_u2x) { *_ucsra = 1 << _u2x;