minor formatting cleanups

This commit is contained in:
sigil-03 2025-10-19 10:53:58 -06:00
parent 8d612d1559
commit fba0fbec61
11 changed files with 92 additions and 111 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
ch32v-insert-coin/bins.zip Normal file

Binary file not shown.

View file

@ -0,0 +1,4 @@
TO FLASH / RUN:
`wlink -v flash --enable-sdi-print --watch-serial bins/coin_sound_8ksps.bin`

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -14,5 +14,5 @@
"max-atomic-width": 32, "max-atomic-width": 32,
"panic-strategy": "abort", "panic-strategy": "abort",
"relocation-model": "static", "relocation-model": "static",
"target-pointer-width": "32" "target-pointer-width": 32
} }

View file

@ -5,11 +5,10 @@
mod insert_coin; mod insert_coin;
use ch32_hal::{adc::AdcChannel, interrupt::typelevel::Handler, timer::low_level::OutputPolarity}; use ch32_hal::{adc::AdcChannel, interrupt::typelevel::Handler, timer::low_level::OutputPolarity};
use insert_coin::{InsertCoin, SimplePwmCore, CoreConfig}; use insert_coin::{CoreConfig, InsertCoin, SimplePwmCore};
use ch32_hal as hal;
use {ch32_hal as hal}; use hal::bind_interrupts;
use hal::{bind_interrupts};
use hal::delay::Delay; use hal::delay::Delay;
use hal::gpio::{AnyPin, Input, Pin, Pull}; use hal::gpio::{AnyPin, Input, Pin, Pull};
use hal::time::Hertz; use hal::time::Hertz;
@ -20,7 +19,6 @@ use hal::println;
use qingke::riscv; use qingke::riscv;
struct DebouncedGPIO<'a> { struct DebouncedGPIO<'a> {
input: Input<'a>, input: Input<'a>,
// value of the GPIO // value of the GPIO
@ -38,7 +36,10 @@ impl<'a> DebouncedGPIO<'a> {
input: Input::new(pin, Pull::Up), input: Input::new(pin, Pull::Up),
value: false, value: false,
ready: false, ready: false,
timer: TickTimerService::new(TickServiceData::new(system_tick_rate_hz * debounce_time_ms / 1000), false), timer: TickTimerService::new(
TickServiceData::new(system_tick_rate_hz * debounce_time_ms / 1000),
false,
),
} }
} }
@ -70,7 +71,6 @@ impl<'a> DebouncedGPIO<'a> {
} }
} }
// DeepSleep --coin button irq--> Active // DeepSleep --coin button irq--> Active
// Active --2s button--> Idle // Active --2s button--> Idle
// Idle/Active --5s button--> DeepSleep // Idle/Active --5s button--> DeepSleep
@ -95,7 +95,6 @@ pub enum SystemState {
/// 1. any interrupt/event (set in external interrupt register) /// 1. any interrupt/event (set in external interrupt register)
unsafe fn enter_standby(pin: usize) { unsafe fn enter_standby(pin: usize) {
critical_section::with(|_| { critical_section::with(|_| {
use hal::pac::Interrupt; use hal::pac::Interrupt;
use qingke_rt::CoreInterrupt; use qingke_rt::CoreInterrupt;
@ -123,7 +122,6 @@ unsafe fn enter_standby(pin: usize) {
// let bits = 0xFFFFFFFF; // let bits = 0xFFFFFFFF;
// exti.intfr().write(|w| w.0 = bits); // exti.intfr().write(|w| w.0 = bits);
// // enable all exti interrupts // // enable all exti interrupts
// let exti = &hal::pac::EXTI; // let exti = &hal::pac::EXTI;
// exti.intenr().write(| w| { // exti.intenr().write(| w| {
@ -133,13 +131,12 @@ unsafe fn enter_standby(pin: usize) {
// }); // });
unsafe { unsafe {
// qingke::pfic::disable_interrupt(Interrupt::EXTI7_0 as u8); // qingke::pfic::disable_interrupt(Interrupt::EXTI7_0 as u8);
// qingke::pfic::disable_interrupt(CoreInterrupt::SysTick as u8); // qingke::pfic::disable_interrupt(CoreInterrupt::SysTick as u8);
// qingke::pfic::disable_interrupt(CoreInterrupt::SysTick as u8); // qingke::pfic::disable_interrupt(CoreInterrupt::SysTick as u8);
qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8); qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8);
} }
// execute WFI // execute WFI
println!("WFI CONFIGURED HOPEFULLY"); println!("WFI CONFIGURED HOPEFULLY");
@ -148,29 +145,27 @@ unsafe fn enter_standby(pin: usize) {
// pfic. // pfic.
// qingke::pfic:: // qingke::pfic::
}); });
} }
unsafe fn init_gpio_irq(pin: u8, port: u8, rising: bool, falling: bool) { unsafe fn init_gpio_irq(pin: u8, port: u8, rising: bool, falling: bool) {
critical_section::with(|_| { critical_section::with(|_| {
println!("init_gpio_irq"); println!("init_gpio_irq");
let exti = &hal::pac::EXTI; let exti = &hal::pac::EXTI;
let afio = &hal::pac::AFIO; let afio = &hal::pac::AFIO;
let port = port as u8; let port = port as u8;
let pin = pin as usize; let pin = pin as usize;
// let b = afio.exticr().read(); // let b = afio.exticr().read();
afio.exticr().modify(|w| w.set_exti(pin, port)); afio.exticr().modify(|w| w.set_exti(pin, port));
exti.intenr().modify(|w| w.set_mr(pin, true)); // enable interrupt exti.intenr().modify(|w| w.set_mr(pin, true)); // enable interrupt
exti.rtenr().modify(|w| w.set_tr(pin, rising)); exti.rtenr().modify(|w| w.set_tr(pin, rising));
exti.ftenr().modify(|w| w.set_tr(pin, falling)); exti.ftenr().modify(|w| w.set_tr(pin, falling));
}); });
} }
fn clear_interrupt(coin_pin: u8, button_pin: u8) {
fn clear_interrupt (coin_pin: u8, button_pin: u8) {
let exti = &hal::pac::EXTI; let exti = &hal::pac::EXTI;
let coin_pin = coin_pin as usize; let coin_pin = coin_pin as usize;
@ -179,7 +174,6 @@ fn clear_interrupt (coin_pin: u8, button_pin: u8) {
exti.intenr().modify(|w| w.set_mr(coin_pin, false)); // disable interrupt exti.intenr().modify(|w| w.set_mr(coin_pin, false)); // disable interrupt
exti.intenr().modify(|w| w.set_mr(button_pin, false)); // disable interrupt exti.intenr().modify(|w| w.set_mr(button_pin, false)); // disable interrupt
let bits = exti.intfr().read(); let bits = exti.intfr().read();
// We don't handle or change any EXTI lines above 24. // We don't handle or change any EXTI lines above 24.
@ -188,40 +182,44 @@ fn clear_interrupt (coin_pin: u8, button_pin: u8) {
// coin_flag // coin_flag
if (bits & (0x1 << coin_pin)) != 0x0 { if (bits & (0x1 << coin_pin)) != 0x0 {
println!("coin irq!"); println!("coin irq!");
unsafe { INPUT_FLAGS.coin_flag = true; } unsafe {
INPUT_FLAGS.coin_flag = true;
}
} }
// button_flag // button_flag
if (bits & (0x1 << button_pin)) != 0x0 { if (bits & (0x1 << button_pin)) != 0x0 {
println!("button irq!"); println!("button irq!");
unsafe { INPUT_FLAGS.button_flag = true; } unsafe {
INPUT_FLAGS.button_flag = true;
}
} }
// Clear pending - Clears the EXTI's line pending bits. // Clear pending - Clears the EXTI's line pending bits.
exti.intfr().write(|w| w.0 = bits); exti.intfr().write(|w| w.0 = bits);
use hal::pac::Interrupt; use hal::pac::Interrupt;
unsafe { qingke::pfic::unpend_interrupt(Interrupt::EXTI7_0 as u8); }; unsafe {
qingke::pfic::unpend_interrupt(Interrupt::EXTI7_0 as u8);
};
// exti.intenr().modify(|w| w.0 = w.0 & !bits); // exti.intenr().modify(|w| w.0 = w.0 & !bits);
exti.intenr().modify(|w| w.set_mr(coin_pin, true)); // enable interrupt exti.intenr().modify(|w| w.set_mr(coin_pin, true)); // enable interrupt
exti.intenr().modify(|w| w.set_mr(button_pin, true)); // enable interrupt exti.intenr().modify(|w| w.set_mr(button_pin, true)); // enable interrupt
} }
#[derive(Debug)] #[derive(Debug)]
struct InputFlags { struct InputFlags {
coin_flag: bool, coin_flag: bool,
button_flag: bool, button_flag: bool,
} }
static mut INPUT_FLAGS: InputFlags = InputFlags{coin_flag: false, button_flag: false}; static mut INPUT_FLAGS: InputFlags = InputFlags {
coin_flag: false,
button_flag: false,
};
struct Test { struct Test {}
}
impl Handler<hal::interrupt::typelevel::EXTI7_0> for Test { impl Handler<hal::interrupt::typelevel::EXTI7_0> for Test {
unsafe fn on_interrupt() { unsafe fn on_interrupt() {
println!("on_interrupt()"); println!("on_interrupt()");
@ -235,11 +233,9 @@ bind_interrupts!(struct Irqs {
EXTI7_0 => Test; EXTI7_0 => Test;
}); });
// TODO: remove // TODO: remove
use insert_coin::{TickService, TickServiceData};
use insert_coin::TickTimerService; use insert_coin::TickTimerService;
use insert_coin::{TickService, TickServiceData};
#[qingke_rt::entry] #[qingke_rt::entry]
fn main() -> ! { fn main() -> ! {
@ -252,7 +248,6 @@ fn main() -> ! {
let mut delay = Delay; let mut delay = Delay;
delay.delay_ms(1000); delay.delay_ms(1000);
// === output setup === // === output setup ===
// LED0 output setup // LED0 output setup
@ -267,15 +262,12 @@ fn main() -> ! {
let led1_pin = PwmPin::new_ch1::<0>(p.PD2); let led1_pin = PwmPin::new_ch1::<0>(p.PD2);
let led1_ch = hal::timer::Channel::Ch1; let led1_ch = hal::timer::Channel::Ch1;
// LED2 output setup // LED2 output setup
// DAC output setup // DAC output setup
let dac_pin = PwmPin::new_ch4::<0>(p.PC4); let dac_pin = PwmPin::new_ch4::<0>(p.PC4);
// let dac_ch = hal::timer::Channel::Ch4; // let dac_ch = hal::timer::Channel::Ch4;
// PWM timer setup // PWM timer setup
let mut pwm = SimplePwm::new( let mut pwm = SimplePwm::new(
p.TIM1, p.TIM1,
@ -296,7 +288,6 @@ fn main() -> ! {
let core_config = CoreConfig::new(tick_rate_hz); let core_config = CoreConfig::new(tick_rate_hz);
// === input setup === // === input setup ===
// adc // adc
@ -307,29 +298,32 @@ fn main() -> ! {
let adc_cal = adc.calibrate(); let adc_cal = adc.calibrate();
println!("ADC calibration value: {}", adc_cal); println!("ADC calibration value: {}", adc_cal);
// definitions // definitions
let coin_pin = p.PC2; let coin_pin = p.PC2;
let button_pin = p.PD6; let button_pin = p.PD6;
println!("coin pin: {} | coin port: {}", coin_pin.pin(), coin_pin.port()); // println!(
println!("push pin: {} | push port: {}", button_pin.pin(), button_pin.port()); // "coin pin: {} | coin port: {}",
// coin_pin.pin(),
// coin_pin.port()
// );
// println!(
// "push pin: {} | push port: {}",
// button_pin.pin(),
// button_pin.port()
// );
//2025-09-10 00:32:23.514: coin pin: 4 | coin port: 3 //2025-09-10 00:32:23.514: coin pin: 4 | coin port: 3
// 2025-09-10 00:32:23.515: push pin: 6 | push port: 3 // 2025-09-10 00:32:23.515: push pin: 6 | push port: 3
// set up interrupts // set up interrupts
unsafe {init_gpio_irq(coin_pin.pin(), coin_pin.port(), false, true)}; unsafe { init_gpio_irq(coin_pin.pin(), coin_pin.port(), false, true) };
unsafe {init_gpio_irq(button_pin.pin(), button_pin.port(), true, true)}; unsafe { init_gpio_irq(button_pin.pin(), button_pin.port(), true, true) };
// coin debouncer (100ms) // coin debouncer (100ms)
let mut coin_input = DebouncedGPIO::new(coin_pin.degrade(), core_config.tick_rate_hz, 100); let mut coin_input = DebouncedGPIO::new(coin_pin.degrade(), core_config.tick_rate_hz, 100);
// button debouncer (100ms) // button debouncer (100ms)
let mut button_input = DebouncedGPIO::new(button_pin.degrade(), core_config.tick_rate_hz, 100); let mut button_input = DebouncedGPIO::new(button_pin.degrade(), core_config.tick_rate_hz, 100);
let pwm_core = SimplePwmCore::new(pwm); let pwm_core = SimplePwmCore::new(pwm);
let mut interfaces = InsertCoin::new(core_config, pwm_core); let mut interfaces = InsertCoin::new(core_config, pwm_core);
@ -344,17 +338,16 @@ fn main() -> ! {
// tick timer 0 // tick timer 0
let tt0_fire_rate_hz = 9; let tt0_fire_rate_hz = 9;
let tt0_tick_per_service = interfaces.config.tick_rate_hz/(tt0_fire_rate_hz * 2); let tt0_tick_per_service = interfaces.config.tick_rate_hz / (tt0_fire_rate_hz * 2);
let tt0_service_data = TickServiceData::new(tt0_tick_per_service); let tt0_service_data = TickServiceData::new(tt0_tick_per_service);
let mut tt0 = TickTimerService::new(tt0_service_data, true); let mut tt0 = TickTimerService::new(tt0_service_data, true);
// tick timer 1 // tick timer 1
let tt1_fire_rate_hz = 3; let tt1_fire_rate_hz = 3;
let tt1_tick_per_service = interfaces.config.tick_rate_hz/(tt1_fire_rate_hz * 2); let tt1_tick_per_service = interfaces.config.tick_rate_hz / (tt1_fire_rate_hz * 2);
let tt1_service_data = TickServiceData::new(tt1_tick_per_service); let tt1_service_data = TickServiceData::new(tt1_tick_per_service);
let mut tt1 = TickTimerService::new(tt1_service_data, true); let mut tt1 = TickTimerService::new(tt1_service_data, true);
// short press timer // short press timer
let sp_ticks = 2 * interfaces.config.tick_rate_hz; let sp_ticks = 2 * interfaces.config.tick_rate_hz;
let sp_timer_data = TickServiceData::new(sp_ticks); let sp_timer_data = TickServiceData::new(sp_ticks);
@ -374,13 +367,11 @@ fn main() -> ! {
adc1_timer.reset(); adc1_timer.reset();
adc1_timer.enable(true); adc1_timer.enable(true);
let tick_interval_us = 1000000 / interfaces.config.tick_rate_hz - 10;
let tick_interval_us = 1000000/interfaces.config.tick_rate_hz - 10;
// dac data // dac data
let coin_sound = include_bytes!("../audio/sweep_dpcm_u4.raw"); let coin_sound = include_bytes!("../audio/coin.raw");
let button_sound = include_bytes!("../audio/sweep_dpcm_u4.raw"); // let button_sound = include_bytes!("../audio/coinMixTest1_dpcm_u4.raw");
let mut system_state = SystemState::Active; let mut system_state = SystemState::Active;
@ -392,14 +383,12 @@ fn main() -> ! {
use hal::pac::Interrupt; use hal::pac::Interrupt;
// use qingke_rt::CoreInterrupt; // use qingke_rt::CoreInterrupt;
// qingke::pfic::unpend_interrupt(Interrupt::EXTI7_0 as u8); // qingke::pfic::unpend_interrupt(Interrupt::EXTI7_0 as u8);
clear_interrupt(4, 6); clear_interrupt(4, 6);
qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8); qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8);
// qingke::pfic::enable_interrupt(CoreInterrupt::SysTick as u8); // qingke::pfic::enable_interrupt(CoreInterrupt::SysTick as u8);
} }
// MAIN APPLICATION // MAIN APPLICATION
// process // process
// -depress big button (insert coin button) and it wakes up and turns on led one led at a fixed brightness // -depress big button (insert coin button) and it wakes up and turns on led one led at a fixed brightness
@ -424,7 +413,6 @@ fn main() -> ! {
tt0.enable(true); tt0.enable(true);
tt1.enable(true); tt1.enable(true);
interfaces.dac.load_data(coin_sound); interfaces.dac.load_data(coin_sound);
} }
if INPUT_FLAGS.button_flag { if INPUT_FLAGS.button_flag {
println!("button flag active"); println!("button flag active");
@ -437,32 +425,27 @@ fn main() -> ! {
coin_input.service(); coin_input.service();
button_input.service(); button_input.service();
if coin_input.ready() { if coin_input.ready() {
println!("debounced coin_input value: {}", coin_input.value()); println!("debounced coin_input value: {}", coin_input.value());
coin_input.reset(); coin_input.reset();
} }
if button_input.ready() { if button_input.ready() {
let value = button_input.value(); let value = button_input.value();
button_input.reset(); button_input.reset();
println!("debounced button_input value: {}", value); println!("debounced button_input value: {}", value);
if !value { if !value {
interfaces.dac.load_data(button_sound); // interfaces.dac.load_data(button_sound);
println!("reset hold timers + enable"); println!("reset hold timers + enable");
sp_timer.reset(); sp_timer.reset();
sp_timer.enable(true); sp_timer.enable(true);
lp_timer.reset(); lp_timer.reset();
lp_timer.enable(true); lp_timer.enable(true);
} } else {
else {
sp_timer.reset(); sp_timer.reset();
lp_timer.reset(); lp_timer.reset();
} }
} }
// timers // timers
@ -500,27 +483,23 @@ fn main() -> ! {
adc1_timer.reset(); adc1_timer.reset();
adc1_timer.enable(true); adc1_timer.enable(true);
} }
} }
match system_state { match system_state {
SystemState::DeepSleep => { SystemState::DeepSleep => {
// TODO: make this REALLY deep sleep // TODO: make this REALLY deep sleep
unsafe{enter_standby(4)}; unsafe { enter_standby(4) };
loop { loop {
riscv::asm::wfi(); riscv::asm::wfi();
unsafe{ unsafe {
if INPUT_FLAGS.coin_flag { if INPUT_FLAGS.coin_flag {
break; break;
} }
}; };
} }
}, }
SystemState::Idle => { SystemState::Idle => {}
},
SystemState::Active => { SystemState::Active => {
tt0.tick(); tt0.tick();
tt1.tick(); tt1.tick();
@ -544,16 +523,14 @@ fn main() -> ! {
} }
interfaces.service(); interfaces.service();
}, }
} }
delay.delay_us(tick_interval_us as u32); delay.delay_us(tick_interval_us as u32);
} }
} }
#[panic_handler] #[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }