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 +++++++++++++--------- examples/mspm0g3507/src/bin/mathacl_ops.rs | 13 +++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) 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) } } diff --git a/examples/mspm0g3507/src/bin/mathacl_ops.rs b/examples/mspm0g3507/src/bin/mathacl_ops.rs index 06265ae18..aeca96d2b 100644 --- a/examples/mspm0g3507/src/bin/mathacl_ops.rs +++ b/examples/mspm0g3507/src/bin/mathacl_ops.rs @@ -5,6 +5,7 @@ #![no_std] #![no_main] +use core::f32::consts::PI; use defmt::*; use embassy_executor::Spawner; use embassy_mspm0::mathacl::{Mathacl, Precision}; @@ -19,15 +20,15 @@ async fn main(_spawner: Spawner) -> ! { let mut macl = Mathacl::new(d.MATHACL); - // in range [-1,1) - let angle = 0.5; - match macl.sin(angle, Precision::High) { - Ok(res) => info!("sin({}) = {}", angle * 180.0, res), + // value radians [-PI; PI] + let rads = PI * 0.5; + match macl.sin(rads, Precision::High) { + Ok(res) => info!("sin({}) = {}", rads, res), Err(e) => error!("sin Error: {:?}", e), } - match macl.cos(angle, Precision::Medium) { - Ok(res) => info!("cos({}) = {}", angle * 180.0, res), + match macl.cos(rads, Precision::Medium) { + Ok(res) => info!("cos({}) = {}", rads, res), Err(e) => error!("cos Error: {:?}", e), } -- cgit