diff options
Diffstat (limited to 'embassy-stm32/src')
| -rw-r--r-- | embassy-stm32/src/flash/common.rs | 19 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/f0.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/f3.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/f4.rs | 84 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/f7.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/h7.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/l.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/mod.rs | 1 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/other.rs | 2 |
9 files changed, 92 insertions, 24 deletions
diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs index 6d7f55974..432c8a43b 100644 --- a/embassy-stm32/src/flash/common.rs +++ b/embassy-stm32/src/flash/common.rs | |||
| @@ -17,6 +17,7 @@ impl<'d> Flash<'d> { | |||
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | pub fn into_regions(self) -> FlashLayout<'d> { | 19 | pub fn into_regions(self) -> FlashLayout<'d> { |
| 20 | family::set_default_layout(); | ||
| 20 | FlashLayout::new(self.release()) | 21 | FlashLayout::new(self.release()) |
| 21 | } | 22 | } |
| 22 | 23 | ||
| @@ -25,11 +26,11 @@ impl<'d> Flash<'d> { | |||
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { | 28 | pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { |
| 28 | unsafe { blocking_write(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes) } | 29 | unsafe { blocking_write_chunked(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes) } |
| 29 | } | 30 | } |
| 30 | 31 | ||
| 31 | pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { | 32 | pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { |
| 32 | unsafe { blocking_erase(FLASH_BASE as u32, from, to) } | 33 | unsafe { blocking_erase_sectored(FLASH_BASE as u32, from, to) } |
| 33 | } | 34 | } |
| 34 | 35 | ||
| 35 | pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> { | 36 | pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> { |
| @@ -37,7 +38,7 @@ impl<'d> Flash<'d> { | |||
| 37 | } | 38 | } |
| 38 | } | 39 | } |
| 39 | 40 | ||
| 40 | fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { | 41 | pub(super) fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { |
| 41 | if offset + bytes.len() as u32 > size { | 42 | if offset + bytes.len() as u32 > size { |
| 42 | return Err(Error::Size); | 43 | return Err(Error::Size); |
| 43 | } | 44 | } |
| @@ -48,7 +49,7 @@ fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result< | |||
| 48 | Ok(()) | 49 | Ok(()) |
| 49 | } | 50 | } |
| 50 | 51 | ||
| 51 | unsafe fn blocking_write(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Result<(), Error> { | 52 | pub(super) unsafe fn blocking_write_chunked(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Result<(), Error> { |
| 52 | if offset + bytes.len() as u32 > size { | 53 | if offset + bytes.len() as u32 > size { |
| 53 | return Err(Error::Size); | 54 | return Err(Error::Size); |
| 54 | } | 55 | } |
| @@ -81,7 +82,7 @@ unsafe fn blocking_write(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Res | |||
| 81 | Ok(()) | 82 | Ok(()) |
| 82 | } | 83 | } |
| 83 | 84 | ||
| 84 | unsafe fn blocking_erase(base: u32, from: u32, to: u32) -> Result<(), Error> { | 85 | pub(super) unsafe fn blocking_erase_sectored(base: u32, from: u32, to: u32) -> Result<(), Error> { |
| 85 | let start_address = base + from; | 86 | let start_address = base + from; |
| 86 | let end_address = base + to; | 87 | let end_address = base + to; |
| 87 | let regions = family::get_flash_regions(); | 88 | let regions = family::get_flash_regions(); |
| @@ -154,11 +155,11 @@ impl FlashRegion { | |||
| 154 | } | 155 | } |
| 155 | 156 | ||
| 156 | pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { | 157 | pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { |
| 157 | unsafe { blocking_write(self.base, self.size, offset, bytes) } | 158 | unsafe { blocking_write_chunked(self.base, self.size, offset, bytes) } |
| 158 | } | 159 | } |
| 159 | 160 | ||
| 160 | pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { | 161 | pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { |
| 161 | unsafe { blocking_erase(self.base, from, to) } | 162 | unsafe { blocking_erase_sectored(self.base, from, to) } |
| 162 | } | 163 | } |
| 163 | } | 164 | } |
| 164 | 165 | ||
| @@ -199,11 +200,11 @@ foreach_flash_region! { | |||
| 199 | } | 200 | } |
| 200 | 201 | ||
| 201 | pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { | 202 | pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { |
| 202 | unsafe { blocking_write(self.0.base, self.0.size, offset, bytes) } | 203 | unsafe { blocking_write_chunked(self.0.base, self.0.size, offset, bytes) } |
| 203 | } | 204 | } |
| 204 | 205 | ||
| 205 | pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { | 206 | pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { |
| 206 | unsafe { blocking_erase(self.0.base, from, to) } | 207 | unsafe { blocking_erase_sectored(self.0.base, from, to) } |
| 207 | } | 208 | } |
| 208 | } | 209 | } |
| 209 | 210 | ||
diff --git a/embassy-stm32/src/flash/f0.rs b/embassy-stm32/src/flash/f0.rs index 033afecd5..c6441ef16 100644 --- a/embassy-stm32/src/flash/f0.rs +++ b/embassy-stm32/src/flash/f0.rs | |||
| @@ -7,6 +7,8 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | |||
| 7 | use crate::flash::Error; | 7 | use crate::flash::Error; |
| 8 | use crate::pac; | 8 | use crate::pac; |
| 9 | 9 | ||
| 10 | pub const fn set_default_layout() {} | ||
| 11 | |||
| 10 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { | 12 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { |
| 11 | &FLASH_REGIONS | 13 | &FLASH_REGIONS |
| 12 | } | 14 | } |
diff --git a/embassy-stm32/src/flash/f3.rs b/embassy-stm32/src/flash/f3.rs index 10a09c42c..4c8172203 100644 --- a/embassy-stm32/src/flash/f3.rs +++ b/embassy-stm32/src/flash/f3.rs | |||
| @@ -7,6 +7,8 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | |||
| 7 | use crate::flash::Error; | 7 | use crate::flash::Error; |
| 8 | use crate::pac; | 8 | use crate::pac; |
| 9 | 9 | ||
| 10 | pub const fn set_default_layout() {} | ||
| 11 | |||
| 10 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { | 12 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { |
| 11 | &FLASH_REGIONS | 13 | &FLASH_REGIONS |
| 12 | } | 14 | } |
diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs index 60ac62c17..7f1c5f671 100644 --- a/embassy-stm32/src/flash/f4.rs +++ b/embassy-stm32/src/flash/f4.rs | |||
| @@ -11,8 +11,11 @@ mod alt_regions { | |||
| 11 | use embassy_hal_common::PeripheralRef; | 11 | use embassy_hal_common::PeripheralRef; |
| 12 | use stm32_metapac::FLASH_SIZE; | 12 | use stm32_metapac::FLASH_SIZE; |
| 13 | 13 | ||
| 14 | use crate::_generated::flash_regions::{BANK1_REGION1, BANK1_REGION2, BANK1_REGION3}; | 14 | use crate::_generated::flash_regions::{OTPRegion, BANK1_REGION1, BANK1_REGION2, BANK1_REGION3, OTP_REGION}; |
| 15 | use crate::flash::{Bank1Region1, Bank1Region2, Flash, FlashBank, FlashRegion}; | 15 | use crate::flash::{ |
| 16 | blocking_erase_sectored, blocking_read, blocking_write_chunked, Bank1Region1, Bank1Region2, Error, Flash, | ||
| 17 | FlashBank, FlashRegion, | ||
| 18 | }; | ||
| 16 | use crate::peripherals::FLASH; | 19 | use crate::peripherals::FLASH; |
| 17 | 20 | ||
| 18 | pub const ALT_BANK1_REGION3: FlashRegion = FlashRegion { | 21 | pub const ALT_BANK1_REGION3: FlashRegion = FlashRegion { |
| @@ -45,20 +48,19 @@ mod alt_regions { | |||
| 45 | &ALT_BANK2_REGION3, | 48 | &ALT_BANK2_REGION3, |
| 46 | ]; | 49 | ]; |
| 47 | 50 | ||
| 48 | pub type AltBank1Region1<'d> = Bank1Region1<'d>; | ||
| 49 | pub type AltBank1Region2<'d> = Bank1Region2<'d>; | ||
| 50 | pub struct AltBank1Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); | 51 | pub struct AltBank1Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); |
| 51 | pub struct AltBank2Region1<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); | 52 | pub struct AltBank2Region1<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); |
| 52 | pub struct AltBank2Region2<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); | 53 | pub struct AltBank2Region2<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); |
| 53 | pub struct AltBank2Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); | 54 | pub struct AltBank2Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); |
| 54 | 55 | ||
| 55 | pub struct AltFlashLayout<'d> { | 56 | pub struct AltFlashLayout<'d> { |
| 56 | pub bank1_region1: AltBank1Region1<'d>, | 57 | pub bank1_region1: Bank1Region1<'d>, |
| 57 | pub bank1_region2: AltBank1Region2<'d>, | 58 | pub bank1_region2: Bank1Region2<'d>, |
| 58 | pub bank1_region3: AltBank1Region3<'d>, | 59 | pub bank1_region3: AltBank1Region3<'d>, |
| 59 | pub bank2_region1: AltBank2Region1<'d>, | 60 | pub bank2_region1: AltBank2Region1<'d>, |
| 60 | pub bank2_region2: AltBank2Region2<'d>, | 61 | pub bank2_region2: AltBank2Region2<'d>, |
| 61 | pub bank2_region3: AltBank2Region3<'d>, | 62 | pub bank2_region3: AltBank2Region3<'d>, |
| 63 | pub otp_region: OTPRegion<'d>, | ||
| 62 | } | 64 | } |
| 63 | 65 | ||
| 64 | impl<'d> Flash<'d> { | 66 | impl<'d> Flash<'d> { |
| @@ -66,7 +68,7 @@ mod alt_regions { | |||
| 66 | unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(true)) }; | 68 | unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(true)) }; |
| 67 | 69 | ||
| 68 | // SAFETY: We never expose the cloned peripheral references, and their instance is not public. | 70 | // SAFETY: We never expose the cloned peripheral references, and their instance is not public. |
| 69 | // Also, all flash region operations are protected with a cs. | 71 | // Also, all blocking flash region operations are protected with a cs. |
| 70 | let p = self.release(); | 72 | let p = self.release(); |
| 71 | AltFlashLayout { | 73 | AltFlashLayout { |
| 72 | bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }), | 74 | bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }), |
| @@ -75,22 +77,74 @@ mod alt_regions { | |||
| 75 | bank2_region1: AltBank2Region1(&ALT_BANK2_REGION1, unsafe { p.clone_unchecked() }), | 77 | bank2_region1: AltBank2Region1(&ALT_BANK2_REGION1, unsafe { p.clone_unchecked() }), |
| 76 | bank2_region2: AltBank2Region2(&ALT_BANK2_REGION2, unsafe { p.clone_unchecked() }), | 78 | bank2_region2: AltBank2Region2(&ALT_BANK2_REGION2, unsafe { p.clone_unchecked() }), |
| 77 | bank2_region3: AltBank2Region3(&ALT_BANK2_REGION3, unsafe { p.clone_unchecked() }), | 79 | bank2_region3: AltBank2Region3(&ALT_BANK2_REGION3, unsafe { p.clone_unchecked() }), |
| 80 | otp_region: OTPRegion(&OTP_REGION, unsafe { p.clone_unchecked() }), | ||
| 78 | } | 81 | } |
| 79 | } | 82 | } |
| 80 | } | 83 | } |
| 81 | 84 | ||
| 82 | impl Drop for AltFlashLayout<'_> { | 85 | macro_rules! foreach_altflash_region { |
| 83 | fn drop(&mut self) { | 86 | ($type_name:ident, $region:ident) => { |
| 84 | unsafe { | 87 | impl $type_name<'_> { |
| 85 | super::lock(); | 88 | pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { |
| 86 | crate::pac::FLASH.optcr().modify(|r| r.set_db1m(false)) | 89 | blocking_read(self.0.base, self.0.size, offset, bytes) |
| 87 | }; | 90 | } |
| 88 | } | 91 | |
| 92 | pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { | ||
| 93 | unsafe { blocking_write_chunked(self.0.base, self.0.size, offset, bytes) } | ||
| 94 | } | ||
| 95 | |||
| 96 | pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { | ||
| 97 | unsafe { blocking_erase_sectored(self.0.base, from, to) } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | impl embedded_storage::nor_flash::ErrorType for $type_name<'_> { | ||
| 102 | type Error = Error; | ||
| 103 | } | ||
| 104 | |||
| 105 | impl embedded_storage::nor_flash::ReadNorFlash for $type_name<'_> { | ||
| 106 | const READ_SIZE: usize = 1; | ||
| 107 | |||
| 108 | fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { | ||
| 109 | self.blocking_read(offset, bytes) | ||
| 110 | } | ||
| 111 | |||
| 112 | fn capacity(&self) -> usize { | ||
| 113 | self.0.size as usize | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | impl embedded_storage::nor_flash::NorFlash for $type_name<'_> { | ||
| 118 | const WRITE_SIZE: usize = $region.write_size as usize; | ||
| 119 | const ERASE_SIZE: usize = $region.erase_size as usize; | ||
| 120 | |||
| 121 | fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { | ||
| 122 | self.blocking_write(offset, bytes) | ||
| 123 | } | ||
| 124 | |||
| 125 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 126 | self.blocking_erase(from, to) | ||
| 127 | } | ||
| 128 | } | ||
| 129 | }; | ||
| 89 | } | 130 | } |
| 131 | |||
| 132 | foreach_altflash_region!(AltBank1Region3, ALT_BANK1_REGION3); | ||
| 133 | foreach_altflash_region!(AltBank2Region1, ALT_BANK2_REGION1); | ||
| 134 | foreach_altflash_region!(AltBank2Region2, ALT_BANK2_REGION2); | ||
| 135 | foreach_altflash_region!(AltBank2Region3, ALT_BANK2_REGION3); | ||
| 90 | } | 136 | } |
| 91 | 137 | ||
| 92 | #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] | 138 | #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] |
| 93 | pub use alt_regions::{AltFlashLayout, ALT_FLASH_REGIONS}; | 139 | pub use alt_regions::*; |
| 140 | |||
| 141 | #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] | ||
| 142 | pub fn set_default_layout() { | ||
| 143 | unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(false)) }; | ||
| 144 | } | ||
| 145 | |||
| 146 | #[cfg(not(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479)))] | ||
| 147 | pub const fn set_default_layout() {} | ||
| 94 | 148 | ||
| 95 | #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] | 149 | #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] |
| 96 | pub fn get_flash_regions() -> &'static [&'static FlashRegion] { | 150 | pub fn get_flash_regions() -> &'static [&'static FlashRegion] { |
diff --git a/embassy-stm32/src/flash/f7.rs b/embassy-stm32/src/flash/f7.rs index 6427d5a09..ac2834a84 100644 --- a/embassy-stm32/src/flash/f7.rs +++ b/embassy-stm32/src/flash/f7.rs | |||
| @@ -6,6 +6,8 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | |||
| 6 | use crate::flash::Error; | 6 | use crate::flash::Error; |
| 7 | use crate::pac; | 7 | use crate::pac; |
| 8 | 8 | ||
| 9 | pub const fn set_default_layout() {} | ||
| 10 | |||
| 9 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { | 11 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { |
| 10 | &FLASH_REGIONS | 12 | &FLASH_REGIONS |
| 11 | } | 13 | } |
diff --git a/embassy-stm32/src/flash/h7.rs b/embassy-stm32/src/flash/h7.rs index 4f38d50c0..1b1631068 100644 --- a/embassy-stm32/src/flash/h7.rs +++ b/embassy-stm32/src/flash/h7.rs | |||
| @@ -7,6 +7,8 @@ use super::{FlashRegion, FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE}; | |||
| 7 | use crate::flash::Error; | 7 | use crate::flash::Error; |
| 8 | use crate::pac; | 8 | use crate::pac; |
| 9 | 9 | ||
| 10 | pub const fn set_default_layout() {} | ||
| 11 | |||
| 10 | const fn is_dual_bank() -> bool { | 12 | const fn is_dual_bank() -> bool { |
| 11 | FLASH_REGIONS.len() == 2 | 13 | FLASH_REGIONS.len() == 2 |
| 12 | } | 14 | } |
diff --git a/embassy-stm32/src/flash/l.rs b/embassy-stm32/src/flash/l.rs index 7d9cc6ea3..c94f61900 100644 --- a/embassy-stm32/src/flash/l.rs +++ b/embassy-stm32/src/flash/l.rs | |||
| @@ -6,6 +6,8 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | |||
| 6 | use crate::flash::Error; | 6 | use crate::flash::Error; |
| 7 | use crate::pac; | 7 | use crate::pac; |
| 8 | 8 | ||
| 9 | pub const fn set_default_layout() {} | ||
| 10 | |||
| 9 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { | 11 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { |
| 10 | &FLASH_REGIONS | 12 | &FLASH_REGIONS |
| 11 | } | 13 | } |
diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs index f6efa7753..b93270ae1 100644 --- a/embassy-stm32/src/flash/mod.rs +++ b/embassy-stm32/src/flash/mod.rs | |||
| @@ -19,6 +19,7 @@ pub struct FlashRegion { | |||
| 19 | pub erase_size: u32, | 19 | pub erase_size: u32, |
| 20 | pub write_size: u32, | 20 | pub write_size: u32, |
| 21 | pub erase_value: u8, | 21 | pub erase_value: u8, |
| 22 | pub(crate) _ensure_internal: (), | ||
| 22 | } | 23 | } |
| 23 | 24 | ||
| 24 | #[derive(Debug, PartialEq)] | 25 | #[derive(Debug, PartialEq)] |
diff --git a/embassy-stm32/src/flash/other.rs b/embassy-stm32/src/flash/other.rs index c151cb828..556034654 100644 --- a/embassy-stm32/src/flash/other.rs +++ b/embassy-stm32/src/flash/other.rs | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | 2 | ||
| 3 | use super::{Error, FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; | 3 | use super::{Error, FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; |
| 4 | 4 | ||
| 5 | pub const fn set_default_layout() {} | ||
| 6 | |||
| 5 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { | 7 | pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { |
| 6 | &FLASH_REGIONS | 8 | &FLASH_REGIONS |
| 7 | } | 9 | } |
