Skip to content

Commit 059c7b5

Browse files
authored
Merge pull request #3158 from diondokter/stm-dualcore
Stm dualcore better init
2 parents a94f197 + e322732 commit 059c7b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+737
-108
lines changed

ci.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ cargo batch \
179179
--- build --release --manifest-path embassy-boot-nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features embassy-nrf/nrf9160-ns \
180180
--- build --release --manifest-path embassy-boot-nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features embassy-nrf/nrf9120-ns \
181181
--- build --release --manifest-path embassy-boot-rp/Cargo.toml --target thumbv6m-none-eabi \
182-
--- build --release --manifest-path embassy-boot-stm32/Cargo.toml --target thumbv7em-none-eabi --features embassy-stm32/stm32wl55jc-cm4 \
182+
--- build --release --manifest-path embassy-boot-stm32/Cargo.toml --target thumbv7em-none-eabi --features embassy-stm32/stm32l496zg \
183183
--- build --release --manifest-path docs/examples/basic/Cargo.toml --target thumbv7em-none-eabi \
184184
--- build --release --manifest-path docs/examples/layer-by-layer/blinky-pac/Cargo.toml --target thumbv7em-none-eabi \
185185
--- build --release --manifest-path docs/examples/layer-by-layer/blinky-hal/Cargo.toml --target thumbv7em-none-eabi \
@@ -207,6 +207,8 @@ cargo batch \
207207
--- build --release --manifest-path examples/stm32h5/Cargo.toml --target thumbv8m.main-none-eabihf --out-dir out/examples/stm32h5 \
208208
--- build --release --manifest-path examples/stm32h7/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/stm32h7 \
209209
--- build --release --manifest-path examples/stm32h735/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/stm32h735 \
210+
--- build --release --manifest-path examples/stm32h755cm4/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/stm32h755cm4 \
211+
--- build --release --manifest-path examples/stm32h755cm7/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/stm32h755cm7 \
210212
--- build --release --manifest-path examples/stm32h7rs/Cargo.toml --target thumbv7em-none-eabi --out-dir out/examples/stm32h7rs \
211213
--- build --release --manifest-path examples/stm32l0/Cargo.toml --target thumbv6m-none-eabi --out-dir out/examples/stm32l0 \
212214
--- build --release --manifest-path examples/stm32l1/Cargo.toml --target thumbv7m-none-eabi --out-dir out/examples/stm32l1 \
@@ -233,9 +235,9 @@ cargo batch \
233235
--- build --release --manifest-path examples/boot/bootloader/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features embassy-nrf/nrf9160-ns \
234236
--- build --release --manifest-path examples/boot/bootloader/nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features embassy-nrf/nrf9120-ns \
235237
--- build --release --manifest-path examples/boot/bootloader/rp/Cargo.toml --target thumbv6m-none-eabi \
236-
--- build --release --manifest-path examples/boot/bootloader/stm32/Cargo.toml --target thumbv7em-none-eabi --features embassy-stm32/stm32wl55jc-cm4 \
238+
--- build --release --manifest-path examples/boot/bootloader/stm32/Cargo.toml --target thumbv7em-none-eabi --features embassy-stm32/stm32l496zg \
237239
--- build --release --manifest-path examples/boot/bootloader/stm32wb-dfu/Cargo.toml --target thumbv7em-none-eabi --features embassy-stm32/stm32wb55rg \
238-
--- build --release --manifest-path examples/boot/bootloader/stm32-dual-bank/Cargo.toml --target thumbv7em-none-eabi --features embassy-stm32/stm32h747xi-cm7 \
240+
--- build --release --manifest-path examples/boot/bootloader/stm32-dual-bank/Cargo.toml --target thumbv7em-none-eabi --features embassy-stm32/stm32h743zi \
239241
--- build --release --manifest-path examples/wasm/Cargo.toml --target wasm32-unknown-unknown --out-dir out/examples/wasm \
240242
--- build --release --manifest-path tests/stm32/Cargo.toml --target thumbv7m-none-eabi --features stm32f103c8 --out-dir out/tests/stm32f103c8 \
241243
--- build --release --manifest-path tests/stm32/Cargo.toml --target thumbv7em-none-eabi --features stm32f429zi --out-dir out/tests/stm32f429zi \

