aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-12-25 22:26:53 +0000
committerGitHub <[email protected]>2024-12-25 22:26:53 +0000
commit5cbd520ba92dade7c574cd6a44b5cce5f0fa5a5e (patch)
tree172a540a6ddb5c7b444866d8a9dcdb65203ba39a
parentfd2b6c80e39e7e07a5ec40f784bed123e3f69b06 (diff)
parent595ce48eb2be88a42ceb33865c337020a328a4dd (diff)
Merge pull request #3687 from diondokter/improve-dualcore-safety
stm32: Improve dualcore safety
-rw-r--r--embassy-stm32/src/lib.rs44
1 files changed, 42 insertions, 2 deletions
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs
index 39f3dfd61..857090303 100644
--- a/embassy-stm32/src/lib.rs
+++ b/embassy-stm32/src/lib.rs
@@ -326,10 +326,11 @@ mod dual_core {
326 /// ``` 326 /// ```
327 /// 327 ///
328 /// This static must be placed in the same position for both cores. How and where this is done is left to the user. 328 /// This static must be placed in the same position for both cores. How and where this is done is left to the user.
329 #[repr(C)]
329 pub struct SharedData { 330 pub struct SharedData {
330 init_flag: AtomicUsize, 331 init_flag: AtomicUsize,
331 clocks: UnsafeCell<MaybeUninit<Clocks>>, 332 clocks: UnsafeCell<MaybeUninit<Clocks>>,
332 config: UnsafeCell<MaybeUninit<Config>>, 333 config: UnsafeCell<MaybeUninit<SharedConfig>>,
333 } 334 }
334 335
335 unsafe impl Sync for SharedData {} 336 unsafe impl Sync for SharedData {}
@@ -349,10 +350,15 @@ mod dual_core {
349 pub fn init_primary(config: Config, shared_data: &'static MaybeUninit<SharedData>) -> Peripherals { 350 pub fn init_primary(config: Config, shared_data: &'static MaybeUninit<SharedData>) -> Peripherals {
350 let shared_data = unsafe { shared_data.assume_init_ref() }; 351 let shared_data = unsafe { shared_data.assume_init_ref() };
351 352
353 // Write the flag as soon as possible. Reading this flag uninitialized in the `init_secondary`
354 // is maybe unsound? Unclear. If it is indeed unsound, writing it sooner doesn't fix it all,
355 // but improves the odds of it going right
356 shared_data.init_flag.store(0, Ordering::SeqCst);
357
352 rcc::set_freqs_ptr(shared_data.clocks.get()); 358 rcc::set_freqs_ptr(shared_data.clocks.get());
353 let p = init_hw(config); 359 let p = init_hw(config);
354 360
355 unsafe { *shared_data.config.get() }.write(config); 361 unsafe { *shared_data.config.get() }.write(config.into());
356 362
357 shared_data.init_flag.store(INIT_DONE_FLAG, Ordering::SeqCst); 363 shared_data.init_flag.store(INIT_DONE_FLAG, Ordering::SeqCst);
358 364
@@ -424,6 +430,40 @@ mod dual_core {
424 430
425 Peripherals::take() 431 Peripherals::take()
426 } 432 }
433
434 #[repr(C)]
435 #[derive(Clone, Copy)]
436 struct SharedConfig {
437 #[cfg(bdma)]
438 bdma_interrupt_priority: Priority,
439 #[cfg(dma)]
440 dma_interrupt_priority: Priority,
441 #[cfg(gpdma)]
442 gpdma_interrupt_priority: Priority,
443 }
444
445 impl From<Config> for SharedConfig {
446 fn from(value: Config) -> Self {
447 let Config {
448 #[cfg(bdma)]
449 bdma_interrupt_priority,
450 #[cfg(dma)]
451 dma_interrupt_priority,
452 #[cfg(gpdma)]
453 gpdma_interrupt_priority,
454 ..
455 } = value;
456
457 SharedConfig {
458 #[cfg(bdma)]
459 bdma_interrupt_priority,
460 #[cfg(dma)]
461 dma_interrupt_priority,
462 #[cfg(gpdma)]
463 gpdma_interrupt_priority,
464 }
465 }
466 }
427} 467}
428 468
429#[cfg(feature = "_dual-core")] 469#[cfg(feature = "_dual-core")]