diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index 97f7bc3..3f9e9ef 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -4,9 +4,11 @@ #![feature(impl_trait_in_assoc_type)] use adpcm_pwm_dac::dac::DpcmDac; -use hal::delay::Delay; -use hal::gpio::{AnyPin, Level, Output, Pin}; use {ch32_hal as hal}; +use hal::peripherals::EXTI4; +use hal::{bind_interrupts, interrupt}; +use hal::delay::Delay; +use hal::gpio::{AnyPin, Level, Input, Output, Pin, Pull}; use hal::time::Hertz; use hal::timer::low_level::CountingMode; use hal::timer::simple_pwm::{PwmPin, SimplePwm}; @@ -30,6 +32,7 @@ const DPCM_DAC_DATA: [u8; 4] = [0xBB, 0xBB, 0xAA, 0xAA]; // const DATA2: [u8; 1] = [0x00u8]; +static mut IRQ1_FLAG: bool = false; struct SimplePwmDacPin<'d, T: GeneralInstance16bit>{ pin: SimplePwm<'d, T>, @@ -95,6 +98,15 @@ fn pwm_blink(mut pwm_dac_pin: SimplePwmDacPin<'_, T>) { // } // } + +#[qingke_rt::interrupt] +fn EXTI4(){ + unsafe{IRQ1_FLAG = true;}; +} +// bind_interrupts!(struct Irqs { +// EXTI4 => button_press(); +// }); + #[qingke_rt::entry] fn main() -> ! { let mut config = hal::Config::default(); @@ -102,6 +114,12 @@ fn main() -> ! { let p = hal::init(config); + // button 1 setup + // let b1 = Input::new(p.PA2, Pull::Up); + + // p.EXTI4 + let ei = hal::exti::ExtiInput::new(p.PD4, p.EXTI4, Pull::Up); + // let led_pin = PwmPin::new_ch4::<0>(p.PD0); // let ch = hal::timer::Channel::Ch4; // let mut pwm = SimplePwm::new( @@ -120,6 +138,8 @@ fn main() -> ! { // LED output setup let mut led = Output::new(p.PD0, Level::Low, Default::default()); + // LED1 output setup + let mut led1 = Output::new(p.PD6, Level::High, Default::default()); // PWM DAC output pin setup @@ -160,10 +180,22 @@ fn main() -> ! { let led_blink_rate_hz = 3; let led_tick_per_service = (tick_rate_hz/(led_blink_rate_hz * 2)); + // LED1 servicer vars + let mut led1_need_service = false; + // pwm_blink(pwm_dac_pin); let mut tick: usize = 0; loop{ + // handle IRQ flags + unsafe { + if(IRQ1_FLAG) { + led1_need_service = true; + // led_active = true; + IRQ1_FLAG = false; + } + } + let dac_need_service = ((tick % dac_tick_per_service) == 0); if(dac_need_service && dac_active) { dac_active = dac.output_next(); @@ -173,6 +205,12 @@ fn main() -> ! { if(led_need_service && led_active) { led.toggle(); } + + if led1_need_service { + led1.set_low(); + led1_need_service = false; + } + tick = tick.wrapping_add(1); delay.delay_us(tick_interval as u32); }; @@ -182,4 +220,5 @@ fn main() -> ! { #[panic_handler] fn panic(info: &core::panic::PanicInfo) -> ! { loop {} -} \ No newline at end of file +} +