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/stm32h5 | |
| parent | 36292ada6228bff99cb0baa916786da394e05a42 (diff) | |
feat: change SOF timer to input capture
Diffstat (limited to 'examples/stm32h5')
| -rw-r--r-- | examples/stm32h5/src/bin/usb_uac_speaker.rs | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/examples/stm32h5/src/bin/usb_uac_speaker.rs b/examples/stm32h5/src/bin/usb_uac_speaker.rs index 6b992690f..4fd4ccbbd 100644 --- a/examples/stm32h5/src/bin/usb_uac_speaker.rs +++ b/examples/stm32h5/src/bin/usb_uac_speaker.rs | |||
| @@ -94,6 +94,8 @@ async fn feedback_handler<'d, T: usb::Instance + 'd>( | |||
| 94 | 94 | ||
| 95 | let value = value as u32; | 95 | let value = value as u32; |
| 96 | 96 | ||
| 97 | debug!("Feedback value: {}", value); | ||
| 98 | |||
| 97 | packet.push(value as u8).unwrap(); | 99 | packet.push(value as u8).unwrap(); |
| 98 | packet.push((value >> 8) as u8).unwrap(); | 100 | packet.push((value >> 8) as u8).unwrap(); |
| 99 | packet.push((value >> 16) as u8).unwrap(); | 101 | packet.push((value >> 16) as u8).unwrap(); |
| @@ -215,25 +217,24 @@ fn TIM5() { | |||
| 215 | 217 | ||
| 216 | critical_section::with(|cs| { | 218 | critical_section::with(|cs| { |
| 217 | // Read timer counter. | 219 | // Read timer counter. |
| 218 | let ticks = TIMER.borrow(cs).borrow().as_ref().unwrap().regs_gp32().cnt().read(); | 220 | let timer = TIMER.borrow(cs).borrow().as_ref().unwrap().regs_gp32(); |
| 221 | |||
| 222 | let status = timer.sr().read(); | ||
| 223 | |||
| 224 | const CHANNEL_INDEX: usize = 0; | ||
| 225 | if status.ccif(CHANNEL_INDEX) { | ||
| 226 | let ticks = timer.ccr(CHANNEL_INDEX).read(); | ||
| 227 | |||
| 228 | *FRAME_COUNT += 1; | ||
| 229 | if *FRAME_COUNT >= FEEDBACK_REFRESH_PERIOD.frame_count() { | ||
| 230 | *FRAME_COUNT = 0; | ||
| 231 | FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(*LAST_TICKS)); | ||
| 232 | *LAST_TICKS = ticks; | ||
| 233 | } | ||
| 234 | }; | ||
| 219 | 235 | ||
| 220 | // Clear trigger interrupt flag. | 236 | // Clear trigger interrupt flag. |
| 221 | TIMER | 237 | timer.sr().modify(|r| r.set_tif(false)); |
| 222 | .borrow(cs) | ||
| 223 | .borrow_mut() | ||
| 224 | .as_mut() | ||
| 225 | .unwrap() | ||
| 226 | .regs_gp32() | ||
| 227 | .sr() | ||
| 228 | .modify(|r| r.set_tif(false)); | ||
| 229 | |||
| 230 | // Count up frames and emit a signal, when the refresh period is reached (here, every 8 ms). | ||
| 231 | *FRAME_COUNT += 1; | ||
| 232 | if *FRAME_COUNT >= FEEDBACK_REFRESH_PERIOD.frame_count() { | ||
| 233 | *FRAME_COUNT = 0; | ||
| 234 | FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(*LAST_TICKS)); | ||
| 235 | *LAST_TICKS = ticks; | ||
| 236 | } | ||
| 237 | }); | 238 | }); |
| 238 | } | 239 | } |
| 239 | 240 | ||
| @@ -347,8 +348,18 @@ async fn main(spawner: Spawner) { | |||
| 347 | let mut tim5 = timer::low_level::Timer::new(p.TIM5); | 348 | let mut tim5 = timer::low_level::Timer::new(p.TIM5); |
| 348 | tim5.set_tick_freq(Hertz(FEEDBACK_COUNTER_TICK_RATE)); | 349 | tim5.set_tick_freq(Hertz(FEEDBACK_COUNTER_TICK_RATE)); |
| 349 | tim5.set_trigger_source(timer::low_level::TriggerSource::ITR12); // The USB SOF signal. | 350 | tim5.set_trigger_source(timer::low_level::TriggerSource::ITR12); // The USB SOF signal. |
| 350 | tim5.set_slave_mode(timer::low_level::SlaveMode::TRIGGER_MODE); | 351 | |
| 351 | tim5.regs_gp16().dier().modify(|r| r.set_tie(true)); // Enable the trigger interrupt. | 352 | const TIMER_CHANNEL: timer::Channel = timer::Channel::Ch1; |
| 353 | tim5.set_input_ti_selection(TIMER_CHANNEL, timer::low_level::InputTISelection::TRC); | ||
| 354 | tim5.set_input_capture_prescaler(TIMER_CHANNEL, 0); | ||
| 355 | tim5.set_input_capture_filter(TIMER_CHANNEL, timer::low_level::FilterValue::FCK_INT_N2); | ||
| 356 | |||
| 357 | // Reset all interrupt flags. | ||
| 358 | tim5.regs_gp32().sr().write(|r| r.0 = 0); | ||
| 359 | |||
| 360 | tim5.enable_channel(TIMER_CHANNEL, true); | ||
| 361 | tim5.enable_input_interrupt(TIMER_CHANNEL, true); | ||
| 362 | |||
| 352 | tim5.start(); | 363 | tim5.start(); |
| 353 | 364 | ||
| 354 | TIMER.lock(|p| p.borrow_mut().replace(tim5)); | 365 | TIMER.lock(|p| p.borrow_mut().replace(tim5)); |
