Skip to content

Commit a86a100

Browse files
Merge #1377
1377: (embassy-stm32): implement embedded-storage traits for full flash struct r=MathiasKoch a=MathiasKoch Co-authored-by: Mathias <[email protected]>
2 parents 46227be + bba8b0d commit a86a100

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

embassy-stm32/build.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ fn main() {
255255
];
256256
});
257257

258+
let max_erase_size = flash_memory_regions
259+
.iter()
260+
.map(|region| region.settings.as_ref().unwrap().erase_size)
261+
.max()
262+
.unwrap();
263+
264+
g.extend(quote! { pub const MAX_ERASE_SIZE: usize = #max_erase_size as usize; });
265+
258266
g.extend(quote! { pub mod flash_regions { #flash_regions } });
259267

260268
// ========

embassy-stm32/src/flash/common.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use atomic_polyfill::{fence, Ordering};
22
use embassy_hal_common::drop::OnDrop;
33
use embassy_hal_common::{into_ref, PeripheralRef};
44

5-
use super::{family, Error, FlashLayout, FlashRegion, FlashSector, FLASH_BASE, FLASH_SIZE, WRITE_SIZE};
5+
use super::{family, Error, FlashLayout, FlashRegion, FlashSector, FLASH_BASE, FLASH_SIZE, MAX_ERASE_SIZE, WRITE_SIZE};
66
use crate::flash::FlashBank;
77
use crate::Peripheral;
88

@@ -162,6 +162,35 @@ impl FlashRegion {
162162
}
163163
}
164164

165+
impl embedded_storage::nor_flash::ErrorType for Flash<'_> {
166+
type Error = Error;
167+
}
168+
169+
impl embedded_storage::nor_flash::ReadNorFlash for Flash<'_> {
170+
const READ_SIZE: usize = 1;
171+
172+
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
173+
self.blocking_read(offset, bytes)
174+
}
175+
176+
fn capacity(&self) -> usize {
177+
FLASH_SIZE
178+
}
179+
}
180+
181+
impl embedded_storage::nor_flash::NorFlash for Flash<'_> {
182+
const WRITE_SIZE: usize = WRITE_SIZE;
183+
const ERASE_SIZE: usize = MAX_ERASE_SIZE;
184+
185+
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
186+
self.blocking_write(offset, bytes)
187+
}
188+
189+
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
190+
self.blocking_erase(from, to)
191+
}
192+
}
193+
165194
foreach_flash_region! {
166195
($type_name:ident, $write_size:literal, $erase_size:literal) => {
167196
impl crate::_generated::flash_regions::$type_name<'_> {

embassy-stm32/src/flash/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod common;
77
pub use common::*;
88

99
pub use crate::_generated::flash_regions::*;
10+
pub use crate::_generated::MAX_ERASE_SIZE;
1011
pub use crate::pac::{FLASH_BASE, FLASH_SIZE, WRITE_SIZE};
1112

1213
#[derive(Debug)]

0 commit comments

Comments
 (0)