aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDion Dokter <[email protected]>2025-05-01 11:15:14 +0200
committerDion Dokter <[email protected]>2025-05-01 11:15:14 +0200
commita83726b77e1f40293b6c0701c7d203160bc150fb (patch)
treefdee8c390aa7fc3148b69c01dcc67dbf752588d1
parentace20f40ad1b2168c9d2b78155c0cf29244bc456 (diff)
Fix the f4 flash tests by throwing away alt mode
-rw-r--r--embassy-stm32/src/flash/f4.rs317
1 files changed, 68 insertions, 249 deletions
diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs
index ba3067556..7d789a6bb 100644
--- a/embassy-stm32/src/flash/f4.rs
+++ b/embassy-stm32/src/flash/f4.rs
@@ -8,173 +8,6 @@ use super::{FlashBank, FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE};
8use crate::_generated::FLASH_SIZE; 8use crate::_generated::FLASH_SIZE;
9use crate::flash::Error; 9use crate::flash::Error;
10use crate::pac; 10use crate::pac;
11#[allow(missing_docs)] // TODO
12#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))]
13mod alt_regions {
14 use core::marker::PhantomData;
15
16 use crate::Peri;
17 use crate::_generated::flash_regions::{OTPRegion, BANK1_REGION1, BANK1_REGION2, BANK1_REGION3, OTP_REGION};
18 use crate::_generated::FLASH_SIZE;
19 use crate::flash::{asynch, Async, Bank1Region1, Bank1Region2, Blocking, Error, Flash, FlashBank, FlashRegion};
20 use crate::peripherals::FLASH;
21
22 pub const ALT_BANK1_REGION3: FlashRegion = FlashRegion {
23 size: 3 * BANK1_REGION3.erase_size,
24 ..BANK1_REGION3
25 };
26 pub const ALT_BANK2_REGION1: FlashRegion = FlashRegion {
27 bank: FlashBank::Bank2,
28 base: BANK1_REGION1.base + FLASH_SIZE as u32 / 2,
29 ..BANK1_REGION1
30 };
31 pub const ALT_BANK2_REGION2: FlashRegion = FlashRegion {
32 bank: FlashBank::Bank2,
33 base: BANK1_REGION2.base + FLASH_SIZE as u32 / 2,
34 ..BANK1_REGION2
35 };
36 pub const ALT_BANK2_REGION3: FlashRegion = FlashRegion {
37 bank: FlashBank::Bank2,
38 base: BANK1_REGION3.base + FLASH_SIZE as u32 / 2,
39 size: 3 * BANK1_REGION3.erase_size,
40 ..BANK1_REGION3
41 };
42
43 pub const ALT_FLASH_REGIONS: [&FlashRegion; 6] = [
44 &BANK1_REGION1,
45 &BANK1_REGION2,
46 &ALT_BANK1_REGION3,
47 &ALT_BANK2_REGION1,
48 &ALT_BANK2_REGION2,
49 &ALT_BANK2_REGION3,
50 ];
51
52 pub struct AltBank1Region3<'d, MODE = Async>(pub &'static FlashRegion, Peri<'d, FLASH>, PhantomData<MODE>);
53 pub struct AltBank2Region1<'d, MODE = Async>(pub &'static FlashRegion, Peri<'d, FLASH>, PhantomData<MODE>);
54 pub struct AltBank2Region2<'d, MODE = Async>(pub &'static FlashRegion, Peri<'d, FLASH>, PhantomData<MODE>);
55 pub struct AltBank2Region3<'d, MODE = Async>(pub &'static FlashRegion, Peri<'d, FLASH>, PhantomData<MODE>);
56
57 pub struct AltFlashLayout<'d, MODE = Async> {
58 pub bank1_region1: Bank1Region1<'d, MODE>,
59 pub bank1_region2: Bank1Region2<'d, MODE>,
60 pub bank1_region3: AltBank1Region3<'d, MODE>,
61 pub bank2_region1: AltBank2Region1<'d, MODE>,
62 pub bank2_region2: AltBank2Region2<'d, MODE>,
63 pub bank2_region3: AltBank2Region3<'d, MODE>,
64 pub otp_region: OTPRegion<'d, MODE>,
65 }
66
67 impl<'d> Flash<'d> {
68 pub fn into_alt_regions(self) -> AltFlashLayout<'d, Async> {
69 assert!(!super::is_default_layout());
70
71 // SAFETY: We never expose the cloned peripheral references, and their instance is not public.
72 // Also, all async flash region operations are protected with a mutex.
73 let p = self.inner;
74 AltFlashLayout {
75 bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }, PhantomData),
76 bank1_region2: Bank1Region2(&BANK1_REGION2, unsafe { p.clone_unchecked() }, PhantomData),
77 bank1_region3: AltBank1Region3(&ALT_BANK1_REGION3, unsafe { p.clone_unchecked() }, PhantomData),
78 bank2_region1: AltBank2Region1(&ALT_BANK2_REGION1, unsafe { p.clone_unchecked() }, PhantomData),
79 bank2_region2: AltBank2Region2(&ALT_BANK2_REGION2, unsafe { p.clone_unchecked() }, PhantomData),
80 bank2_region3: AltBank2Region3(&ALT_BANK2_REGION3, unsafe { p.clone_unchecked() }, PhantomData),
81 otp_region: OTPRegion(&OTP_REGION, unsafe { p.clone_unchecked() }, PhantomData),
82 }
83 }
84
85 pub fn into_alt_blocking_regions(self) -> AltFlashLayout<'d, Blocking> {
86 assert!(!super::is_default_layout());
87
88 // SAFETY: We never expose the cloned peripheral references, and their instance is not public.
89 // Also, all blocking flash region operations are protected with a cs.
90 let p = self.inner;
91 AltFlashLayout {
92 bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }, PhantomData),
93 bank1_region2: Bank1Region2(&BANK1_REGION2, unsafe { p.clone_unchecked() }, PhantomData),
94 bank1_region3: AltBank1Region3(&ALT_BANK1_REGION3, unsafe { p.clone_unchecked() }, PhantomData),
95 bank2_region1: AltBank2Region1(&ALT_BANK2_REGION1, unsafe { p.clone_unchecked() }, PhantomData),
96 bank2_region2: AltBank2Region2(&ALT_BANK2_REGION2, unsafe { p.clone_unchecked() }, PhantomData),
97 bank2_region3: AltBank2Region3(&ALT_BANK2_REGION3, unsafe { p.clone_unchecked() }, PhantomData),
98 otp_region: OTPRegion(&OTP_REGION, unsafe { p.clone_unchecked() }, PhantomData),
99 }
100 }
101 }
102
103 macro_rules! foreach_altflash_region {
104 ($type_name:ident, $region:ident) => {
105 impl<MODE> $type_name<'_, MODE> {
106 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
107 crate::flash::common::blocking_read(self.0.base, self.0.size, offset, bytes)
108 }
109 }
110
111 impl $type_name<'_, Async> {
112 pub async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
113 self.blocking_read(offset, bytes)
114 }
115
116 pub async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
117 let _guard = asynch::REGION_ACCESS.lock().await;
118 unsafe { asynch::write_chunked(self.0.base, self.0.size, offset, bytes).await }
119 }
120
121 pub async fn erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
122 let _guard = asynch::REGION_ACCESS.lock().await;
123 unsafe { asynch::erase_sectored(self.0.base, from, to).await }
124 }
125 }
126
127 impl<MODE> embedded_storage::nor_flash::ErrorType for $type_name<'_, MODE> {
128 type Error = Error;
129 }
130
131 impl<MODE> embedded_storage::nor_flash::ReadNorFlash for $type_name<'_, MODE> {
132 const READ_SIZE: usize = crate::flash::READ_SIZE;
133
134 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
135 self.blocking_read(offset, bytes)
136 }
137
138 fn capacity(&self) -> usize {
139 self.0.size as usize
140 }
141 }
142
143 impl embedded_storage_async::nor_flash::ReadNorFlash for $type_name<'_, Async> {
144 const READ_SIZE: usize = crate::flash::READ_SIZE;
145
146 async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
147 self.read(offset, bytes).await
148 }
149
150 fn capacity(&self) -> usize {
151 self.0.size as usize
152 }
153 }
154
155 impl embedded_storage_async::nor_flash::NorFlash for $type_name<'_, Async> {
156 const WRITE_SIZE: usize = $region.write_size as usize;
157 const ERASE_SIZE: usize = $region.erase_size as usize;
158
159 async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
160 self.write(offset, bytes).await
161 }
162
163 async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
164 self.erase(from, to).await
165 }
166 }
167 };
168 }
169
170 foreach_altflash_region!(AltBank1Region3, ALT_BANK1_REGION3);
171 foreach_altflash_region!(AltBank2Region1, ALT_BANK2_REGION1);
172 foreach_altflash_region!(AltBank2Region2, ALT_BANK2_REGION2);
173 foreach_altflash_region!(AltBank2Region3, ALT_BANK2_REGION3);
174}
175
176#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))]
177pub use alt_regions::*;
178 11
179static WAKER: AtomicWaker = AtomicWaker::new(); 12static WAKER: AtomicWaker = AtomicWaker::new();
180static DATA_CACHE_WAS_ENABLED: AtomicBool = AtomicBool::new(false); 13static DATA_CACHE_WAS_ENABLED: AtomicBool = AtomicBool::new(false);
@@ -185,26 +18,10 @@ impl FlashSector {
185 } 18 }
186} 19}
187 20
188#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))]
189pub(crate) fn is_default_layout() -> bool {
190 !pac::FLASH.optcr().read().db1m()
191}
192
193#[cfg(not(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479)))]
194pub(crate) const fn is_default_layout() -> bool { 21pub(crate) const fn is_default_layout() -> bool {
195 true 22 true
196} 23}
197 24
198#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))]
199pub fn get_flash_regions() -> &'static [&'static FlashRegion] {
200 if is_default_layout() {
201 &FLASH_REGIONS
202 } else {
203 &ALT_FLASH_REGIONS
204 }
205}
206
207#[cfg(not(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479)))]
208pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { 25pub const fn get_flash_regions() -> &'static [&'static FlashRegion] {
209 &FLASH_REGIONS 26 &FLASH_REGIONS
210} 27}
@@ -487,72 +304,74 @@ mod tests {
487 const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024; 304 const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024;
488 const LARGE_SECTOR_SIZE: u32 = 128 * 1024; 305 const LARGE_SECTOR_SIZE: u32 = 128 * 1024;
489 306
490 let assert_sector = |snb: u8, index_in_bank: u8, start: u32, size: u32, address: u32| { 307 if !cfg!(feature = "dual-bank") {
491 let sector = get_sector(address, &FLASH_REGIONS); 308 let assert_sector = |snb: u8, index_in_bank: u8, start: u32, size: u32, address: u32| {
492 assert_eq!(snb, sector.snb()); 309 let sector = get_sector(address, &FLASH_REGIONS);
493 assert_eq!( 310 assert_eq!(snb, sector.snb());
494 FlashSector { 311 assert_eq!(
495 bank: FlashBank::Bank1, 312 FlashSector {
496 index_in_bank, 313 bank: sector.bank,
497 start, 314 index_in_bank,
498 size 315 start,
499 }, 316 size
500 sector 317 },
501 ); 318 sector
502 }; 319 );
503 320 };
504 assert_sector(0x00, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_0000); 321
505 assert_sector(0x00, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_3FFF); 322 assert_sector(0x00, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_0000);
506 assert_sector(0x03, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_C000); 323 assert_sector(0x00, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_3FFF);
507 assert_sector(0x03, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_FFFF); 324 assert_sector(0x03, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_C000);
508 325 assert_sector(0x03, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_FFFF);
509 assert_sector(0x04, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_0000); 326
510 assert_sector(0x04, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_FFFF); 327 assert_sector(0x04, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_0000);
511 328 assert_sector(0x04, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_FFFF);
512 assert_sector(0x05, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0802_0000); 329
513 assert_sector(0x05, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0803_FFFF); 330 assert_sector(0x05, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0802_0000);
514 assert_sector(0x0B, 11, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080E_0000); 331 assert_sector(0x05, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0803_FFFF);
515 assert_sector(0x0B, 11, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080F_FFFF); 332 assert_sector(0x0B, 11, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080E_0000);
516 333 assert_sector(0x0B, 11, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080F_FFFF);
517 let assert_sector = |snb: u8, bank: FlashBank, index_in_bank: u8, start: u32, size: u32, address: u32| { 334 } else {
518 let sector = get_sector(address, &ALT_FLASH_REGIONS); 335 let assert_sector = |snb: u8, bank: FlashBank, index_in_bank: u8, start: u32, size: u32, address: u32| {
519 assert_eq!(snb, sector.snb()); 336 let sector = get_sector(address, &FLASH_REGIONS);
520 assert_eq!( 337 assert_eq!(snb, sector.snb());
521 FlashSector { 338 assert_eq!(
522 bank, 339 FlashSector {
523 index_in_bank, 340 bank,
524 start, 341 index_in_bank,
525 size 342 start,
526 }, 343 size
527 sector 344 },
528 ) 345 sector
529 }; 346 )
530 347 };
531 assert_sector(0x00, FlashBank::Bank1, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_0000); 348
532 assert_sector(0x00, FlashBank::Bank1, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_3FFF); 349 assert_sector(0x00, FlashBank::Bank1, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_0000);
533 assert_sector(0x03, FlashBank::Bank1, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_C000); 350 assert_sector(0x00, FlashBank::Bank1, 0, 0x0800_0000, SMALL_SECTOR_SIZE, 0x0800_3FFF);
534 assert_sector(0x03, FlashBank::Bank1, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_FFFF); 351 assert_sector(0x03, FlashBank::Bank1, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_C000);
535 352 assert_sector(0x03, FlashBank::Bank1, 3, 0x0800_C000, SMALL_SECTOR_SIZE, 0x0800_FFFF);
536 assert_sector(0x04, FlashBank::Bank1, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_0000); 353
537 assert_sector(0x04, FlashBank::Bank1, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_FFFF); 354 assert_sector(0x04, FlashBank::Bank1, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_0000);
538 355 assert_sector(0x04, FlashBank::Bank1, 4, 0x0801_0000, MEDIUM_SECTOR_SIZE, 0x0801_FFFF);
539 assert_sector(0x05, FlashBank::Bank1, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0802_0000); 356
540 assert_sector(0x05, FlashBank::Bank1, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0803_FFFF); 357 assert_sector(0x05, FlashBank::Bank1, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0802_0000);
541 assert_sector(0x07, FlashBank::Bank1, 7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0806_0000); 358 assert_sector(0x05, FlashBank::Bank1, 5, 0x0802_0000, LARGE_SECTOR_SIZE, 0x0803_FFFF);
542 assert_sector(0x07, FlashBank::Bank1, 7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0807_FFFF); 359 assert_sector(0x07, FlashBank::Bank1, 7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0806_0000);
543 360 assert_sector(0x07, FlashBank::Bank1, 7, 0x0806_0000, LARGE_SECTOR_SIZE, 0x0807_FFFF);
544 assert_sector(0x10, FlashBank::Bank2, 0, 0x0808_0000, SMALL_SECTOR_SIZE, 0x0808_0000); 361
545 assert_sector(0x10, FlashBank::Bank2, 0, 0x0808_0000, SMALL_SECTOR_SIZE, 0x0808_3FFF); 362 assert_sector(0x10, FlashBank::Bank2, 0, 0x0808_0000, SMALL_SECTOR_SIZE, 0x0808_0000);
546 assert_sector(0x13, FlashBank::Bank2, 3, 0x0808_C000, SMALL_SECTOR_SIZE, 0x0808_C000); 363 assert_sector(0x10, FlashBank::Bank2, 0, 0x0808_0000, SMALL_SECTOR_SIZE, 0x0808_3FFF);
547 assert_sector(0x13, FlashBank::Bank2, 3, 0x0808_C000, SMALL_SECTOR_SIZE, 0x0808_FFFF); 364 assert_sector(0x13, FlashBank::Bank2, 3, 0x0808_C000, SMALL_SECTOR_SIZE, 0x0808_C000);
548 365 assert_sector(0x13, FlashBank::Bank2, 3, 0x0808_C000, SMALL_SECTOR_SIZE, 0x0808_FFFF);
549 assert_sector(0x14, FlashBank::Bank2, 4, 0x0809_0000, MEDIUM_SECTOR_SIZE, 0x0809_0000); 366
550 assert_sector(0x14, FlashBank::Bank2, 4, 0x0809_0000, MEDIUM_SECTOR_SIZE, 0x0809_FFFF); 367 assert_sector(0x14, FlashBank::Bank2, 4, 0x0809_0000, MEDIUM_SECTOR_SIZE, 0x0809_0000);
551 368 assert_sector(0x14, FlashBank::Bank2, 4, 0x0809_0000, MEDIUM_SECTOR_SIZE, 0x0809_FFFF);
552 assert_sector(0x15, FlashBank::Bank2, 5, 0x080A_0000, LARGE_SECTOR_SIZE, 0x080A_0000); 369
553 assert_sector(0x15, FlashBank::Bank2, 5, 0x080A_0000, LARGE_SECTOR_SIZE, 0x080B_FFFF); 370 assert_sector(0x15, FlashBank::Bank2, 5, 0x080A_0000, LARGE_SECTOR_SIZE, 0x080A_0000);
554 assert_sector(0x17, FlashBank::Bank2, 7, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080E_0000); 371 assert_sector(0x15, FlashBank::Bank2, 5, 0x080A_0000, LARGE_SECTOR_SIZE, 0x080B_FFFF);
555 assert_sector(0x17, FlashBank::Bank2, 7, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080F_FFFF); 372 assert_sector(0x17, FlashBank::Bank2, 7, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080E_0000);
373 assert_sector(0x17, FlashBank::Bank2, 7, 0x080E_0000, LARGE_SECTOR_SIZE, 0x080F_FFFF);
374 }
556 } 375 }
557} 376}
558 377