aboutsummaryrefslogtreecommitdiff
path: root/embassy-mspm0
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 /embassy-mspm0
parentf236bb49301e3e726d60e0af8f6b998083b6215e (diff)
mspm0-mathacl: switch to radians as input param for sincos operations
Diffstat (limited to 'embassy-mspm0')
-rw-r--r--embassy-mspm0/src/mathacl.rs22
1 files changed, 13 insertions, 9 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