Skip to content

Commit 7cf1c96

Browse files
Merge pull request #583 from Foundation-Devices/SFT-4402
SFT-4402: Implement erase for simulator flash.
2 parents 97a92a6 + a870c6a commit 7cf1c96

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

extmod/foundation-rust/embedded-storage-fs/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ impl<
6060
where
6161
P: AsRef<Path>,
6262
{
63-
let mut file = fs::File::open(&path)?;
6463
let mut storage = Vec::new();
65-
file.read_to_end(&mut storage)?;
64+
match fs::File::open(&path) {
65+
Ok(mut f) => {
66+
f.read_to_end(&mut storage)?;
67+
}
68+
// If no file is found, the storage will become a new file
69+
Err(e) if e.kind() == io::ErrorKind::NotFound => (),
70+
Err(e) => Err(e)?,
71+
};
6672

6773
// If the file doesn't match the capacity just resize it, it can
6874
// silently truncate the file or extend it and fill it with blank
@@ -124,7 +130,7 @@ impl<
124130

125131
let from = usize::try_from(from).expect("u32 bigger than usize");
126132
let to = usize::try_from(to).expect("u32 bigger than usize");
127-
self.storage[from..from + to].fill(0xFF);
133+
self.storage[from..to].fill(0xFF);
128134
fs::write(&self.path, &self.storage)?;
129135
Ok(())
130136
}

extmod/foundation-rust/src/flash.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,23 @@ pub extern "C" fn write(offset: u32, data: *const u8, len: usize) -> bool {
143143
/// Erase a sector of the flash storage.
144144
#[export_name = "foundation_flash_sector_erase"]
145145
pub extern "C" fn sector_erase(offset: u32) -> bool {
146+
let mut flash = unsafe { FLASH.borrow_mut() };
147+
146148
#[cfg(target_arch = "arm")]
147149
{
148-
let mut flash = unsafe { FLASH.borrow_mut() };
150+
if flash.as_mut_inner().sector_erase(offset).is_err() {
151+
return false;
152+
}
153+
}
149154

150-
if let Err(_) = flash.as_mut_inner().sector_erase(offset) {
155+
#[cfg(not(target_arch = "arm"))]
156+
{
157+
// Erase 4 KiB.
158+
if flash
159+
.as_mut_inner()
160+
.erase(offset, offset + (4 * 1024))
161+
.is_err()
162+
{
151163
return false;
152164
}
153165
}
@@ -158,11 +170,23 @@ pub extern "C" fn sector_erase(offset: u32) -> bool {
158170
/// Erase a block of the flash storage.
159171
#[export_name = "foundation_flash_block_erase"]
160172
pub extern "C" fn block_erase(offset: u32) -> bool {
173+
let mut flash = unsafe { FLASH.borrow_mut() };
174+
161175
#[cfg(target_arch = "arm")]
162176
{
163-
let mut flash = unsafe { FLASH.borrow_mut() };
177+
if flash.as_mut_inner().block_erase_64kib(offset).is_err() {
178+
return false;
179+
}
180+
}
164181

165-
if let Err(_) = flash.as_mut_inner().block_erase_64kib(offset) {
182+
#[cfg(not(target_arch = "arm"))]
183+
{
184+
// Erase 64 KiB.
185+
if flash
186+
.as_mut_inner()
187+
.erase(offset, offset + (64 * 1024))
188+
.is_err()
189+
{
166190
return false;
167191
}
168192
}

0 commit comments

Comments
 (0)