I have this code and it is with an error:
#![no_std]
#![no_main]
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::peripherals::{PA1, PA2, PA3, PA4};
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
use embassy_sync::blocking_mutex::Mutex;
use embassy_executor::Spawner;
use embassy_stm32::interrupt;
use embassy_time::{Duration, Timer};
use embassy_executor::{Executor, InterruptExecutor};
use embassy_stm32::interrupt::{InterruptExt, Priority};
use static_cell::StaticCell;
static LED1: Mutex<ThreadModeRawMutex, Option<Output<'static, PA1>>> = Mutex::new(None);
static LED2: Mutex<ThreadModeRawMutex, Option<Output<'static, PA2>>> = Mutex::new(None);
static LED3: Mutex<ThreadModeRawMutex, Option<Output<'static, PA3>>> = Mutex::new(None);
static LED4: Mutex<ThreadModeRawMutex, Option<Output<'static, PA4>>> = Mutex::new(None);
#[embassy_executor::task]
async fn run_high() {
loop {
LED1.lock(|led1| {
if let Some(l1) = led1.as_mut() {
l1.set_high(); // Chamada direta
}
});
LED2.lock(|led2| {
if let Some(l2) = led2.as_mut() {
l2.set_low();
}
});
LED3.lock(|led3| {
if let Some(l3) = led3.as_mut() {
l3.set_low();
}
});
LED4.lock(|led4| {
if let Some(l4) = led4.as_mut() {
l4.set_low();
}
});
Timer::after(Duration::from_millis(300)).await;
}
}
#[embassy_executor::task]
async fn run_med() {
loop {
LED1.lock(|led1| {
if let Some(l1) = led1.as_mut() {
l1.set_low();
}
});
LED2.lock(|led2| {
if let Some(l2) = led2.as_mut() {
l2.set_high();
}
});
LED3.lock(|led3| {
if let Some(l3) = led3.as_mut() {
l3.set_low();
}
});
LED4.lock(|led4| {
if let Some(l4) = led4.as_mut() {
l4.set_low();
}
});
Timer::after(Duration::from_millis(400)).await;
}
}
#[embassy_executor::task]
async fn run_low() {
loop {
LED1.lock(|led1| {
if let Some(l1) = led1.as_mut() {
l1.set_low();
}
});
LED2.lock(|led2| {
if let Some(l2) = led2.as_mut() {
l2.set_low();
}
});
LED3.lock(|led3| {
if let Some(l3) = led3.as_mut() {
l3.set_high();
}
});
LED4.lock(|led4| {
if let Some(l4) = led4.as_mut() {
l4.set_low();
}
});
Timer::after(Duration::from_millis(500)).await;
}
}
#[cortex_m_rt::entry]
fn main() -> ! {
let p = embassy_stm32::init(Default::default());
LED1.lock(|led1| {
*led1 = Some(Output::new(p.PA1, Level::Low, Speed::Low));
});
LED2.lock(|led2| {
*led2 = Some(Output::new(p.PA2, Level::Low, Speed::Low));
});
LED3.lock(|led3| {
*led3 = Some(Output::new(p.PA3, Level::Low, Speed::Low));
});
LED4.lock(|led4| {
*led4 = Some(Output::new(p.PA4, Level::Low, Speed::Low));
});
static EXECUTOR_HIGH: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_MED: InterruptExecutor = InterruptExecutor::new();
static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new();
#[interrupt]
unsafe fn USART1() {
EXECUTOR_HIGH.on_interrupt()
}
#[interrupt]
unsafe fn USART2() {
EXECUTOR_MED.on_interrupt()
}
// Start LED initialization in a separate task
let executor = EXECUTOR_LOW.init(Executor::new());
// Configure interrupts for high and medium tasks
interrupt::USART1.set_priority(interrupt::Priority::P6);
let spawner_high = EXECUTOR_HIGH.start(interrupt::USART1);
spawner_high.spawn(run_high()).unwrap();
interrupt::USART2.set_priority(interrupt::Priority::P7);
let spawner_med = EXECUTOR_MED.start(interrupt::USART2);
spawner_med.spawn(run_med()).unwrap();
executor.run(|spawner| {
spawner.spawn(run_low());
});
}
error:
error[E0596]: cannot borrow `*led1` as mutable, as it is behind a `&` reference
--> src/main.rs:598:31
|
597 | LED1.lock(|led1| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA1>>`
598 | if let Some(l1) = led1.as_mut() {
| ^^^^ `led1` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led2` as mutable, as it is behind a `&` reference
--> src/main.rs:603:31
|
602 | LED2.lock(|led2| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA2>>`
603 | if let Some(l2) = led2.as_mut() {
| ^^^^ `led2` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led3` as mutable, as it is behind a `&` reference
--> src/main.rs:608:31
|
607 | LED3.lock(|led3| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA3>>`
608 | if let Some(l3) = led3.as_mut() {
| ^^^^ `led3` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led4` as mutable, as it is behind a `&` reference
--> src/main.rs:613:31
|
612 | LED4.lock(|led4| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA4>>`
613 | if let Some(l4) = led4.as_mut() {
| ^^^^ `led4` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led1` as mutable, as it is behind a `&` reference
--> src/main.rs:626:31
|
625 | LED1.lock(|led1| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA1>>`
626 | if let Some(l1) = led1.as_mut() {
| ^^^^ `led1` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led2` as mutable, as it is behind a `&` reference
--> src/main.rs:631:31
|
630 | LED2.lock(|led2| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA2>>`
631 | if let Some(l2) = led2.as_mut() {
| ^^^^ `led2` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led3` as mutable, as it is behind a `&` reference
--> src/main.rs:636:31
|
635 | LED3.lock(|led3| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA3>>`
636 | if let Some(l3) = led3.as_mut() {
| ^^^^ `led3` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led4` as mutable, as it is behind a `&` reference
--> src/main.rs:641:31
|
640 | LED4.lock(|led4| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA4>>`
641 | if let Some(l4) = led4.as_mut() {
| ^^^^ `led4` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led1` as mutable, as it is behind a `&` reference
--> src/main.rs:654:31
|
653 | LED1.lock(|led1| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA1>>`
654 | if let Some(l1) = led1.as_mut() {
| ^^^^ `led1` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led2` as mutable, as it is behind a `&` reference
--> src/main.rs:659:31
|
658 | LED2.lock(|led2| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA2>>`
659 | if let Some(l2) = led2.as_mut() {
| ^^^^ `led2` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led3` as mutable, as it is behind a `&` reference
--> src/main.rs:664:31
|
663 | LED3.lock(|led3| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA3>>`
664 | if let Some(l3) = led3.as_mut() {
| ^^^^ `led3` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0596]: cannot borrow `*led4` as mutable, as it is behind a `&` reference
--> src/main.rs:669:31
|
668 | LED4.lock(|led4| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA4>>`
669 | if let Some(l4) = led4.as_mut() {
| ^^^^ `led4` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error[E0594]: cannot assign to `*led1`, which is behind a `&` reference
--> src/main.rs:684:9
|
683 | LED1.lock(|led1| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA1>>`
684 | *led1 = Some(Output::new(p.PA1, Level::Low, Speed::Low));
| ^^^^^ `led1` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*led2`, which is behind a `&` reference
--> src/main.rs:687:9
|
686 | LED2.lock(|led2| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA2>>`
687 | *led2 = Some(Output::new(p.PA2, Level::Low, Speed::Low));
| ^^^^^ `led2` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*led3`, which is behind a `&` reference
--> src/main.rs:690:9
|
689 | LED3.lock(|led3| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA3>>`
690 | *led3 = Some(Output::new(p.PA3, Level::Low, Speed::Low));
| ^^^^^ `led3` is a `&` reference, so the data it refers to cannot be written
error[E0594]: cannot assign to `*led4`, which is behind a `&` reference
--> src/main.rs:693:9
|
692 | LED4.lock(|led4| {
| ---- consider changing this binding's type to be: `&mut Option<Output<'_, PA4>>`
693 | *led4 = Some(Output::new(p.PA4, Level::Low, Speed::Low));
| ^^^^^ `led4` is a `&` reference, so the data it refers to cannot be written
error: aborting due to 16 previous errors; 2 warnings emitted
Some errors have detailed explanations: E0594, E0596.
For more information about an error, try `rustc --explain E0594`.
Error Failed to run cargo build: exit code = Some(101).
Can Someone please help me I really need this code but it never works. I don't know what else do to. The problem this in the mut parte of the LEDs. Thank you!