diff options
author | David A. Mellis <d.mellis@arduino.cc> | 2010-11-11 23:28:21 -0500 |
---|---|---|
committer | David A. Mellis <d.mellis@arduino.cc> | 2010-11-11 23:28:21 -0500 |
commit | 08102b63704bc0ce4e89e1a2ec46aaa35f0af10a (patch) | |
tree | 329d912433209654ae9a4ec594739067aaaa57ae /cores/arduino/HardwareSerial.cpp | |
parent | cba47898401c844449dade61bae0c43a6d1c05e6 (diff) |
Changing baud rate calculation to always use double speed mode except for 57600 baud at 16 MHz.
http://code.google.com/p/arduino/issues/detail?id=394
Diffstat (limited to 'cores/arduino/HardwareSerial.cpp')
-rw-r--r-- | cores/arduino/HardwareSerial.cpp | 24 |
1 files changed, 9 insertions, 15 deletions
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; - - // 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); + bool use_u2x = true; + +#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; |