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

stm32: Improve dualcore safety #3687

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions embassy-stm32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,11 @@ mod dual_core {
/// ```
///
/// This static must be placed in the same position for both cores. How and where this is done is left to the user.
#[repr(C)]
pub struct SharedData {
init_flag: AtomicUsize,
clocks: UnsafeCell<MaybeUninit<Clocks>>,
config: UnsafeCell<MaybeUninit<Config>>,
config: UnsafeCell<MaybeUninit<SharedConfig>>,
}

unsafe impl Sync for SharedData {}
Expand All @@ -349,10 +350,15 @@ mod dual_core {
pub fn init_primary(config: Config, shared_data: &'static MaybeUninit<SharedData>) -> Peripherals {
let shared_data = unsafe { shared_data.assume_init_ref() };

// Write the flag as soon as possible. Reading this flag uninitialized in the `init_secondary`
// is maybe unsound? Unclear. If it is indeed unsound, writing it sooner doesn't fix it all,
// but improves the odds of it going right
shared_data.init_flag.store(0, Ordering::SeqCst);

rcc::set_freqs_ptr(shared_data.clocks.get());
let p = init_hw(config);

unsafe { *shared_data.config.get() }.write(config);
unsafe { *shared_data.config.get() }.write(config.into());

shared_data.init_flag.store(INIT_DONE_FLAG, Ordering::SeqCst);

Expand Down Expand Up @@ -424,6 +430,40 @@ mod dual_core {

Peripherals::take()
}

#[repr(C)]
#[derive(Clone, Copy)]
struct SharedConfig {
#[cfg(bdma)]
bdma_interrupt_priority: Priority,
#[cfg(dma)]
dma_interrupt_priority: Priority,
#[cfg(gpdma)]
gpdma_interrupt_priority: Priority,
}

impl From<Config> for SharedConfig {
fn from(value: Config) -> Self {
let Config {
#[cfg(bdma)]
bdma_interrupt_priority,
#[cfg(dma)]
dma_interrupt_priority,
#[cfg(gpdma)]
gpdma_interrupt_priority,
..
} = value;

SharedConfig {
#[cfg(bdma)]
bdma_interrupt_priority,
#[cfg(dma)]
dma_interrupt_priority,
#[cfg(gpdma)]
gpdma_interrupt_priority,
}
}
}
}

#[cfg(feature = "_dual-core")]
Expand Down
Loading