-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More fine grained control of serial baudrate #80
Comments
I will try to start this. Probably I will have lot of questions...
|
Yes, it's this code: avr-hal/avr-hal-generic/src/serial.rs Lines 56 to 58 in bad9277
In comparison, Arduino upstream uses this code: // Try u2x mode first
uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;
*_ucsra = 1 << U2X0;
// 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;
}
// assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register)
*_ubrrh = baud_setting >> 8;
*_ubrrl = baud_setting;
No, I'd prefer putting that in
Sure, please don't hesitate asking if anything is unclear! |
Currently, the USART/Serial driver has a hardcoded algorithm for selecting the UBRR value that best fits a certain baudrate. This, however, has a number of problems: - Downstream code has no possibility to specify exact values if needed. - Board-specific workarounds cannot be transparently applied. To resolve these issues, add a new `Baudrate` type which represents a baudrate selection. It can easily be created from an integer using 115200.into_baudrate() (provided by `BaudrateExt`). But it also allows custom selection of exact values using the `Baudrate::with_exact()` constructor. For board-specific workarounds, the `.into_baudrate()` method is provided by a different extension trait (e.g. `BaudrateArduinoExt`). Ref: #80
Currently, the code only allows specifying a baudrate which the HAL-driver will try to match as closely as possible. The "generic" algorithm used for this will not always produce the most optimal settings, however. This means, for example, that 115200 baud on Arduino Uno is completely broken.
It would be nice to also allow setting exact baudrate parameters or/and providing alternate algorithms. There are quite a few ways to implement this, here are a few ideas:
A new
Baudrate
type with methods for the different modes:The
Usart
constructor now takesBaudrate<CLOCK>
instead ofu32
.Using an
Into<Baudrate<CLOCK>>
bound in theUsart
constructor. We also implementFrom<u32> for Baudrate<CLOCK>
. This would allow backwards compatibility with current code (but I'm not sure if that is a good thing ...).The text was updated successfully, but these errors were encountered: