aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Melchior Jacobsen <[email protected]>2023-03-30 08:32:36 +0200
committerRasmus Melchior Jacobsen <[email protected]>2023-03-30 08:32:36 +0200
commite7129371d0d543394c070671490a796ddbb10e3f (patch)
tree6ccaf414fbd474dc78e4f1fddf1a94d0dff58c5d
parente3c4e00be0469030f163568efa2902d73e2b8a4c (diff)
Let sector computation be shared across families
-rwxr-xr-xci.sh1
-rw-r--r--embassy-stm32/build.rs13
-rw-r--r--embassy-stm32/src/flash/common.rs35
-rw-r--r--embassy-stm32/src/flash/f3.rs16
-rw-r--r--embassy-stm32/src/flash/f4.rs183
-rw-r--r--embassy-stm32/src/flash/f7.rs95
-rw-r--r--embassy-stm32/src/flash/h7.rs21
-rw-r--r--embassy-stm32/src/flash/l.rs18
-rw-r--r--embassy-stm32/src/flash/mod.rs15
-rw-r--r--embassy-stm32/src/flash/other.rs12
10 files changed, 209 insertions, 200 deletions
diff --git a/ci.sh b/ci.sh
index d86c93520..b9dddad3a 100755
--- a/ci.sh
+++ b/ci.sh
@@ -49,6 +49,7 @@ cargo batch \
49 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32f411ce,defmt,exti,time-driver-any,unstable-traits \ 49 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32f411ce,defmt,exti,time-driver-any,unstable-traits \
50 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32f413vh,defmt,exti,time-driver-any,unstable-traits \ 50 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32f413vh,defmt,exti,time-driver-any,unstable-traits \
51 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32f429zi,log,exti,time-driver-any,unstable-traits,embedded-sdmmc \ 51 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32f429zi,log,exti,time-driver-any,unstable-traits,embedded-sdmmc \
52 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32f730i8,defmt,exti,time-driver-any,unstable-traits \
52 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32h755zi-cm7,defmt,exti,time-driver-any,unstable-traits \ 53 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32h755zi-cm7,defmt,exti,time-driver-any,unstable-traits \
53 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32h7b3ai,defmt,exti,time-driver-any,unstable-traits \ 54 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32h7b3ai,defmt,exti,time-driver-any,unstable-traits \
54 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32l476vg,defmt,exti,time-driver-any,unstable-traits \ 55 --- build --release --manifest-path embassy-stm32/Cargo.toml --target thumbv7em-none-eabi --features nightly,stm32l476vg,defmt,exti,time-driver-any,unstable-traits \
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs
index 6b203a3f1..007409242 100644
--- a/embassy-stm32/build.rs
+++ b/embassy-stm32/build.rs
@@ -113,6 +113,18 @@ fn main() {
113 .collect(); 113 .collect();
114 for region in flash_memory_regions.iter() { 114 for region in flash_memory_regions.iter() {
115 let region_name = format_ident!("{}", get_flash_region_name(region.name)); 115 let region_name = format_ident!("{}", get_flash_region_name(region.name));
116 let bank = format_ident!(
117 "{}",
118 if region.name.starts_with("BANK_1") {
119 "Bank1"
120 } else if region.name.starts_with("BANK_2") {
121 "Bank2"
122 } else if region.name == "OTP" {
123 "Otp"
124 } else {
125 unimplemented!()
126 }
127 );
116 let base = region.address; 128 let base = region.address;
117 let size = region.size; 129 let size = region.size;
118 let settings = region.settings.as_ref().unwrap(); 130 let settings = region.settings.as_ref().unwrap();
@@ -122,6 +134,7 @@ fn main() {
122 134
123 flash_regions.extend(quote! { 135 flash_regions.extend(quote! {
124 pub const #region_name: crate::flash::FlashRegion = crate::flash::FlashRegion { 136 pub const #region_name: crate::flash::FlashRegion = crate::flash::FlashRegion {
137 bank: crate::flash::FlashBank::#bank,
125 base: #base, 138 base: #base,
126 size: #size, 139 size: #size,
127 erase_size: #erase_size, 140 erase_size: #erase_size,
diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs
index 6534e1b8e..31dd1136f 100644
--- a/embassy-stm32/src/flash/common.rs
+++ b/embassy-stm32/src/flash/common.rs
@@ -3,7 +3,8 @@ use embassy_hal_common::{into_ref, PeripheralRef};
3use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; 3use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
4use embassy_sync::mutex::{Mutex, MutexGuard}; 4use embassy_sync::mutex::{Mutex, MutexGuard};
5 5
6use super::{family, Error, FlashLayout, FlashRegion, FLASH_BASE, FLASH_SIZE, WRITE_SIZE}; 6use super::{family, Error, FlashLayout, FlashRegion, FlashSector, FLASH_BASE, FLASH_SIZE, WRITE_SIZE};
7use crate::flash::FlashBank;
7use crate::Peripheral; 8use crate::Peripheral;
8 9
9pub struct Flash<'d> { 10pub struct Flash<'d> {
@@ -79,10 +80,12 @@ impl<'d> Flash<'d> {
79 } 80 }
80 81
81 unsafe fn blocking_erase_inner(start_address: u32, end_address: u32) -> Result<(), Error> { 82 unsafe fn blocking_erase_inner(start_address: u32, end_address: u32) -> Result<(), Error> {
83 let regions = family::get_flash_regions();
84
82 // Test if the address range is aligned at sector base addresses 85 // Test if the address range is aligned at sector base addresses
83 let mut address = start_address; 86 let mut address = start_address;
84 while address < end_address { 87 while address < end_address {
85 let sector = family::get_sector(address); 88 let sector = get_sector(address, regions);
86 if sector.start != address { 89 if sector.start != address {
87 return Err(Error::Unaligned); 90 return Err(Error::Unaligned);
88 } 91 }
@@ -103,7 +106,8 @@ impl<'d> Flash<'d> {
103 106
104 let mut address = start_address; 107 let mut address = start_address;
105 while address < end_address { 108 while address < end_address {
106 let sector = family::get_sector(address); 109 let sector = get_sector(address, regions);
110 trace!("Erasing sector: {}", sector);
107 family::blocking_erase_sector(&sector)?; 111 family::blocking_erase_sector(&sector)?;
108 address += sector.size; 112 address += sector.size;
109 } 113 }
@@ -138,6 +142,31 @@ fn take_lock_spin() -> MutexGuard<'static, CriticalSectionRawMutex, ()> {
138 } 142 }
139} 143}
140 144
145pub(crate) fn get_sector(address: u32, regions: &[&FlashRegion]) -> FlashSector {
146 let mut current_bank = FlashBank::Bank1;
147 let mut bank_offset = 0;
148 for region in regions {
149 if region.bank != current_bank {
150 current_bank = region.bank;
151 bank_offset = 0;
152 }
153
154 if address < region.end() {
155 let index_in_region = (address - region.base) / region.erase_size;
156 return FlashSector {
157 bank: region.bank,
158 index_in_bank: bank_offset + index_in_region as u8,
159 start: region.base + index_in_region * region.erase_size,
160 size: region.erase_size,
161 };
162 }
163
164 bank_offset += region.sectors();
165 }
166
167 panic!("Flash sector not found");
168}
169
141impl FlashRegion { 170impl FlashRegion {
142 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { 171 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
143 unsafe { self.blocking_read_inner(offset, bytes) } 172 unsafe { self.blocking_read_inner(offset, bytes) }
diff --git a/embassy-stm32/src/flash/f3.rs b/embassy-stm32/src/flash/f3.rs
index 5ac1d8fdf..c1ed33f9d 100644
--- a/embassy-stm32/src/flash/f3.rs
+++ b/embassy-stm32/src/flash/f3.rs
@@ -3,10 +3,14 @@ use core::ptr::write_volatile;
3 3
4use atomic_polyfill::{fence, Ordering}; 4use atomic_polyfill::{fence, Ordering};
5 5
6use super::{FlashSector, BANK1_REGION, WRITE_SIZE}; 6use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
7use crate::flash::Error; 7use crate::flash::Error;
8use crate::pac; 8use crate::pac;
9 9
10pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
11 &FLASH_REGIONS
12}
13
10pub(crate) unsafe fn lock() { 14pub(crate) unsafe fn lock() {
11 pac::FLASH.cr().modify(|w| w.set_lock(true)); 15 pac::FLASH.cr().modify(|w| w.set_lock(true));
12} 16}
@@ -99,13 +103,3 @@ unsafe fn blocking_wait_ready() -> Result<(), Error> {
99 } 103 }
100 } 104 }
101} 105}
102
103pub(crate) fn get_sector(address: u32) -> FlashSector {
104 let sector_size = BANK1_REGION.erase_size;
105 let index = address / sector_size;
106 FlashSector {
107 index: index as u8,
108 start: BANK1_REGION.base + index * sector_size,
109 size: sector_size,
110 }
111}
diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs
index 52ef7ad50..61ac8afd5 100644
--- a/embassy-stm32/src/flash/f4.rs
+++ b/embassy-stm32/src/flash/f4.rs
@@ -2,22 +2,17 @@ use core::convert::TryInto;
2use core::ptr::write_volatile; 2use core::ptr::write_volatile;
3use core::sync::atomic::{fence, Ordering}; 3use core::sync::atomic::{fence, Ordering};
4 4
5use super::{FlashSector, FLASH_BASE, FLASH_SIZE, WRITE_SIZE}; 5use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
6use crate::flash::Error; 6use crate::flash::Error;
7use crate::pac; 7use crate::pac;
8 8
9const SMALL_SECTOR_SIZE: u32 = 16 * 1024;
10const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024;
11const LARGE_SECTOR_SIZE: u32 = 128 * 1024;
12const SECOND_BANK_SECTOR_OFFSET: u8 = 12;
13
14#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] 9#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))]
15mod alt_regions { 10mod alt_regions {
16 use embassy_hal_common::PeripheralRef; 11 use embassy_hal_common::PeripheralRef;
17 use stm32_metapac::FLASH_SIZE; 12 use stm32_metapac::FLASH_SIZE;
18 13
19 use crate::_generated::flash_regions::{BANK1_REGION1, BANK1_REGION2, BANK1_REGION3}; 14 use crate::_generated::flash_regions::{BANK1_REGION1, BANK1_REGION2, BANK1_REGION3};
20 use crate::flash::{Bank1Region1, Bank1Region2, Flash, FlashRegion}; 15 use crate::flash::{Bank1Region1, Bank1Region2, Flash, FlashBank, FlashRegion};
21 use crate::peripherals::FLASH; 16 use crate::peripherals::FLASH;
22 17
23 pub const ALT_BANK1_REGION3: FlashRegion = FlashRegion { 18 pub const ALT_BANK1_REGION3: FlashRegion = FlashRegion {
@@ -25,19 +20,31 @@ mod alt_regions {
25 ..BANK1_REGION3 20 ..BANK1_REGION3
26 }; 21 };
27 pub const ALT_BANK2_REGION1: FlashRegion = FlashRegion { 22 pub const ALT_BANK2_REGION1: FlashRegion = FlashRegion {
23 bank: FlashBank::Bank2,
28 base: BANK1_REGION1.base + FLASH_SIZE as u32 / 2, 24 base: BANK1_REGION1.base + FLASH_SIZE as u32 / 2,
29 ..BANK1_REGION1 25 ..BANK1_REGION1
30 }; 26 };
31 pub const ALT_BANK2_REGION2: FlashRegion = FlashRegion { 27 pub const ALT_BANK2_REGION2: FlashRegion = FlashRegion {
28 bank: FlashBank::Bank2,
32 base: BANK1_REGION2.base + FLASH_SIZE as u32 / 2, 29 base: BANK1_REGION2.base + FLASH_SIZE as u32 / 2,
33 ..BANK1_REGION2 30 ..BANK1_REGION2
34 }; 31 };
35 pub const ALT_BANK2_REGION3: FlashRegion = FlashRegion { 32 pub const ALT_BANK2_REGION3: FlashRegion = FlashRegion {
33 bank: FlashBank::Bank2,
36 base: BANK1_REGION3.base + FLASH_SIZE as u32 / 2, 34 base: BANK1_REGION3.base + FLASH_SIZE as u32 / 2,
37 size: 3 * BANK1_REGION3.erase_size, 35 size: 3 * BANK1_REGION3.erase_size,
38 ..BANK1_REGION3 36 ..BANK1_REGION3
39 }; 37 };
40 38
39 pub const ALT_FLASH_REGIONS: [&FlashRegion; 6] = [
40 &BANK1_REGION1,
41 &BANK1_REGION2,
42 &ALT_BANK1_REGION3,
43 &ALT_BANK2_REGION1,
44 &ALT_BANK2_REGION2,
45 &ALT_BANK2_REGION3,
46 ];
47
41 pub type AltBank1Region1 = Bank1Region1; 48 pub type AltBank1Region1 = Bank1Region1;
42 pub type AltBank1Region2 = Bank1Region2; 49 pub type AltBank1Region2 = Bank1Region2;
43 pub struct AltBank1Region3(&'static FlashRegion); 50 pub struct AltBank1Region3(&'static FlashRegion);
@@ -81,25 +88,22 @@ mod alt_regions {
81} 88}
82 89
83#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] 90#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))]
84pub use alt_regions::AltFlashLayout; 91pub use alt_regions::{AltFlashLayout, ALT_FLASH_REGIONS};
85 92
86fn is_dual_bank() -> bool { 93#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))]
87 match FLASH_SIZE / 1024 { 94pub(crate) fn get_flash_regions() -> &'static [&'static FlashRegion] {
88 // 1 MB devices depend on configuration 95 if unsafe { pac::FLASH.optcr().read().db1m() } {
89 1024 => { 96 &ALT_FLASH_REGIONS
90 if cfg!(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479)) { 97 } else {
91 unsafe { pac::FLASH.optcr().read().db1m() } 98 &FLASH_REGIONS
92 } else {
93 false
94 }
95 }
96 // 2 MB devices are always dual bank
97 2048 => true,
98 // All other devices are single bank
99 _ => false,
100 } 99 }
101} 100}
102 101
102#[cfg(not(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479)))]
103pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
104 &FLASH_REGIONS
105}
106
103pub(crate) unsafe fn lock() { 107pub(crate) unsafe fn lock() {
104 pac::FLASH.cr().modify(|w| w.set_lock(true)); 108 pac::FLASH.cr().modify(|w| w.set_lock(true));
105} 109}
@@ -136,11 +140,7 @@ pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE])
136} 140}
137 141
138pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> { 142pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
139 let sector = sector.index; 143 let snb = ((sector.bank as u8) << 4) + sector.index_in_bank;
140 let bank = sector / SECOND_BANK_SECTOR_OFFSET as u8;
141 let snb = (bank << 4) + (sector % SECOND_BANK_SECTOR_OFFSET as u8);
142
143 trace!("Erasing sector: {}", sector);
144 144
145 pac::FLASH.cr().modify(|w| { 145 pac::FLASH.cr().modify(|w| {
146 w.set_ser(true); 146 w.set_ser(true);
@@ -194,72 +194,27 @@ unsafe fn blocking_wait_ready() -> Result<(), Error> {
194 } 194 }
195} 195}
196 196
197pub(crate) fn get_sector(address: u32) -> FlashSector {
198 get_sector_inner(address, is_dual_bank(), FLASH_SIZE as u32)
199}
200
201fn get_sector_inner(address: u32, dual_bank: bool, flash_size: u32) -> FlashSector {
202 let offset = address - FLASH_BASE as u32;
203 if !dual_bank {
204 get_single_bank_sector(offset)
205 } else {
206 let bank_size = flash_size / 2;
207 if offset < bank_size {
208 get_single_bank_sector(offset)
209 } else {
210 let sector = get_single_bank_sector(offset - bank_size);
211 FlashSector {
212 index: SECOND_BANK_SECTOR_OFFSET + sector.index,
213 start: sector.start + bank_size,
214 size: sector.size,
215 }
216 }
217 }
218}
219
220fn get_single_bank_sector(offset: u32) -> FlashSector {
221 // First 4 sectors are 16KB, then one 64KB, and rest are 128KB
222 match offset / LARGE_SECTOR_SIZE {
223 0 => {
224 if offset < 4 * SMALL_SECTOR_SIZE {
225 let small_sector_index = offset / SMALL_SECTOR_SIZE;
226 FlashSector {
227 index: small_sector_index as u8,
228 start: FLASH_BASE as u32 + small_sector_index * SMALL_SECTOR_SIZE,
229 size: SMALL_SECTOR_SIZE,
230 }
231 } else {
232 FlashSector {
233 index: 4,
234 start: FLASH_BASE as u32 + 4 * SMALL_SECTOR_SIZE,
235 size: MEDIUM_SECTOR_SIZE,
236 }
237 }
238 }
239 i => {
240 let large_sector_index = i - 1;
241 FlashSector {
242 index: (5 + large_sector_index) as u8,
243 start: FLASH_BASE as u32
244 + 4 * SMALL_SECTOR_SIZE
245 + MEDIUM_SECTOR_SIZE
246 + large_sector_index * LARGE_SECTOR_SIZE,
247 size: LARGE_SECTOR_SIZE,
248 }
249 }
250 }
251}
252
253#[cfg(test)] 197#[cfg(test)]
254mod tests { 198mod tests {
255 use super::*; 199 use super::*;
200 use crate::flash::{get_sector, FlashBank};
256 201
257 #[test] 202 #[test]
203 #[cfg(stm32f429)]
258 fn can_get_sector_single_bank() { 204 fn can_get_sector_single_bank() {
259 let assert_sector = |index: u8, start: u32, size: u32, addr: u32| { 205 const SMALL_SECTOR_SIZE: u32 = 16 * 1024;
206 const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024;
207 const LARGE_SECTOR_SIZE: u32 = 128 * 1024;
208
209 let assert_sector = |index_in_bank: u8, start: u32, size: u32, address: u32| {
260 assert_eq!( 210 assert_eq!(
261 FlashSector { index, start, size }, 211 FlashSector {
262 get_sector_inner(addr, false, 1024 * 1024) 212 bank: FlashBank::Bank1,
213 index_in_bank,
214 start,
215 size
216 },
217 get_sector(address, &FLASH_REGIONS)
263 ) 218 )
264 }; 219 };
265 220
@@ -275,41 +230,43 @@ mod tests {
275 assert_sector(5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0803_FFFF); 230 assert_sector(5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0803_FFFF);
276 assert_sector(11, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080E_0000); 231 assert_sector(11, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080E_0000);
277 assert_sector(11, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080F_FFFF); 232 assert_sector(11, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080F_FFFF);
278 }
279 233
280 #[test] 234 let assert_sector = |bank: FlashBank, index_in_bank: u8, start: u32, size: u32, address: u32| {
281 fn can_get_sector_dual_bank() {
282 let assert_sector = |index: u8, start: u32, size: u32, addr: u32| {
283 assert_eq!( 235 assert_eq!(
284 FlashSector { index, start, size }, 236 FlashSector {
285 get_sector_inner(addr, true, 1024 * 1024) 237 bank,
238 index_in_bank,
239 start,
240 size
241 },
242 get_sector(address, &ALT_FLASH_REGIONS)
286 ) 243 )
287 }; 244 };
288 245
289 assert_sector(0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_0000); 246 assert_sector(FlashBank::Bank1, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_0000);
290 assert_sector(0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_3FFF); 247 assert_sector(FlashBank::Bank1, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_3FFF);
291 assert_sector(3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_C000); 248 assert_sector(FlashBank::Bank1, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_C000);
292 assert_sector(3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_FFFF); 249 assert_sector(FlashBank::Bank1, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_FFFF);
293 250
294 assert_sector(4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_0000); 251 assert_sector(FlashBank::Bank1, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_0000);
295 assert_sector(4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_FFFF); 252 assert_sector(FlashBank::Bank1, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_FFFF);
296 253
297 assert_sector(5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0802_0000); 254 assert_sector(FlashBank::Bank1, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0802_0000);
298 assert_sector(5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0803_FFFF); 255 assert_sector(FlashBank::Bank1, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0803_FFFF);
299 assert_sector(7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0806_0000); 256 assert_sector(FlashBank::Bank1, 7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0806_0000);
300 assert_sector(7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0807_FFFF); 257 assert_sector(FlashBank::Bank1, 7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0807_FFFF);
301 258
302 assert_sector(12, 0x0808_0000, SMALL_SECTOR_SIZE, 0x0808_0000); 259 assert_sector(FlashBank::Bank2, 0, 0x0808_0000, SMALL_SECTOR_SIZE, 0x0808_0000);
303 assert_sector(12, 0x0808_0000, SMALL_SECTOR_SIZE, 0x0808_3FFF); 260 assert_sector(FlashBank::Bank2, 0, 0x0808_0000, SMALL_SECTOR_SIZE, 0x0808_3FFF);
304 assert_sector(15, 0x0808_C000, SMALL_SECTOR_SIZE, 0x0808_C000); 261 assert_sector(FlashBank::Bank2, 3, 0x0808_C000, SMALL_SECTOR_SIZE, 0x0808_C000);
305 assert_sector(15, 0x0808_C000, SMALL_SECTOR_SIZE, 0x0808_FFFF); 262 assert_sector(FlashBank::Bank2, 3, 0x0808_C000, SMALL_SECTOR_SIZE, 0x0808_FFFF);
306 263
307 assert_sector(16, 0x0809_0000, MEDIUM_SECTOR_SIZE, 0x0809_0000); 264 assert_sector(FlashBank::Bank2, 4, 0x0809_0000, MEDIUM_SECTOR_SIZE, 0x0809_0000);
308 assert_sector(16, 0x0809_0000, MEDIUM_SECTOR_SIZE, 0x0809_FFFF); 265 assert_sector(FlashBank::Bank2, 4, 0x0809_0000, MEDIUM_SECTOR_SIZE, 0x0809_FFFF);
309 266
310 assert_sector(17, 0x080A_0000, LARGE_SECTOR_SIZE, 0x080A_0000); 267 assert_sector(FlashBank::Bank2, 5, 0x080A_0000, LARGE_SECTOR_SIZE, 0x080A_0000);
311 assert_sector(17, 0x080A_0000, LARGE_SECTOR_SIZE, 0x080B_FFFF); 268 assert_sector(FlashBank::Bank2, 5, 0x080A_0000, LARGE_SECTOR_SIZE, 0x080B_FFFF);
312 assert_sector(19, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080E_0000); 269 assert_sector(FlashBank::Bank2, 7, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080E_0000);
313 assert_sector(19, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080F_FFFF); 270 assert_sector(FlashBank::Bank2, 7, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080F_FFFF);
314 } 271 }
315} 272}
diff --git a/embassy-stm32/src/flash/f7.rs b/embassy-stm32/src/flash/f7.rs
index eba7df467..7111f5cc4 100644
--- a/embassy-stm32/src/flash/f7.rs
+++ b/embassy-stm32/src/flash/f7.rs
@@ -2,13 +2,13 @@ use core::convert::TryInto;
2use core::ptr::write_volatile; 2use core::ptr::write_volatile;
3use core::sync::atomic::{fence, Ordering}; 3use core::sync::atomic::{fence, Ordering};
4 4
5use super::{FlashSector, FLASH_BASE, WRITE_SIZE}; 5use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
6use crate::flash::Error; 6use crate::flash::Error;
7use crate::pac; 7use crate::pac;
8 8
9const SMALL_SECTOR_SIZE: u32 = 32 * 1024; 9pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
10const MEDIUM_SECTOR_SIZE: u32 = 128 * 1024; 10 &FLASH_REGIONS
11const LARGE_SECTOR_SIZE: u32 = 256 * 1024; 11}
12 12
13pub(crate) unsafe fn lock() { 13pub(crate) unsafe fn lock() {
14 pac::FLASH.cr().modify(|w| w.set_lock(true)); 14 pac::FLASH.cr().modify(|w| w.set_lock(true));
@@ -48,7 +48,7 @@ pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE])
48pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> { 48pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
49 pac::FLASH.cr().modify(|w| { 49 pac::FLASH.cr().modify(|w| {
50 w.set_ser(true); 50 w.set_ser(true);
51 w.set_snb(sector.index) 51 w.set_snb(sector.index_in_bank)
52 }); 52 });
53 53
54 pac::FLASH.cr().modify(|w| { 54 pac::FLASH.cr().modify(|w| {
@@ -110,48 +110,61 @@ unsafe fn blocking_wait_ready() -> Result<(), Error> {
110 } 110 }
111} 111}
112 112
113pub(crate) fn get_sector(address: u32) -> FlashSector {
114 // First 4 sectors are 32KB, then one 128KB, and rest are 256KB
115 let offset = address - FLASH_BASE as u32;
116 match offset / LARGE_SECTOR_SIZE {
117 0 => {
118 if offset < 4 * SMALL_SECTOR_SIZE {
119 let small_sector_index = offset / SMALL_SECTOR_SIZE;
120 FlashSector {
121 index: small_sector_index as u8,
122 start: FLASH_BASE as u32 + small_sector_index * SMALL_SECTOR_SIZE,
123 size: SMALL_SECTOR_SIZE,
124 }
125 } else {
126 FlashSector {
127 index: 4,
128 start: FLASH_BASE as u32 + 4 * SMALL_SECTOR_SIZE,
129 size: MEDIUM_SECTOR_SIZE,
130 }
131 }
132 }
133 i => {
134 let large_sector_index = i - 1;
135 FlashSector {
136 index: (5 + large_sector_index) as u8,
137 start: FLASH_BASE as u32
138 + 4 * SMALL_SECTOR_SIZE
139 + MEDIUM_SECTOR_SIZE
140 + large_sector_index * LARGE_SECTOR_SIZE,
141 size: LARGE_SECTOR_SIZE,
142 }
143 }
144 }
145}
146
147#[cfg(test)] 113#[cfg(test)]
148mod tests { 114mod tests {
149 use super::*; 115 use super::*;
116 use crate::flash::{get_sector, FlashBank};
150 117
151 #[test] 118 #[test]
119 #[cfg(stm32f732)]
152 fn can_get_sector() { 120 fn can_get_sector() {
153 let assert_sector = |index: u8, start: u32, size: u32, addr: u32| { 121 const SMALL_SECTOR_SIZE: u32 = 16 * 1024;
154 assert_eq!(FlashSector { index, start, size }, get_sector(addr)) 122 const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024;
123 const LARGE_SECTOR_SIZE: u32 = 128 * 1024;
124
125 let assert_sector = |index_in_bank: u8, start: u32, size: u32, address: u32| {
126 assert_eq!(
127 FlashSector {
128 bank: FlashBank::Bank1,
129 index_in_bank,
130 start,
131 size
132 },
133 get_sector(address, &FLASH_REGIONS)
134 )
135 };
136
137 assert_sector(0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_0000);
138 assert_sector(0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_3FFF);
139 assert_sector(3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_C000);
140 assert_sector(3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_FFFF);
141
142 assert_sector(4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_0000);
143 assert_sector(4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_FFFF);
144
145 assert_sector(5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0802_0000);
146 assert_sector(5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0803_FFFF);
147 assert_sector(7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0806_0000);
148 assert_sector(7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0807_FFFF);
149 }
150
151 #[test]
152 #[cfg(stm32f769)]
153 fn can_get_sector() {
154 const SMALL_SECTOR_SIZE: u32 = 32 * 1024;
155 const MEDIUM_SECTOR_SIZE: u32 = 128 * 1024;
156 const LARGE_SECTOR_SIZE: u32 = 256 * 1024;
157
158 let assert_sector = |index_in_bank: u8, start: u32, size: u32, address: u32| {
159 assert_eq!(
160 FlashSector {
161 bank: FlashBank::Bank1,
162 index_in_bank,
163 start,
164 size
165 },
166 get_sector(address, &FLASH_REGIONS)
167 )
155 }; 168 };
156 169
157 assert_sector(0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_0000); 170 assert_sector(0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_0000);
diff --git a/embassy-stm32/src/flash/h7.rs b/embassy-stm32/src/flash/h7.rs
index 360ad77db..5ea57ccde 100644
--- a/embassy-stm32/src/flash/h7.rs
+++ b/embassy-stm32/src/flash/h7.rs
@@ -3,7 +3,7 @@ use core::ptr::write_volatile;
3 3
4use atomic_polyfill::{fence, Ordering}; 4use atomic_polyfill::{fence, Ordering};
5 5
6use super::{FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE}; 6use super::{FlashRegion, FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE};
7use crate::flash::Error; 7use crate::flash::Error;
8use crate::pac; 8use crate::pac;
9 9
@@ -11,6 +11,10 @@ const fn is_dual_bank() -> bool {
11 FLASH_REGIONS.len() == 2 11 FLASH_REGIONS.len() == 2
12} 12}
13 13
14pub(crate) fn get_flash_regions() -> &'static [&'static FlashRegion] {
15 &FLASH_REGIONS
16}
17
14pub(crate) unsafe fn lock() { 18pub(crate) unsafe fn lock() {
15 pac::FLASH.bank(0).cr().modify(|w| w.set_lock(true)); 19 pac::FLASH.bank(0).cr().modify(|w| w.set_lock(true));
16 if is_dual_bank() { 20 if is_dual_bank() {
@@ -75,11 +79,10 @@ pub(crate) unsafe fn blocking_write(start_address: u32, buf: &[u8; WRITE_SIZE])
75} 79}
76 80
77pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> { 81pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), Error> {
78 let bank = pac::FLASH.bank(if sector.index >= 8 { 1 } else { 0 }); 82 let bank = pac::FLASH.bank(sector.bank as usize);
79 let sector = sector.index % 8;
80 bank.cr().modify(|w| { 83 bank.cr().modify(|w| {
81 w.set_ser(true); 84 w.set_ser(true);
82 w.set_snb(sector) 85 w.set_snb(sector.index_in_bank)
83 }); 86 });
84 87
85 bank.cr().modify(|w| { 88 bank.cr().modify(|w| {
@@ -175,13 +178,3 @@ unsafe fn blocking_wait_ready(bank: pac::flash::Bank) -> Result<(), Error> {
175 } 178 }
176 } 179 }
177} 180}
178
179pub(crate) fn get_sector(address: u32) -> FlashSector {
180 let sector_size = BANK1_REGION.erase_size;
181 let index = address / sector_size;
182 FlashSector {
183 index: index as u8,
184 start: BANK1_REGION.base + index * sector_size,
185 size: sector_size,
186 }
187}
diff --git a/embassy-stm32/src/flash/l.rs b/embassy-stm32/src/flash/l.rs
index f4b11011f..f5ebc0a5f 100644
--- a/embassy-stm32/src/flash/l.rs
+++ b/embassy-stm32/src/flash/l.rs
@@ -2,10 +2,14 @@ use core::ptr::write_volatile;
2 2
3use atomic_polyfill::{fence, Ordering}; 3use atomic_polyfill::{fence, Ordering};
4 4
5use super::{FlashSector, BANK1_REGION, WRITE_SIZE}; 5use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
6use crate::flash::Error; 6use crate::flash::Error;
7use crate::pac; 7use crate::pac;
8 8
9pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
10 &FLASH_REGIONS
11}
12
9pub(crate) unsafe fn lock() { 13pub(crate) unsafe fn lock() {
10 #[cfg(any(flash_wl, flash_wb, flash_l4))] 14 #[cfg(any(flash_wl, flash_wb, flash_l4))]
11 pac::FLASH.cr().modify(|w| w.set_lock(true)); 15 pac::FLASH.cr().modify(|w| w.set_lock(true));
@@ -73,7 +77,7 @@ pub(crate) unsafe fn blocking_erase_sector(sector: &FlashSector) -> Result<(), E
73 77
74 #[cfg(any(flash_wl, flash_wb, flash_l4))] 78 #[cfg(any(flash_wl, flash_wb, flash_l4))]
75 { 79 {
76 let idx = (sector.start - super::FLASH_BASE as u32) / BANK1_REGION.erase_size as u32; 80 let idx = (sector.start - super::FLASH_BASE as u32) / super::BANK1_REGION.erase_size as u32;
77 81
78 #[cfg(flash_l4)] 82 #[cfg(flash_l4)]
79 let (idx, bank) = if idx > 255 { (idx - 256, true) } else { (idx, false) }; 83 let (idx, bank) = if idx > 255 { (idx - 256, true) } else { (idx, false) };
@@ -180,13 +184,3 @@ unsafe fn blocking_wait_ready() -> Result<(), Error> {
180 } 184 }
181 } 185 }
182} 186}
183
184pub(crate) fn get_sector(address: u32) -> FlashSector {
185 let sector_size = BANK1_REGION.erase_size;
186 let index = address / sector_size;
187 FlashSector {
188 index: index as u8,
189 start: BANK1_REGION.base + index * sector_size,
190 size: sector_size,
191 }
192}
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs
index 110b81496..794d32ccd 100644
--- a/embassy-stm32/src/flash/mod.rs
+++ b/embassy-stm32/src/flash/mod.rs
@@ -10,6 +10,7 @@ pub use crate::_generated::flash_regions::*;
10pub use crate::pac::{FLASH_BASE, FLASH_SIZE, WRITE_SIZE}; 10pub use crate::pac::{FLASH_BASE, FLASH_SIZE, WRITE_SIZE};
11 11
12pub struct FlashRegion { 12pub struct FlashRegion {
13 pub bank: FlashBank,
13 pub base: u32, 14 pub base: u32,
14 pub size: u32, 15 pub size: u32,
15 pub erase_size: u32, 16 pub erase_size: u32,
@@ -19,15 +20,27 @@ pub struct FlashRegion {
19 20
20#[derive(Debug, PartialEq)] 21#[derive(Debug, PartialEq)]
21pub struct FlashSector { 22pub struct FlashSector {
22 pub index: u8, 23 pub bank: FlashBank,
24 pub index_in_bank: u8,
23 pub start: u32, 25 pub start: u32,
24 pub size: u32, 26 pub size: u32,
25} 27}
26 28
29#[derive(Clone, Copy, Debug, PartialEq)]
30pub enum FlashBank {
31 Bank1 = 0,
32 Bank2 = 1,
33 Otp,
34}
35
27impl FlashRegion { 36impl FlashRegion {
28 pub const fn end(&self) -> u32 { 37 pub const fn end(&self) -> u32 {
29 self.base + self.size 38 self.base + self.size
30 } 39 }
40
41 pub const fn sectors(&self) -> u8 {
42 (self.size / self.erase_size) as u8
43 }
31} 44}
32 45
33#[cfg_attr(any(flash_l0, flash_l1, flash_l4, flash_wl, flash_wb), path = "l.rs")] 46#[cfg_attr(any(flash_l0, flash_l1, flash_l4, flash_wl, flash_wb), path = "l.rs")]
diff --git a/embassy-stm32/src/flash/other.rs b/embassy-stm32/src/flash/other.rs
index fd3551224..d329dcab4 100644
--- a/embassy-stm32/src/flash/other.rs
+++ b/embassy-stm32/src/flash/other.rs
@@ -1,5 +1,10 @@
1#[allow(unused)] 1#![allow(unused)]
2use super::{Error, FlashSector, WRITE_SIZE}; 2
3use super::{Error, FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
4
5pub(crate) const fn get_flash_regions() -> &'static [&'static FlashRegion] {
6 &FLASH_REGIONS
7}
3 8
4pub(crate) unsafe fn lock() { 9pub(crate) unsafe fn lock() {
5 unimplemented!(); 10 unimplemented!();
@@ -22,6 +27,3 @@ pub(crate) unsafe fn blocking_erase_sector(_sector: &FlashSector) -> Result<(),
22pub(crate) unsafe fn clear_all_err() { 27pub(crate) unsafe fn clear_all_err() {
23 unimplemented!(); 28 unimplemented!();
24} 29}
25pub(crate) fn get_sector(_address: u32) -> FlashSector {
26 unimplemented!();
27}