diff --git a/ch32v-insert-coin/Cargo.lock b/ch32v-insert-coin/Cargo.lock index 028f1cb..348625b 100644 --- a/ch32v-insert-coin/Cargo.lock +++ b/ch32v-insert-coin/Cargo.lock @@ -18,6 +18,27 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" +[[package]] +name = "bitfields" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcdbce6688e3ab66aff2ab413b762ccde9f37990e27bba0bb38a4b2ad1b5d877" +dependencies = [ + "bitfields-impl", +] + +[[package]] +name = "bitfields-impl" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57413e4b276d883b77fb368b7b33ae6a5eb97692852d49a5394d4f72ba961827" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", + "thiserror", +] + [[package]] name = "byteorder" version = "1.5.0" @@ -71,6 +92,7 @@ name = "ch32v-insert-coin" version = "0.1.0" dependencies = [ "adpcm-pwm-dac", + "bitfields", "ch32-hal", "embassy-executor", "embedded-hal 1.0.0", @@ -593,6 +615,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", +] + [[package]] name = "unicode-ident" version = "1.0.18" diff --git a/ch32v-insert-coin/Cargo.toml b/ch32v-insert-coin/Cargo.toml index 566357b..b4fc9c7 100644 --- a/ch32v-insert-coin/Cargo.toml +++ b/ch32v-insert-coin/Cargo.toml @@ -25,6 +25,7 @@ embedded-hal = "1.0.0" qingke-rt = { version = "*", features = ["highcode"] } adpcm-pwm-dac = { path = "ext/adpcm-pwm-dac/" } +bitfields = "1.0.0" [profile.release] strip = false # symbols are not flashed to the microcontroller, so don't strip them. @@ -32,4 +33,4 @@ lto = true opt-level = "s" # Optimize for size. [profile.dev] -overflow-checks = false \ No newline at end of file +overflow-checks = false diff --git a/ch32v-insert-coin/ext/adpcm-pwm-dac b/ch32v-insert-coin/ext/adpcm-pwm-dac index 8502ed7..e6114d2 160000 --- a/ch32v-insert-coin/ext/adpcm-pwm-dac +++ b/ch32v-insert-coin/ext/adpcm-pwm-dac @@ -1 +1 @@ -Subproject commit 8502ed75bf8d3e8df1eb5f1b5576079f55c6e13e +Subproject commit e6114d2c521ef3a769bd2a52411e3a938b566908 diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index 3ff3278..97f7bc3 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -3,6 +3,7 @@ #![feature(type_alias_impl_trait)] #![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}; @@ -14,6 +15,22 @@ use hal::timer::{Channel, GeneralInstance16bit}; // const DAC_DATA: [u8; 4] = [0x0, 0x80, 0xFF, 0x80]; const DAC_DATA: [u8; 8] = [0, 25, 50, 75, 100, 75, 50, 25]; +// DPCS DATA +// step size: 5 +// 0 +// + 25 -> [1, 0, 1, 1] -> 0xB +// + 25 -> 0xB +// + 25 -> 0xB +// + 25 -> 0xB +// - 25 -> 0xA +// - 25 -> 0xA +// - 25 -> 0xA +const DPCM_DAC_DATA: [u8; 4] = [0xBB, 0xBB, 0xAA, 0xAA]; + +// const DATA2: [u8; 1] = [0x00u8]; + + + struct SimplePwmDacPin<'d, T: GeneralInstance16bit>{ pin: SimplePwm<'d, T>, ch: Channel, @@ -65,26 +82,47 @@ fn pwm_blink(mut pwm_dac_pin: SimplePwmDacPin<'_, T>) { } } -fn dac_run(mut dac: Dac<'_, T>, sample_rate: usize) { - let mut delay = Delay; +// // fn dac_run(mut dac: Dac<'_, T>, sample_rate: usize) { +// fn dac_run(mut dac: DpcmDac<'_, T>, sample_rate: usize) { + +// dac.load_data(data); +// // dac.load_data(&DAC_DATA); - dac.load_data(&DAC_DATA); +// let interval_us = 1000000/sample_rate as u32; +// loop { - let interval_us = 1000000/sample_rate as u32; - loop { - dac.output_next(); - delay.delay_us(interval_us); - } -} +// } +// } #[qingke_rt::entry] fn main() -> ! { let mut config = hal::Config::default(); config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSE; let p = hal::init(config); + + + // let led_pin = PwmPin::new_ch4::<0>(p.PD0); + // let ch = hal::timer::Channel::Ch4; + // let mut pwm = SimplePwm::new( + // p.TIM1, + // None, + // None, + // None, + // Some(pin), + // Hertz::khz(100), + // CountingMode::default(), + // ); + // pwm.enable(ch); + + // + + // LED output setup + let mut led = Output::new(p.PD0, Level::Low, Default::default()); + + // PWM DAC output pin setup let pin = PwmPin::new_ch4::<0>(p.PC4); let ch = hal::timer::Channel::Ch4; let mut pwm = SimplePwm::new( @@ -93,19 +131,51 @@ fn main() -> ! { None, None, Some(pin), - Hertz::khz(24), + Hertz::khz(100), CountingMode::default(), ); pwm.enable(ch); - - let mut pwm_dac_pin = SimplePwmDacPin{pin: pwm, ch}; - let mut dac = Dac::new(pwm_dac_pin); - let sample_rate_hz = 440 * DAC_DATA.len(); - // pwm_blink(pwm_dac_pin); - dac_run(dac, sample_rate_hz); - blink(p.PD6.degrade(), 100); - loop{}; + + + let mut delay = Delay; + + + + // DAC setup + let mut dac = DpcmDac::new(pwm_dac_pin); + let data = include_bytes!("../../../dpcm-encoder-decoder/sweep_dpcm_u4.raw"); + dac.load_data(data); + + + // DAC servicer computations + let mut dac_active = true; + let sample_rate_hz = 16000; + let dac_tick_per_service = 5; + let tick_rate_hz = sample_rate_hz * dac_tick_per_service; + let tick_interval = 1000000/(tick_rate_hz); + + // LED servicer computations + let mut led_active = true; + let led_blink_rate_hz = 3; + let led_tick_per_service = (tick_rate_hz/(led_blink_rate_hz * 2)); + + + // pwm_blink(pwm_dac_pin); + let mut tick: usize = 0; + loop{ + let dac_need_service = ((tick % dac_tick_per_service) == 0); + if(dac_need_service && dac_active) { + dac_active = dac.output_next(); + } + + let led_need_service = ((tick % led_tick_per_service) == 0); + if(led_need_service && led_active) { + led.toggle(); + } + tick = tick.wrapping_add(1); + delay.delay_us(tick_interval as u32); + }; }