first working deep sleep entry with all peripherals enabled prior

This commit is contained in:
sigil-03 2025-11-15 14:56:22 -07:00
parent cd91b3f540
commit b6fa0e3b2d
4 changed files with 164 additions and 52 deletions

@ -1 +1 @@
Subproject commit f41336744c4e2548c8f6ba9b323ae4aa39959f1d
Subproject commit 4f11d68e62dcb0e7098eecf357168724a8322d80

View file

@ -551,16 +551,20 @@ impl App {
pub fn shut_down(&mut self) {
self.interfaces
.pwm_core
.write_amplitude(self.services.led0.channel, 0);
.disable(ch32_hal::timer::Channel::Ch1);
self.interfaces
.pwm_core
.write_amplitude(self.services.led1.channel, 0);
// self.interfaces
// .pwm_core
// .write_amplitude(self.services.led2.channel, 0);
.disable(ch32_hal::timer::Channel::Ch2);
self.interfaces
.pwm_core
.disable(ch32_hal::timer::Channel::Ch3);
self.interfaces
.pwm_core
.disable(ch32_hal::timer::Channel::Ch4);
self.interfaces.pwm_core.pwm.borrow().shutdown();
self.interfaces.adc_core.shutdown();
self.interfaces.amp.disable();
}
pub fn volume_button(&mut self) {

View file

@ -11,7 +11,7 @@ use crate::insert_coin::services::{DacService, LedService, Service, TickService,
// static LED1_DCS: [u8; 5] = [0u8, 25u8, 50u8, 75u8, 100u8];
pub struct SimplePwmCore<'d, T: GeneralInstance16bit> {
pwm: core::cell::RefCell<SimplePwm<'d, T>>,
pub pwm: core::cell::RefCell<SimplePwm<'d, T>>,
}
impl<'d, T: GeneralInstance16bit> SimplePwmCore<'d, T> {

View file

@ -126,6 +126,10 @@ impl AdcCore {
}
sum / self.batt_values.len() as u16
}
pub fn shutdown(&mut self) {
self.adc.shutdown()
}
}
#[derive(Debug)]
@ -244,7 +248,7 @@ bind_interrupts!(struct Irqs {
use insert_coin::TickTimerService;
use insert_coin::{TickService, TickServiceData};
fn app_main(mut p: hal::Peripherals) -> ! {
fn app_main(mut p: hal::Peripherals) {
// === output setup ===
// LED0 output setup
@ -309,8 +313,8 @@ fn app_main(mut p: hal::Peripherals) -> ! {
let volume_btn_pin = p.PC6;
let light_ctrl_btn_pin = p.PC7;
let amp_en = p.PC5;
let extra_io_1 = p.PD0;
let extra_io_2 = p.PD3;
// let extra_io_1 = p.PD0;
// let extra_io_2 = p.PD3;
let mut amp_en_output = OutputOpenDrain::new(amp_en, Level::Low, Default::default());
let amp = Amplifier::new(amp_en_output);
@ -340,6 +344,7 @@ fn app_main(mut p: hal::Peripherals) -> ! {
led0_timer_ms: 100,
led1_timer_ms: 100,
// 4 hours:
// shutdown_timer_s: 1,
shutdown_timer_s: 4 * 60 * 60,
// led2_timer_ms: 100,
};
@ -516,48 +521,8 @@ fn app_main(mut p: hal::Peripherals) -> ! {
// enter standby
app::State::DeepSleep => {
app.shut_down();
loop {
unsafe {
#[allow(static_mut_refs)]
INPUT_FLAGS.sense_coin_flag.clear();
#[allow(static_mut_refs)]
INPUT_FLAGS.main_btn_flag.clear();
}
unsafe { system::enter_standby() };
riscv::asm::wfi();
unsafe {
#[allow(static_mut_refs)]
if (INPUT_FLAGS.sense_coin_flag.active()
|| (INPUT_FLAGS.main_btn_flag.active()
&& main_btn_input.is_high_immediate()))
&& app.should_wake()
{
break;
}
}
}
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
unsafe {
hal::rcc::init(config.rcc);
}
unsafe {
use hal::pac::Interrupt;
use qingke::interrupt::Priority;
use qingke_rt::CoreInterrupt;
system::clear_interrupt(2, 6);
qingke::pfic::set_priority(CoreInterrupt::SysTick as u8, Priority::P15 as u8);
qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8);
qingke::pfic::enable_interrupt(CoreInterrupt::SysTick as u8);
}
app.set_state(State::Active);
return;
}
// for everything else, don't do anything
_ => {}
}
}
@ -573,6 +538,137 @@ fn app_main(mut p: hal::Peripherals) -> ! {
// // }
// }
use ch32_hal::timer::low_level::{OutputCompareMode, Timer};
use ch32_hal::timer::Channel;
// fn shutdown_main(p: Peripherals) {
fn shutdown_main(p: hal::Peripherals) {
// drop(p.TIM1);
// LED0 output setup
let led0_pin = OutputOpenDrain::new(p.PC3, Level::Low, Default::default());
let led1_pin = OutputOpenDrain::new(p.PD2, Level::High, Default::default());
let led2_pin = OutputOpenDrain::new(p.PA1, Level::High, Default::default());
let dac_pin = OutputOpenDrain::new(p.PC4, Level::Low, Default::default());
let mut amp_pin = OutputOpenDrain::new(p.PC5, Level::Low, Default::default());
amp_pin.set_high();
let volume_btn_pin = OutputOpenDrain::new(p.PC6, Level::Low, Default::default());
let light_ctrl_btn_pin = OutputOpenDrain::new(p.PC7, Level::Low, Default::default());
let usb_detect_input = OutputOpenDrain::new(p.PD5, Level::Low, Default::default());
let sense_coin_pin = p.PC2;
let main_btn_pin = p.PD6;
unsafe { system::init_gpio_irq(sense_coin_pin.pin(), sense_coin_pin.port(), true, false) };
unsafe { system::init_gpio_irq(main_btn_pin.pin(), main_btn_pin.port(), true, true) };
let sense_coin_pin = Input::new(sense_coin_pin, Pull::None);
let main_btn_pin = Input::new(main_btn_pin, Pull::None);
let mut t = Timer::new(p.TIM1);
// // t.stop();
// // t.enable_channel(ch32_hal::timer::Channel::Ch1, false);
// // t.enable_channel(ch32_hal::timer::Channel::Ch2, false);
// // t.enable_channel(ch32_hal::timer::Channel::Ch3, false);
// // t.enable_channel(ch32_hal::timer::Channel::Ch4, false);
// // t.enable_update_interrupt(false);
// // t.set_moe(false);
t.set_counting_mode(CountingMode::default());
t.set_frequency(Hertz::khz(300));
t.enable_outputs();
t.start();
[Channel::Ch1, Channel::Ch2, Channel::Ch3, Channel::Ch4]
.iter()
.for_each(|&channel| {
t.set_output_compare_mode(channel, OutputCompareMode::PwmMode1);
t.set_output_compare_preload(channel, true);
t.set_compare_value(channel, 1000);
t.enable_channel(channel, true);
});
// [Channel::Ch1, Channel::Ch2, Channel::Ch3, Channel::Ch4]
// .iter()
// .for_each(|&channel| {
// t.set_output_compare_mode(channel, OutputCompareMode::PwmMode1);
// t.set_output_compare_preload(channel, false);
// t.enable_channel(channel, false);
// });
// t.stop();
// t.disable_outputs()
// PWM timer setup
// let mut pwm = SimplePwm::new(
// p.TIM1,
// None,
// // Some(led2_pin),
// None,
// None,
// None,
// Hertz::hz(1),
// CountingMode::default(),
// );
// drop(pwm);
// pwm.disable();
riscv::asm::delay(1_000_000);
// let sense_coin_pin = p.PC2;
// let main_btn_pin = p.PD6;
// let volume_btn_pin = p.PC6;
// let light_ctrl_btn_pin = p.PC7;
unsafe { system::enter_standby() };
riscv::asm::wfi();
// t.set_moe(false);
// // pwm.shutdown();
// riscv::asm::delay(1_000_000);
// unsafe { system::enter_standby() };
// riscv::asm::wfi();
// let mut b = t.get_compare_value(Channel::Ch1);
// b += 1;
// loop {
// unsafe {
// #[allow(static_mut_refs)]
// INPUT_FLAGS.sense_coin_flag.clear();
// #[allow(static_mut_refs)]
// INPUT_FLAGS.main_btn_flag.clear();
// }
// unsafe { system::enter_standby() };
// riscv::asm::wfi();
// unsafe {
// #[allow(static_mut_refs)]
// if (INPUT_FLAGS.sense_coin_flag.active()
// || (INPUT_FLAGS.main_btn_flag.active() && main_btn_input.is_high_immediate()))
// && app.should_wake()
// {
// break;
// }
// }
// }
// let mut config = hal::Config::default();
// config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI;
// unsafe {
// hal::rcc::init(config.rcc);
// }
// unsafe {
// use hal::pac::Interrupt;
// use qingke::interrupt::Priority;
// use qingke_rt::CoreInterrupt;
// system::clear_interrupt(2, 6);
// qingke::pfic::set_priority(CoreInterrupt::SysTick as u8, Priority::P15 as u8);
// qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8);
// qingke::pfic::enable_interrupt(CoreInterrupt::SysTick as u8);
// }
}
#[qingke_rt::entry]
fn main() -> ! {
#[cfg(feature = "enable_print")]
@ -588,7 +684,19 @@ fn main() -> ! {
// println!("post");
// debug_main(p);
app_main(p);
loop {
unsafe {
hal::rcc::init(hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI);
}
let mut p = unsafe { hal::Peripherals::steal() };
app_main(p);
unsafe {
hal::rcc::init(hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI);
}
let mut p = unsafe { hal::Peripherals::steal() };
shutdown_main(p);
}
}
#[panic_handler]