From 91d8afd371c20d21765713a45625f62ce25d97b6 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Thu, 30 Mar 2023 05:27:57 +0200 Subject: [PATCH] Add AltFlashLayout for supported F4 chips --- embassy-stm32/build.rs | 16 +++++-- embassy-stm32/src/flash/common.rs | 4 +- embassy-stm32/src/flash/f4.rs | 75 +++++++++++++++++++++++++++++++ embassy-stm32/src/flash/mod.rs | 58 +++++++++++++----------- 4 files changed, 120 insertions(+), 33 deletions(-) diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs index ec9b6dc0..6b203a3f 100644 --- a/embassy-stm32/build.rs +++ b/embassy-stm32/build.rs @@ -910,10 +910,18 @@ macro_rules! {} {{ .unwrap(); } -fn get_flash_region_type_name(name: &str) -> String { - name.replace("BANK_", "Bank").replace("REGION_", "Region") +fn get_flash_region_name(name: &str) -> String { + let name = name.replace("BANK_", "BANK").replace("REGION_", "REGION"); + if name.contains("REGION") { + name + } else { + name + "_REGION" + } } -fn get_flash_region_name(name: &str) -> String { - name.replace("BANK_", "BANK").replace("REGION_", "REGION") +fn get_flash_region_type_name(name: &str) -> String { + get_flash_region_name(name) + .replace("BANK", "Bank") + .replace("REGION", "Region") + .replace("_", "") } diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs index b190a5a0..c239d967 100644 --- a/embassy-stm32/src/flash/common.rs +++ b/embassy-stm32/src/flash/common.rs @@ -3,9 +3,7 @@ use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::mutex::{Mutex, MutexGuard}; -use super::{family, Error, FlashRegion}; -pub use crate::_generated::flash_regions::*; -pub use crate::pac::{FLASH_BASE, FLASH_SIZE, WRITE_SIZE}; +use super::{family, Error, FlashLayout, FlashRegion, FLASH_BASE, FLASH_SIZE, WRITE_SIZE}; use crate::Peripheral; pub struct Flash<'d> { diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs index cb420c69..257aae2d 100644 --- a/embassy-stm32/src/flash/f4.rs +++ b/embassy-stm32/src/flash/f4.rs @@ -11,7 +11,82 @@ const MEDIUM_SECTOR_SIZE: u32 = 64 * 1024; const LARGE_SECTOR_SIZE: u32 = 128 * 1024; const SECOND_BANK_SECTOR_OFFSET: u8 = 12; +#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] +mod alt_regions { + use embassy_hal_common::PeripheralRef; + use stm32_metapac::FLASH_SIZE; + + use crate::_generated::flash_regions::{BANK1_REGION1, BANK1_REGION2, BANK1_REGION3}; + use crate::flash::{Bank1Region1, Bank1Region2, Flash, FlashRegion}; + use crate::peripherals::FLASH; + + pub const ALT_BANK1_REGION3: FlashRegion = FlashRegion { + size: 3 * BANK1_REGION3.erase_size, + ..BANK1_REGION3 + }; + pub const ALT_BANK2_REGION1: FlashRegion = FlashRegion { + base: BANK1_REGION1.base + FLASH_SIZE as u32 / 2, + ..BANK1_REGION1 + }; + pub const ALT_BANK2_REGION2: FlashRegion = FlashRegion { + base: BANK1_REGION2.base + FLASH_SIZE as u32 / 2, + ..BANK1_REGION2 + }; + pub const ALT_BANK2_REGION3: FlashRegion = FlashRegion { + base: BANK1_REGION3.base + FLASH_SIZE as u32 / 2, + size: 3 * BANK1_REGION3.erase_size, + ..BANK1_REGION3 + }; + + pub type AltBank1Region1 = Bank1Region1; + pub type AltBank1Region2 = Bank1Region2; + pub struct AltBank1Region3(&'static FlashRegion); + pub struct AltBank2Region1(&'static FlashRegion); + pub struct AltBank2Region2(&'static FlashRegion); + pub struct AltBank2Region3(&'static FlashRegion); + + pub struct AltFlashLayout<'d> { + _inner: PeripheralRef<'d, FLASH>, + pub bank1_region1: AltBank1Region1, + pub bank1_region2: AltBank1Region2, + pub bank1_region3: AltBank1Region3, + pub bank2_region1: AltBank2Region1, + pub bank2_region2: AltBank2Region2, + pub bank2_region3: AltBank2Region3, + } + + impl<'d> Flash<'d> { + pub fn into_alt_regions(self) -> AltFlashLayout<'d> { + unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(true)) }; + AltFlashLayout { + _inner: self.release(), + bank1_region1: Bank1Region1(&BANK1_REGION1), + bank1_region2: Bank1Region2(&BANK1_REGION2), + bank1_region3: AltBank1Region3(&ALT_BANK1_REGION3), + bank2_region1: AltBank2Region1(&ALT_BANK2_REGION1), + bank2_region2: AltBank2Region2(&ALT_BANK2_REGION2), + bank2_region3: AltBank2Region3(&ALT_BANK2_REGION3), + } + } + } + + impl Drop for AltFlashLayout<'_> { + fn drop(&mut self) { + unsafe { + super::lock(); + crate::pac::FLASH.optcr().modify(|r| r.set_db1m(false)) + }; + } + } +} + +#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] +pub use alt_regions::AltFlashLayout; + fn is_dual_bank() -> bool { + // let asd: super::Bank1Region1; + // let sad = &super::BANK_1_REGION_1; + match FLASH_SIZE / 1024 { // 1 MB devices depend on configuration 1024 => { diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs index 1e7d4c65..7dc71471 100644 --- a/embassy-stm32/src/flash/mod.rs +++ b/embassy-stm32/src/flash/mod.rs @@ -1,5 +1,35 @@ use embedded_storage::nor_flash::{NorFlashError, NorFlashErrorKind}; +#[cfg(flash)] +mod common; + +#[cfg(flash)] +pub use common::*; + +pub use crate::_generated::flash_regions::*; +pub use crate::pac::{FLASH_BASE, FLASH_SIZE, WRITE_SIZE}; + +pub struct FlashRegion { + pub base: u32, + pub size: u32, + pub erase_size: u32, + pub write_size: u32, + pub erase_value: u8, +} + +#[derive(Debug, PartialEq)] +pub struct FlashSector { + pub index: u8, + pub start: u32, + pub size: u32, +} + +impl Drop for FlashLayout<'_> { + fn drop(&mut self) { + unsafe { family::lock() }; + } +} + #[cfg_attr(any(flash_l0, flash_l1, flash_l4, flash_wl, flash_wb), path = "l.rs")] #[cfg_attr(flash_f3, path = "f3.rs")] #[cfg_attr(flash_f4, path = "f4.rs")] @@ -39,32 +69,8 @@ mod family { } } -#[cfg(flash)] -mod common; - -#[cfg(flash)] -pub use common::*; - -pub struct FlashRegion { - pub base: u32, - pub size: u32, - pub erase_size: u32, - pub write_size: u32, - pub erase_value: u8, -} - -#[derive(Debug, PartialEq)] -pub struct FlashSector { - pub index: u8, - pub start: u32, - pub size: u32, -} - -impl Drop for FlashLayout<'_> { - fn drop(&mut self) { - unsafe { family::lock() }; - } -} +#[allow(unused_imports)] +pub use family::*; #[derive(Debug, Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))]