aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSiarhei B <[email protected]>2025-11-16 11:59:57 +0100
committerSiarhei B <[email protected]>2025-11-16 11:59:57 +0100
commitd34dd3006dbcaff198c4e72469b5598dc3a8faa0 (patch)
tree5580552557330744283eacf8802927eb1ad5d36f
parentf236bb49301e3e726d60e0af8f6b998083b6215e (diff)
mspm0-mathacl: switch to radians as input param for sincos operations
-rw-r--r--embassy-mspm0/src/mathacl.rs22
-rw-r--r--examples/mspm0g3507/src/bin/mathacl_ops.rs13
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 @@
4 4
5#![macro_use] 5#![macro_use]
6 6
7use core::f32::consts::PI;
7use embassy_hal_internal::PeripheralType; 8use embassy_hal_internal::PeripheralType;
8use micromath::F32Ext; 9use micromath::F32Ext;
9 10
@@ -21,7 +22,7 @@ pub enum Precision {
21#[cfg_attr(feature = "defmt", derive(defmt::Format))] 22#[cfg_attr(feature = "defmt", derive(defmt::Format))]
22#[non_exhaustive] 23#[non_exhaustive]
23pub enum Error { 24pub enum Error {
24 AngleInWrongRange, 25 ValueInWrongRange,
25 NBitsTooBig, 26 NBitsTooBig,
26} 27}
27 28
@@ -53,17 +54,20 @@ impl Mathacl {
53 } 54 }
54 55
55 /// Internal helper SINCOS function. 56 /// Internal helper SINCOS function.
56 fn sincos(&mut self, angle: f32, precision: Precision, sin: bool) -> Result<f32, Error> { 57 fn sincos(&mut self, rad: f32, precision: Precision, sin: bool) -> Result<f32, Error> {
57 self.regs.ctl().write(|w| { 58 self.regs.ctl().write(|w| {
58 w.set_func(vals::Func::SINCOS); 59 w.set_func(vals::Func::SINCOS);
59 w.set_numiter(precision as u8); 60 w.set_numiter(precision as u8);
60 }); 61 });
61 62
62 if angle > 1.0 || angle < -1.0 { 63 if rad > PI || rad < -PI {
63 return Err(Error::AngleInWrongRange); 64 return Err(Error::ValueInWrongRange);
64 } 65 }
65 66
66 match signed_f32_to_register(angle, 0) { 67 // TODO: make f32 division on CPU
68 let native = rad / PI;
69
70 match signed_f32_to_register(native, 0) {
67 Ok(val) => self.regs.op1().write(|w| { 71 Ok(val) => self.regs.op1().write(|w| {
68 w.set_data(val); 72 w.set_data(val);
69 }), 73 }),
@@ -80,13 +84,13 @@ impl Mathacl {
80 } 84 }
81 85
82 /// Calsulates trigonometric sine operation in the range [-1,1) with a give precision. 86 /// Calsulates trigonometric sine operation in the range [-1,1) with a give precision.
83 pub fn sin(&mut self, angle: f32, precision: Precision) -> Result<f32, Error> { 87 pub fn sin(&mut self, rad: f32, precision: Precision) -> Result<f32, Error> {
84 self.sincos(angle, precision, true) 88 self.sincos(rad, precision, true)
85 } 89 }
86 90
87 /// Calsulates trigonometric cosine operation in the range [-1,1) with a give precision. 91 /// Calsulates trigonometric cosine operation in the range [-1,1) with a give precision.
88 pub fn cos(&mut self, angle: f32, precision: Precision) -> Result<f32, Error> { 92 pub fn cos(&mut self, rad: f32, precision: Precision) -> Result<f32, Error> {
89 self.sincos(angle, precision, false) 93 self.sincos(rad, precision, false)
90 } 94 }
91} 95}
92 96
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 @@
5#![no_std] 5#![no_std]
6#![no_main] 6#![no_main]
7 7
8use core::f32::consts::PI;
8use defmt::*; 9use defmt::*;
9use embassy_executor::Spawner; 10use embassy_executor::Spawner;
10use embassy_mspm0::mathacl::{Mathacl, Precision}; 11use embassy_mspm0::mathacl::{Mathacl, Precision};
@@ -19,15 +20,15 @@ async fn main(_spawner: Spawner) -> ! {
19 20
20 let mut macl = Mathacl::new(d.MATHACL); 21 let mut macl = Mathacl::new(d.MATHACL);
21 22
22 // in range [-1,1) 23 // value radians [-PI; PI]
23 let angle = 0.5; 24 let rads = PI * 0.5;
24 match macl.sin(angle, Precision::High) { 25 match macl.sin(rads, Precision::High) {
25 Ok(res) => info!("sin({}) = {}", angle * 180.0, res), 26 Ok(res) => info!("sin({}) = {}", rads, res),
26 Err(e) => error!("sin Error: {:?}", e), 27 Err(e) => error!("sin Error: {:?}", e),
27 } 28 }
28 29
29 match macl.cos(angle, Precision::Medium) { 30 match macl.cos(rads, Precision::Medium) {
30 Ok(res) => info!("cos({}) = {}", angle * 180.0, res), 31 Ok(res) => info!("cos({}) = {}", rads, res),
31 Err(e) => error!("cos Error: {:?}", e), 32 Err(e) => error!("cos Error: {:?}", e),
32 } 33 }
33 34