diff options
| author | Rasmus Melchior Jacobsen <[email protected]> | 2023-03-25 06:26:00 +0100 |
|---|---|---|
| committer | Rasmus Melchior Jacobsen <[email protected]> | 2023-03-25 06:26:00 +0100 |
| commit | a8567f06174c7a3fc2c708a2acb3763d9a59c8b7 (patch) | |
| tree | 23076a52048002c9a9a78815ff99367f032e4450 /embassy-stm32/src/flash/f7.rs | |
| parent | 99c4346579cd000128d6b14aca98968bc4bbad22 (diff) | |
Align F7 family
Diffstat (limited to 'embassy-stm32/src/flash/f7.rs')
| -rw-r--r-- | embassy-stm32/src/flash/f7.rs | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/embassy-stm32/src/flash/f7.rs b/embassy-stm32/src/flash/f7.rs index dd0d8439d..ee0513fa4 100644 --- a/embassy-stm32/src/flash/f7.rs +++ b/embassy-stm32/src/flash/f7.rs | |||
| @@ -1,10 +1,17 @@ | |||
| 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 | use core::sync::atomic::{fence, Ordering}; | 4 | use core::sync::atomic::{fence, Ordering}; |
| 4 | 5 | ||
| 6 | use embassy_hal_common::stm32::flash::f7::get_sector; | ||
| 7 | |||
| 8 | use super::FlashRegion; | ||
| 5 | use crate::flash::Error; | 9 | use crate::flash::Error; |
| 6 | use crate::pac; | 10 | use crate::pac; |
| 7 | 11 | ||
| 12 | pub(crate) const MAX_WRITE_SIZE: usize = super::MAINC::WRITE_SIZE; | ||
| 13 | pub(crate) const MAX_ERASE_SIZE: usize = super::MAINC::ERASE_SIZE; | ||
| 14 | |||
| 8 | pub(crate) unsafe fn lock() { | 15 | pub(crate) unsafe fn lock() { |
| 9 | pac::FLASH.cr().modify(|w| w.set_lock(true)); | 16 | pac::FLASH.cr().modify(|w| w.set_lock(true)); |
| 10 | } | 17 | } |
| @@ -14,7 +21,7 @@ pub(crate) unsafe fn unlock() { | |||
| 14 | pac::FLASH.keyr().write(|w| w.set_key(0xCDEF_89AB)); | 21 | pac::FLASH.keyr().write(|w| w.set_key(0xCDEF_89AB)); |
| 15 | } | 22 | } |
| 16 | 23 | ||
| 17 | pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error> { | 24 | pub(crate) unsafe fn blocking_write(first_address: u32, buf: &[u8]) -> Result<(), Error> { |
| 18 | pac::FLASH.cr().write(|w| { | 25 | pac::FLASH.cr().write(|w| { |
| 19 | w.set_pg(true); | 26 | w.set_pg(true); |
| 20 | w.set_psize(pac::flash::vals::Psize::PSIZE32); | 27 | w.set_psize(pac::flash::vals::Psize::PSIZE32); |
| @@ -22,11 +29,13 @@ pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error | |||
| 22 | 29 | ||
| 23 | let ret = { | 30 | let ret = { |
| 24 | let mut ret: Result<(), Error> = Ok(()); | 31 | let mut ret: Result<(), Error> = Ok(()); |
| 25 | let mut offset = offset; | 32 | let mut address = first_address; |
| 26 | for chunk in buf.chunks(super::WRITE_SIZE) { | 33 | for chunk in buf.chunks(MAX_WRITE_SIZE) { |
| 27 | for val in chunk.chunks(4) { | 34 | let vals = chunk.chunks_exact(size_of::<u32>()); |
| 28 | write_volatile(offset as *mut u32, u32::from_le_bytes(val[0..4].try_into().unwrap())); | 35 | assert!(vals.remainder().is_empty()); |
| 29 | offset += val.len() as u32; | 36 | for val in vals { |
| 37 | write_volatile(address as *mut u32, u32::from_le_bytes(val.try_into().unwrap())); | ||
| 38 | address += val.len() as u32; | ||
| 30 | 39 | ||
| 31 | // prevents parallelism errors | 40 | // prevents parallelism errors |
| 32 | fence(Ordering::SeqCst); | 41 | fence(Ordering::SeqCst); |
| @@ -45,20 +54,10 @@ pub(crate) unsafe fn blocking_write(offset: u32, buf: &[u8]) -> Result<(), Error | |||
| 45 | ret | 54 | ret |
| 46 | } | 55 | } |
| 47 | 56 | ||
| 48 | pub(crate) unsafe fn blocking_erase(from: u32, to: u32) -> Result<(), Error> { | 57 | pub(crate) unsafe fn blocking_erase(from_address: u32, to_address: u32) -> Result<(), Error> { |
| 49 | let start_sector = if from >= (super::FLASH_BASE + super::ERASE_SIZE / 2) as u32 { | 58 | let start_sector = get_sector(from_address); |
| 50 | 4 + (from - super::FLASH_BASE as u32) / super::ERASE_SIZE as u32 | 59 | let end_sector = get_sector(to_address); |
| 51 | } else { | 60 | for sector in start_sector.index..end_sector.index { |
| 52 | (from - super::FLASH_BASE as u32) / (super::ERASE_SIZE as u32 / 8) | ||
| 53 | }; | ||
| 54 | |||
| 55 | let end_sector = if to >= (super::FLASH_BASE + super::ERASE_SIZE / 2) as u32 { | ||
| 56 | 4 + (to - super::FLASH_BASE as u32) / super::ERASE_SIZE as u32 | ||
| 57 | } else { | ||
| 58 | (to - super::FLASH_BASE as u32) / (super::ERASE_SIZE as u32 / 8) | ||
| 59 | }; | ||
| 60 | |||
| 61 | for sector in start_sector..end_sector { | ||
| 62 | let ret = erase_sector(sector as u8); | 61 | let ret = erase_sector(sector as u8); |
| 63 | if ret.is_err() { | 62 | if ret.is_err() { |
| 64 | return ret; | 63 | return ret; |
