From 8a16bd166f7953beb14fc6ffe90c3d57c2712046 Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Tue, 28 Oct 2025 15:34:36 -0600 Subject: [PATCH 1/2] FUCKKKK IT'S WORKING WITH INTERRUPTS FINALLY --- ch32v-insert-coin/ext/wavetable-synth | 2 +- ch32v-insert-coin/src/main.rs | 197 ++++++++++++++++++++++---- ch32v-insert-coin/src/synthesizer.rs | 21 ++- 3 files changed, 194 insertions(+), 26 deletions(-) diff --git a/ch32v-insert-coin/ext/wavetable-synth b/ch32v-insert-coin/ext/wavetable-synth index 9417e6e..aa57096 160000 --- a/ch32v-insert-coin/ext/wavetable-synth +++ b/ch32v-insert-coin/ext/wavetable-synth @@ -1 +1 @@ -Subproject commit 9417e6edbf590f3fe777e39ad76efc21a024e01d +Subproject commit aa57096e4b560ee4f65149ddf5ef6c4c8f82e4ce diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index ac9bac5..1bcb438 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -195,6 +195,107 @@ impl Handler for Test { } } +static mut LED: Option> = None; +static mut SYSTICK_COUNT: u32 = 0; +static mut SYNTHESIZERS: Option> = None; +static mut TOGGLE_COUNT: u32 = 0; +static mut TICK_FLAG: bool = false; +static mut SYNTH_TICK_FLAG: bool = false; +static mut NOTE_TICK_FLAG: bool = false; +static mut FREQ: [usize; 2] = [220, 440]; +static mut INDEX: usize = 0; + +fn flag() -> bool { + unsafe { core::ptr::read_volatile(&raw const TICK_FLAG as *const bool) } +} + +fn clear_flag() { + unsafe { + TICK_FLAG = false; + } +} + +fn synth_flag() -> bool { + unsafe { core::ptr::read_volatile(&raw const SYNTH_TICK_FLAG as *const bool) } +} + +fn clear_synth_flag() { + unsafe { + SYNTH_TICK_FLAG = false; + } +} + +fn note_flag() -> bool { + unsafe { core::ptr::read_volatile(&raw const NOTE_TICK_FLAG as *const bool) } +} + +fn clear_note_flag() { + unsafe { + NOTE_TICK_FLAG = false; + } +} + +#[qingke_rt::interrupt(core)] +fn SysTick() { + let r = &ch32_hal::pac::SYSTICK; + + // Clear interrupt flag + r.sr().write(|w| w.set_cntif(false)); + + unsafe { + // if let Some(ref mut synthesizers) = SYNTHESIZERS { + // println!("tick"); + // synthesizers.tick(); + // } + SYNTH_TICK_FLAG = true; + + if SYSTICK_COUNT % 5000 == 0 { + NOTE_TICK_FLAG = true; + } + + if SYSTICK_COUNT % 5000 == 0 { + TICK_FLAG = true; + // if let Some(ref mut led) = LED { + // let toggle = TOGGLE_COUNT; + // // println!("TOGGLE {toggle}"); + // TOGGLE_COUNT += 1; + // led.toggle(); + // } + // println!("HERE"); + } + SYSTICK_COUNT = SYSTICK_COUNT.wrapping_add(1); + } +} + +fn systick_init(tick_freq_hz: usize) { + let r = &ch32_hal::pac::SYSTICK; + + // Calculate counts per millisecond using HCLK/8 as clock source + // HCLK/8 = 48MHz/8 = 6MHz + // For tick_freq_hz interrupt: 6MHz / tick_freq_hz + let systick_per_tick = (48_000_000 / 8 / tick_freq_hz) as u32; + + // Reset SysTick + r.ctlr().write(|w| { + // Start with everything disabled + }); + + // Set compare register and reset counter + r.cmp().write_value(systick_per_tick - 1); + r.cnt().write_value(0); + + // Clear interrupt flag + r.sr().write(|w| w.set_cntif(false)); + + // Configure and start SysTick + r.ctlr().write(|w| { + w.set_ste(true); // Enable counter + w.set_stie(true); // Enable interrupt + w.set_stre(true); // Auto reload enable + w.set_stclk(ch32_hal::pac::systick::vals::Stclk::HCLK_DIV8); // HCLK/8 clock source + }); +} + bind_interrupts!(struct Irqs { EXTI7_0 => Test; }); @@ -203,15 +304,20 @@ bind_interrupts!(struct Irqs { use insert_coin::TickTimerService; use insert_coin::{TickService, TickServiceData}; -fn debug_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { +fn debug_main(mut p: hal::Peripherals) -> ! { // LED0 output setup use hal::gpio::{Level, Output}; - let mut led0_pin = Output::new(p.PC3, Level::High, Default::default()); + let mut led = Output::new(p.PC3, Level::High, Default::default()); - delay.delay_ms(1000); + // unsafe { + // LED = Some(led); + // } - let tick_rate_hz = 100000; // 100khz - let tick_interval_us = 1000000 / tick_rate_hz; + println!("pre"); + riscv::asm::delay(20_000_000); + println!("post2"); + + let tick_rate_hz = 50000; // 50khz // DAC output setup let dac_pin = PwmPin::new_ch4::<0>(p.PC4); @@ -229,19 +335,60 @@ fn debug_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { ); let pwm_core = SimplePwmCore::new(pwm); + unsafe { + use qingke_rt::CoreInterrupt; + + qingke::pfic::disable_interrupt(CoreInterrupt::SysTick as u8); + } // === synthesizer setup === - let mut synthesizer = AppSynthesizers::new(tick_rate_hz, pwm_core); - synthesizer.square.set_freq(440); + // unsafe { + // let mut synth = AppSynthesizers::new(tick_rate_hz, pwm_core); + // synth.square.set_freq(440); + // SYNTHESIZERS = Some(synth); + // } + let mut synth = AppSynthesizers::new(tick_rate_hz, pwm_core); + synth.square.set_freq(440); #[cfg(feature = "enable_print")] println!("begin loop"); - led0_pin.toggle(); + systick_init(tick_rate_hz); + + unsafe { + use qingke::interrupt::Priority; + use qingke_rt::CoreInterrupt; + qingke::pfic::set_priority(CoreInterrupt::SysTick as u8, Priority::P15 as u8); + qingke::pfic::enable_interrupt(CoreInterrupt::SysTick as u8); + } + + let mut index = 0; + let freqs = [110, 440]; loop { - synthesizer.service(); - delay.delay_us(tick_interval_us as u32); + if flag() { + clear_flag(); + led.toggle(); + } + if synth_flag() { + clear_synth_flag(); + synth.tick(); + } + if note_flag() { + clear_note_flag(); + synth.square.set_freq(freqs[index]); + index += 1; + if index > freqs.len() - 1 { + index = 0; + } + } + // if (unsafe { TICK_FLAG == true }) { + // println!("TICK!"); + // led.toggle(); + // unsafe { + // TICK_FLAG = false; + // } + // } } } @@ -394,11 +541,11 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { // dac data // let coin_sound = include_bytes!("../audio/coin2.raw"); // let coin_sound = include_bytes!("../audio/sweep_dpcm_u4.raw"); - let coin_sound = include_bytes!("../audio/button_1.raw"); + // let coin_sound = include_bytes!("../audio/button_1.raw"); - let button_sound_1 = include_bytes!("../audio/button_1.raw"); - let button_sound_2 = include_bytes!("../audio/button_2.raw"); - let button_sound_3 = include_bytes!("../audio/button_3.raw"); + // let button_sound_1 = include_bytes!("../audio/button_1.raw"); + // let button_sound_2 = include_bytes!("../audio/button_2.raw"); + // let button_sound_3 = include_bytes!("../audio/button_3.raw"); let mut system_state = SystemState::Active; @@ -464,7 +611,7 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { // todo: enter active tt0.enable(true); tt1.enable(true); - interfaces.dac.load_data(coin_sound); + // interfaces.dac.load_data(coin_sound); } if INPUT_FLAGS.main_btn_flag { #[cfg(feature = "enable_print")] @@ -504,12 +651,12 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { println!("debounced button_input value: {}", value); if !value { - interfaces.dac.load_data(match settings.button_sound_index { - 0 => button_sound_1, - 1 => button_sound_2, - 2 => button_sound_3, - _ => button_sound_1, - }); + // interfaces.dac.load_data(match settings.button_sound_index { + // 0 => button_sound_1, + // 1 => button_sound_2, + // 2 => button_sound_3, + // _ => button_sound_1, + // }); // interfaces // .dac // .load_data(button_sounds[settings.button_sound_index]); @@ -645,14 +792,16 @@ fn main() -> ! { let mut p = hal::init(config); // delay to let the debugger attach - let mut delay = Delay; - delay.delay_ms(1000); + println!("pre"); + riscv::asm::delay(20_000_000); + println!("post"); + debug_main(p); - debug_main(p, delay); // app_main(p, delay); } #[panic_handler] fn panic(_info: &core::panic::PanicInfo) -> ! { + // println!("panic: {info:?}"); loop {} } diff --git a/ch32v-insert-coin/src/synthesizer.rs b/ch32v-insert-coin/src/synthesizer.rs index ab50fe6..bc2aec6 100644 --- a/ch32v-insert-coin/src/synthesizer.rs +++ b/ch32v-insert-coin/src/synthesizer.rs @@ -21,11 +21,30 @@ impl<'a, T: GeneralInstance16bit> AppSynthesizers<'a, T> { Self { square, output } } - pub fn service(&mut self) { + pub fn tick(&mut self) { self.square.tick(); if self.square.has_new_output() { let out = self.square.get_output(); // println!("OUTPUT: {out}"); + + // println!("new out"); + self.output.write_amplitude( + ch32_hal::timer::Channel::Ch4, + out / 2, + // (out as f32 * (u8::MAX as f32 / u32::MAX as f32)) as u8, + ); + } + + // println!("{}{}", self.square.counter, self.square.has_new_output()); + } + + pub fn service(&mut self) { + // println!("HERE"); + if self.square.has_new_output() { + let out = self.square.get_output(); + // println!("OUTPUT: {out}"); + + // println!("new out"); self.output.write_amplitude( ch32_hal::timer::Channel::Ch4, out / 2, From c58b327c5405d84c04eee10b2c9a626192a14285 Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Tue, 28 Oct 2025 18:58:43 -0600 Subject: [PATCH 2/2] some more fiddling --- ch32v-insert-coin/audio/sequences | 18 ++++++++++++++++++ ch32v-insert-coin/ext/wavetable-synth | 2 +- ch32v-insert-coin/src/main.rs | 19 +++++++++++++++---- ch32v-insert-coin/src/synthesizer.rs | 9 +++++---- 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 ch32v-insert-coin/audio/sequences diff --git a/ch32v-insert-coin/audio/sequences b/ch32v-insert-coin/audio/sequences new file mode 100644 index 0000000..edef1a1 --- /dev/null +++ b/ch32v-insert-coin/audio/sequences @@ -0,0 +1,18 @@ +"wahwahwahwah" sound - let play a few times +note timing: 6hz +data: let freqs = [100, 200, 300, 400, 500, 600, 700, 800, 900]; + +"falling" sound +note timing: 6hz +[ +1000, +990, 980, 970, 960, 950, 940, 930, 920, 910, 900, +890, 880, 870, 860, 850, 840, 830, 820, 810, 800, +790, 780, 770, 760, 750, 740, 730, 720, 710, 700, +690, 680, 670, 660, 650, 640, 630, 620, 610, 600, +590, 580, 570, 560, 550, 540, 530, 520, 510, 500, +490, 480, 470, 460, 450, 440, 430, 420, 410, 400, +390, 380, 370, 360, 350, 340, 330, 320, 310, 300, +290, 280, 270, 260, 250, 240, 230, 220, 210, 200, +190, 180, 170, 160, 150, 140, 130, 120, 110, 100, +] diff --git a/ch32v-insert-coin/ext/wavetable-synth b/ch32v-insert-coin/ext/wavetable-synth index aa57096..e6ca9c7 160000 --- a/ch32v-insert-coin/ext/wavetable-synth +++ b/ch32v-insert-coin/ext/wavetable-synth @@ -1 +1 @@ -Subproject commit aa57096e4b560ee4f65149ddf5ef6c4c8f82e4ce +Subproject commit e6ca9c7ed8b9af193333be793983df5e48cb961a diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index 1bcb438..ed5e986 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -253,7 +253,7 @@ fn SysTick() { NOTE_TICK_FLAG = true; } - if SYSTICK_COUNT % 5000 == 0 { + if SYSTICK_COUNT % 50000 == 0 { TICK_FLAG = true; // if let Some(ref mut led) = LED { // let toggle = TOGGLE_COUNT; @@ -317,7 +317,7 @@ fn debug_main(mut p: hal::Peripherals) -> ! { riscv::asm::delay(20_000_000); println!("post2"); - let tick_rate_hz = 50000; // 50khz + let tick_rate_hz = 100000; // 50khz // DAC output setup let dac_pin = PwmPin::new_ch4::<0>(p.PC4); @@ -330,7 +330,7 @@ fn debug_main(mut p: hal::Peripherals) -> ! { None, None, Some(dac_pin), - Hertz::khz(200), + Hertz::khz(500), CountingMode::default(), ); @@ -363,7 +363,16 @@ fn debug_main(mut p: hal::Peripherals) -> ! { } let mut index = 0; - let freqs = [110, 440]; + let freqs = [1567, 1396, 1318, 1174, 1046, 987, 880, 783, 698]; + // let freqs = [100, 200, 300, 400, 500, 600, 700, 800, 900]; + // let freqs = [ + // 1000, 990, 980, 970, 960, 950, 940, 930, 920, 910, 900, 890, 880, 870, 860, 850, 840, 830, + // 820, 810, 800, 790, 780, 770, 760, 750, 740, 730, 720, 710, 700, 690, 680, 670, 660, 650, + // 640, 630, 620, 610, 600, 590, 580, 570, 560, 550, 540, 530, 520, 510, 500, 490, 480, 470, + // 460, 450, 440, 430, 420, 410, 400, 390, 380, 370, 360, 350, 340, 330, 320, 310, 300, 290, + // 280, 270, 260, 250, 240, 230, 220, 210, 200, 190, 180, 170, 160, 150, 140, 130, 120, 110, + // 100, + // ]; loop { if flag() { @@ -377,6 +386,8 @@ fn debug_main(mut p: hal::Peripherals) -> ! { if note_flag() { clear_note_flag(); synth.square.set_freq(freqs[index]); + // println!("{}", { freqs[index] }); + // println!("{}", freqs[index]); index += 1; if index > freqs.len() - 1 { index = 0; diff --git a/ch32v-insert-coin/src/synthesizer.rs b/ch32v-insert-coin/src/synthesizer.rs index bc2aec6..ad265c1 100644 --- a/ch32v-insert-coin/src/synthesizer.rs +++ b/ch32v-insert-coin/src/synthesizer.rs @@ -3,10 +3,11 @@ use ch32_hal::println; use ch32_hal::timer::GeneralInstance16bit; use wavetable_synth::{synthesizer::SimpleWavetableSynthesizer, wavetable::SimpleWavetable}; -const SQUARE_WAVETABLE: [u8; 32] = [ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, - 100, 100, 100, 100, 100, 100, 100, -]; +const SQUARE_WAVETABLE: [u8; 2] = [0, 100]; +// const SQUARE_WAVETABLE: [u8; 32] = [ +// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, +// 100, 100, 100, 100, 100, 100, 100, +// ]; pub struct AppSynthesizers<'a, T: GeneralInstance16bit> { pub square: SimpleWavetableSynthesizer>,