diff options
| -rw-r--r-- | embassy-stm32/src/flash/f3.rs | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/embassy-stm32/src/flash/f3.rs b/embassy-stm32/src/flash/f3.rs index 1cb08ee1a..294fcffc2 100644 --- a/embassy-stm32/src/flash/f3.rs +++ b/embassy-stm32/src/flash/f3.rs | |||
| @@ -1,9 +1,14 @@ | |||
| 1 | use core::convert::TryInto; | 1 | use core::convert::TryInto; |
| 2 | use core::mem::size_of; | ||
| 2 | use core::ptr::write_volatile; | 3 | use core::ptr::write_volatile; |
| 3 | 4 | ||
| 5 | use super::FlashRegion; | ||
| 4 | use crate::flash::Error; | 6 | use crate::flash::Error; |
| 5 | use crate::pac; | 7 | use crate::pac; |
| 6 | 8 | ||
| 9 | pub(crate) const MAX_WRITE_SIZE: usize = super::MAINA::WRITE_SIZE; | ||
| 10 | pub(crate) const MAX_ERASE_SIZE: usize = super::MAINA::ERASE_SIZE; | ||
| 11 | |||
| 7 | pub(crate) unsafe fn lock() { | 12 | pub(crate) unsafe fn lock() { |
| 8 | pac::FLASH.cr().modify(|w| w.set_lock(true)); | 13 | pac::FLASH.cr().modify(|w| w.set_lock(true)); |
| 9 | } | 14 | } |
| @@ -13,15 +18,17 @@ pub(crate) unsafe fn unlock() { | |||
| 13 | pac::FLASH.keyr().write(|w| w.set_fkeyr(0xCDEF_89AB)); | 18 | pac::FLASH.keyr().write(|w| w.set_fkeyr(0xCDEF_89AB)); |
| 14 | } | 19 | } |
| 15 | 20 | ||
| 16 | pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error> { | 21 | pub(crate) unsafe fn blocking_write(first_address: u32, buf: &[u8]) -> Result<(), Error> { |
| 17 | pac::FLASH.cr().write(|w| w.set_pg(true)); | 22 | pac::FLASH.cr().write(|w| w.set_pg(true)); |
| 18 | 23 | ||
| 19 | let ret = { | 24 | let ret = { |
| 20 | let mut ret: Result<(), Error> = Ok(()); | 25 | let mut ret: Result<(), Error> = Ok(()); |
| 21 | let mut offset = offset; | 26 | let mut address = first_address; |
| 22 | for chunk in buf.chunks(2) { | 27 | let chunks = buf.chunks_exact(size_of::<u16>()); |
| 23 | write_volatile(offset as *mut u16, u16::from_le_bytes(chunk[0..2].try_into().unwrap())); | 28 | assert!(chunks.remainder().is_empty()); |
| 24 | offset += chunk.len() as u32; | 29 | for chunk in chunks { |
| 30 | write_volatile(address as *mut u16, u16::from_le_bytes(chunk.try_into().unwrap())); | ||
| 31 | address += chunk.len() as u32; | ||
| 25 | 32 | ||
| 26 | ret = blocking_wait_ready(); | 33 | ret = blocking_wait_ready(); |
| 27 | if ret.is_err() { | 34 | if ret.is_err() { |
| @@ -36,8 +43,8 @@ pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error | |||
| 36 | ret | 43 | ret |
| 37 | } | 44 | } |
| 38 | 45 | ||
| 39 | pub(crate) unsafe fn blocking_erase(from: u32, to: u32) -> Result<(), Error> { | 46 | pub(crate) unsafe fn blocking_erase(from_address: u32, to_address: u32) -> Result<(), Error> { |
| 40 | for page in (from..to).step_by(super::ERASE_SIZE) { | 47 | for page in (from_address..to_address).step_by(MAX_ERASE_SIZE) { |
| 41 | pac::FLASH.cr().modify(|w| { | 48 | pac::FLASH.cr().modify(|w| { |
| 42 | w.set_per(true); | 49 | w.set_per(true); |
| 43 | }); | 50 | }); |
