Remove use of static mut.

This commit is contained in:
Dario Nieuwenhuis
2024-12-16 16:09:01 +01:00
parent 87280463b2
commit 2846647c94
2 changed files with 22 additions and 16 deletions

View File

@@ -1,7 +1,7 @@
#![no_std]
#![no_main]
use core::cell::RefCell;
use core::cell::{Cell, RefCell};
use defmt::{panic, *};
use embassy_executor::Spawner;
@@ -217,8 +217,8 @@ async fn usb_control_task(control_monitor: speaker::ControlMonitor<'static>) {
/// This gives an (ideal) counter value of 336.000 for every update of the `FEEDBACK_SIGNAL`.
#[interrupt]
fn TIM2() {
static mut LAST_TICKS: u32 = 0;
static mut FRAME_COUNT: usize = 0;
static LAST_TICKS: Mutex<CriticalSectionRawMutex, Cell<u32>> = Mutex::new(Cell::new(0));
static FRAME_COUNT: Mutex<CriticalSectionRawMutex, Cell<usize>> = Mutex::new(Cell::new(0));
critical_section::with(|cs| {
// Read timer counter.
@@ -230,11 +230,14 @@ fn TIM2() {
if status.ccif(CHANNEL_INDEX) {
let ticks = timer.ccr(CHANNEL_INDEX).read();
*FRAME_COUNT += 1;
if *FRAME_COUNT >= FEEDBACK_REFRESH_PERIOD.frame_count() {
*FRAME_COUNT = 0;
FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(*LAST_TICKS));
*LAST_TICKS = ticks;
let frame_count = FRAME_COUNT.borrow(cs);
let last_ticks = LAST_TICKS.borrow(cs);
frame_count.set(frame_count.get() + 1);
if frame_count.get() >= FEEDBACK_REFRESH_PERIOD.frame_count() {
frame_count.set(0);
FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(last_ticks.get()));
last_ticks.set(ticks);
}
};

View File

@@ -1,7 +1,7 @@
#![no_std]
#![no_main]
use core::cell::RefCell;
use core::cell::{Cell, RefCell};
use defmt::{panic, *};
use embassy_executor::Spawner;
@@ -212,8 +212,8 @@ async fn usb_control_task(control_monitor: speaker::ControlMonitor<'static>) {
/// This gives an (ideal) counter value of 336.000 for every update of the `FEEDBACK_SIGNAL`.
#[interrupt]
fn TIM5() {
static mut LAST_TICKS: u32 = 0;
static mut FRAME_COUNT: usize = 0;
static LAST_TICKS: Mutex<CriticalSectionRawMutex, Cell<u32>> = Mutex::new(Cell::new(0));
static FRAME_COUNT: Mutex<CriticalSectionRawMutex, Cell<usize>> = Mutex::new(Cell::new(0));
critical_section::with(|cs| {
// Read timer counter.
@@ -225,11 +225,14 @@ fn TIM5() {
if status.ccif(CHANNEL_INDEX) {
let ticks = timer.ccr(CHANNEL_INDEX).read();
*FRAME_COUNT += 1;
if *FRAME_COUNT >= FEEDBACK_REFRESH_PERIOD.frame_count() {
*FRAME_COUNT = 0;
FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(*LAST_TICKS));
*LAST_TICKS = ticks;
let frame_count = FRAME_COUNT.borrow(cs);
let last_ticks = LAST_TICKS.borrow(cs);
frame_count.set(frame_count.get() + 1);
if frame_count.get() >= FEEDBACK_REFRESH_PERIOD.frame_count() {
frame_count.set(0);
FEEDBACK_SIGNAL.signal(ticks.wrapping_sub(last_ticks.get()));
last_ticks.set(ticks);
}
};