Add assume_noise_free to usart configuration

Effectively setting cr3.onebit
This commit is contained in:
Rasmus Melchior Jacobsen 2023-05-19 15:22:32 +02:00
parent 06f5c309c0
commit cd6256a924

View File

@ -120,6 +120,11 @@ pub struct Config {
/// read will abort, the error reported and cleared /// read will abort, the error reported and cleared
/// if false, the error is ignored and cleared /// if false, the error is ignored and cleared
pub detect_previous_overrun: bool, pub detect_previous_overrun: bool,
/// Set this to true if the line is considered noise free.
/// This will increase the receivers tolerance to clock deviations,
/// but will effectively disable noise detection.
pub assume_noise_free: bool,
} }
impl Default for Config { impl Default for Config {
@ -131,6 +136,7 @@ impl Default for Config {
parity: Parity::ParityNone, parity: Parity::ParityNone,
// historical behavior // historical behavior
detect_previous_overrun: false, detect_previous_overrun: false,
assume_noise_free: false,
} }
} }
} }
@ -832,7 +838,13 @@ fn configure(r: Regs, config: &Config, pclk_freq: Hertz, kind: Kind, enable_rx:
for &(presc, _presc_val) in &DIVS { for &(presc, _presc_val) in &DIVS {
let denom = (config.baudrate * presc as u32) as u64; let denom = (config.baudrate * presc as u32) as u64;
let div = (pclk_freq.0 as u64 * mul + (denom / 2)) / denom; let div = (pclk_freq.0 as u64 * mul + (denom / 2)) / denom;
trace!("USART: presc={} div={:08x}", presc, div); trace!(
"USART: presc={}, div=0x{:08x} (mantissa = {}, fraction = {})",
presc,
div,
div >> 4,
div & 0x0F
);
if div < brr_min { if div < brr_min {
#[cfg(not(usart_v1))] #[cfg(not(usart_v1))]
@ -863,6 +875,14 @@ fn configure(r: Regs, config: &Config, pclk_freq: Hertz, kind: Kind, enable_rx:
assert!(found, "USART: baudrate too low"); assert!(found, "USART: baudrate too low");
let brr = unsafe { r.brr().read().brr() as u32 };
trace!(
"Using {}, desired baudrate: {}, actual baudrate: {}",
if over8 { "OVER8" } else { "OVER16" },
config.baudrate,
pclk_freq.0 / brr
);
unsafe { unsafe {
r.cr2().write(|w| { r.cr2().write(|w| {
w.set_stop(match config.stop_bits { w.set_stop(match config.stop_bits {
@ -895,6 +915,10 @@ fn configure(r: Regs, config: &Config, pclk_freq: Hertz, kind: Kind, enable_rx:
#[cfg(not(usart_v1))] #[cfg(not(usart_v1))]
w.set_over8(vals::Over8(over8 as _)); w.set_over8(vals::Over8(over8 as _));
}); });
r.cr3().modify(|w| {
w.set_onebit(config.assume_noise_free);
});
} }
} }