1#[allow(dead_code)]
2use core::marker::PhantomData;
3use core::sync::atomic::{fence, Ordering};
4
5use crate::Peri;
6use embassy_hal_internal::drop::OnDrop;
7
8use super::{family, Async, Blocking, Error, FlashSector, FLASH_SIZE, READ_SIZE, WRITE_SIZE};
9use crate::peripherals::FLASH;
10
11pub const FLASH_BASE: usize = 0x0800_0000;
13pub struct Flash<'d, MODE = Async> {
18 pub(crate) _inner: Peri<'d, FLASH>,
19 pub(crate) _mode: PhantomData<MODE>,
20}
21
22impl<'d> Flash<'d, Blocking> {
23 pub fn new_blocking(p: Peri<'d, FLASH>) -> Self {
25
26 Self {
27 _inner: p,
28 _mode: PhantomData,
29 }
30 }
31}
32
33impl<'d, MODE> Flash<'d, MODE> {
34 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
39 blocking_read(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes)
40 }
41
42 pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
47 unsafe {
48 blocking_write(
49 FLASH_BASE as u32,
50 FLASH_SIZE as u32,
51 offset,
52 bytes,
53 write_chunk_unlocked,
54 )
55 }
56 }
57
58 pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
63 unsafe { blocking_erase(FLASH_BASE as u32, from, to, erase_sector_unlocked) }
64 }
65}
66
67pub(super) fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
68 if offset + bytes.len() as u32 > size {
69 return Err(Error::Size);
70 }
71
72 let start_address = base + offset;
73
74 #[cfg(flash_f4)]
75 family::assert_not_corrupted_read(start_address + bytes.len() as u32);
76
77 let flash_data = unsafe { core::slice::from_raw_parts(start_address as *const u8, bytes.len()) };
78 bytes.copy_from_slice(flash_data);
79 Ok(())
80}
81
82pub(super) unsafe fn blocking_write(
83 base: u32,
84 size: u32,
85 offset: u32,
86 bytes: &[u8],
87 write_chunk: unsafe fn(u32, &[u8]) -> Result<(), Error>,
88) -> Result<(), Error> {
89 if offset + bytes.len() as u32 > size {
90 return Err(Error::Size);
91 }
92 if offset % WRITE_SIZE as u32 != 0 || bytes.len() % WRITE_SIZE != 0 {
93 return Err(Error::Unaligned);
94 }
95
96 let mut address = base + offset;
97
98 for chunk in bytes.chunks(WRITE_SIZE) {
99 write_chunk(address, chunk)?;
100 address += WRITE_SIZE as u32;
101 }
102 Ok(())
103}
104
105pub(super) unsafe fn write_chunk_unlocked(address: u32, chunk: &[u8]) -> Result<(), Error> {
106 family::clear_all_err();
107 fence(Ordering::SeqCst);
108 family::unlock();
109 fence(Ordering::SeqCst);
110 family::enable_blocking_write();
111 fence(Ordering::SeqCst);
112
113 let _on_drop = OnDrop::new(|| {
114 family::disable_blocking_write();
115 fence(Ordering::SeqCst);
116 family::lock();
117 });
118
119 family::blocking_write(address, unwrap!(chunk.try_into()))
120}
121
122#[allow(unused)]
123pub(super) unsafe fn write_chunk_with_critical_section(address: u32, chunk: &[u8]) -> Result<(), Error> {
124 critical_section::with(|_| write_chunk_unlocked(address, chunk))
125}
126
127pub(super) unsafe fn blocking_erase(
128 base: u32,
129 from: u32,
130 to: u32,
131 erase_sector: unsafe fn(&FlashSector) -> Result<(), Error>,
132) -> Result<(), Error> {
133 let start_address = base + from;
134 let end_address = base + to;
135
136 ensure_sector_aligned(start_address, end_address)?;
137 family::unlock();
138 let _on_drop = OnDrop::new(|| {
139 family::lock();
140 });
141
142 trace!("Erasing from 0x{:x} to 0x{:x}", start_address, end_address);
143
144 let mut address = start_address;
145 while address < end_address {
146 let sector = get_sector(address);
147 trace!("Erasing sector: {:?}", sector);
148 erase_sector(§or)?;
149 address += sector.size;
150 }
151 Ok(())
152}
153
154pub(super) unsafe fn erase_sector_unlocked(sector: &FlashSector) -> Result<(), Error> {
155 family::clear_all_err();
156 fence(Ordering::SeqCst);
157 family::unlock();
158 fence(Ordering::SeqCst);
159
160 let _on_drop = OnDrop::new(|| family::lock());
161
162 family::blocking_erase_sector(sector)
163}
164
165#[allow(unused)]
166pub(super) unsafe fn erase_sector_with_critical_section(sector: &FlashSector) -> Result<(), Error> {
167 critical_section::with(|_| erase_sector_unlocked(sector))
168}
169
170pub(super) fn get_sector(address: u32) -> FlashSector {
171 let _bank_offset = 0;
172 let index_in_region = (address - FLASH_BASE as u32) / WRITE_SIZE as u32;
173 if (address as usize) < (FLASH_BASE + FLASH_SIZE) {
174 return FlashSector {
175 start: FLASH_BASE as u32 + index_in_region * WRITE_SIZE as u32,
176 size: WRITE_SIZE as u32,
177 };
178 }
179
180 panic!("Flash sector not found");
181}
182
183pub(super) fn ensure_sector_aligned(start_address: u32, end_address: u32) -> Result<(), Error> {
184 let mut address = start_address;
185 while address < end_address {
186 let sector = get_sector(address);
187 if sector.start != address {
188 return Err(Error::Unaligned);
189 }
190 address += sector.size;
191 }
192 if address != end_address {
193 return Err(Error::Unaligned);
194 }
195 Ok(())
196}
197
198impl<MODE> embedded_storage::nor_flash::ErrorType for Flash<'_, MODE> {
199 type Error = Error;
200}
201
202impl<MODE> embedded_storage::nor_flash::ReadNorFlash for Flash<'_, MODE> {
203 const READ_SIZE: usize = READ_SIZE;
204
205 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
206 self.blocking_read(offset, bytes)
207 }
208
209 fn capacity(&self) -> usize {
210 FLASH_SIZE
211 }
212}
213
214impl<MODE> embedded_storage::nor_flash::NorFlash for Flash<'_, MODE> {
215 const WRITE_SIZE: usize = WRITE_SIZE;
216 const ERASE_SIZE: usize = WRITE_SIZE;
217
218 fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
219 self.blocking_write(offset, bytes)
220 }
221
222 fn erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
223 self.blocking_erase(from, to)
224 }
225}