add usb power detection + check to ADC shutdown logic
This commit is contained in:
parent
8ea4b4401e
commit
c43cc5599e
2 changed files with 25 additions and 6 deletions
|
|
@ -310,6 +310,7 @@ pub struct Interfaces {
|
||||||
pub pwm_core: SimplePwmCore<'static, ch32_hal::peripherals::TIM1>,
|
pub pwm_core: SimplePwmCore<'static, ch32_hal::peripherals::TIM1>,
|
||||||
pub adc_core: crate::AdcCore,
|
pub adc_core: crate::AdcCore,
|
||||||
pub amp: crate::Amplifier,
|
pub amp: crate::Amplifier,
|
||||||
|
pub usb: crate::Usb,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
|
|
@ -400,12 +401,14 @@ impl App {
|
||||||
}
|
}
|
||||||
if self.timers.batt_adc_timer.need_service() {
|
if self.timers.batt_adc_timer.need_service() {
|
||||||
self.timers.batt_adc_timer.service();
|
self.timers.batt_adc_timer.service();
|
||||||
let bv = self.interfaces.adc_core.get_battery_voltage();
|
if !self.interfaces.usb.powered() {
|
||||||
let avg = self.interfaces.adc_core.get_average();
|
let bv = self.interfaces.adc_core.get_battery_voltage();
|
||||||
// #[cfg(feature = "enable_print")]
|
let avg = self.interfaces.adc_core.get_average();
|
||||||
// println!("batt adc service: {bv}, {avg}");
|
// #[cfg(feature = "enable_print")]
|
||||||
if avg < 421 {
|
// println!("batt adc service: {bv}, {avg}");
|
||||||
self.set_state(State::DeepSleep);
|
if avg < 421 {
|
||||||
|
self.set_state(State::DeepSleep);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.timers.usb_adc_timer.need_service() {
|
if self.timers.usb_adc_timer.need_service() {
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,19 @@ use crate::app::sequencer::{DynamicSequence, SequenceEntry};
|
||||||
|
|
||||||
static LED0_SEQ: [u8; 8] = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8];
|
static LED0_SEQ: [u8; 8] = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8];
|
||||||
|
|
||||||
|
pub struct Usb {
|
||||||
|
usb_pin: Input<'static>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Usb {
|
||||||
|
pub fn new(usb_pin: Input<'static>) -> Self {
|
||||||
|
Self { usb_pin }
|
||||||
|
}
|
||||||
|
pub fn powered(&self) -> bool {
|
||||||
|
self.usb_pin.is_high()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Amplifier {
|
pub struct Amplifier {
|
||||||
amp_en: Output<'static>,
|
amp_en: Output<'static>,
|
||||||
}
|
}
|
||||||
|
|
@ -276,6 +289,8 @@ fn app_main(mut p: hal::Peripherals) -> ! {
|
||||||
// adc2
|
// adc2
|
||||||
// let mut usb_detect_dc = hal::adc::Adc::new(p.ADC1, Default::default());
|
// let mut usb_detect_dc = hal::adc::Adc::new(p.ADC1, Default::default());
|
||||||
let mut usb_detect_pin = p.PD5;
|
let mut usb_detect_pin = p.PD5;
|
||||||
|
let usb_detect_input = Input::new(usb_detect_pin, Pull::Down);
|
||||||
|
let usb = Usb::new(usb_detect_input);
|
||||||
|
|
||||||
// println!("ADC_PIN CHANNEL: {}", adc_pin.channel().channel());
|
// println!("ADC_PIN CHANNEL: {}", adc_pin.channel().channel());
|
||||||
|
|
||||||
|
|
@ -360,6 +375,7 @@ fn app_main(mut p: hal::Peripherals) -> ! {
|
||||||
pwm_core,
|
pwm_core,
|
||||||
adc_core,
|
adc_core,
|
||||||
amp,
|
amp,
|
||||||
|
usb,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut app = App::new(app_config, app_services, app_sequences, app_interfaces);
|
let mut app = App::new(app_config, app_services, app_sequences, app_interfaces);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue