From 3d4e8477c1192d65b558548c0cd00f18cf33fd1d Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 4 Jul 2024 18:26:56 +0200 Subject: [PATCH] improve UART clock calculation --- va108xx-hal/CHANGELOG.md | 9 +++++++++ va108xx-hal/Cargo.toml | 1 - va108xx-hal/src/uart.rs | 18 ++++++++++++------ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/va108xx-hal/CHANGELOG.md b/va108xx-hal/CHANGELOG.md index a66e4c4..30161ef 100644 --- a/va108xx-hal/CHANGELOG.md +++ b/va108xx-hal/CHANGELOG.md @@ -6,6 +6,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [v0.7.0] 2024-07-04 + +- Replace `uarta` and `uartb` `Uart` constructors by `new` constructor +- Replace SPI `spia`, `spib` and `spic` constructors by `new` constructor +- Replace I2C `i2ca`, `i2cb` constructors by `new` constructor. Update constructor + to fail on invalid fast I2C speed system clock values +- Renamed `gpio::pins` to `gpio::pin` and `gpio::dynpins` to `gpio::dynpin` +- Simplify UART clock divider calculations and remove `libm` dependency consequently + ## [v0.6.0] 2024-06-16 - Updated `embedded-hal` to v1 diff --git a/va108xx-hal/Cargo.toml b/va108xx-hal/Cargo.toml index 415cbe4..17b286d 100644 --- a/va108xx-hal/Cargo.toml +++ b/va108xx-hal/Cargo.toml @@ -16,7 +16,6 @@ cortex-m-rt = "0.7" nb = "1" paste = "1" embedded-hal-nb = "1" -libm = "0.2" embedded-io = "0.6" fugit = "0.3" typenum = "1" diff --git a/va108xx-hal/src/uart.rs b/va108xx-hal/src/uart.rs index 136bacd..db1e081 100644 --- a/va108xx-hal/src/uart.rs +++ b/va108xx-hal/src/uart.rs @@ -7,7 +7,6 @@ use core::{marker::PhantomData, ops::Deref}; use embedded_hal_nb::serial::Read; use fugit::RateExtU32; -use libm::floorf; pub use crate::IrqCfg; use crate::{ @@ -363,12 +362,19 @@ impl UartBase { false => 16, true => 8, }; + + // This is the calculation: (64.0 * (x - integer_part as f32) + 0.5) as u32 without floating + // point calculations. + let frac = ((sys_clk.raw() % (config.baudrate.raw() * 16)) * 64 + + (config.baudrate.raw() * 8)) + / (config.baudrate.raw() * 16); + // Calculations here are derived from chapter 10.4.4 (p.74) of the datasheet. let x = sys_clk.raw() as f32 / (config.baudrate.raw() * baud_multiplier) as f32; - let integer_part = floorf(x) as u32; - let frac = floorf(64.0 * (x - integer_part as f32) + 0.5) as u32; - self.uart - .clkscale() - .write(|w| unsafe { w.bits(integer_part * 64 + frac) }); + let integer_part = x as u32; + self.uart.clkscale().write(|w| unsafe { + w.frac().bits(frac as u8); + w.int().bits(integer_part) + }); let (paren, pareven) = match config.parity { Parity::None => (false, false),