Add missing implementations for f4 alternate regions

This commit is contained in:
Rasmus Melchior Jacobsen 2023-05-23 22:51:26 +02:00
parent faf506b300
commit 14e3e72b0f
2 changed files with 70 additions and 17 deletions

View File

@ -26,11 +26,11 @@ impl<'d> Flash<'d> {
} }
pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
unsafe { blocking_write(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes) } unsafe { blocking_write_chunked(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes) }
} }
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
unsafe { blocking_erase(FLASH_BASE as u32, from, to) } unsafe { blocking_erase_sectored(FLASH_BASE as u32, from, to) }
} }
pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> { pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> {
@ -38,7 +38,7 @@ impl<'d> Flash<'d> {
} }
} }
fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { pub(super) fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
if offset + bytes.len() as u32 > size { if offset + bytes.len() as u32 > size {
return Err(Error::Size); return Err(Error::Size);
} }
@ -49,7 +49,7 @@ fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<
Ok(()) Ok(())
} }
unsafe fn blocking_write(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Result<(), Error> { pub(super) unsafe fn blocking_write_chunked(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Result<(), Error> {
if offset + bytes.len() as u32 > size { if offset + bytes.len() as u32 > size {
return Err(Error::Size); return Err(Error::Size);
} }
@ -82,7 +82,7 @@ unsafe fn blocking_write(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Res
Ok(()) Ok(())
} }
unsafe fn blocking_erase(base: u32, from: u32, to: u32) -> Result<(), Error> { pub(super) unsafe fn blocking_erase_sectored(base: u32, from: u32, to: u32) -> Result<(), Error> {
let start_address = base + from; let start_address = base + from;
let end_address = base + to; let end_address = base + to;
let regions = family::get_flash_regions(); let regions = family::get_flash_regions();
@ -155,11 +155,11 @@ impl FlashRegion {
} }
pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
unsafe { blocking_write(self.base, self.size, offset, bytes) } unsafe { blocking_write_chunked(self.base, self.size, offset, bytes) }
} }
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
unsafe { blocking_erase(self.base, from, to) } unsafe { blocking_erase_sectored(self.base, from, to) }
} }
} }
@ -200,11 +200,11 @@ foreach_flash_region! {
} }
pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
unsafe { blocking_write(self.0.base, self.0.size, offset, bytes) } unsafe { blocking_write_chunked(self.0.base, self.0.size, offset, bytes) }
} }
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
unsafe { blocking_erase(self.0.base, from, to) } unsafe { blocking_erase_sectored(self.0.base, from, to) }
} }
} }

View File

