diff options
| author | Rasmus Melchior Jacobsen <[email protected]> | 2023-03-30 04:24:41 +0200 |
|---|---|---|
| committer | Rasmus Melchior Jacobsen <[email protected]> | 2023-03-30 04:24:41 +0200 |
| commit | def576ac4688fb2113aacca46d5bfb4001c8dc1a (patch) | |
| tree | 0476c852a8900c9bfd0cb998b397627f7f62b857 /embassy-stm32/src/flash/common.rs | |
| parent | ef1890e9110c8ef3553e6a2d0979dfb52520b025 (diff) | |
Remove FlashRegion trait and rename Settings to FlashRegion
Diffstat (limited to 'embassy-stm32/src/flash/common.rs')
| -rw-r--r-- | embassy-stm32/src/flash/common.rs | 98 |
1 files changed, 70 insertions, 28 deletions
diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs index f92236bb0..b190a5a07 100644 --- a/embassy-stm32/src/flash/common.rs +++ b/embassy-stm32/src/flash/common.rs | |||
| @@ -2,7 +2,6 @@ use embassy_hal_common::drop::OnDrop; | |||
| 2 | use embassy_hal_common::{into_ref, PeripheralRef}; | 2 | use embassy_hal_common::{into_ref, PeripheralRef}; |
| 3 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | 3 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; |
| 4 | use embassy_sync::mutex::{Mutex, MutexGuard}; | 4 | use embassy_sync::mutex::{Mutex, MutexGuard}; |
| 5 | use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash}; | ||
| 6 | 5 | ||
| 7 | use super::{family, Error, FlashRegion}; | 6 | use super::{family, Error, FlashRegion}; |
| 8 | pub use crate::_generated::flash_regions::*; | 7 | pub use crate::_generated::flash_regions::*; |
| @@ -19,10 +18,8 @@ impl<'d> Flash<'d> { | |||
| 19 | Self { inner: p } | 18 | Self { inner: p } |
| 20 | } | 19 | } |
| 21 | 20 | ||
| 22 | pub fn into_regions(self) -> FlashRegions<'d> { | 21 | pub fn into_regions(self) -> FlashLayout<'d> { |
| 23 | let mut flash = self; | 22 | FlashLayout::new(self.release()) |
| 24 | let p = unsafe { flash.inner.clone_unchecked() }; | ||
| 25 | FlashRegions::new(p) | ||
| 26 | } | 23 | } |
| 27 | 24 | ||
| 28 | pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { | 25 | pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { |
| @@ -114,6 +111,11 @@ impl<'d> Flash<'d> { | |||
| 114 | } | 111 | } |
| 115 | Ok(()) | 112 | Ok(()) |
| 116 | } | 113 | } |
| 114 | |||
| 115 | pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> { | ||
| 116 | let mut flash = self; | ||
| 117 | unsafe { flash.inner.clone_unchecked() } | ||
| 118 | } | ||
| 117 | } | 119 | } |
| 118 | 120 | ||
| 119 | impl Drop for Flash<'_> { | 121 | impl Drop for Flash<'_> { |
| @@ -132,45 +134,85 @@ fn take_lock_spin() -> MutexGuard<'static, CriticalSectionRawMutex, ()> { | |||
| 132 | } | 134 | } |
| 133 | } | 135 | } |
| 134 | 136 | ||
| 137 | impl FlashRegion { | ||
| 138 | pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { | ||
| 139 | unsafe { self.blocking_read_inner(offset, bytes) } | ||
| 140 | } | ||
| 141 | |||
| 142 | pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { | ||
| 143 | unsafe { self.blocking_write_inner(offset, bytes) } | ||
| 144 | } | ||
| 145 | |||
| 146 | pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { | ||
| 147 | unsafe { self.blocking_erase_inner(from, to) } | ||
| 148 | } | ||
| 149 | |||
| 150 | unsafe fn blocking_read_inner(&self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { | ||
| 151 | Flash::blocking_read_inner(self.base + offset, bytes) | ||
| 152 | } | ||
| 153 | |||
| 154 | unsafe fn blocking_write_inner(&self, offset: u32, bytes: &[u8]) -> Result<(), Error> { | ||
| 155 | let start_address = self.base + offset; | ||
| 156 | |||
| 157 | // Protect agains simultaneous write/erase to multiple regions. | ||
| 158 | let _guard = take_lock_spin(); | ||
| 159 | |||
| 160 | Flash::blocking_write_inner(start_address, bytes) | ||
| 161 | } | ||
| 162 | |||
| 163 | unsafe fn blocking_erase_inner(&self, from: u32, to: u32) -> Result<(), Error> { | ||
| 164 | let start_address = self.base + from; | ||
| 165 | let end_address = self.base + to; | ||
| 166 | |||
| 167 | // Protect agains simultaneous write/erase to multiple regions. | ||
| 168 | let _guard = take_lock_spin(); | ||
| 169 | |||
| 170 | Flash::blocking_erase_inner(start_address, end_address) | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 135 | foreach_flash_region! { | 174 | foreach_flash_region! { |
| 136 | ($name:ident) => { | 175 | ($type_name:ident, $write_size:ident, $erase_size:ident) => { |
| 137 | impl ErrorType for crate::_generated::flash_regions::$name { | 176 | impl crate::_generated::flash_regions::$type_name { |
| 177 | pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { | ||
| 178 | unsafe { self.0.blocking_read_inner(offset, bytes) } | ||
| 179 | } | ||
| 180 | |||
| 181 | pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { | ||
| 182 | unsafe { self.0.blocking_write_inner(offset, bytes) } | ||
| 183 | } | ||
| 184 | |||
| 185 | pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { | ||
| 186 | unsafe { self.0.blocking_erase_inner(from, to) } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | impl ErrorType for crate::_generated::flash_regions::$type_name { | ||
| 138 | type Error = Error; | 191 | type Error = Error; |
| 139 | } | 192 | } |
| 140 | 193 | ||
| 141 | impl ReadNorFlash for crate::_generated::flash_regions::$name { | 194 | impl ReadNorFlash for crate::_generated::flash_regions::$type_name { |
| 142 | const READ_SIZE: usize = 1; | 195 | const READ_SIZE: usize = 1; |
| 143 | 196 | ||
| 144 | fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { | 197 | fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { |
| 145 | Flash::blocking_read_inner(Self::SETTINGS.base as u32 + offset, bytes) | 198 | unsafe { self.0.blocking_read_inner(offset, bytes) } |
| 146 | } | 199 | } |
| 147 | 200 | ||
| 148 | fn capacity(&self) -> usize { | 201 | fn capacity(&self) -> usize { |
| 149 | <crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.size | 202 | self.0.size as usize |
| 150 | } | 203 | } |
| 151 | } | 204 | } |
| 152 | 205 | ||
| 153 | impl NorFlash for crate::_generated::flash_regions::$name { | 206 | impl NorFlash for crate::_generated::flash_regions::$type_name { |
| 154 | const WRITE_SIZE: usize = <crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.write_size; | 207 | const WRITE_SIZE: usize = $write_size; |
| 155 | const ERASE_SIZE: usize = <crate::_generated::flash_regions::$name as FlashRegion>::SETTINGS.erase_size; | 208 | const ERASE_SIZE: usize = $erase_size; |
| 156 | |||
| 157 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { | ||
| 158 | let start_address = Self::SETTINGS.base as u32 + from; | ||
| 159 | let end_address = Self::SETTINGS.base as u32 + to; | ||
| 160 | |||
| 161 | // Protect agains simultaneous write/erase to multiple regions. | ||
| 162 | let _guard = take_lock_spin(); | ||
| 163 | |||
| 164 | unsafe { Flash::blocking_erase_inner(start_address, end_address) } | ||
| 165 | } | ||
| 166 | 209 | ||
| 167 | fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { | 210 | fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { |
| 168 | let start_address = Self::SETTINGS.base as u32 + offset; | 211 | unsafe { self.0.blocking_write_inner(offset, bytes) } |
| 169 | 212 | } | |
| 170 | // Protect agains simultaneous write/erase to multiple regions. | ||
| 171 | let _guard = take_lock_spin(); | ||
| 172 | 213 | ||
| 173 | unsafe { Flash::blocking_write_inner(start_address, bytes) } | 214 | fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { |
| 215 | unsafe { self.0.blocking_erase_inner(from, to) } | ||
| 174 | } | 216 | } |
| 175 | } | 217 | } |
| 176 | }; | 218 | }; |