embassy-stm32/Cargo.toml

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ stm32-fmc = "0.3.0"
8080
cfg-if = "1.0.0"
8181
embedded-io = { version = "0.6.0" }
8282
embedded-io-async = { version = "0.6.1" }
83-
chrono = { version = "^0.4", default-features = false, optional = true}
83+
chrono = { version = "^0.4", default-features = false, optional = true }
8484
bit_field = "0.10.2"
8585
document-features = "0.2.7"
8686

@@ -181,6 +181,9 @@ split-pc3 = ["_split-pins-enabled"]
181181
## internal use only
182182
_split-pins-enabled = []
183183

184+
## internal use only
185+
_dual-core = []
186+
184187
#! ## Chip-selection features
185188
#! Select your chip by specifying the model as a feature, e.g. `stm32c011d6`.
186189
#! Check the `Cargo.toml` for the latest list of supported chips.
@@ -1004,40 +1007,40 @@ stm32h743xg = [ "stm32-metapac/stm32h743xg" ]
10041007
stm32h743xi = [ "stm32-metapac/stm32h743xi" ]
10051008
stm32h743zg = [ "stm32-metapac/stm32h743zg" ]
10061009
stm32h743zi = [ "stm32-metapac/stm32h743zi" ]
1007-
stm32h745bg-cm7 = [ "stm32-metapac/stm32h745bg-cm7" ]
1008-
stm32h745bg-cm4 = [ "stm32-metapac/stm32h745bg-cm4" ]
1009-
stm32h745bi-cm7 = [ "stm32-metapac/stm32h745bi-cm7" ]
1010-
stm32h745bi-cm4 = [ "stm32-metapac/stm32h745bi-cm4" ]
1011-
stm32h745ig-cm7 = [ "stm32-metapac/stm32h745ig-cm7" ]
1012-
stm32h745ig-cm4 = [ "stm32-metapac/stm32h745ig-cm4" ]
1013-
stm32h745ii-cm7 = [ "stm32-metapac/stm32h745ii-cm7" ]
1014-
stm32h745ii-cm4 = [ "stm32-metapac/stm32h745ii-cm4" ]
1015-
stm32h745xg-cm7 = [ "stm32-metapac/stm32h745xg-cm7" ]
1016-
stm32h745xg-cm4 = [ "stm32-metapac/stm32h745xg-cm4" ]
1017-
stm32h745xi-cm7 = [ "stm32-metapac/stm32h745xi-cm7" ]
1018-
stm32h745xi-cm4 = [ "stm32-metapac/stm32h745xi-cm4" ]
1019-
stm32h745zg-cm7 = [ "stm32-metapac/stm32h745zg-cm7" ]
1020-
stm32h745zg-cm4 = [ "stm32-metapac/stm32h745zg-cm4" ]
1021-
stm32h745zi-cm7 = [ "stm32-metapac/stm32h745zi-cm7" ]
1022-
stm32h745zi-cm4 = [ "stm32-metapac/stm32h745zi-cm4" ]
1023-
stm32h747ag-cm7 = [ "stm32-metapac/stm32h747ag-cm7" ]
1024-
stm32h747ag-cm4 = [ "stm32-metapac/stm32h747ag-cm4" ]
1025-
stm32h747ai-cm7 = [ "stm32-metapac/stm32h747ai-cm7" ]
1026-
stm32h747ai-cm4 = [ "stm32-metapac/stm32h747ai-cm4" ]
1027-
stm32h747bg-cm7 = [ "stm32-metapac/stm32h747bg-cm7" ]
1028-
stm32h747bg-cm4 = [ "stm32-metapac/stm32h747bg-cm4" ]
1029-
stm32h747bi-cm7 = [ "stm32-metapac/stm32h747bi-cm7" ]
1030-
stm32h747bi-cm4 = [ "stm32-metapac/stm32h747bi-cm4" ]
1031-
stm32h747ig-cm7 = [ "stm32-metapac/stm32h747ig-cm7" ]
1032-
stm32h747ig-cm4 = [ "stm32-metapac/stm32h747ig-cm4" ]
1033-
stm32h747ii-cm7 = [ "stm32-metapac/stm32h747ii-cm7" ]
1034-
stm32h747ii-cm4 = [ "stm32-metapac/stm32h747ii-cm4" ]
1035-
stm32h747xg-cm7 = [ "stm32-metapac/stm32h747xg-cm7" ]
1036-
stm32h747xg-cm4 = [ "stm32-metapac/stm32h747xg-cm4" ]
1037-
stm32h747xi-cm7 = [ "stm32-metapac/stm32h747xi-cm7" ]
1038-
stm32h747xi-cm4 = [ "stm32-metapac/stm32h747xi-cm4" ]
1039-
stm32h747zi-cm7 = [ "stm32-metapac/stm32h747zi-cm7" ]
1040-
stm32h747zi-cm4 = [ "stm32-metapac/stm32h747zi-cm4" ]
1010+
stm32h745bg-cm7 = [ "stm32-metapac/stm32h745bg-cm7", "_dual-core" ]
1011+
stm32h745bg-cm4 = [ "stm32-metapac/stm32h745bg-cm4", "_dual-core" ]
1012+
stm32h745bi-cm7 = [ "stm32-metapac/stm32h745bi-cm7", "_dual-core" ]
1013+
stm32h745bi-cm4 = [ "stm32-metapac/stm32h745bi-cm4", "_dual-core" ]
1014+
stm32h745ig-cm7 = [ "stm32-metapac/stm32h745ig-cm7", "_dual-core" ]
1015+
stm32h745ig-cm4 = [ "stm32-metapac/stm32h745ig-cm4", "_dual-core" ]
1016+
stm32h745ii-cm7 = [ "stm32-metapac/stm32h745ii-cm7", "_dual-core" ]
1017+
stm32h745ii-cm4 = [ "stm32-metapac/stm32h745ii-cm4", "_dual-core" ]
1018+
stm32h745xg-cm7 = [ "stm32-metapac/stm32h745xg-cm7", "_dual-core" ]
1019+
stm32h745xg-cm4 = [ "stm32-metapac/stm32h745xg-cm4", "_dual-core" ]
1020+
stm32h745xi-cm7 = [ "stm32-metapac/stm32h745xi-cm7", "_dual-core" ]
1021+
stm32h745xi-cm4 = [ "stm32-metapac/stm32h745xi-cm4", "_dual-core" ]
1022+
stm32h745zg-cm7 = [ "stm32-metapac/stm32h745zg-cm7", "_dual-core" ]
1023+
stm32h745zg-cm4 = [ "stm32-metapac/stm32h745zg-cm4", "_dual-core" ]
1024+
stm32h745zi-cm7 = [ "stm32-metapac/stm32h745zi-cm7", "_dual-core" ]
1025+
stm32h745zi-cm4 = [ "stm32-metapac/stm32h745zi-cm4", "_dual-core" ]
1026+
stm32h747ag-cm7 = [ "stm32-metapac/stm32h747ag-cm7", "_dual-core" ]
1027+
stm32h747ag-cm4 = [ "stm32-metapac/stm32h747ag-cm4", "_dual-core" ]
1028+
stm32h747ai-cm7 = [ "stm32-metapac/stm32h747ai-cm7", "_dual-core" ]
1029+
stm32h747ai-cm4 = [ "stm32-metapac/stm32h747ai-cm4", "_dual-core" ]
1030+
stm32h747bg-cm7 = [ "stm32-metapac/stm32h747bg-cm7", "_dual-core" ]
1031+
stm32h747bg-cm4 = [ "stm32-metapac/stm32h747bg-cm4", "_dual-core" ]
1032+
stm32h747bi-cm7 = [ "stm32-metapac/stm32h747bi-cm7", "_dual-core" ]
1033+
stm32h747bi-cm4 = [ "stm32-metapac/stm32h747bi-cm4", "_dual-core" ]
1034+
stm32h747ig-cm7 = [ "stm32-metapac/stm32h747ig-cm7", "_dual-core" ]
1035+
stm32h747ig-cm4 = [ "stm32-metapac/stm32h747ig-cm4", "_dual-core" ]
1036+
stm32h747ii-cm7 = [ "stm32-metapac/stm32h747ii-cm7", "_dual-core" ]
1037+
stm32h747ii-cm4 = [ "stm32-metapac/stm32h747ii-cm4", "_dual-core" ]
1038+
stm32h747xg-cm7 = [ "stm32-metapac/stm32h747xg-cm7", "_dual-core" ]
1039+
stm32h747xg-cm4 = [ "stm32-metapac/stm32h747xg-cm4", "_dual-core" ]
1040+
stm32h747xi-cm7 = [ "stm32-metapac/stm32h747xi-cm7", "_dual-core" ]
1041+
stm32h747xi-cm4 = [ "stm32-metapac/stm32h747xi-cm4", "_dual-core" ]
1042+
stm32h747zi-cm7 = [ "stm32-metapac/stm32h747zi-cm7", "_dual-core" ]
1043+
stm32h747zi-cm4 = [ "stm32-metapac/stm32h747zi-cm4", "_dual-core" ]
10411044
stm32h750ib = [ "stm32-metapac/stm32h750ib" ]
10421045
stm32h750vb = [ "stm32-metapac/stm32h750vb" ]
10431046
stm32h750xb = [ "stm32-metapac/stm32h750xb" ]
@@ -1048,24 +1051,24 @@ stm32h753ii = [ "stm32-metapac/stm32h753ii" ]
10481051
stm32h753vi = [ "stm32-metapac/stm32h753vi" ]
10491052
stm32h753xi = [ "stm32-metapac/stm32h753xi" ]
10501053
stm32h753zi = [ "stm32-metapac/stm32h753zi" ]
1051-
stm32h755bi-cm7 = [ "stm32-metapac/stm32h755bi-cm7" ]
1052-
stm32h755bi-cm4 = [ "stm32-metapac/stm32h755bi-cm4" ]
1053-
stm32h755ii-cm7 = [ "stm32-metapac/stm32h755ii-cm7" ]
1054-
stm32h755ii-cm4 = [ "stm32-metapac/stm32h755ii-cm4" ]
1055-
stm32h755xi-cm7 = [ "stm32-metapac/stm32h755xi-cm7" ]
1056-
stm32h755xi-cm4 = [ "stm32-metapac/stm32h755xi-cm4" ]
1057-
stm32h755zi-cm7 = [ "stm32-metapac/stm32h755zi-cm7" ]
1058-
stm32h755zi-cm4 = [ "stm32-metapac/stm32h755zi-cm4" ]
1059-
stm32h757ai-cm7 = [ "stm32-metapac/stm32h757ai-cm7" ]
1060-
stm32h757ai-cm4 = [ "stm32-metapac/stm32h757ai-cm4" ]
1061-
stm32h757bi-cm7 = [ "stm32-metapac/stm32h757bi-cm7" ]
1062-
stm32h757bi-cm4 = [ "stm32-metapac/stm32h757bi-cm4" ]
1063-
stm32h757ii-cm7 = [ "stm32-metapac/stm32h757ii-cm7" ]
1064-
stm32h757ii-cm4 = [ "stm32-metapac/stm32h757ii-cm4" ]
1065-
stm32h757xi-cm7 = [ "stm32-metapac/stm32h757xi-cm7" ]
1066-
stm32h757xi-cm4 = [ "stm32-metapac/stm32h757xi-cm4" ]
1067-
stm32h757zi-cm7 = [ "stm32-metapac/stm32h757zi-cm7" ]
1068-
stm32h757zi-cm4 = [ "stm32-metapac/stm32h757zi-cm4" ]
1054+
stm32h755bi-cm7 = [ "stm32-metapac/stm32h755bi-cm7", "_dual-core" ]
1055+
stm32h755bi-cm4 = [ "stm32-metapac/stm32h755bi-cm4", "_dual-core" ]
1056+
stm32h755ii-cm7 = [ "stm32-metapac/stm32h755ii-cm7", "_dual-core" ]
1057+
stm32h755ii-cm4 = [ "stm32-metapac/stm32h755ii-cm4", "_dual-core" ]
1058+
stm32h755xi-cm7 = [ "stm32-metapac/stm32h755xi-cm7", "_dual-core" ]
1059+
stm32h755xi-cm4 = [ "stm32-metapac/stm32h755xi-cm4", "_dual-core" ]
1060+
stm32h755zi-cm7 = [ "stm32-metapac/stm32h755zi-cm7", "_dual-core" ]
1061+
stm32h755zi-cm4 = [ "stm32-metapac/stm32h755zi-cm4", "_dual-core" ]
1062+
stm32h757ai-cm7 = [ "stm32-metapac/stm32h757ai-cm7", "_dual-core" ]
1063+
stm32h757ai-cm4 = [ "stm32-metapac/stm32h757ai-cm4", "_dual-core" ]
1064+
stm32h757bi-cm7 = [ "stm32-metapac/stm32h757bi-cm7", "_dual-core" ]
1065+
stm32h757bi-cm4 = [ "stm32-metapac/stm32h757bi-cm4", "_dual-core" ]
1066+
stm32h757ii-cm7 = [ "stm32-metapac/stm32h757ii-cm7", "_dual-core" ]
1067+
stm32h757ii-cm4 = [ "stm32-metapac/stm32h757ii-cm4", "_dual-core" ]
1068+
stm32h757xi-cm7 = [ "stm32-metapac/stm32h757xi-cm7", "_dual-core" ]
1069+
stm32h757xi-cm4 = [ "stm32-metapac/stm32h757xi-cm4", "_dual-core" ]
1070+
stm32h757zi-cm7 = [ "stm32-metapac/stm32h757zi-cm7", "_dual-core" ]
1071+
stm32h757zi-cm4 = [ "stm32-metapac/stm32h757zi-cm4", "_dual-core" ]
10691072
stm32h7a3ag = [ "stm32-metapac/stm32h7a3ag" ]
10701073
stm32h7a3ai = [ "stm32-metapac/stm32h7a3ai" ]
10711074
stm32h7a3ig = [ "stm32-metapac/stm32h7a3ig" ]
@@ -1598,14 +1601,14 @@ stm32wba55he = [ "stm32-metapac/stm32wba55he" ]
15981601
stm32wba55hg = [ "stm32-metapac/stm32wba55hg" ]
15991602
stm32wba55ue = [ "stm32-metapac/stm32wba55ue" ]
16001603
stm32wba55ug = [ "stm32-metapac/stm32wba55ug" ]
1601-
stm32wl54cc-cm4 = [ "stm32-metapac/stm32wl54cc-cm4" ]
1602-
stm32wl54cc-cm0p = [ "stm32-metapac/stm32wl54cc-cm0p" ]
1603-
stm32wl54jc-cm4 = [ "stm32-metapac/stm32wl54jc-cm4" ]
1604-
stm32wl54jc-cm0p = [ "stm32-metapac/stm32wl54jc-cm0p" ]
1605-
stm32wl55cc-cm4 = [ "stm32-metapac/stm32wl55cc-cm4" ]
1606-
stm32wl55cc-cm0p = [ "stm32-metapac/stm32wl55cc-cm0p" ]
1607-
stm32wl55jc-cm4 = [ "stm32-metapac/stm32wl55jc-cm4" ]
1608-
stm32wl55jc-cm0p = [ "stm32-metapac/stm32wl55jc-cm0p" ]
1604+
stm32wl54cc-cm4 = [ "stm32-metapac/stm32wl54cc-cm4", "_dual-core" ]
1605+
stm32wl54cc-cm0p = [ "stm32-metapac/stm32wl54cc-cm0p", "_dual-core" ]
1606+
stm32wl54jc-cm4 = [ "stm32-metapac/stm32wl54jc-cm4", "_dual-core" ]
1607+
stm32wl54jc-cm0p = [ "stm32-metapac/stm32wl54jc-cm0p", "_dual-core" ]
1608+
stm32wl55cc-cm4 = [ "stm32-metapac/stm32wl55cc-cm4", "_dual-core" ]
1609+
stm32wl55cc-cm0p = [ "stm32-metapac/stm32wl55cc-cm0p", "_dual-core" ]
1610+
stm32wl55jc-cm4 = [ "stm32-metapac/stm32wl55jc-cm4", "_dual-core" ]
1611+
stm32wl55jc-cm0p = [ "stm32-metapac/stm32wl55jc-cm0p", "_dual-core" ]
16091612
stm32wle4c8 = [ "stm32-metapac/stm32wle4c8" ]
16101613
stm32wle4cb = [ "stm32-metapac/stm32wle4cb" ]
16111614
stm32wle4cc = [ "stm32-metapac/stm32wle4cc" ]