@ -11,8 +11,11 @@ mod alt_regions {
use embassy_hal_common::PeripheralRef; use embassy_hal_common::PeripheralRef;
use stm32_metapac::FLASH_SIZE; use stm32_metapac::FLASH_SIZE;
use crate::_generated::flash_regions::{BANK1_REGION1, BANK1_REGION2, BANK1_REGION3}; use crate::_generated::flash_regions::{OTPRegion, BANK1_REGION1, BANK1_REGION2, BANK1_REGION3, OTP_REGION};
use crate::flash::{Bank1Region1, Bank1Region2, Flash, FlashBank, FlashRegion}; use crate::flash::{
blocking_erase_sectored, blocking_read, blocking_write_chunked, Bank1Region1, Bank1Region2, Error, Flash,
FlashBank, FlashRegion,
};
use crate::peripherals::FLASH; use crate::peripherals::FLASH;
pub const ALT_BANK1_REGION3: FlashRegion = FlashRegion { pub const ALT_BANK1_REGION3: FlashRegion = FlashRegion {
@ -45,20 +48,19 @@ mod alt_regions {
&ALT_BANK2_REGION3, &ALT_BANK2_REGION3,
]; ];
pub type AltBank1Region1<'d> = Bank1Region1<'d>;
pub type AltBank1Region2<'d> = Bank1Region2<'d>;
pub struct AltBank1Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); pub struct AltBank1Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>);
pub struct AltBank2Region1<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); pub struct AltBank2Region1<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>);
pub struct AltBank2Region2<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); pub struct AltBank2Region2<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>);
pub struct AltBank2Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); pub struct AltBank2Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>);
pub struct AltFlashLayout<'d> { pub struct AltFlashLayout<'d> {
pub bank1_region1: AltBank1Region1<'d>, pub bank1_region1: Bank1Region1<'d>,
pub bank1_region2: AltBank1Region2<'d>, pub bank1_region2: Bank1Region2<'d>,
pub bank1_region3: AltBank1Region3<'d>, pub bank1_region3: AltBank1Region3<'d>,
pub bank2_region1: AltBank2Region1<'d>, pub bank2_region1: AltBank2Region1<'d>,
pub bank2_region2: AltBank2Region2<'d>, pub bank2_region2: AltBank2Region2<'d>,
pub bank2_region3: AltBank2Region3<'d>, pub bank2_region3: AltBank2Region3<'d>,
pub otp_region: OTPRegion<'d>,
} }
impl<'d> Flash<'d> { impl<'d> Flash<'d> {
@ -66,7 +68,7 @@ mod alt_regions {
unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(true)) }; unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(true)) };
// SAFETY: We never expose the cloned peripheral references, and their instance is not public. // SAFETY: We never expose the cloned peripheral references, and their instance is not public.
// Also, all flash region operations are protected with a cs. // Also, all blocking flash region operations are protected with a cs.
let p = self.release(); let p = self.release();
AltFlashLayout { AltFlashLayout {
bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }), bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }),
@ -75,15 +77,66 @@ mod alt_regions {
bank2_region1: AltBank2Region1(&ALT_BANK2_REGION1, unsafe { p.clone_unchecked() }), bank2_region1: AltBank2Region1(&ALT_BANK2_REGION1, unsafe { p.clone_unchecked() }),
bank2_region2: AltBank2Region2(&ALT_BANK2_REGION2, unsafe { p.clone_unchecked() }), bank2_region2: AltBank2Region2(&ALT_BANK2_REGION2, unsafe { p.clone_unchecked() }),
bank2_region3: AltBank2Region3(&ALT_BANK2_REGION3, unsafe { p.clone_unchecked() }), bank2_region3: AltBank2Region3(&ALT_BANK2_REGION3, unsafe { p.clone_unchecked() }),
otp_region: OTPRegion(&OTP_REGION, unsafe { p.clone_unchecked() }),
} }
} }
} }
macro_rules! foreach_altflash_region {
($type_name:ident, $region:ident) => {
impl $type_name<'_> {
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
blocking_read(self.0.base, self.0.size, offset, bytes)
} }
pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> {
unsafe { blocking_write_chunked(self.0.base, self.0.size, offset, bytes) }
}
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
unsafe { blocking_erase_sectored(self.0.base, from, to) }
}
}
impl embedded_storage::nor_flash::ErrorType for $type_name<'_> {
type Error = Error;
}
impl embedded_storage::nor_flash::ReadNorFlash for $type_name<'_> {
const READ_SIZE: usize = 1;
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
self.blocking_read(offset, bytes)
}
fn capacity(&self) -> usize {
self.0.size as usize
}
}
impl embedded_storage::nor_flash::NorFlash for $type_name<'_> {
const WRITE_SIZE: usize = $region.write_size as usize;
const ERASE_SIZE: usize = $region.erase_size as usize;
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
self.blocking_write(offset, bytes)
}
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
self.blocking_erase(from, to)
}
}
};
}
foreach_altflash_region!(AltBank1Region3, ALT_BANK1_REGION3);
foreach_altflash_region!(AltBank2Region1, ALT_BANK2_REGION1);
foreach_altflash_region!(AltBank2Region2, ALT_BANK2_REGION2);
foreach_altflash_region!(AltBank2Region3, ALT_BANK2_REGION3);
} }
#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))]
pub use alt_regions::{AltFlashLayout, ALT_FLASH_REGIONS}; pub use alt_regions::*;
#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))]
pub fn set_default_layout() { pub fn set_default_layout() {