1use core::marker::PhantomData;
2use core::sync::atomic::{fence, Ordering};
3
4use embassy_hal_internal::drop::OnDrop;
5use embassy_hal_internal::{into_ref, PeripheralRef};
6
7use super::{
8 family, Async, Blocking, Error, FlashBank, FlashLayout, FlashRegion, FlashSector, FLASH_SIZE, MAX_ERASE_SIZE,
9 READ_SIZE, WRITE_SIZE,
10};
11use crate::_generated::FLASH_BASE;
12use crate::peripherals::FLASH;
13use crate::Peripheral;
14
15pub struct Flash<'d, MODE = Async> {
17 pub(crate) inner: PeripheralRef<'d, FLASH>,
18 pub(crate) _mode: PhantomData<MODE>,
19}
20
21impl<'d> Flash<'d, Blocking> {
22 pub fn new_blocking(p: impl Peripheral<P = FLASH> + 'd) -> Self {
24 into_ref!(p);
25
26 Self {
27 inner: p,
28 _mode: PhantomData,
29 }
30 }
31}
32
33impl<'d, MODE> Flash<'d, MODE> {
34 pub fn into_blocking_regions(self) -> FlashLayout<'d, Blocking> {
38 assert!(family::is_default_layout());
39 FlashLayout::new(self.inner)
40 }
41
42 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
47 blocking_read(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes)
48 }
49
50 pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
55 unsafe {
56 blocking_write(
57 FLASH_BASE as u32,
58 FLASH_SIZE as u32,
59 offset,
60 bytes,
61 write_chunk_unlocked,
62 )
63 }
64 }
65
66 pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
71 unsafe { blocking_erase(FLASH_BASE as u32, from, to, erase_sector_unlocked) }
72 }
73}
74
75pub(super) fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
76 if offset + bytes.len() as u32 > size {
77 return Err(Error::Size);
78 }
79
80 let start_address = base + offset;
81
82 #[cfg(flash_f4)]
83 family::assert_not_corrupted_read(start_address + bytes.len() as u32);
84
85 let flash_data = unsafe { core::slice::from_raw_parts(start_address as *const u8, bytes.len()) };
86 bytes.copy_from_slice(flash_data);
87 Ok(())
88}
89
90pub(super) unsafe fn blocking_write(
91 base: u32,
92 size: u32,
93 offset: u32,
94 bytes: &[u8],
95 write_chunk: unsafe fn(u32, &[u8]) -> Result<(), Error>,
96) -> Result<(), Error> {
97 if offset + bytes.len() as u32 > size {
98 return Err(Error::Size);
99 }
100 if offset % WRITE_SIZE as u32 != 0 || bytes.len() % WRITE_SIZE != 0 {
101 return Err(Error::Unaligned);
102 }
103
104 let mut address = base + offset;
105 trace!("Writing {} bytes at 0x{:x}", bytes.len(), address);
106
107 for chunk in bytes.chunks(WRITE_SIZE) {
108 write_chunk(address, chunk)?;
109 address += WRITE_SIZE as u32;
110 }
111 Ok(())
112}
113
114pub(super) unsafe fn write_chunk_unlocked(address: u32, chunk: &[u8]) -> Result<(), Error> {
115 family::clear_all_err();
116 fence(Ordering::SeqCst);
117 family::unlock();
118 fence(Ordering::SeqCst);
119 family::enable_blocking_write();
120 fence(Ordering::SeqCst);
121
122 let _on_drop = OnDrop::new(|| {
123 family::disable_blocking_write();
124 fence(Ordering::SeqCst);
125 family::lock();
126 });
127
128 family::blocking_write(address, unwrap!(chunk.try_into()))
129}
130
131pub(super) unsafe fn write_chunk_with_critical_section(address: u32, chunk: &[u8]) -> Result<(), Error> {
132 critical_section::with(|_| write_chunk_unlocked(address, chunk))
133}
134
135pub(super) unsafe fn blocking_erase(
136 base: u32,
137 from: u32,
138 to: u32,
139 erase_sector: unsafe fn(&FlashSector) -> Result<(), Error>,
140) -> Result<(), Error> {
141 let start_address = base + from;
142 let end_address = base + to;
143 let regions = family::get_flash_regions();
144
145 ensure_sector_aligned(start_address, end_address, regions)?;
146
147 trace!("Erasing from 0x{:x} to 0x{:x}", start_address, end_address);
148
149 let mut address = start_address;
150 while address < end_address {
151 let sector = get_sector(address, regions);
152 trace!("Erasing sector: {:?}", sector);
153 erase_sector(§or)?;
154 address += sector.size;
155 }
156 Ok(())
157}
158
159pub(super) unsafe fn erase_sector_unlocked(sector: &FlashSector) -> Result<(), Error> {
160 family::clear_all_err();
161 fence(Ordering::SeqCst);
162 family::unlock();
163 fence(Ordering::SeqCst);
164
165 let _on_drop = OnDrop::new(|| family::lock());
166
167 family::blocking_erase_sector(sector)
168}
169
170pub(super) unsafe fn erase_sector_with_critical_section(sector: &FlashSector) -> Result<(), Error> {
171 critical_section::with(|_| erase_sector_unlocked(sector))
172}
173
174pub(super) fn get_sector(address: u32, regions: &[&FlashRegion]) -> FlashSector {
175 let mut current_bank = FlashBank::Bank1;
176 let mut bank_offset = 0;
177 for region in regions {
178 if region.bank != current_bank {
179 current_bank = region.bank;
180 bank_offset = 0;
181 }
182
183 if address >= region.base && address < region.end() {
184 let index_in_region = (address - region.base) / region.erase_size;
185 return FlashSector {
186 bank: region.bank,
187 index_in_bank: bank_offset + index_in_region as u8,
188 start: region.base + index_in_region * region.erase_size,
189 size: region.erase_size,
190 };
191 }
192
193 bank_offset += region.sectors();
194 }
195
196 panic!("Flash sector not found");
197}
198
199pub(super) fn ensure_sector_aligned(
200 start_address: u32,
201 end_address: u32,
202 regions: &[&FlashRegion],
203) -> Result<(), Error> {
204 let mut address = start_address;
205 while address < end_address {
206 let sector = get_sector(address, regions);
207 if sector.start != address {
208 return Err(Error::Unaligned);
209 }
210 address += sector.size;
211 }
212 if address != end_address {
213 return Err(Error::Unaligned);
214 }
215 Ok(())
216}
217
218impl<MODE> embedded_storage::nor_flash::ErrorType for Flash<'_, MODE> {
219 type Error = Error;
220}
221
222impl<MODE> embedded_storage::nor_flash::ReadNorFlash for Flash<'_, MODE> {
223 const READ_SIZE: usize = READ_SIZE;
224
225 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
226 self.blocking_read(offset, bytes)
227 }
228
229 fn capacity(&self) -> usize {
230 FLASH_SIZE
231 }
232}
233
234impl<MODE> embedded_storage::nor_flash::NorFlash for Flash<'_, MODE> {
235 const WRITE_SIZE: usize = WRITE_SIZE;
236 const ERASE_SIZE: usize = MAX_ERASE_SIZE;
237
238 fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
239 self.blocking_write(offset, bytes)
240 }
241
242 fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
243 self.blocking_erase(from, to)
244 }
245}
246
247foreach_flash_region! {
248 ($type_name:ident, $write_size:literal, $erase_size:literal) => {
249 impl<MODE> crate::_generated::flash_regions::$type_name<'_, MODE> {
250 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
255 blocking_read(self.0.base, self.0.size, offset, bytes)
256 }
257 }
258
259 impl crate::_generated::flash_regions::$type_name<'_, Blocking> {
260 pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
265 unsafe { blocking_write(self.0.base, self.0.size, offset, bytes, write_chunk_with_critical_section) }
266 }
267
268 pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
273 unsafe { blocking_erase(self.0.base, from, to, erase_sector_with_critical_section) }
274 }
275 }
276
277 impl<MODE> embedded_storage::nor_flash::ErrorType for crate::_generated::flash_regions::$type_name<'_, MODE> {
278 type Error = Error;
279 }
280
281 impl<MODE> embedded_storage::nor_flash::ReadNorFlash for crate::_generated::flash_regions::$type_name<'_, MODE> {
282 const READ_SIZE: usize = READ_SIZE;
283
284 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
285 self.blocking_read(offset, bytes)
286 }
287
288 fn capacity(&self) -> usize {
289 self.0.size as usize
290 }
291 }
292
293 impl embedded_storage::nor_flash::NorFlash for crate::_generated::flash_regions::$type_name<'_, Blocking> {
294 const WRITE_SIZE: usize = $write_size;
295 const ERASE_SIZE: usize = $erase_size;
296
297 fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
298 self.blocking_write(offset, bytes)
299 }
300
301 fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
302 self.blocking_erase(from, to)
303 }
304 }
305 };
306}