992: (embassy-stm32): remove flash lock/unlock public API from stm32 flash r=lulf a=MathiasKoch

Instead, perform the unlocking and locking automatically on erase and write operations.

This makes the `embedded-storage` abstraction actually useable in libraries, while still keeping the flash peripheral locked the majority of the time.

Co-authored-by: Mathias <mk@blackbird.online>
This commit is contained in:
bors[bot] 2022-09-30 11:32:27 +00:00 committed by GitHub
commit aabc02506b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 28 additions and 29 deletions

View File

@ -23,17 +23,6 @@ impl<'d> Flash<'d> {
Self { _inner: p } Self { _inner: p }
} }
pub fn unlock(p: impl Peripheral<P = FLASH> + 'd) -> Self {
let flash = Self::new(p);
unsafe { family::unlock() };
flash
}
pub fn lock(&mut self) {
unsafe { family::lock() };
}
pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> {
let offset = FLASH_BASE as u32 + offset; let offset = FLASH_BASE as u32 + offset;
if offset as usize >= FLASH_END || offset as usize + bytes.len() > FLASH_END { if offset as usize >= FLASH_END || offset as usize + bytes.len() > FLASH_END {
@ -57,7 +46,12 @@ impl<'d> Flash<'d> {
self.clear_all_err(); self.clear_all_err();
unsafe { family::blocking_write(offset, buf) } unsafe {
family::unlock();
let res = family::blocking_write(offset, buf);
family::lock();
res
}
} }
pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> {
@ -72,7 +66,12 @@ impl<'d> Flash<'d> {
self.clear_all_err(); self.clear_all_err();
unsafe { family::blocking_erase(from, to) } unsafe {
family::unlock();
let res = family::blocking_erase(from, to);
family::lock();
res
}
} }
fn clear_all_err(&mut self) { fn clear_all_err(&mut self) {
@ -82,7 +81,7 @@ impl<'d> Flash<'d> {
impl Drop for Flash<'_> { impl Drop for Flash<'_> {
fn drop(&mut self) { fn drop(&mut self) {
self.lock(); unsafe { family::lock() };
} }
} }

View File

@ -17,7 +17,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::unlock(p.FLASH); let flash = Flash::new(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PC13, Pull::Up); let button = Input::new(p.PC13, Pull::Up);

View File

@ -16,7 +16,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let mut flash = Flash::unlock(p.FLASH); let mut flash = Flash::new(p.FLASH);
let button = Input::new(p.PC13, Pull::Down); let button = Input::new(p.PC13, Pull::Down);
let mut button = ExtiInput::new(button, p.EXTI13); let mut button = ExtiInput::new(button, p.EXTI13);

View File

@ -16,7 +16,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let mut flash = Flash::unlock(p.FLASH); let mut flash = Flash::new(p.FLASH);
let button = Input::new(p.PC13, Pull::Down); let button = Input::new(p.PC13, Pull::Down);
let mut button = ExtiInput::new(button, p.EXTI13); let mut button = ExtiInput::new(button, p.EXTI13);

View File

@ -18,7 +18,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::unlock(p.FLASH); let flash = Flash::new(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PB2, Pull::Up); let button = Input::new(p.PB2, Pull::Up);

View File

@ -18,7 +18,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::unlock(p.FLASH); let flash = Flash::new(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PB2, Pull::Up); let button = Input::new(p.PB2, Pull::Up);

View File

@ -17,7 +17,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::unlock(p.FLASH); let flash = Flash::new(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PC13, Pull::Up); let button = Input::new(p.PC13, Pull::Up);

View File

@ -17,7 +17,7 @@ static APP_B: &[u8] = include_bytes!("../../b.bin");
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let flash = Flash::unlock(p.FLASH); let flash = Flash::new(p.FLASH);
let mut flash = BlockingAsync::new(flash); let mut flash = BlockingAsync::new(flash);
let button = Input::new(p.PA0, Pull::Up); let button = Input::new(p.PA0, Pull::Up);

View File

@ -20,7 +20,7 @@ fn main() -> ! {
*/ */
let mut bl: BootLoader<ERASE_SIZE, WRITE_SIZE> = BootLoader::default(); let mut bl: BootLoader<ERASE_SIZE, WRITE_SIZE> = BootLoader::default();
let flash = Flash::unlock(p.FLASH); let flash = Flash::new(p.FLASH);
let mut flash = BootFlash::<_, ERASE_SIZE, ERASE_VALUE>::new(flash); let mut flash = BootFlash::<_, ERASE_SIZE, ERASE_VALUE>::new(flash);
let start = bl.prepare(&mut SingleFlashConfig::new(&mut flash)); let start = bl.prepare(&mut SingleFlashConfig::new(&mut flash));
core::mem::drop(flash); core::mem::drop(flash);

View File

@ -15,7 +15,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x26000; const ADDR: u32 = 0x26000;
let mut f = Flash::unlock(p.FLASH); let mut f = Flash::new(p.FLASH);
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];

View File

@ -13,7 +13,7 @@ async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
info!("Hello Flash!"); info!("Hello Flash!");
let mut f = Flash::unlock(p.FLASH); let mut f = Flash::new(p.FLASH);
// Sector 5 // Sector 5
test_flash(&mut f, 128 * 1024, 128 * 1024); test_flash(&mut f, 128 * 1024, 128 * 1024);

View File

@ -19,7 +19,7 @@ async fn main(_spawner: Spawner) {
// wait a bit before accessing the flash // wait a bit before accessing the flash
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
let mut f = Flash::unlock(p.FLASH); let mut f = Flash::new(p.FLASH);
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 32]; let mut buf = [0u8; 32];

View File

@ -19,7 +19,7 @@ async fn main(_spawner: Spawner) {
// wait a bit before accessing the flash // wait a bit before accessing the flash
Timer::after(Duration::from_millis(300)).await; Timer::after(Duration::from_millis(300)).await;
let mut f = Flash::unlock(p.FLASH); let mut f = Flash::new(p.FLASH);
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 32]; let mut buf = [0u8; 32];

View File

@ -15,7 +15,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x26000; const ADDR: u32 = 0x26000;
let mut f = Flash::unlock(p.FLASH); let mut f = Flash::new(p.FLASH);
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];

View File

@ -15,7 +15,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x26000; const ADDR: u32 = 0x26000;
let mut f = Flash::unlock(p.FLASH); let mut f = Flash::new(p.FLASH);
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];

View File

@ -15,7 +15,7 @@ async fn main(_spawner: Spawner) {
const ADDR: u32 = 0x36000; const ADDR: u32 = 0x36000;
let mut f = Flash::unlock(p.FLASH); let mut f = Flash::new(p.FLASH);
info!("Reading..."); info!("Reading...");
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];