Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutating struct in ISR #390

Closed
willAlbertini707 opened this issue Mar 7, 2024 · 1 comment
Closed

Mutating struct in ISR #390

willAlbertini707 opened this issue Mar 7, 2024 · 1 comment

Comments

@willAlbertini707
Copy link

I have an ISR that is attached to a timer alarm and called every two seconds. In this ISR there is a flag (AtomicBool) and a struct I want to update. I have the struct contained in a Mutex<RefCell<>>.

use esp_idf_svc::hal;
use hal::peripherals::Peripherals;
use hal::timer::TimerDriver;
use hal::timer::config;

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use std::cell::RefCell;

static FLAG: AtomicBool = AtomicBool::new(false);

struct InterruptCount {
count: i32,
}

static COUNTER: Mutex<RefCell> = Mutex::new(RefCell::new(InterruptCount{count:1}));

fn increment_interrupt() {
hal::interrupt::free(|| {
FLAG.store(true, Ordering::Relaxed);
COUNTER.lock().unwrap().borrow_mut().count += 1;
});
}

I can change the flag (runs without COUNTER.lock().unwrap().borrow_mut().count += 1;), but get the following error when trying to increment the struct field "count":

Guru Meditation Error: Core 0 panic'ed (Interrupt wdt timeout on CPU0).

I know I am probably missing something important. The ISR works without trying to modify COUNTER. I am wondering how to safely mutate a struct in an ISR?

@ivmarkov
Copy link
Collaborator

You cannot use Rust STD mutexes from. An ISR. Use an esp idf hal CriticalSection I stead, or a Queue or post to an esp idf event loop.

@github-project-automation github-project-automation bot moved this from Todo to Done in esp-rs Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants