diff --git a/Cargo.toml b/Cargo.toml index 5061f46..c77a2cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,11 +5,11 @@ edition = "2018" version = "0.1.0" [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-rtic = "1.1.3" defmt = "0.3.2" -defmt-rtt = "0.3.2" +defmt-rtt = "0.4" panic-probe = { version = "0.3.0", features = ["print-defmt"] } stm32f1xx-hal = { version = "0.9.0", features = ["stm32f103", "rt", "medium"] } embedded-hal = {version = "0.2.3"} diff --git a/src/main.rs b/src/main.rs index c598944..48e6ee6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,11 +27,11 @@ mod app { }, i2c, i2c::BlockingI2c, - pac::{ADC1, I2C1, TIM4}, + pac::{ADC1, I2C1, TIM2, TIM4}, prelude::*, serial::{self, Config, Serial}, stm32, - timer::{self, Channel, Event, Tim3NoRemap, Tim4NoRemap}, + timer::{self, Channel, CounterHz, Event, Tim3NoRemap, Tim4NoRemap}, }; use systick_monotonic::Systick; @@ -74,6 +74,7 @@ mod app { q_in: gpio::Pin, phase: f32, audio_pwm: AudioPwm, + timer: CounterHz, } #[init] @@ -145,7 +146,7 @@ mod app { let mut audio_pwm = cx.device.TIM4.pwm_hz::( audio_out, &mut afio.mapr, - 4800.Hz(), + 192.kHz(), &clocks, ); audio_pwm.enable(Channel::C3); @@ -180,16 +181,17 @@ mod app { q_in, phase: 0.0, audio_pwm, + timer, }, 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) { - *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 { - *ctx.local.phase -= core::f32::consts::PI; + *ctx.local.phase -= 2.0 * core::f32::consts::PI; ctx.local.board_led.toggle(); } @@ -201,7 +203,9 @@ mod app { 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.timer.clear_interrupt(Event::Update); } }