diff options
author | Matthijs Kooijman <matthijs@stdin.nl> | 2013-04-19 16:32:33 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2014-01-16 16:04:33 +0100 |
commit | fc45ef0846a426bbc15b1f74b2c70b04bb68e557 (patch) | |
tree | 27d77d753e84c89e2466e6cf83859272e26aec0a /cores/arduino | |
parent | d43fd2014c664d1af7fd71585f4716806288b546 (diff) |
Simplify HardwareSerial::begin()
This simplifies the baud rate calculation, removing the need for a goto
and shortening the code a bit. Other than that, this code should not use
any different settings than before.
Code was suggested by Rob Tillaart on github.
Closes: #1262
Diffstat (limited to 'cores/arduino')
-rw-r--r-- | cores/arduino/HardwareSerial.cpp | 35 |
1 files changed, 11 insertions, 24 deletions
diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 28a3ef6..d9059a9 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -283,33 +283,20 @@ HardwareSerial::HardwareSerial( void HardwareSerial::begin(unsigned long baud, byte config) { - uint16_t baud_setting; - 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 - -try_again: - - if (use_u2x) { - *_ucsra = 1 << _u2x; - baud_setting = (F_CPU / 4 / baud - 1) / 2; - } else { + // Try u2x mode first + uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2; + *_ucsra = 1 << _u2x; + + // hardcoded exception for 57600 for compatibility with the bootloader + // shipped with the Duemilanove and previous boards and the firmware + // on the 8U2 on the Uno and Mega 2560. Also, The baud_setting cannot + // be > 4095, so switch back to non-u2x mode if the baud rate is too + // low. + if (((F_CPU == 16000000UL) && (baud == 57600)) || (baud_setting >4095)) + { *_ucsra = 0; baud_setting = (F_CPU / 8 / baud - 1) / 2; } - - if ((baud_setting > 4095) && use_u2x) - { - use_u2x = false; - goto try_again; - } // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) *_ubrrh = baud_setting >> 8; |