initial working demo of PWM dac w/ sinusoidal data
This commit is contained in:
parent
faa1622857
commit
0ac166bf5e
4 changed files with 79 additions and 6 deletions
6
ch32v-insert-coin/Cargo.lock
generated
6
ch32v-insert-coin/Cargo.lock
generated
|
|
@ -2,6 +2,10 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "adpcm-pwm-dac"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
|
|
@ -66,9 +70,9 @@ dependencies = [
|
||||||
name = "ch32v-insert-coin"
|
name = "ch32v-insert-coin"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"adpcm-pwm-dac",
|
||||||
"ch32-hal",
|
"ch32-hal",
|
||||||
"embassy-executor",
|
"embassy-executor",
|
||||||
"embassy-time",
|
|
||||||
"embedded-hal 1.0.0",
|
"embedded-hal 1.0.0",
|
||||||
"panic-halt",
|
"panic-halt",
|
||||||
"qingke-rt",
|
"qingke-rt",
|
||||||
|
|
|
||||||
|
|
@ -11,21 +11,20 @@ ch32-hal = { path = "ext/ch32-hal/", features = [
|
||||||
"time-driver-tim2",
|
"time-driver-tim2",
|
||||||
"rt",
|
"rt",
|
||||||
] }
|
] }
|
||||||
|
|
||||||
embassy-executor = { version = "0.7.0", features = [
|
embassy-executor = { version = "0.7.0", features = [
|
||||||
"arch-spin",
|
"arch-spin",
|
||||||
"executor-thread",
|
"executor-thread",
|
||||||
"task-arena-size-128", # or better use nightly, but fails on recent Rust versions
|
"task-arena-size-128", # or better use nightly, but fails on recent Rust versions
|
||||||
#"nightly",
|
|
||||||
] }
|
] }
|
||||||
|
|
||||||
embassy-time = "0.4.0"
|
|
||||||
panic-halt = "1.0"
|
panic-halt = "1.0"
|
||||||
|
|
||||||
embedded-hal = "1.0.0"
|
embedded-hal = "1.0.0"
|
||||||
|
|
||||||
qingke-rt = { version = "*", features = ["highcode"] }
|
qingke-rt = { version = "*", features = ["highcode"] }
|
||||||
|
|
||||||
|
adpcm-pwm-dac = { path = "ext/adpcm-pwm-dac/" }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
strip = false # symbols are not flashed to the microcontroller, so don't strip them.
|
strip = false # symbols are not flashed to the microcontroller, so don't strip them.
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit d06329a7259f126987355045281cbdf292258710
|
Subproject commit 8502ed75bf8d3e8df1eb5f1b5576079f55c6e13e
|
||||||
|
|
@ -6,7 +6,30 @@
|
||||||
use hal::delay::Delay;
|
use hal::delay::Delay;
|
||||||
use hal::gpio::{AnyPin, Level, Output, Pin};
|
use hal::gpio::{AnyPin, Level, Output, Pin};
|
||||||
use {ch32_hal as hal};
|
use {ch32_hal as hal};
|
||||||
|
use hal::time::Hertz;
|
||||||
|
use hal::timer::low_level::CountingMode;
|
||||||
|
use hal::timer::simple_pwm::{PwmPin, SimplePwm};
|
||||||
|
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];
|
||||||
|
|
||||||
|
struct SimplePwmDacPin<'d, T: GeneralInstance16bit>{
|
||||||
|
pin: SimplePwm<'d, T>,
|
||||||
|
ch: Channel,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
use adpcm_pwm_dac::{interface::DacInterface, dac::Dac};
|
||||||
|
|
||||||
|
impl<T> DacInterface for SimplePwmDacPin<'_, T>
|
||||||
|
where T: GeneralInstance16bit {
|
||||||
|
fn write_amplitude(&mut self, amplitude: u8) {
|
||||||
|
let max_duty = self.pin.get_max_duty();
|
||||||
|
let dc = amplitude as u32 * max_duty / 100;
|
||||||
|
self.pin.set_duty(self.ch, dc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn blink(pin: AnyPin, interval_ms: u64) {
|
fn blink(pin: AnyPin, interval_ms: u64) {
|
||||||
let mut led = Output::new(pin, Level::Low, Default::default());
|
let mut led = Output::new(pin, Level::Low, Default::default());
|
||||||
|
|
@ -26,14 +49,61 @@ fn blink(pin: AnyPin, interval_ms: u64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pwm_blink<T: GeneralInstance16bit>(mut pwm_dac_pin: SimplePwmDacPin<'_, T>) {
|
||||||
|
let mut delay = Delay;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
let interval_ms = 1000u32;
|
||||||
|
loop {
|
||||||
|
pwm_dac_pin.write_amplitude(75);
|
||||||
|
delay.delay_ms((interval_ms/2) as u32);
|
||||||
|
pwm_dac_pin.write_amplitude(50);
|
||||||
|
delay.delay_ms((interval_ms/2) as u32);
|
||||||
|
pwm_dac_pin.write_amplitude(25);
|
||||||
|
delay.delay_ms((interval_ms/2) as u32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dac_run<T: DacInterface>(mut dac: Dac<'_, T>, sample_rate: usize) {
|
||||||
|
let mut delay = Delay;
|
||||||
|
|
||||||
|
|
||||||
|
dac.load_data(&DAC_DATA);
|
||||||
|
|
||||||
|
let interval_us = 1000000/sample_rate as u32;
|
||||||
|
loop {
|
||||||
|
dac.output_next();
|
||||||
|
delay.delay_us(interval_us);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[qingke_rt::entry]
|
#[qingke_rt::entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
let mut config = hal::Config::default();
|
let mut config = hal::Config::default();
|
||||||
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSE;
|
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSE;
|
||||||
let p = hal::init(config);
|
let p = hal::init(config);
|
||||||
|
|
||||||
|
|
||||||
let mut delay = Delay;
|
let pin = PwmPin::new_ch4::<0>(p.PC4);
|
||||||
|
let ch = hal::timer::Channel::Ch4;
|
||||||
|
let mut pwm = SimplePwm::new(
|
||||||
|
p.TIM1,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Some(pin),
|
||||||
|
Hertz::khz(24),
|
||||||
|
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);
|
blink(p.PD6.degrade(), 100);
|
||||||
loop{};
|
loop{};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue