aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/flash/common.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-12-17 22:09:14 +0100
committerDario Nieuwenhuis <[email protected]>2023-12-18 00:53:18 +0100
commit80c9d04bbd83367340a4f3a1e991df825a0b6029 (patch)
treed79b74b0ca17dd943dfcb3b809e895918f4ae629 /embassy-stm32/src/flash/common.rs
parenta2d4bab2f8a4a9b994bc0289938a9f725950715f (diff)
stm32: add some docs.
Diffstat (limited to 'embassy-stm32/src/flash/common.rs')
-rw-r--r--embassy-stm32/src/flash/common.rs33
1 files changed, 31 insertions, 2 deletions
diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs
index 8acad1c7c..f8561edb3 100644
--- a/embassy-stm32/src/flash/common.rs
+++ b/embassy-stm32/src/flash/common.rs
@@ -12,12 +12,14 @@ use super::{
12use crate::peripherals::FLASH; 12use crate::peripherals::FLASH;
13use crate::Peripheral; 13use crate::Peripheral;
14 14
15/// Internal flash memory driver.
15pub struct Flash<'d, MODE = Async> { 16pub struct Flash<'d, MODE = Async> {
16 pub(crate) inner: PeripheralRef<'d, FLASH>, 17 pub(crate) inner: PeripheralRef<'d, FLASH>,
17 pub(crate) _mode: PhantomData<MODE>, 18 pub(crate) _mode: PhantomData<MODE>,
18} 19}
19 20
20impl<'d> Flash<'d, Blocking> { 21impl<'d> Flash<'d, Blocking> {
22 /// Create a new flash driver, usable in blocking mode.
21 pub fn new_blocking(p: impl Peripheral<P = FLASH> + 'd) -> Self { 23 pub fn new_blocking(p: impl Peripheral<P = FLASH> + 'd) -> Self {
22 into_ref!(p); 24 into_ref!(p);
23 25
@@ -29,15 +31,26 @@ impl<'d> Flash<'d, Blocking> {
29} 31}
30 32
31impl<'d, MODE> Flash<'d, MODE> { 33impl<'d, MODE> Flash<'d, MODE> {
34 /// Split this flash driver into one instance per flash memory region.
35 ///
36 /// See module-level documentation for details on how memory regions work.
32 pub fn into_blocking_regions(self) -> FlashLayout<'d, Blocking> { 37 pub fn into_blocking_regions(self) -> FlashLayout<'d, Blocking> {
33 assert!(family::is_default_layout()); 38 assert!(family::is_default_layout());
34 FlashLayout::new(self.inner) 39 FlashLayout::new(self.inner)
35 } 40 }
36 41
37 pub fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { 42 /// Blocking read.
43 ///
44 /// NOTE: `offset` is an offset from the flash start, NOT an absolute address.
45 /// For example, to read address `0x0800_1234` you have to use offset `0x1234`.
46 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
38 blocking_read(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes) 47 blocking_read(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes)
39 } 48 }
40 49
50 /// Blocking write.
51 ///
52 /// NOTE: `offset` is an offset from the flash start, NOT an absolute address.
53 /// For example, to write address `0x0800_1234` you have to use offset `0x1234`.
41 pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { 54 pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
42 unsafe { 55 unsafe {
43 blocking_write( 56 blocking_write(
@@ -50,6 +63,10 @@ impl<'d, MODE> Flash<'d, MODE> {
50 } 63 }
51 } 64 }
52 65
66 /// Blocking erase.
67 ///
68 /// NOTE: `from` and `to` are offsets from the flash start, NOT an absolute address.
69 /// For example, to erase address `0x0801_0000` you have to use offset `0x1_0000`.
53 pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { 70 pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
54 unsafe { blocking_erase(FLASH_BASE as u32, from, to, erase_sector_unlocked) } 71 unsafe { blocking_erase(FLASH_BASE as u32, from, to, erase_sector_unlocked) }
55 } 72 }
@@ -206,7 +223,7 @@ impl<MODE> embedded_storage::nor_flash::ReadNorFlash for Flash<'_, MODE> {
206 const READ_SIZE: usize = READ_SIZE; 223 const READ_SIZE: usize = READ_SIZE;
207 224
208 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { 225 fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
209 self.read(offset, bytes) 226 self.blocking_read(offset, bytes)
210 } 227 }
211 228
212 fn capacity(&self) -> usize { 229 fn capacity(&self) -> usize {
@@ -230,16 +247,28 @@ impl<MODE> embedded_storage::nor_flash::NorFlash for Flash<'_, MODE> {
230foreach_flash_region! { 247foreach_flash_region! {
231 ($type_name:ident, $write_size:literal, $erase_size:literal) => { 248 ($type_name:ident, $write_size:literal, $erase_size:literal) => {
232 impl<MODE> crate::_generated::flash_regions::$type_name<'_, MODE> { 249 impl<MODE> crate::_generated::flash_regions::$type_name<'_, MODE> {
250 /// Blocking read.
251 ///
252 /// NOTE: `offset` is an offset from the flash start, NOT an absolute address.
253 /// For example, to read address `0x0800_1234` you have to use offset `0x1234`.
233 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { 254 pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
234 blocking_read(self.0.base, self.0.size, offset, bytes) 255 blocking_read(self.0.base, self.0.size, offset, bytes)
235 } 256 }
236 } 257 }
237 258
238 impl crate::_generated::flash_regions::$type_name<'_, Blocking> { 259 impl crate::_generated::flash_regions::$type_name<'_, Blocking> {
260 /// Blocking write.
261 ///
262 /// NOTE: `offset` is an offset from the flash start, NOT an absolute address.
263 /// For example, to write address `0x0800_1234` you have to use offset `0x1234`.
239 pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { 264 pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
240 unsafe { blocking_write(self.0.base, self.0.size, offset, bytes, write_chunk_with_critical_section) } 265 unsafe { blocking_write(self.0.base, self.0.size, offset, bytes, write_chunk_with_critical_section) }
241 } 266 }
242 267
268 /// Blocking erase.
269 ///
270 /// NOTE: `from` and `to` are offsets from the flash start, NOT an absolute address.
271 /// For example, to erase address `0x0801_0000` you have to use offset `0x1_0000`.
243 pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { 272 pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
244 unsafe { blocking_erase(self.0.base, from, to, erase_sector_with_critical_section) } 273 unsafe { blocking_erase(self.0.base, from, to, erase_sector_with_critical_section) }
245 } 274 }