Fixed pwm timer not getting cleared

This commit is contained in:
Sebastian 2022-12-29 17:04:01 +01:00
parent 0c5614e38e
commit b37ae75b74
2 changed files with 13 additions and 9 deletions

View File

@ -5,11 +5,11 @@ edition = "2018"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
cortex-m = "0.7.6" cortex-m = { version = "0.7.6", features = ["critical-section-single-core"]}
cortex-m-rt = "0.7.2" cortex-m-rt = "0.7.2"
cortex-m-rtic = "1.1.3" cortex-m-rtic = "1.1.3"
defmt = "0.3.2" defmt = "0.3.2"
defmt-rtt = "0.3.2" defmt-rtt = "0.4"
panic-probe = { version = "0.3.0", features = ["print-defmt"] } panic-probe = { version = "0.3.0", features = ["print-defmt"] }
stm32f1xx-hal = { version = "0.9.0", features = ["stm32f103", "rt", "medium"] } stm32f1xx-hal = { version = "0.9.0", features = ["stm32f103", "rt", "medium"] }
embedded-hal = {version = "0.2.3"} embedded-hal = {version = "0.2.3"}

View File

@ -27,11 +27,11 @@ mod app {
}, },
i2c, i2c,
i2c::BlockingI2c, i2c::BlockingI2c,
pac::{ADC1, I2C1, TIM4}, pac::{ADC1, I2C1, TIM2, TIM4},
prelude::*, prelude::*,
serial::{self, Config, Serial}, serial::{self, Config, Serial},
stm32, stm32,
timer::{self, Channel, Event, Tim3NoRemap, Tim4NoRemap}, timer::{self, Channel, CounterHz, Event, Tim3NoRemap, Tim4NoRemap},
}; };
use systick_monotonic::Systick; use systick_monotonic::Systick;
@ -74,6 +74,7 @@ mod app {
q_in: gpio::Pin<Analog, CRL, 'A', 0>, q_in: gpio::Pin<Analog, CRL, 'A', 0>,
phase: f32, phase: f32,
audio_pwm: AudioPwm, audio_pwm: AudioPwm,
timer: CounterHz<TIM2>,
} }
#[init] #[init]
@ -145,7 +146,7 @@ mod app {
let mut audio_pwm = cx.device.TIM4.pwm_hz::<Tim4NoRemap, _, _>( let mut audio_pwm = cx.device.TIM4.pwm_hz::<Tim4NoRemap, _, _>(
audio_out, audio_out,
&mut afio.mapr, &mut afio.mapr,
4800.Hz(), 192.kHz(),
&clocks, &clocks,
); );
audio_pwm.enable(Channel::C3); audio_pwm.enable(Channel::C3);
@ -180,16 +181,17 @@ mod app {
q_in, q_in,
phase: 0.0, phase: 0.0,
audio_pwm, audio_pwm,
timer,
}, },
init::Monotonics(mono), init::Monotonics(mono),
) )
} }
#[task(binds=TIM2, local=[pll, i2c, adc1, mic_in, i_in, q_in, audio_pwm, phase, board_led])] #[task(binds=TIM2, local=[timer, pll, i2c, adc1, mic_in, i_in, q_in, audio_pwm, phase, board_led])]
fn transmit(mut ctx: transmit::Context) { fn transmit(mut ctx: transmit::Context) {
*ctx.local.phase += core::f32::consts::PI * 2.0 * 1000.0 / 4800.0; *ctx.local.phase += core::f32::consts::PI * 2.0 * 2000.0 / 4800.0;
if *ctx.local.phase > 2.0 * core::f32::consts::PI { if *ctx.local.phase > 2.0 * core::f32::consts::PI {
*ctx.local.phase -= core::f32::consts::PI; *ctx.local.phase -= 2.0 * core::f32::consts::PI;
ctx.local.board_led.toggle(); ctx.local.board_led.toggle();
} }
@ -201,7 +203,9 @@ mod app {
2.0.powi(16) 2.0.powi(16)
}; };
let sample = (ctx.local.phase.sin() + 0.5) * max_duty; let sample = (ctx.local.phase.sin() + 1.0) * max_duty / 2.0;
ctx.local.audio_pwm.set_duty(Channel::C3, sample as u16); ctx.local.audio_pwm.set_duty(Channel::C3, sample as u16);
ctx.local.timer.clear_interrupt(Event::Update);
} }
} }