embassy-nrf: Fix PDM gain register value derivation

Co-authored-by: Sol Harter <sol@glowinthedark.co.uk>
This commit is contained in:
Frank Plowman 2024-02-16 17:24:58 +00:00
parent 26fc17e8a7
commit bcb0be21c1
No known key found for this signature in database
GPG key ID: EE35607A4B0031A4

View file

@ -4,6 +4,7 @@
use core::future::poll_fn;
use core::marker::PhantomData;
use core::mem;
use core::sync::atomic::{compiler_fence, Ordering};
use core::task::Poll;
@ -145,15 +146,14 @@ impl<'d, T: Instance> Pdm<'d, T> {
}
fn _set_gain(r: &crate::pac::pdm::RegisterBlock, gain_left: I7F1, gain_right: I7F1) {
let gain_left = gain_left
.saturating_add(I7F1::from_bits(40))
.saturating_to_num::<u8>()
.clamp(0, 0x50);
let gain_right = gain_right
.saturating_add(I7F1::from_bits(40))
.saturating_to_num::<u8>()
.clamp(0, 0x50);
let gain_to_bits = |gain: I7F1| -> u8 {
let gain = gain.saturating_add(I7F1::from_bits(0x28))
.to_bits()
.clamp(0, 0x50);
unsafe { mem::transmute(gain) }
};
let gain_left = gain_to_bits(gain_left);
let gain_right = gain_to_bits(gain_right);
r.gainl.write(|w| unsafe { w.gainl().bits(gain_left) });
r.gainr.write(|w| unsafe { w.gainr().bits(gain_right) });
}