ld_pwr_exc_protector: Apply 32-pt avg to pd_v

- Reduce noise amplitude from STM32f4 ADC
This commit is contained in:
linuswck 2024-06-13 15:36:17 +08:00
parent d5a620c76b
commit c09da0db98
3 changed files with 29 additions and 1 deletions

View File

@ -157,6 +157,10 @@ impl LdDrive {
LdCurrentOutCtrlTimer::set_target_i_and_listen_irq(self.settings.ld_drive_current, self.ctrl.get_i_set()); LdCurrentOutCtrlTimer::set_target_i_and_listen_irq(self.settings.ld_drive_current, self.ctrl.get_i_set());
} }
pub fn poll_pd_mon_v(&mut self) -> ElectricPotential {
LdPwrExcProtector::poll_pd_mon_v()
}
pub fn poll_and_update_output_current(&mut self) -> ElectricCurrent { pub fn poll_and_update_output_current(&mut self) -> ElectricCurrent {
match LdCurrentOutCtrlTimer::get_irq_status() { match LdCurrentOutCtrlTimer::get_irq_status() {
Some(i_set) => { Some(i_set) => {

View File

@ -1,3 +1,4 @@
use num_traits::Zero;
use stm32f4xx_hal::{adc::{config::{self, AdcConfig}, use stm32f4xx_hal::{adc::{config::{self, AdcConfig},
Adc}, Adc},
gpio::{gpioa::PA3, gpiod::PD9, Analog, Output, PushPull}, gpio::{gpioa::PA3, gpiod::PD9, Analog, Output, PushPull},
@ -45,6 +46,8 @@ pub struct LdPwrExcProtector {
alarm_status: Status, alarm_status: Status,
calibrated_vdda: u32, calibrated_vdda: u32,
offset: u32, offset: u32,
prev_samples: [u16; 32],
ptr: u16,
} }
impl LdPwrExcProtector { impl LdPwrExcProtector {
@ -143,6 +146,8 @@ impl LdPwrExcProtector {
alarm_status: Status::default(), alarm_status: Status::default(),
calibrated_vdda: 3300, calibrated_vdda: 3300,
offset: offset, offset: offset,
prev_samples: [0; 32],
ptr: 0,
}); });
} }
} }
@ -177,9 +182,27 @@ impl LdPwrExcProtector {
} }
} }
pub fn poll_pd_mon_v() -> ElectricPotential {
if let Some(ref mut wdg) = LdPwrExcProtector::get() {
if wdg.pac.sr.read().eoc().bit() {
wdg.prev_samples[wdg.ptr as usize] = wdg.pac.dr.read().data().bits();
wdg.ptr = (wdg.ptr + 1) % 32 as u16;
let mut samples: u32 = 0;
for idx in 0..32 {
samples += wdg.prev_samples[idx] as u32;
}
samples = samples >> 5;
wdg.alarm_status.v = LdPwrExcProtector::convert_sample_to_volt(samples as u16);
}
return wdg.alarm_status.v;
}
ElectricPotential::zero()
}
pub fn get_status() -> Status { pub fn get_status() -> Status {
if let Some(ref mut wdg) = LdPwrExcProtector::get() { if let Some(ref mut wdg) = LdPwrExcProtector::get() {
wdg.alarm_status.v = LdPwrExcProtector::convert_sample_to_volt(wdg.pac.dr.read().data().bits()); LdPwrExcProtector::poll_pd_mon_v();
return wdg.alarm_status.clone(); return wdg.alarm_status.clone();
} }
Status::default() Status::default()

View File

@ -153,6 +153,7 @@ fn main() -> ! {
} }
State::MainLoop => { State::MainLoop => {
laser.poll_and_update_output_current(); laser.poll_and_update_output_current();
laser.poll_pd_mon_v();
net::net::eth_update_iface_poll_timer(); net::net::eth_update_iface_poll_timer();