Skip to content

Commit 5524712

Browse files
committed
Fix bugs in flash interface
1 parent 754b443 commit 5524712

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

src/flash.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::convert::TryInto;
22
use core::{mem, ptr, slice};
33

44
use embedded_storage::nor_flash::{
5-
ErrorType, MultiwriteNorFlash, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash,
5+
ErrorType, NorFlash, NorFlashError, NorFlashErrorKind, ReadNorFlash,
66
};
77

88
use crate::pac::FLASH;
@@ -85,7 +85,7 @@ pub trait FlashExt {
8585
/// Size in bytes
8686
fn len(&self) -> usize;
8787
/// Returns a read-only view of flash memory
88-
fn read(&self) -> &[u8] {
88+
fn read_all(&self) -> &[u8] {
8989
let ptr = self.address() as *const _;
9090
unsafe { slice::from_raw_parts(ptr, self.len()) }
9191
}
@@ -204,23 +204,19 @@ pub trait WriteErase {
204204
impl WriteErase for UnlockedFlash<'_> {
205205
type NativeType = u16;
206206

207-
fn program_native(
208-
&mut self,
209-
mut offset: usize,
210-
data: &[Self::NativeType],
211-
) -> Result<(), Error> {
207+
fn program_native(&mut self, address: usize, data: &[Self::NativeType]) -> Result<(), Error> {
212208
// Wait for ready bit
213209
self.wait_ready();
214210

215-
let addr = self.flash.address() as *mut u16;
211+
let mut addr = address as *mut Self::NativeType;
216212

217213
// Write the data to flash
218214
for &half_word in data {
219215
self.flash.cr.modify(|_, w| w.pg().set_bit());
220216
unsafe {
221-
ptr::write_volatile(addr.add(offset), half_word);
217+
ptr::write_volatile(addr, half_word);
218+
addr = addr.add(mem::size_of::<Self::NativeType>());
222219
}
223-
offset += mem::size_of::<Self::NativeType>();
224220
}
225221

226222
self.wait_ready();
@@ -231,8 +227,8 @@ impl WriteErase for UnlockedFlash<'_> {
231227
self.ok()
232228
}
233229

234-
fn program(&mut self, mut offset: usize, data: &[u8]) -> Result<(), Error> {
235-
if offset % mem::align_of::<Self::NativeType>() != 0 {
230+
fn program(&mut self, mut address: usize, data: &[u8]) -> Result<(), Error> {
231+
if address % mem::align_of::<Self::NativeType>() != 0 {
236232
return Err(Error::Alignment);
237233
}
238234

@@ -242,8 +238,8 @@ impl WriteErase for UnlockedFlash<'_> {
242238
let native = &[Self::NativeType::from_ne_bytes(
243239
exact_chunk.try_into().unwrap(),
244240
)];
245-
self.program_native(offset, native)?;
246-
offset += mem::size_of::<Self::NativeType>();
241+
self.program_native(address, native)?;
242+
address += mem::size_of::<Self::NativeType>();
247243
}
248244

249245
let remainder = chunks.remainder();
@@ -256,7 +252,7 @@ impl WriteErase for UnlockedFlash<'_> {
256252
}
257253

258254
let native = &[data];
259-
self.program_native(offset, native)?;
255+
self.program_native(address, native)?;
260256
}
261257

262258
self.ok()
@@ -420,7 +416,7 @@ impl ReadNorFlash for LockedFlash {
420416

421417
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
422418
let offset = offset as usize;
423-
bytes.copy_from_slice(&self.flash.read()[offset..offset + bytes.len()]);
419+
bytes.copy_from_slice(&self.flash.read_all()[offset..offset + bytes.len()]);
424420
Ok(())
425421
}
426422

@@ -434,7 +430,7 @@ impl<'a> ReadNorFlash for UnlockedFlash<'a> {
434430

435431
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
436432
let offset = offset as usize;
437-
bytes.copy_from_slice(&self.flash.read()[offset..offset + bytes.len()]);
433+
bytes.copy_from_slice(&self.flash.read_all()[offset..offset + bytes.len()]);
438434
Ok(())
439435
}
440436

@@ -466,9 +462,6 @@ impl<'a> NorFlash for UnlockedFlash<'a> {
466462
}
467463

468464
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
469-
self.program(offset as usize, bytes)
465+
self.program(self.flash.address() + offset as usize, bytes)
470466
}
471467
}
472-
473-
// STM32F4 supports multiple writes
474-
impl<'a> MultiwriteNorFlash for UnlockedFlash<'a> {}

0 commit comments

Comments
 (0)