-
Notifications
You must be signed in to change notification settings - Fork 8
/
flash.rs
164 lines (139 loc) · 5.17 KB
/
flash.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! Flash storage trait.
//!
//! You must implement this trait for your flash storage to use it with `ekv`.
use core::fmt::Debug;
#[cfg(feature = "std")]
use crate::config::*;
pub use crate::types::PageID;
// TODO: use embedded-storage instead
// or make an adapter instead?
/// Flash storage trait
pub trait Flash {
/// Error type for the flash operations.
type Error: Debug;
/// Get the page count of the flash storage.
fn page_count(&self) -> usize;
/// Erase a page.
///
/// If power is lost during an erase, the resulting data is allowed to be
/// anything, but data on other pages must stay unaffected.
///
/// After erasing, all bytes in the page must be equal to [`ERASE_VALUE`](crate::config::ERASE_VALUE).
async fn erase(&mut self, page_id: PageID) -> Result<(), Self::Error>;
/// Read data.
///
/// `offset` and `data.len()` are guaranteed to be a multiple of [`ALIGN`](crate::config::ALIGN).
async fn read(&mut self, page_id: PageID, offset: usize, data: &mut [u8]) -> Result<(), Self::Error>;
/// Write data.
///
/// If power is lost during a write, the resulting data in the written bytes
/// is allowed to be anything, but data on other bytes within the page must
/// stay unaffected. This means it is NOT okay to implement this with underlying
/// "read-erase-modify-write" strategies.
///
/// `offset` and `data.len()` are guaranteed to be a multiple of [`ALIGN`](crate::config::ALIGN).
///
/// It is guaranteed that each byte will be written only once after each erase.
/// This ensures maximum compatibility with different flash hardware, in particular
/// flash memory with ECC.
async fn write(&mut self, page_id: PageID, offset: usize, data: &[u8]) -> Result<(), Self::Error>;
}
impl<T: Flash> Flash for &mut T {
type Error = T::Error;
fn page_count(&self) -> usize {
T::page_count(self)
}
async fn erase(&mut self, page_id: PageID) -> Result<(), Self::Error> {
T::erase(self, page_id).await
}
async fn read(&mut self, page_id: PageID, offset: usize, data: &mut [u8]) -> Result<(), Self::Error> {
T::read(self, page_id, offset, data).await
}
async fn write(&mut self, page_id: PageID, offset: usize, data: &[u8]) -> Result<(), Self::Error> {
T::write(self, page_id, offset, data).await
}
}
/// Fake in-memory flash
#[cfg(feature = "std")]
pub struct MemFlash {
/// Data. Length must equal `PAGE_SIZE * MAX_PAGE_COUNT`
pub data: Vec<u8>,
/// Count of read operations.
pub read_count: usize,
/// Count of read bytes (sum of all operations)
pub read_bytes: usize,
/// Count of write operations.
pub write_count: usize,
/// Count of written bytes (sum of all operations)
pub write_bytes: usize,
/// Count of erase operations.
pub erase_count: usize,
/// Count of erased bytes (sum of all operations)
pub erase_bytes: usize,
}
#[cfg(feature = "std")]
impl MemFlash {
/// Create a new MemFlash
pub fn new() -> Self {
Self {
data: vec![ERASE_VALUE; PAGE_SIZE * MAX_PAGE_COUNT],
read_count: 0,
read_bytes: 0,
write_count: 0,
write_bytes: 0,
erase_count: 0,
erase_bytes: 0,
}
}
/// Reset statistics counters.
pub fn reset_counters(&mut self) {
self.read_count = 0;
self.read_bytes = 0;
self.write_count = 0;
self.write_bytes = 0;
self.erase_count = 0;
self.erase_bytes = 0;
}
}
#[cfg(feature = "std")]
impl Flash for MemFlash {
type Error = core::convert::Infallible;
fn page_count(&self) -> usize {
MAX_PAGE_COUNT
}
async fn erase(&mut self, page_id: PageID) -> Result<(), Self::Error> {
let page_id = page_id.index();
assert!(page_id < self.page_count());
self.data[page_id * PAGE_SIZE..][..PAGE_SIZE].fill(ERASE_VALUE);
self.erase_count += 1;
self.erase_bytes += PAGE_SIZE;
Ok(())
}
async fn read(&mut self, page_id: PageID, offset: usize, data: &mut [u8]) -> Result<(), Self::Error> {
let page_id = page_id.index();
assert!(page_id < self.page_count());
assert!(offset <= PAGE_SIZE);
assert!(offset + data.len() <= PAGE_SIZE);
assert!(offset % ALIGN == 0);
assert!(data.len() % ALIGN == 0);
let mem = &self.data[page_id * PAGE_SIZE + offset..][..data.len()];
data.copy_from_slice(mem);
self.read_count += 1;
self.read_bytes += data.len();
Ok(())
}
async fn write(&mut self, page_id: PageID, offset: usize, data: &[u8]) -> Result<(), Self::Error> {
let page_id = page_id.index();
assert!(page_id < self.page_count());
assert!(offset <= PAGE_SIZE);
assert!(offset + data.len() <= PAGE_SIZE);
assert!(offset % ALIGN == 0);
assert!(data.len() % ALIGN == 0);
let mem = &mut self.data[page_id * PAGE_SIZE + offset..][..data.len()];
assert!(mem.iter().all(|x| *x == ERASE_VALUE));
mem.copy_from_slice(data);
self.write_count += 1;
self.write_bytes += data.len();
Ok(())
}
}