1
0
Fork 0

LdPwrExcProtector: Cleanup

This commit is contained in:
linuswck 2024-01-26 12:56:53 +08:00
parent f50505feaf
commit 6af0f992d5
1 changed files with 7 additions and 12 deletions

View File

@ -10,7 +10,6 @@ use uom::si::{
f64::ElectricPotential, f64::ElectricPotential,
ratio::ratio ratio::ratio
}; };
use crate::info;
// 12 bit Resolution // 12 bit Resolution
const MAX_SAMPLE: u16 = 4095; const MAX_SAMPLE: u16 = 4095;
@ -44,13 +43,11 @@ impl Default for Status {
} }
} }
} }
// power excursion protection
// LdPwrExcProtector
pub struct LdPwrExcProtector { pub struct LdPwrExcProtector {
pac: ADC2, pac: ADC2,
phy: LdPwrExcProtectorPhy, phy: LdPwrExcProtectorPhy,
alarm_status: Status, alarm_status: Status,
//Calibrated VDDA in millivolt from Adc<ADC1>.calibrate()
calibrated_vdda: u32, calibrated_vdda: u32,
} }
@ -103,13 +100,13 @@ impl LdPwrExcProtector {
pac_adc.sqr3.write(|w| w pac_adc.sqr3.write(|w| w
.sq1().variant(PD_MON_ADC_CH_ID) .sq1().variant(PD_MON_ADC_CH_ID)
); );
// Set all sampling channel to have fastest sampling interval // Set all sampling channels to have fastest sampling interval
pac_adc.smpr1.reset(); pac_adc.smpr1.reset();
pac_adc.smpr2.reset(); pac_adc.smpr2.reset();
// Set the high threshold to be max value initially // Set the higher threshold to be max value initially
pac_adc.htr.write(|w| w.ht().variant(MAX_SAMPLE)); pac_adc.htr.write(|w| w.ht().variant(MAX_SAMPLE));
// Set the low threshold to be min value initially // Set the lower threshold to be min value initially
pac_adc.ltr.write(|w| w.lt().variant(0)); pac_adc.ltr.write(|w| w.lt().variant(0));
// SWStart should only be set when ADON = 1. Otherwise no conversion is launched. // SWStart should only be set when ADON = 1. Otherwise no conversion is launched.
@ -117,7 +114,6 @@ impl LdPwrExcProtector {
.swstart().set_bit() .swstart().set_bit()
); );
// Turn LD Power Off by default
phy.pwr_en_ch0.set_low(); phy.pwr_en_ch0.set_low();
unsafe { unsafe {
@ -147,7 +143,6 @@ impl LdPwrExcProtector {
if let Some(ref mut wdg ) = LdPwrExcProtector::get() { if let Some(ref mut wdg ) = LdPwrExcProtector::get() {
let code: u32 = ((htr / (ElectricPotential::new::<millivolt>(wdg.calibrated_vdda as f64))).get::<ratio>() * (MAX_SAMPLE as f64)) as u32; let code: u32 = ((htr / (ElectricPotential::new::<millivolt>(wdg.calibrated_vdda as f64))).get::<ratio>() * (MAX_SAMPLE as f64)) as u32;
wdg.pac.htr.write(|w| unsafe {w.bits(code)}); wdg.pac.htr.write(|w| unsafe {w.bits(code)});
info!("trigger_threshold_v: {:?}", code);
} }
} }
@ -220,7 +215,7 @@ impl LdPwrExcProtector {
} }
} }
fn power_excursion_handler(){ fn pwr_excursion_handler(){
if let Some(ref mut wdg ) = LdPwrExcProtector::get() { if let Some(ref mut wdg ) = LdPwrExcProtector::get() {
let sample = wdg.pac.dr.read().data().bits(); let sample = wdg.pac.dr.read().data().bits();
LdPwrExcProtector::pwr_off(); LdPwrExcProtector::pwr_off();
@ -233,8 +228,8 @@ impl LdPwrExcProtector {
#[interrupt] #[interrupt]
fn ADC(){ fn ADC(){
cortex_m::interrupt::free(|_| { cortex_m::interrupt::free(|_| {
LdPwrExcProtector::power_excursion_handler(); LdPwrExcProtector::pwr_excursion_handler();
// Disable interrupt to avoid getting stuck in infinite interrupt loop // Disable interrupt to avoid getting stuck in infinite loop
LdPwrExcProtector::disable_watchdog_interrupt(); LdPwrExcProtector::disable_watchdog_interrupt();
LdPwrExcProtector::clear_interrupt_bit(); LdPwrExcProtector::clear_interrupt_bit();
} }