From d34dd3006dbcaff198c4e72469b5598dc3a8faa0 Mon Sep 17 00:00:00 2001 From: Siarhei B Date: Sun, 16 Nov 2025 11:59:57 +0100 Subject: mspm0-mathacl: switch to radians as input param for sincos operations --- embassy-mspm0/src/mathacl.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'embassy-mspm0') diff --git a/embassy-mspm0/src/mathacl.rs b/embassy-mspm0/src/mathacl.rs index 1b1c67a71..60e8cc5ed 100644 --- a/embassy-mspm0/src/mathacl.rs +++ b/embassy-mspm0/src/mathacl.rs @@ -4,6 +4,7 @@ #![macro_use] +use core::f32::consts::PI; use embassy_hal_internal::PeripheralType; use micromath::F32Ext; @@ -21,7 +22,7 @@ pub enum Precision { #[cfg_attr(feature = "defmt", derive(defmt::Format))] #[non_exhaustive] pub enum Error { - AngleInWrongRange, + ValueInWrongRange, NBitsTooBig, } @@ -53,17 +54,20 @@ impl Mathacl { } /// Internal helper SINCOS function. - fn sincos(&mut self, angle: f32, precision: Precision, sin: bool) -> Result { + fn sincos(&mut self, rad: f32, precision: Precision, sin: bool) -> Result { self.regs.ctl().write(|w| { w.set_func(vals::Func::SINCOS); w.set_numiter(precision as u8); }); - if angle > 1.0 || angle < -1.0 { - return Err(Error::AngleInWrongRange); + if rad > PI || rad < -PI { + return Err(Error::ValueInWrongRange); } - match signed_f32_to_register(angle, 0) { + // TODO: make f32 division on CPU + let native = rad / PI; + + match signed_f32_to_register(native, 0) { Ok(val) => self.regs.op1().write(|w| { w.set_data(val); }), @@ -80,13 +84,13 @@ impl Mathacl { } /// Calsulates trigonometric sine operation in the range [-1,1) with a give precision. - pub fn sin(&mut self, angle: f32, precision: Precision) -> Result { - self.sincos(angle, precision, true) + pub fn sin(&mut self, rad: f32, precision: Precision) -> Result { + self.sincos(rad, precision, true) } /// Calsulates trigonometric cosine operation in the range [-1,1) with a give precision. - pub fn cos(&mut self, angle: f32, precision: Precision) -> Result { - self.sincos(angle, precision, false) + pub fn cos(&mut self, rad: f32, precision: Precision) -> Result { + self.sincos(rad, precision, false) } } -- cgit