diff options
| author | elagil <[email protected]> | 2024-11-23 22:52:43 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2024-11-24 00:33:08 +0100 |
| commit | cc4b5ae9cb4d79f9fa378b2a06073eb7ae96d369 (patch) | |
| tree | dc29a8fa2839518af739d9028a0c2f5554f44585 /examples/stm32f4 | |
| parent | 36292ada6228bff99cb0baa916786da394e05a42 (diff) | |
feat: change SOF timer to input capture
Diffstat (limited to 'examples/stm32f4')
| -rw-r--r-- | examples/stm32f4/src/bin/usb_uac_speaker.rs | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/examples/stm32f4/src/bin/usb_uac_speaker.rs b/examples/stm32f4/src/bin/usb_uac_speaker.rs index 77c693ace..8d83afd1a 100644 --- a/examples/stm32f4/src/bin/usb_uac_speaker.rs +++ b/examples/stm32f4/src/bin/usb_uac_speaker.rs | |||
| @@ -222,25 +222,24 @@ fn TIM2() { | |||
| 222 | 222 | ||
| 223 | critical_section::with(|cs| { | 223 | critical_section::with(|cs| { |
| 224 | // Read timer counter. | 224 | // Read timer counter. |
| 225 | let ticks = TIMER.borrow(cs).borrow().as_ref().unwrap().regs_gp32().cnt().read(); | 225 | let timer = TIMER.borrow(cs).borrow().as_ref().unwrap().regs_gp32(); |
| 226 | |||
| 227 | let status = timer.sr().read(); | ||
| 228 | |||
| 229 | const CHANNEL_INDEX: usize = 0; | ||
| 230 | if status.ccif(CHANNEL_INDEX) { | ||
| 231 | let ticks = timer.ccr(CHANNEL_INDEX).read(); | ||
| 232 | |||
| 233 | *FRAME_COUNT += 1; | ||
| 234 | if *FRAME_COUNT >= FEEDBACK_REFRESH_PERIOD.frame_count() { | ||
| 235 | *FRAME_COUNT = 0; | ||
| 236 | FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(*LAST_TICKS)); | ||
| 237 | *LAST_TICKS = ticks; | ||
| 238 | } | ||
| 239 | }; | ||
| 226 | 240 | ||
| 227 | // Clear trigger interrupt flag. | 241 | // Clear trigger interrupt flag. |
| 228 | TIMER | 242 | timer.sr().modify(|r| r.set_tif(false)); |
| 229 | .borrow(cs) | ||
| 230 | .borrow_mut() | ||
| 231 | .as_mut() | ||
| 232 | .unwrap() | ||
| 233 | .regs_gp32() | ||
| 234 | .sr() | ||
| 235 | .modify(|r| r.set_tif(false)); | ||
| 236 | |||
| 237 | // Count up frames and emit a signal, when the refresh period is reached (here, every 8 ms). | ||
| 238 | *FRAME_COUNT += 1; | ||
| 239 | if *FRAME_COUNT >= FEEDBACK_REFRESH_PERIOD.frame_count() { | ||
| 240 | *FRAME_COUNT = 0; | ||
| 241 | FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(*LAST_TICKS)); | ||
| 242 | *LAST_TICKS = ticks; | ||
| 243 | } | ||
| 244 | }); | 243 | }); |
| 245 | } | 244 | } |
| 246 | 245 | ||
| @@ -355,8 +354,21 @@ async fn main(spawner: Spawner) { | |||
| 355 | let mut tim2 = timer::low_level::Timer::new(p.TIM2); | 354 | let mut tim2 = timer::low_level::Timer::new(p.TIM2); |
| 356 | tim2.set_tick_freq(Hertz(FEEDBACK_COUNTER_TICK_RATE)); | 355 | tim2.set_tick_freq(Hertz(FEEDBACK_COUNTER_TICK_RATE)); |
| 357 | tim2.set_trigger_source(timer::low_level::TriggerSource::ITR1); // The USB SOF signal. | 356 | tim2.set_trigger_source(timer::low_level::TriggerSource::ITR1); // The USB SOF signal. |
| 358 | tim2.set_slave_mode(timer::low_level::SlaveMode::TRIGGER_MODE); | 357 | |
| 359 | tim2.regs_gp16().dier().modify(|r| r.set_tie(true)); // Enable the trigger interrupt. | 358 | const TIMER_CHANNEL: timer::Channel = timer::Channel::Ch1; |
| 359 | tim2.set_input_ti_selection(TIMER_CHANNEL, timer::low_level::InputTISelection::TRC); | ||
| 360 | tim2.set_input_capture_prescaler(TIMER_CHANNEL, 0); | ||
| 361 | tim2.set_input_capture_filter(TIMER_CHANNEL, timer::low_level::FilterValue::FCK_INT_N2); | ||
| 362 | |||
| 363 | // Reset all interrupt flags. | ||
| 364 | tim2.regs_gp32().sr().write(|r| r.0 = 0); | ||
| 365 | |||
| 366 | // Enable routing of SOF to the timer. | ||
| 367 | tim2.regs_gp32().or().write(|r| *r = 0b10 << 10); | ||
| 368 | |||
| 369 | tim2.enable_channel(TIMER_CHANNEL, true); | ||
| 370 | tim2.enable_input_interrupt(TIMER_CHANNEL, true); | ||
| 371 | |||
| 360 | tim2.start(); | 372 | tim2.start(); |
| 361 | 373 | ||
| 362 | TIMER.lock(|p| p.borrow_mut().replace(tim2)); | 374 | TIMER.lock(|p| p.borrow_mut().replace(tim2)); |