embassy-stm32/build.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ fn main() {
480480
self.clock_names.insert(name.to_ascii_lowercase());
481481
quote!(unsafe {
482482
unwrap!(
483-
crate::rcc::get_freqs().#clock_name,
483+
crate::rcc::get_freqs().#clock_name.to_hertz(),
484484
"peripheral '{}' is configured to use the '{}' clock, which is not running. \
485485
Either enable it in 'config.rcc' or change 'config.rcc.mux' to use another clock",
486486
#peripheral,
@@ -713,9 +713,10 @@ fn main() {
713713
g.extend(quote! {
714714
#[derive(Clone, Copy, Debug)]
715715
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
716+
#[repr(C)]
716717
pub struct Clocks {
717718
#(
718-
pub #clock_idents: Option<crate::time::Hertz>,
719+
pub #clock_idents: crate::time::MaybeHertz,
719720
)*
720721
}
721722
});
@@ -732,7 +733,7 @@ fn main() {
732733
$($(#[$m])* $k: $v,)*
733734
};
734735
crate::rcc::set_freqs(crate::rcc::Clocks {
735-
#( #clock_idents: all.#clock_idents, )*
736+
#( #clock_idents: all.#clock_idents.into(), )*
736737
});
737738
}
738739
};

embassy-stm32/src/adc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(crate) fn blocking_delay_us(us: u32) {
7878
embassy_time::block_for(embassy_time::Duration::from_micros(us as u64));
7979
#[cfg(not(feature = "time"))]
8080
{
81-
let freq = unsafe { crate::rcc::get_freqs() }.sys.unwrap().0 as u64;
81+
let freq = unsafe { crate::rcc::get_freqs() }.sys.to_hertz().unwrap().0 as u64;
8282
let us = us as u64;
8383
let cycles = freq * us / 1_000_000;
8484
cortex_m::asm::delay(cycles as u32);

embassy-stm32/src/dsihost.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn blocking_delay_ms(ms: u32) {
1414
#[cfg(feature = "time")]
1515
embassy_time::block_for(embassy_time::Duration::from_millis(ms as u64));
1616
#[cfg(not(feature = "time"))]
17-
cortex_m::asm::delay(unsafe { crate::rcc::get_freqs() }.sys.unwrap().0 / 1_000 * ms);
17+
cortex_m::asm::delay(unsafe { crate::rcc::get_freqs() }.sys.to_hertz().unwrap().0 / 1_000 * ms);
1818
}
1919

2020
/// PacketTypes extracted from CubeMX

embassy-stm32/src/lib.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,120 @@ impl Default for Config {
273273
/// This returns the peripheral singletons that can be used for creating drivers.
274274
///
275275
/// This should only be called once at startup, otherwise it panics.
276+
#[cfg(not(feature = "_dual-core"))]
276277
pub fn init(config: Config) -> Peripherals {
278+
init_hw(config)
279+
}
280+
281+
#[cfg(feature = "_dual-core")]
282+
mod dual_core {
283+
use core::cell::UnsafeCell;
284+
use core::mem::MaybeUninit;
285+
use core::sync::atomic::{AtomicUsize, Ordering};
286+
287+
use rcc::Clocks;
288+
289+
use super::*;
290+
291+
/// Object containing data that embassy needs to share between cores.
292+
///
293+
/// It cannot be initialized by the user. The intended use is:
294+
///
295+
/// ```
296+
/// #[link_section = ".ram_d3"]
297+
/// static SHARED_DATA: MaybeUninit<SharedData> = MaybeUninit::uninit();
298+
///
299+
/// init_secondary(&SHARED_DATA);
300+
/// ```
301+
///
302+
/// This static must be placed in the same position for both cores. How and where this is done is left to the user.
303+
pub struct SharedData {
304+
init_flag: AtomicUsize,
305+
clocks: UnsafeCell<MaybeUninit<Clocks>>,
306+
}
307+
308+
unsafe impl Sync for SharedData {}
309+
310+
const INIT_DONE_FLAG: usize = 0xca11ab1e;
311+
312+
/// Initialize the `embassy-stm32` HAL with the provided configuration.
313+
/// This function does the actual initialization of the hardware, in contrast to [init_secondary] or [try_init_secondary].
314+
/// Any core can do the init, but it's important only one core does it.
315+
///
316+
/// This returns the peripheral singletons that can be used for creating drivers.
317+
///
318+
/// This should only be called once at startup, otherwise it panics.
319+
///
320+
/// The `shared_data` is used to coordinate the init with the second core. Read the [SharedData] docs
321+
/// for more information on its requirements.
322+
pub fn init_primary(config: Config, shared_data: &'static MaybeUninit<SharedData>) -> Peripherals {
323+
let shared_data = unsafe { shared_data.assume_init_ref() };
324+
325+
rcc::set_freqs_ptr(shared_data.clocks.get());
326+
let p = init_hw(config);
327+
328+
shared_data.init_flag.store(INIT_DONE_FLAG, Ordering::SeqCst);
329+
330+
p
331+
}
332+
333+
/// Try to initialize the `embassy-stm32` HAL based on the init done by the other core using [init_primary].
334+
///
335+
/// This returns the peripheral singletons that can be used for creating drivers if the other core is done with its init.
336+
/// If the other core is not done yet, this will return `None`.
337+
///
338+
/// This should only be called once at startup, otherwise it may panic.
339+
///
340+
/// The `shared_data` is used to coordinate the init with the second core. Read the [SharedData] docs
341+
/// for more information on its requirements.
342+
pub fn try_init_secondary(shared_data: &'static MaybeUninit<SharedData>) -> Option<Peripherals> {
343+
let shared_data = unsafe { shared_data.assume_init_ref() };
344+
345+
if shared_data.init_flag.load(Ordering::SeqCst) != INIT_DONE_FLAG {
346+
return None;
347+
}
348+
349+
// Separate load and store to support the CM0 of the STM32WL
350+
shared_data.init_flag.store(0, Ordering::SeqCst);
351+
352+
Some(init_secondary_hw(shared_data))
353+
}
354+
355+
/// Initialize the `embassy-stm32` HAL based on the init done by the other core using [init_primary].
356+
///
357+
/// This returns the peripheral singletons that can be used for creating drivers when the other core is done with its init.
358+
/// If the other core is not done yet, this will spinloop wait on it.
359+
///
360+
/// This should only be called once at startup, otherwise it may panic.
361+
///
362+
/// The `shared_data` is used to coordinate the init with the second core. Read the [SharedData] docs
363+
/// for more information on its requirements.
364+
pub fn init_secondary(shared_data: &'static MaybeUninit<SharedData>) -> Peripherals {
365+
loop {
366+
if let Some(p) = try_init_secondary(shared_data) {
367+
return p;
368+
}
369+
}
370+
}
371+
372+
fn init_secondary_hw(shared_data: &'static SharedData) -> Peripherals {
373+
rcc::set_freqs_ptr(shared_data.clocks.get());
374+
375+
// We use different timers on the different cores, so we have to still initialize one here
376+
#[cfg(feature = "_time-driver")]
377+
critical_section::with(|cs| {
378+
// must be after rcc init
379+
time_driver::init(cs);
380+
});
381+
382+
Peripherals::take()
383+
}
384+
}
385+
386+
#[cfg(feature = "_dual-core")]
387+
pub use dual_core::*;
388+
389+
fn init_hw(config: Config) -> Peripherals {
277390
critical_section::with(|cs| {
278391
let p = Peripherals::take_with_cs(cs);
279392

0 commit comments

Comments
 (0)