From bc6d9072cec1c5e0b7d7d69ca584e03ed70a9140 Mon Sep 17 00:00:00 2001 From: Trevin Price <113460795+Trevin-Price@users.noreply.github.com> Date: Sun, 29 Sep 2024 14:36:30 -0400 Subject: [PATCH 1/2] Fixed lpuart baud compute bug. Added secondary test. --- src/common/lpuart.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/common/lpuart.rs b/src/common/lpuart.rs index d656b8ad..55109bd3 100644 --- a/src/common/lpuart.rs +++ b/src/common/lpuart.rs @@ -593,8 +593,8 @@ impl Baud { let mut best_sbr = 0; let mut osr = 8; - let mut sbr = 1; while osr <= 32 { + let mut sbr = 1; while sbr < 8192 { let b = source_clock_hz / (sbr * osr); let e = max(baud, b) - min(baud, b); @@ -1068,6 +1068,24 @@ mod tests { assert!(!BAUD.bothedge); } + #[test] + fn non_default_sbr_baud() { + // Assume the 24MHz XTAL clock. + const UART_CLOCK_HZ: u32 = 24_000_000; + // The best baud rate we can get is + const EXPECTED_BAUD: u32 = 9600; + // for a target baud of + const TARGET_BAUD: u32 = 9600; + + const BAUD: Baud = Baud::compute(UART_CLOCK_HZ, TARGET_BAUD); + + assert_eq!(BAUD.value(UART_CLOCK_HZ), EXPECTED_BAUD); + + assert_eq!(BAUD.osr, 10, "OSR: {}", BAUD.osr); + assert_eq!(BAUD.sbr, 250, "SBR: {}", BAUD.sbr); + assert!(!BAUD.bothedge); + } + #[test] fn read_data_flags() { let read_data = ReadData(1 << 15 | 1 << 13); From cb1132a2c34f71f5e4e65046bf4566849863cfa9 Mon Sep 17 00:00:00 2001 From: Trevin Price <113460795+Trevin-Price@users.noreply.github.com> Date: Sun, 29 Sep 2024 14:36:55 -0400 Subject: [PATCH 2/2] Increased max computable lpuart baud. Added associated test. --- src/common/lpuart.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/common/lpuart.rs b/src/common/lpuart.rs index 55109bd3..31a19b4a 100644 --- a/src/common/lpuart.rs +++ b/src/common/lpuart.rs @@ -592,7 +592,7 @@ impl Baud { let mut best_osr = 0; let mut best_sbr = 0; - let mut osr = 8; + let mut osr = if baud > 3_000_000 { 4 } else { 8 }; while osr <= 32 { let mut sbr = 1; while sbr < 8192 { @@ -1086,6 +1086,24 @@ mod tests { assert!(!BAUD.bothedge); } + #[test] + fn max_baud() { + // Assume the 24MHz XTAL clock. + const UART_CLOCK_HZ: u32 = 24_000_000; + // The best baud rate we can get is + const EXPECTED_BAUD: u32 = 6_000_000; + // for a target baud of + const TARGET_BAUD: u32 = 6_000_000; + + const BAUD: Baud = Baud::compute(UART_CLOCK_HZ, TARGET_BAUD); + + assert_eq!(BAUD.value(UART_CLOCK_HZ), EXPECTED_BAUD); + + assert_eq!(BAUD.osr, 4, "OSR: {}", BAUD.osr); + assert_eq!(BAUD.sbr, 1, "SBR: {}", BAUD.sbr); + assert!(BAUD.bothedge); + } + #[test] fn read_data_flags() { let read_data = ReadData(1 << 15 | 1 << 13);