@@ -31,39 +31,71 @@ pub enum VddioLevel {
31
31
pub const BASE_ADDRESS : u32 = 0x00400000 ;
32
32
///The Capacity in bytes of the Flash Memory.
33
33
#[ cfg( feature = "flash-2M" ) ]
34
- pub const CAPACITY : usize = 0x00200000 ;
34
+ pub const CAPACITY : u32 = 0x00200000 ;
35
+ ///The Capacity in bytes of the Flash Memory.
35
36
#[ cfg( feature = "flash-1M" ) ]
36
- pub const CAPACITY : usize = 0x00100000 ;
37
+ pub const CAPACITY : u32 = 0x00100000 ;
38
+ ///The Capacity in bytes of the Flash Memory.
37
39
#[ cfg( feature = "flash-512K" ) ]
38
- pub const CAPACITY : usize = 0x00080000 ;
40
+ pub const CAPACITY : u32 = 0x00080000 ;
39
41
/// The Size in bytes of a page in the Flash Memory.
40
- pub const PAGE_SIZE : usize = 512 ;
42
+ pub const PAGE_SIZE : u32 = 512 ;
41
43
/// The Size in bytes of a sector in the Flash Memory.
42
- pub const SECTOR_SIZE : usize = 0x00020000 ;
44
+ pub const SECTOR_SIZE : u32 = 0x00020000 ;
43
45
44
46
/// An iterator over the Flash Sectors.
45
47
struct SectorIterator {
46
- index : u8 ,
47
- number_of_sectors : u8 ,
48
+ start_offset : u32 ,
49
+ end_offset : u32 ,
48
50
}
49
51
50
52
/// A struct representing a Sector in Flash Memory.
51
53
#[ derive( Clone , Copy ) ]
52
54
struct Sector {
53
- n : u8 ,
55
+ offset : u32 ,
54
56
}
55
57
56
58
impl Iterator for SectorIterator {
57
59
type Item = Sector ;
58
60
59
61
fn next ( & mut self ) -> Option < Self :: Item > {
60
- self . index += 1 ;
62
+ if self . start_offset >= self . end_offset {
63
+ return None ;
64
+ }
65
+ let sector = Sector {
66
+ offset : self . start_offset ,
67
+ } ;
68
+ self . start_offset += SECTOR_SIZE ;
61
69
62
- if self . index < self . number_of_sectors {
63
- Some ( Sector { n : self . index } )
64
- } else {
65
- None
70
+ Some ( sector)
71
+ }
72
+ }
73
+
74
+ /// A struct representing a Page in Flash memory
75
+ #[ derive( Clone , Copy ) ]
76
+ struct Page {
77
+ offset : u32 ,
78
+ }
79
+
80
+ /// An iterator over the Flash Pages.
81
+ struct PageIterator {
82
+ start_offset : u32 ,
83
+ end_offset : u32 ,
84
+ }
85
+
86
+ impl Iterator for PageIterator {
87
+ type Item = Page ;
88
+
89
+ fn next ( & mut self ) -> Option < Self :: Item > {
90
+ if self . start_offset >= self . end_offset {
91
+ return None ;
66
92
}
93
+ let page = Page {
94
+ offset : self . start_offset ,
95
+ } ;
96
+ self . start_offset += PAGE_SIZE ;
97
+
98
+ Some ( page)
67
99
}
68
100
}
69
101
@@ -146,7 +178,7 @@ impl Sector {
146
178
self . efc ( ) . eefc_fcr . write ( |w| {
147
179
w. fkey ( ) . passwd ( ) ;
148
180
w. fcmd ( ) . es ( ) ;
149
- unsafe { w. farg ( ) . bits ( ( 256_u16 * ( self . n as u16 ) ) as u16 ) } ;
181
+ unsafe { w. farg ( ) . bits ( self . page_number ( ) ) } ;
150
182
w
151
183
} ) ;
152
184
loop {
@@ -162,24 +194,37 @@ impl Sector {
162
194
}
163
195
}
164
196
197
+ // #[inline(always)] fn addr(&self) -> *mut u8 {
198
+ // (BASE_ADDRESS + self.offset) as *mut u8
199
+ // }
200
+
201
+ #[ inline( always) ]
202
+ fn page_number ( & self ) -> u16 {
203
+ ( self . offset / PAGE_SIZE ) as u16
204
+ }
205
+
206
+ // #[inline(always)]
207
+ // fn contains(&self, offset: u32) -> bool {
208
+ // offset >= self.offset
209
+ // && offset < self.offset + SECTOR_SIZE
210
+ // }
211
+ }
212
+
213
+ impl Page {
165
214
/// Write page to flash memory.
166
- fn write_page ( & self , page : u8 , data : & [ u8 ] ) -> Result < ( ) , Error > {
167
- if data. len ( ) != PAGE_SIZE {
215
+ fn write_page ( & self , data : & [ u8 ] ) -> Result < ( ) , Error > {
216
+ if data. len ( ) != PAGE_SIZE as usize {
168
217
return Err ( Error :: NotAligned ) ;
169
218
}
170
219
if self . efc ( ) . eefc_fsr . read ( ) . frdy ( ) . bit_is_clear ( ) {
171
220
return Err ( Error :: FlashBusyError ) ;
172
221
}
173
- unsafe {
174
- self . addr ( )
175
- . add ( ( page as usize ) * PAGE_SIZE )
176
- . copy_from ( data. as_ptr ( ) , PAGE_SIZE )
177
- } ;
222
+ unsafe { self . addr ( ) . copy_from ( data. as_ptr ( ) , PAGE_SIZE as usize ) } ;
178
223
179
224
self . efc ( ) . eefc_fcr . write ( |w| {
180
225
w. fkey ( ) . passwd ( ) ;
181
226
w. fcmd ( ) . wp ( ) ;
182
- unsafe { w. farg ( ) . bits ( 256_u16 * ( self . n as u16 ) + ( page as u16 ) ) } ;
227
+ unsafe { w. farg ( ) . bits ( self . page_number ( ) ) } ;
183
228
w
184
229
} ) ;
185
230
loop {
@@ -197,15 +242,20 @@ impl Sector {
197
242
198
243
#[ inline( always) ]
199
244
fn addr ( & self ) -> * mut u8 {
200
- ( BASE_ADDRESS as usize + ( self . n as usize ) * SECTOR_SIZE ) as * mut u8
245
+ ( BASE_ADDRESS + self . offset ) as * mut u8
201
246
}
202
247
203
248
204
249
#[ inline( always) ]
205
- fn contains ( & self , offset : u32 ) -> bool {
206
- offset >= SECTOR_SIZE as u32 * self . n as u32
207
- && offset < SECTOR_SIZE as u32 * ( self . n as u32 + 1 )
250
+ fn page_number ( & self ) -> u16 {
251
+ ( self . offset / PAGE_SIZE ) as u16
208
252
}
253
+
254
+ // #[inline(always)]
255
+ // fn contains(&self, offset: u32) -> bool {
256
+ // offset >= self.offset
257
+ // && offset < self.offset + PAGE_SIZE as u32
258
+ // }
209
259
}
210
260
211
261
trait RegisterAccess {
@@ -215,6 +265,7 @@ trait RegisterAccess {
215
265
}
216
266
217
267
impl RegisterAccess for Sector { }
268
+ impl RegisterAccess for Page { }
218
269
/// [`EFC`] abstraction.
219
270
pub struct Efc {
220
271
pub ( crate ) periph : EFC ,
@@ -252,22 +303,34 @@ impl Efc {
252
303
253
304
trait Flash {
254
305
type SectorIterator ;
255
- fn len ( & self ) -> usize {
306
+ type PageIterator ;
307
+
308
+ fn len ( & self ) -> u32 {
256
309
CAPACITY
257
310
}
311
+
258
312
fn address ( & self ) -> u32 {
259
313
BASE_ADDRESS
260
314
}
261
- fn sectors ( ) -> SectorIterator {
315
+
316
+ fn sectors ( start_offset : u32 , end_offset : u32 ) -> SectorIterator {
262
317
SectorIterator {
263
- index : 0 ,
264
- number_of_sectors : ( CAPACITY / SECTOR_SIZE ) as u8 ,
318
+ start_offset,
319
+ end_offset,
320
+ }
321
+ }
322
+
323
+ fn pages ( start_offset : u32 , end_offset : u32 ) -> PageIterator {
324
+ PageIterator {
325
+ start_offset,
326
+ end_offset,
265
327
}
266
328
}
267
329
}
268
330
269
331
impl Flash for Efc {
270
332
type SectorIterator = SectorIterator ;
333
+ type PageIterator = PageIterator ;
271
334
}
272
335
273
336
impl ErrorType for Efc {
@@ -288,52 +351,30 @@ impl ReadNorFlash for Efc {
288
351
}
289
352
290
353
fn capacity ( & self ) -> usize {
291
- self . len ( )
354
+ self . len ( ) as usize
292
355
}
293
356
}
294
357
295
358
impl NorFlash for Efc {
296
- const WRITE_SIZE : usize = PAGE_SIZE ;
359
+ const WRITE_SIZE : usize = PAGE_SIZE as usize ;
297
360
// NOTE: This number is the sector size, there is an option to erase by page as well, but the
298
361
// minimum/maximum erase size for that varies throughout the flash memory.
299
- const ERASE_SIZE : usize = SECTOR_SIZE ;
362
+ const ERASE_SIZE : usize = SECTOR_SIZE as usize ;
300
363
301
364
fn erase ( & mut self , offset : u32 , to : u32 ) -> Result < ( ) , Self :: Error > {
302
365
check_erase ( self , offset, to) ?;
303
- let mut offset = offset;
304
- for sector in Self :: sectors ( ) {
305
- if sector. contains ( offset) {
306
- sector. erase_sector ( ) ?;
307
- offset += Self :: ERASE_SIZE as u32 ;
308
- }
309
- if offset >= to {
310
- break ;
311
- }
366
+ for sector in Self :: sectors ( offset, to) {
367
+ sector. erase_sector ( ) ?;
312
368
}
313
369
Ok ( ( ) )
314
370
}
315
371
316
372
fn write ( & mut self , offset : u32 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
317
373
check_write ( self , offset, bytes. len ( ) ) ?;
318
- let mut offset = offset;
319
374
let mut bytes_written = 0 ;
320
- for sector in Self :: sectors ( ) {
321
- while sector. contains ( offset) {
322
- let sector_offset = sector. n as usize * SECTOR_SIZE ;
323
- let page = ( ( offset as usize - sector_offset) / Self :: WRITE_SIZE ) as u8 ;
324
- sector. write_page (
325
- page,
326
- & bytes[ bytes_written..( bytes_written + Self :: WRITE_SIZE ) ] ,
327
- ) ?;
328
- bytes_written += Self :: WRITE_SIZE ;
329
- offset += Self :: WRITE_SIZE as u32 ;
330
- if bytes_written >= bytes. len ( ) {
331
- break ;
332
- }
333
- }
334
- if bytes_written >= bytes. len ( ) {
335
- break ;
336
- }
375
+ for page in Self :: pages ( offset, offset + bytes. len ( ) as u32 ) {
376
+ page. write_page ( & bytes[ bytes_written..( bytes_written + Self :: WRITE_SIZE ) ] ) ?;
377
+ bytes_written += Self :: WRITE_SIZE ;
337
378
}
338
379
Ok ( ( ) )
339
380
}
0 commit comments