Skip to content

Commit 966914f

Browse files
authored
Merge pull request #4125 from diondokter/configurable-bank-support
STM32: Configurable bank support
2 parents af6f5ca + 8017d58 commit 966914f

File tree

26 files changed

+287
-429
lines changed

26 files changed

+287
-429
lines changed

.github/ci/test.sh

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ cargo test --manifest-path ./embassy-nrf/Cargo.toml --no-default-features --feat
2929
cargo test --manifest-path ./embassy-rp/Cargo.toml --no-default-features --features time-driver,rp2040,_test
3030
cargo test --manifest-path ./embassy-rp/Cargo.toml --no-default-features --features time-driver,rp235xa,_test
3131

32-
cargo test --manifest-path ./embassy-stm32/Cargo.toml --no-default-features --features stm32f429vg,exti,time-driver-any,exti
33-
cargo test --manifest-path ./embassy-stm32/Cargo.toml --no-default-features --features stm32f732ze,exti,time-driver-any,exti
34-
cargo test --manifest-path ./embassy-stm32/Cargo.toml --no-default-features --features stm32f769ni,exti,time-driver-any,exti
32+
cargo test --manifest-path ./embassy-stm32/Cargo.toml --no-default-features --features stm32f429vg,time-driver-any,exti,single-bank
33+
cargo test --manifest-path ./embassy-stm32/Cargo.toml --no-default-features --features stm32f429vg,time-driver-any,exti,dual-bank
34+
cargo test --manifest-path ./embassy-stm32/Cargo.toml --no-default-features --features stm32f732ze,time-driver-any,exti
35+
cargo test --manifest-path ./embassy-stm32/Cargo.toml --no-default-features --features stm32f769ni,time-driver-any,exti,single-bank
36+
cargo test --manifest-path ./embassy-stm32/Cargo.toml --no-default-features --features stm32f769ni,time-driver-any,exti,dual-bank
3537

3638
cargo test --manifest-path ./embassy-net-adin1110/Cargo.toml

ci.sh

+13-12
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,17 @@ cargo batch \
8888
--- build --release --manifest-path embassy-rp/Cargo.toml --target thumbv8m.main-none-eabihf --features time-driver,defmt,rp235xa \
8989
--- build --release --manifest-path embassy-rp/Cargo.toml --target thumbv8m.main-none-eabihf --features time-driver,log,rp235xa \
9090
--- build --release --manifest-path embassy-rp/Cargo.toml --target thumbv8m.main-none-eabihf --features time-driver,rp235xa,binary-info \
91-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,exti,time-driver-any,time \
92-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,time-driver-any,time \
93-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,exti,time \
94-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,time \
95-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,exti,time-driver-any,time \
96-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,time-driver-any,time \
97-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,exti,time \
98-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,time \
99-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,exti \
100-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt \
91+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,exti,time-driver-any,time \
92+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,time-driver-any,time \
93+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,exti,time \
94+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,time \
95+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,exti,time-driver-any,time \
96+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,time-driver-any,time \
97+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,exti,time \
98+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,time \
99+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,exti \
100+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt \
101+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,single-bank,defmt \
101102
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv6m-none-eabi --features stm32f038f6,defmt,exti,time-driver-any,time \
102103
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv6m-none-eabi --features stm32f030c6,defmt,exti,time-driver-any,time \
103104
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv6m-none-eabi --features stm32f058t8,defmt,exti,time-driver-any,time \
@@ -153,10 +154,10 @@ cargo batch \
153154
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features stm32f378cc,defmt,exti,time-driver-any,time \
154155
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv6m-none-eabi --features stm32g0c1ve,defmt,exti,time-driver-any,time \
155156
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7m-none-eabi --features stm32f217zg,defmt,exti,time-driver-any,time \
156-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,defmt,exti,time-driver-any,low-power,time \
157+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv8m.main-none-eabihf --features stm32l552ze,dual-bank,defmt,exti,time-driver-any,low-power,time \
157158
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv6m-none-eabi --features stm32wl54jc-cm0p,defmt,exti,time-driver-any,time \
158159
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features stm32wle5jb,defmt,exti,time-driver-any,time \
159-
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features stm32g474pe,defmt,exti,time-driver-any,time \
160+
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features stm32g474pe,dual-bank,defmt,exti,time-driver-any,time \
160161
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7m-none-eabi --features stm32f107vc,defmt,exti,time-driver-any,time \
161162
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7m-none-eabi --features stm32f103re,defmt,exti,time-driver-any,time \
162163
--- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7m-none-eabi --features stm32f100c4,defmt,exti,time-driver-any,time \

