@@ -2,7 +2,7 @@ use core::convert::TryInto;
2
2
use core:: { mem, ptr, slice} ;
3
3
4
4
use embedded_storage:: nor_flash:: {
5
- ErrorType , MultiwriteNorFlash , NorFlash , NorFlashError , NorFlashErrorKind , ReadNorFlash ,
5
+ ErrorType , NorFlash , NorFlashError , NorFlashErrorKind , ReadNorFlash ,
6
6
} ;
7
7
8
8
use crate :: pac:: FLASH ;
@@ -85,7 +85,7 @@ pub trait FlashExt {
85
85
/// Size in bytes
86
86
fn len ( & self ) -> usize ;
87
87
/// Returns a read-only view of flash memory
88
- fn read ( & self ) -> & [ u8 ] {
88
+ fn read_all ( & self ) -> & [ u8 ] {
89
89
let ptr = self . address ( ) as * const _ ;
90
90
unsafe { slice:: from_raw_parts ( ptr, self . len ( ) ) }
91
91
}
@@ -204,23 +204,19 @@ pub trait WriteErase {
204
204
impl WriteErase for UnlockedFlash < ' _ > {
205
205
type NativeType = u16 ;
206
206
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 > {
212
208
// Wait for ready bit
213
209
self . wait_ready ( ) ;
214
210
215
- let addr = self . flash . address ( ) as * mut u16 ;
211
+ let mut addr = address as * mut Self :: NativeType ;
216
212
217
213
// Write the data to flash
218
214
for & half_word in data {
219
215
self . flash . cr . modify ( |_, w| w. pg ( ) . set_bit ( ) ) ;
220
216
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 > ( ) ) ;
222
219
}
223
- offset += mem:: size_of :: < Self :: NativeType > ( ) ;
224
220
}
225
221
226
222
self . wait_ready ( ) ;
@@ -231,8 +227,8 @@ impl WriteErase for UnlockedFlash<'_> {
231
227
self . ok ( )
232
228
}
233
229
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 {
236
232
return Err ( Error :: Alignment ) ;
237
233
}
238
234
@@ -242,8 +238,8 @@ impl WriteErase for UnlockedFlash<'_> {
242
238
let native = & [ Self :: NativeType :: from_ne_bytes (
243
239
exact_chunk. try_into ( ) . unwrap ( ) ,
244
240
) ] ;
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 > ( ) ;
247
243
}
248
244
249
245
let remainder = chunks. remainder ( ) ;
@@ -256,7 +252,7 @@ impl WriteErase for UnlockedFlash<'_> {
256
252
}
257
253
258
254
let native = & [ data] ;
259
- self . program_native ( offset , native) ?;
255
+ self . program_native ( address , native) ?;
260
256
}
261
257
262
258
self . ok ( )
@@ -420,7 +416,7 @@ impl ReadNorFlash for LockedFlash {
420
416
421
417
fn read ( & mut self , offset : u32 , bytes : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
422
418
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 ( ) ] ) ;
424
420
Ok ( ( ) )
425
421
}
426
422
@@ -434,7 +430,7 @@ impl<'a> ReadNorFlash for UnlockedFlash<'a> {
434
430
435
431
fn read ( & mut self , offset : u32 , bytes : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
436
432
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 ( ) ] ) ;
438
434
Ok ( ( ) )
439
435
}
440
436
@@ -466,9 +462,6 @@ impl<'a> NorFlash for UnlockedFlash<'a> {
466
462
}
467
463
468
464
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)
470
466
}
471
467
}
472
-
473
- // STM32F4 supports multiple writes
474
- impl < ' a > MultiwriteNorFlash for UnlockedFlash < ' a > { }
0 commit comments