@@ -32,16 +32,14 @@ pub trait NorFlash: ReadNorFlash {
32
32
///
33
33
/// This should return an error if the range is not aligned to a proper
34
34
/// erase resolution
35
- /// Erases page at addr, sets it all to 0xFF
36
35
/// If power is lost during erase, contents of the page are undefined.
37
- /// `from` and `to` must both be multiples of `erase_size() ` and `from` <= `to`.
36
+ /// `from` and `to` must both be multiples of `ERASE_SIZE ` and `from` <= `to`.
38
37
fn try_erase ( & mut self , from : u32 , to : u32 ) -> Result < ( ) , Self :: Error > ;
39
38
40
- /// Writes data to addr, bitwise ANDing if there's already data written at that location,
41
- /// If power is lost during write, the contents of the written words are undefined.
42
- /// The rest of the page is guaranteed to be unchanged.
39
+ /// If power is lost during write, the contents of the written words are undefined,
40
+ /// but the rest of the page is guaranteed to be unchanged.
43
41
/// It is not allowed to write to the same word twice.
44
- /// `offset` and `bytes.len()` must both be multiples of `write_size()` and properly aligned .
42
+ /// `offset` and `bytes.len()` must both be multiples of `WRITE_SIZE` .
45
43
fn try_write ( & mut self , offset : u32 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > ;
46
44
}
47
45
@@ -131,7 +129,7 @@ where
131
129
{
132
130
fn try_write ( & mut self , offset : u32 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
133
131
// Perform read/modify/write operations on the byte slice.
134
- let last_page = ( self . storage . capacity ( ) / S :: ERASE_SIZE ) - 1 ;
132
+ let last_page = self . storage . capacity ( ) / S :: ERASE_SIZE ;
135
133
136
134
// `data` is the part of `bytes` contained within `page`,
137
135
// and `addr` in the address offset of `page` + any offset into the page as requested by `address`
@@ -141,16 +139,18 @@ where
141
139
{
142
140
let offset_into_page = addr. saturating_sub ( page. start ) as usize ;
143
141
144
- self . storage . try_read ( page. start , self . merge_buffer ) ?;
142
+ self . storage
143
+ . try_read ( page. start , & mut self . merge_buffer [ ..S :: ERASE_SIZE ] ) ?;
145
144
146
145
// If we cannot write multiple times to the same page, we will have to erase it
147
146
self . storage . try_erase ( page. start , page. end ( ) ) ?;
148
- self . merge_buffer
147
+ self . merge_buffer [ .. S :: ERASE_SIZE ]
149
148
. iter_mut ( )
150
149
. skip ( offset_into_page)
151
150
. zip ( data)
152
151
. for_each ( |( byte, input) | * byte = * input) ;
153
- self . storage . try_write ( page. start , self . merge_buffer ) ?;
152
+ self . storage
153
+ . try_write ( page. start , & self . merge_buffer [ ..S :: ERASE_SIZE ] ) ?;
154
154
}
155
155
Ok ( ( ) )
156
156
}
@@ -204,7 +204,7 @@ where
204
204
{
205
205
fn try_write ( & mut self , offset : u32 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
206
206
// Perform read/modify/write operations on the byte slice.
207
- let last_page = ( self . storage . capacity ( ) / S :: ERASE_SIZE ) - 1 ;
207
+ let last_page = self . storage . capacity ( ) / S :: ERASE_SIZE ;
208
208
209
209
// `data` is the part of `bytes` contained within `page`,
210
210
// and `addr` in the address offset of `page` + any offset into the page as requested by `address`
@@ -214,24 +214,25 @@ where
214
214
{
215
215
let offset_into_page = addr. saturating_sub ( page. start ) as usize ;
216
216
217
- self . storage . try_read ( page. start , self . merge_buffer ) ?;
217
+ self . storage
218
+ . try_read ( page. start , & mut self . merge_buffer [ ..S :: ERASE_SIZE ] ) ?;
218
219
219
- let rhs = & self . merge_buffer [ offset_into_page..] ;
220
- let is_subset =
221
- data. len ( ) < rhs. len ( ) && data. iter ( ) . zip ( rhs. iter ( ) ) . all ( |( a, b) | ( * a | * b) == * b) ;
220
+ let rhs = & self . merge_buffer [ offset_into_page..S :: ERASE_SIZE ] ;
221
+ let is_subset = data. iter ( ) . zip ( rhs. iter ( ) ) . all ( |( a, b) | * a & * b == * a) ;
222
222
223
223
// Check if we can write the data block directly, under the limitations imposed by NorFlash:
224
224
// - We can only change 1's to 0's
225
225
if is_subset {
226
226
self . storage . try_write ( addr, data) ?;
227
227
} else {
228
228
self . storage . try_erase ( page. start , page. end ( ) ) ?;
229
- self . merge_buffer
229
+ self . merge_buffer [ .. S :: ERASE_SIZE ]
230
230
. iter_mut ( )
231
231
. skip ( offset_into_page)
232
232
. zip ( data)
233
233
. for_each ( |( byte, input) | * byte = * input) ;
234
- self . storage . try_write ( page. start , self . merge_buffer ) ?;
234
+ self . storage
235
+ . try_write ( page. start , & self . merge_buffer [ ..S :: ERASE_SIZE ] ) ?;
235
236
}
236
237
}
237
238
Ok ( ( ) )
0 commit comments