embassy-stm32/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99
- Modify BufferedUart initialization to take pins before interrupts ([#3983](https://github.com/embassy-rs/embassy/pull/3983))
10+
- Added a 'single-bank' and a 'dual-bank' feature so chips with configurable flash bank setups are be supported in embassy ([#4125](https://github.com/embassy-rs/embassy/pull/4125))
1011

1112
## 0.2.0 - 2025-01-10
1213

embassy-stm32/Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ rand_core = "0.6.3"
7373
sdio-host = "0.9.0"
7474
critical-section = "1.1"
7575
#stm32-metapac = { version = "16" }
76-
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-169e38298f9421dddbabc0eb81d0c30fb1eec0a7" }
76+
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-7251801e3273011ce28a89e8f2e45eec2e419e26" }
7777

7878
vcell = "0.1.3"
7979
nb = "1.0.0"
@@ -102,7 +102,7 @@ proc-macro2 = "1.0.36"
102102
quote = "1.0.15"
103103

104104
#stm32-metapac = { version = "16", default-features = false, features = ["metadata"]}
105-
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-169e38298f9421dddbabc0eb81d0c30fb1eec0a7", default-features = false, features = ["metadata"] }
105+
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-7251801e3273011ce28a89e8f2e45eec2e419e26", default-features = false, features = ["metadata"] }
106106

107107
[features]
108108
default = ["rt"]
@@ -197,6 +197,9 @@ split-pc2 = ["_split-pins-enabled"]
197197
## Split PC3
198198
split-pc3 = ["_split-pins-enabled"]
199199

200+
dual-bank = []
201+
single-bank = []
202+
200203
## internal use only
201204
_split-pins-enabled = []
202205

embassy-stm32/build.rs

+47-16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,44 @@ fn main() {
4949
}
5050
}
5151

52+
// ========
53+
// Select the memory variant to use
54+
let memory = {
55+
let single_bank_selected = env::var("CARGO_FEATURE_SINGLE_BANK").is_ok();
56+
let dual_bank_selected = env::var("CARGO_FEATURE_DUAL_BANK").is_ok();
57+
58+
let single_bank_memory = METADATA.memory.iter().find(|mem| {
59+
mem.iter().any(|region| region.name.contains("BANK_1"))
60+
&& !mem.iter().any(|region| region.name.contains("BANK_2"))
61+
});
62+
63+
let dual_bank_memory = METADATA.memory.iter().find(|mem| {
64+
mem.iter().any(|region| region.name.contains("BANK_1"))
65+
&& mem.iter().any(|region| region.name.contains("BANK_2"))
66+
});
67+
68+
cfgs.set(
69+
"bank_setup_configurable",
70+
single_bank_memory.is_some() && dual_bank_memory.is_some(),
71+
);
72+
73+
match (single_bank_selected, dual_bank_selected) {
74+
(true, true) => panic!("Both 'single-bank' and 'dual-bank' features enabled"),
75+
(true, false) => {
76+
single_bank_memory.expect("The 'single-bank' feature is not supported on this dual bank chip")
77+
}
78+
(false, true) => {
79+
dual_bank_memory.expect("The 'dual-bank' feature is not supported on this single bank chip")
80+
}
81+
(false, false) => {
82+
if METADATA.memory.len() != 1 {
83+
panic!("Chip supports single and dual bank configuration. No Cargo feature to select one is enabled. Use the 'single-bank' or 'dual-bank' feature to make your selection")
84+
}
85+
METADATA.memory[0]
86+
}
87+
}
88+
};
89+
5290
// ========
5391
// Generate singletons
5492

@@ -290,8 +328,7 @@ fn main() {
290328
// ========
291329
// Generate FLASH regions
292330
let mut flash_regions = TokenStream::new();
293-
let flash_memory_regions: Vec<_> = METADATA
294-
.memory
331+
let flash_memory_regions: Vec<_> = memory
295332
.iter()
296333
.filter(|x| x.kind == MemoryRegionKind::Flash && x.settings.is_some())
297334
.collect();
@@ -1621,8 +1658,7 @@ fn main() {
16211658
let mut pins_table: Vec<Vec<String>> = Vec::new();
16221659
let mut adc_table: Vec<Vec<String>> = Vec::new();
16231660

1624-
for m in METADATA
1625-
.memory
1661+
for m in memory
16261662
.iter()
16271663
.filter(|m| m.kind == MemoryRegionKind::Flash && m.settings.is_some())
16281664
{
@@ -1860,8 +1896,7 @@ fn main() {
18601896
// ========
18611897
// Generate flash constants
18621898

1863-
let flash_regions: Vec<&MemoryRegion> = METADATA
1864-
.memory
1899+
let flash_regions: Vec<&MemoryRegion> = memory
18651900
.iter()
18661901
.filter(|x| x.kind == MemoryRegionKind::Flash && x.name.starts_with("BANK_"))
18671902
.collect();
@@ -1986,7 +2021,7 @@ fn main() {
19862021
println!("cargo:rerun-if-changed=build.rs");
19872022

19882023
if cfg!(feature = "memory-x") {
1989-
gen_memory_x(out_dir);
2024+
gen_memory_x(memory, out_dir);
19902025
println!("cargo:rustc-link-search={}", out_dir.display());
19912026
}
19922027
}
@@ -2075,11 +2110,11 @@ fn rustfmt(path: impl AsRef<Path>) {
20752110
}
20762111
}
20772112

2078-
fn gen_memory_x(out_dir: &Path) {
2113+
fn gen_memory_x(memory: &[MemoryRegion], out_dir: &Path) {
20792114
let mut memory_x = String::new();
20802115

2081-
let flash = get_memory_range(MemoryRegionKind::Flash);
2082-
let ram = get_memory_range(MemoryRegionKind::Ram);
2116+
let flash = get_memory_range(memory, MemoryRegionKind::Flash);
2117+
let ram = get_memory_range(memory, MemoryRegionKind::Ram);
20832118

20842119
write!(memory_x, "MEMORY\n{{\n").unwrap();
20852120
writeln!(
@@ -2103,12 +2138,8 @@ fn gen_memory_x(out_dir: &Path) {
21032138
std::fs::write(out_dir.join("memory.x"), memory_x.as_bytes()).unwrap();
21042139
}
21052140

2106-
fn get_memory_range(kind: MemoryRegionKind) -> (u32, u32, String) {
2107-
let mut mems: Vec<_> = METADATA
2108-
.memory
2109-
.iter()
2110-
.filter(|m| m.kind == kind && m.size != 0)
2111-
.collect();
2141+
fn get_memory_range(memory: &[MemoryRegion], kind: MemoryRegionKind) -> (u32, u32, String) {
2142+
let mut mems: Vec<_> = memory.iter().filter(|m| m.kind == kind && m.size != 0).collect();
21122143
mems.sort_by_key(|m| m.address);
21132144

21142145
let mut start = u32::MAX;

embassy-stm32/src/flash/asynch.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
66
use embassy_sync::mutex::Mutex;
77

88
use super::{
9-
blocking_read, ensure_sector_aligned, family, get_sector, Async, Error, Flash, FlashLayout, FLASH_BASE, FLASH_SIZE,
10-
WRITE_SIZE,
9+
blocking_read, ensure_sector_aligned, family, get_flash_regions, get_sector, Async, Error, Flash, FlashLayout,
10+
FLASH_BASE, FLASH_SIZE, WRITE_SIZE,
1111
};
1212
use crate::interrupt::InterruptExt;
1313
use crate::peripherals::FLASH;
@@ -34,7 +34,6 @@ impl<'d> Flash<'d, Async> {
3434
///
3535
/// See module-level documentation for details on how memory regions work.
3636
pub fn into_regions(self) -> FlashLayout<'d, Async> {
37-
assert!(family::is_default_layout());
3837
FlashLayout::new(self.inner)
3938
}
4039

@@ -123,7 +122,7 @@ pub(super) async unsafe fn write_chunked(base: u32, size: u32, offset: u32, byte
123122
pub(super) async unsafe fn erase_sectored(base: u32, from: u32, to: u32) -> Result<(), Error> {
124123
let start_address = base + from;
125124
let end_address = base + to;
126-
let regions = family::get_flash_regions();
125+
let regions = get_flash_regions();
127126

128127
ensure_sector_aligned(start_address, end_address, regions)?;
129128

embassy-stm32/src/flash/common.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use core::sync::atomic::{fence, Ordering};
44
use embassy_hal_internal::drop::OnDrop;
55

66
use super::{
7-
family, Async, Blocking, Error, FlashBank, FlashLayout, FlashRegion, FlashSector, FLASH_SIZE, MAX_ERASE_SIZE,
8-
READ_SIZE, WRITE_SIZE,
7+
family, get_flash_regions, Async, Blocking, Error, FlashBank, FlashLayout, FlashRegion, FlashSector, FLASH_SIZE,
8+
MAX_ERASE_SIZE, READ_SIZE, WRITE_SIZE,
99
};
1010
use crate::Peri;
1111
use crate::_generated::FLASH_BASE;
@@ -20,6 +20,10 @@ pub struct Flash<'d, MODE = Async> {
2020
impl<'d> Flash<'d, Blocking> {
2121
/// Create a new flash driver, usable in blocking mode.
2222
pub fn new_blocking(p: Peri<'d, FLASH>) -> Self {
23+
#[cfg(bank_setup_configurable)]
24+
// Check if the configuration matches the embassy setup
25+
super::check_bank_setup();
26+
2327
Self {
2428
inner: p,
2529
_mode: PhantomData,
@@ -32,7 +36,6 @@ impl<'d, MODE> Flash<'d, MODE> {
3236
///
3337
/// See module-level documentation for details on how memory regions work.
3438
pub fn into_blocking_regions(self) -> FlashLayout<'d, Blocking> {
35-
assert!(family::is_default_layout());
3639
FlashLayout::new(self.inner)
3740
}
3841

@@ -137,7 +140,7 @@ pub(super) unsafe fn blocking_erase(
137140
) -> Result<(), Error> {
138141
let start_address = base + from;
139142
let end_address = base + to;
140-
let regions = family::get_flash_regions();
143+
let regions = get_flash_regions();
141144

142145
ensure_sector_aligned(start_address, end_address, regions)?;
143146

embassy-stm32/src/flash/f0.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
use core::ptr::write_volatile;
22
use core::sync::atomic::{fence, Ordering};
33

4-
use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
4+
use super::{FlashSector, WRITE_SIZE};
55
use crate::flash::Error;
66
use crate::pac;
77

8-
pub(crate) const fn is_default_layout() -> bool {
9-
true
10-
}
11-
12-
pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
13-
&FLASH_REGIONS
14-
}
15-
168
pub(crate) unsafe fn lock() {
179
pac::FLASH.cr().modify(|w| w.set_lock(true));
1810
}

embassy-stm32/src/flash/f1f3.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
use core::ptr::write_volatile;
22
use core::sync::atomic::{fence, Ordering};
33

4-
use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
4+
use super::{FlashSector, WRITE_SIZE};
55
use crate::flash::Error;
66
use crate::pac;
77

8-
pub(crate) const fn is_default_layout() -> bool {
9-
true
10-
}
11-
12-
pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
13-
&FLASH_REGIONS
14-
}
15-
168
pub(crate) unsafe fn lock() {
179
pac::FLASH.cr().modify(|w| w.set_lock(true));
1810
}

embassy-stm32/src/flash/f2.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::sync::atomic::{fence, AtomicBool, Ordering};
33

44
use pac::flash::regs::Sr;
55

6-
use super::{FlashBank, FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
6+
use super::{get_flash_regions, FlashBank, FlashSector, WRITE_SIZE};
77
use crate::flash::Error;
88
use crate::pac;
99

@@ -15,14 +15,6 @@ impl FlashSector {
1515
}
1616
}
1717

18-
pub(crate) const fn is_default_layout() -> bool {
19-
true
20-
}
21-
22-
pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
23-
&FLASH_REGIONS
24-
}
25-
2618
pub(crate) unsafe fn lock() {
2719
pac::FLASH.cr().modify(|w| w.set_lock(true));
2820
}

0 commit comments

Comments
 (0)