diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-12-16 16:09:01 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2024-12-16 16:15:28 +0100 |
| commit | 2846647c94d75b433232c959655b88558c560767 (patch) | |
| tree | 15d07ab18e40fcfd12c8092e31e1f5ab324f4eed /examples | |
| parent | 87280463b24ce4d50b655aa526ab034b0dc2ad61 (diff) | |
Remove use of static mut.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f4/src/bin/usb_uac_speaker.rs | 19 | ||||
| -rw-r--r-- | examples/stm32h5/src/bin/usb_uac_speaker.rs | 19 |
2 files changed, 22 insertions, 16 deletions
diff --git a/examples/stm32f4/src/bin/usb_uac_speaker.rs b/examples/stm32f4/src/bin/usb_uac_speaker.rs index 8d83afd1a..e22e07e63 100644 --- a/examples/stm32f4/src/bin/usb_uac_speaker.rs +++ b/examples/stm32f4/src/bin/usb_uac_speaker.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #![no_std] | 1 | #![no_std] |
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use core::cell::RefCell; | 4 | use core::cell::{Cell, RefCell}; |
| 5 | 5 | ||
| 6 | use defmt::{panic, *}; | 6 | use defmt::{panic, *}; |
| 7 | use embassy_executor::Spawner; | 7 | use embassy_executor::Spawner; |
| @@ -217,8 +217,8 @@ async fn usb_control_task(control_monitor: speaker::ControlMonitor<'static>) { | |||
| 217 | /// This gives an (ideal) counter value of 336.000 for every update of the `FEEDBACK_SIGNAL`. | 217 | /// This gives an (ideal) counter value of 336.000 for every update of the `FEEDBACK_SIGNAL`. |
| 218 | #[interrupt] | 218 | #[interrupt] |
| 219 | fn TIM2() { | 219 | fn TIM2() { |
| 220 | static mut LAST_TICKS: u32 = 0; | 220 | static LAST_TICKS: Mutex<CriticalSectionRawMutex, Cell<u32>> = Mutex::new(Cell::new(0)); |
| 221 | static mut FRAME_COUNT: usize = 0; | 221 | static FRAME_COUNT: Mutex<CriticalSectionRawMutex, Cell<usize>> = Mutex::new(Cell::new(0)); |
| 222 | 222 | ||
| 223 | critical_section::with(|cs| { | 223 | critical_section::with(|cs| { |
| 224 | // Read timer counter. | 224 | // Read timer counter. |
| @@ -230,11 +230,14 @@ fn TIM2() { | |||
| 230 | if status.ccif(CHANNEL_INDEX) { | 230 | if status.ccif(CHANNEL_INDEX) { |
| 231 | let ticks = timer.ccr(CHANNEL_INDEX).read(); | 231 | let ticks = timer.ccr(CHANNEL_INDEX).read(); |
| 232 | 232 | ||
| 233 | *FRAME_COUNT += 1; | 233 | let frame_count = FRAME_COUNT.borrow(cs); |
| 234 | if *FRAME_COUNT >= FEEDBACK_REFRESH_PERIOD.frame_count() { | 234 | let last_ticks = LAST_TICKS.borrow(cs); |
| 235 | *FRAME_COUNT = 0; | 235 | |
| 236 | FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(*LAST_TICKS)); | 236 | frame_count.set(frame_count.get() + 1); |
| 237 | *LAST_TICKS = ticks; | 237 | if frame_count.get() >= FEEDBACK_REFRESH_PERIOD.frame_count() { |
| 238 | frame_count.set(0); | ||
| 239 | FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(last_ticks.get())); | ||
| 240 | last_ticks.set(ticks); | ||
| 238 | } | 241 | } |
| 239 | }; | 242 | }; |
| 240 | 243 | ||
diff --git a/examples/stm32h5/src/bin/usb_uac_speaker.rs b/examples/stm32h5/src/bin/usb_uac_speaker.rs index 4fd4ccbbd..8c24fa916 100644 --- a/examples/stm32h5/src/bin/usb_uac_speaker.rs +++ b/examples/stm32h5/src/bin/usb_uac_speaker.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #![no_std] | 1 | #![no_std] |
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use core::cell::RefCell; | 4 | use core::cell::{Cell, RefCell}; |
| 5 | 5 | ||
| 6 | use defmt::{panic, *}; | 6 | use defmt::{panic, *}; |
| 7 | use embassy_executor::Spawner; | 7 | use embassy_executor::Spawner; |
| @@ -212,8 +212,8 @@ async fn usb_control_task(control_monitor: speaker::ControlMonitor<'static>) { | |||
| 212 | /// This gives an (ideal) counter value of 336.000 for every update of the `FEEDBACK_SIGNAL`. | 212 | /// This gives an (ideal) counter value of 336.000 for every update of the `FEEDBACK_SIGNAL`. |
| 213 | #[interrupt] | 213 | #[interrupt] |
| 214 | fn TIM5() { | 214 | fn TIM5() { |
| 215 | static mut LAST_TICKS: u32 = 0; | 215 | static LAST_TICKS: Mutex<CriticalSectionRawMutex, Cell<u32>> = Mutex::new(Cell::new(0)); |
| 216 | static mut FRAME_COUNT: usize = 0; | 216 | static FRAME_COUNT: Mutex<CriticalSectionRawMutex, Cell<usize>> = Mutex::new(Cell::new(0)); |
| 217 | 217 | ||
| 218 | critical_section::with(|cs| { | 218 | critical_section::with(|cs| { |
| 219 | // Read timer counter. | 219 | // Read timer counter. |
| @@ -225,11 +225,14 @@ fn TIM5() { | |||
| 225 | if status.ccif(CHANNEL_INDEX) { | 225 | if status.ccif(CHANNEL_INDEX) { |
| 226 | let ticks = timer.ccr(CHANNEL_INDEX).read(); | 226 | let ticks = timer.ccr(CHANNEL_INDEX).read(); |
| 227 | 227 | ||
| 228 | *FRAME_COUNT += 1; | 228 | let frame_count = FRAME_COUNT.borrow(cs); |
| 229 | if *FRAME_COUNT >= FEEDBACK_REFRESH_PERIOD.frame_count() { | 229 | let last_ticks = LAST_TICKS.borrow(cs); |
| 230 | *FRAME_COUNT = 0; | 230 | |
| 231 | FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(*LAST_TICKS)); | 231 | frame_count.set(frame_count.get() + 1); |
| 232 | *LAST_TICKS = ticks; | 232 | if frame_count.get() >= FEEDBACK_REFRESH_PERIOD.frame_count() { |
| 233 | frame_count.set(0); | ||
| 234 | FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(last_ticks.get())); | ||
| 235 | last_ticks.set(ticks); | ||
| 233 | } | 236 | } |
| 234 | }; | 237 | }; |
| 235 | 238 | ||
