From db6aa1255a48f50eccc067e69a75ec286c70c9f9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 22 Feb 2026 03:33:23 +0800 Subject: [PATCH] rename fixed point converters Q20.12 and Q16.16 come as .h files in the BIO C library, so converters in Rust to shove data to and from the BIO in those formats are provided. --- libs/bio-lib/src/c/math_test/mod.rs | 6 ++-- libs/bio-lib/src/c/mod.rs | 44 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/libs/bio-lib/src/c/math_test/mod.rs b/libs/bio-lib/src/c/math_test/mod.rs index 96ff368c6..7e0fa137a 100644 --- a/libs/bio-lib/src/c/math_test/mod.rs +++ b/libs/bio-lib/src/c/math_test/mod.rs @@ -5,7 +5,7 @@ use bao1x_hal::bio::{Bio, CoreCsr}; use math_test::*; use utralib::utra::bio_bdma; -use super::Fp; +use crate::c::FpQ20_12; pub struct MathTest { bio_ss: Bio, @@ -68,7 +68,7 @@ impl MathTest { pub fn test_cos(&mut self) { for i in (0..360).step_by(4) { // self.bio_ss.debug(self.resource_grant.cores[0]); - let arg = Fp::from_int(i); + let arg = FpQ20_12::from_int(i); self.tx.csr.wo(bio_bdma::SFR_TXF0, arg.0 as u32); // let mut dbg_count: usize = 0; while self.rx.csr.rf(bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 { @@ -80,7 +80,7 @@ impl MathTest { */ } let result = self.rx.csr.r(bio_bdma::SFR_RXF1) as i32; // result should be 0..20 - let result_f32 = Fp::from_raw(result); + let result_f32 = FpQ20_12::from_raw(result); let mut line = String::new(); for _ in 0..((result_f32.to_float() * 20.0f32) as usize) { line.push(' '); diff --git a/libs/bio-lib/src/c/mod.rs b/libs/bio-lib/src/c/mod.rs index 098d72956..b899bfd32 100644 --- a/libs/bio-lib/src/c/mod.rs +++ b/libs/bio-lib/src/c/mod.rs @@ -1,2 +1,46 @@ +#[cfg(feature = "c-colorwheel")] +pub mod colorwheel; #[cfg(feature = "c-math-test")] pub mod math_test; + +/// 19-bit signed integer + 12-bit fraction +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct FpQ20_12(pub i32); + +impl FpQ20_12 { + pub fn frac_bits() -> u32 { 12 } + + pub fn frac_scale() -> i32 { 1 << Self::frac_bits() } + + pub fn from_int(x: i32) -> Self { Self(x << Self::frac_bits()) } + + pub fn from_float(x: f32) -> Self { Self((x * Self::frac_scale() as f32 + 0.5) as i32) } + + pub fn to_int(self) -> i32 { self.0 >> Self::frac_bits() } + + pub fn to_float(self) -> f32 { self.0 as f32 / Self::frac_scale() as f32 } + + /// Wrap a raw i32 coming directly off the hardware register. + pub fn from_raw(raw: i32) -> Self { Self(raw) } +} + +/// 15-bit signed integer + 16-bit fraction +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct FpQ16_16(pub i32); + +impl FpQ16_16 { + pub fn frac_bits() -> u32 { 16 } + + pub fn frac_scale() -> i32 { 1 << Self::frac_bits() } + + pub fn from_int(x: i32) -> Self { Self(x << Self::frac_bits()) } + + pub fn from_float(x: f32) -> Self { Self((x * Self::frac_scale() as f32 + 0.5) as i32) } + + pub fn to_int(self) -> i32 { self.0 >> Self::frac_bits() } + + pub fn to_float(self) -> f32 { self.0 as f32 / Self::frac_scale() as f32 } + + /// Wrap a raw i32 coming directly off the hardware register. + pub fn from_raw(raw: i32) -> Self { Self(raw) } +}