aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f4/src/bin/usb_uac_speaker.rs50
-rw-r--r--examples/stm32h5/src/bin/usb_uac_speaker.rs49
2 files changed, 61 insertions, 38 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));
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));