@@ -8,10 +8,13 @@ use panic_probe as _;
8
8
use embedded_storage:: nor_flash:: { NorFlash , ReadNorFlash } ;
9
9
use nrf52840_hal:: { nvmc:: Nvmc , pac} ;
10
10
11
- const CONFIG_SIZE : usize = 1024 ;
11
+ const NUM_PAGES : u32 = 6 ; // must match memory.x
12
+ const PAGE_SIZE : u32 = 4 * 1024 ;
13
+ const LAST_PAGE : u32 = ( NUM_PAGES - 1 ) * PAGE_SIZE ;
14
+ const CONFIG_SIZE : u32 = NUM_PAGES * PAGE_SIZE ;
12
15
extern "C" {
13
16
#[ link_name = "_config" ]
14
- static mut CONFIG : [ u32 ; CONFIG_SIZE ] ;
17
+ static mut CONFIG : [ u8 ; CONFIG_SIZE as usize ] ;
15
18
}
16
19
17
20
struct State {
@@ -35,66 +38,65 @@ mod tests {
35
38
36
39
#[ test]
37
40
fn check_capacity ( state : & mut State ) {
38
- assert_eq ! ( state. nvmc. capacity( ) , CONFIG_SIZE * 4 ) ;
41
+ assert_eq ! ( state. nvmc. capacity( ) , CONFIG_SIZE as usize ) ;
39
42
}
40
43
41
44
#[ test]
42
- fn read_unaligned ( state : & mut State ) {
43
- let mut buf = [ 0u8 ; 1 ] ;
44
- assert ! ( state. nvmc. read( 1 , & mut buf ) . is_err( ) ) ;
45
+ fn read_outofbounds ( state : & mut State ) {
46
+ assert ! ( state . nvmc . read ( CONFIG_SIZE , & mut [ 0 ] ) . is_err ( ) ) ;
47
+ assert ! ( state. nvmc. read( CONFIG_SIZE - 1 , & mut [ 0 , 0 ] ) . is_err( ) ) ;
45
48
}
46
49
47
50
#[ test]
48
- fn read_beyond_buffer ( state : & mut State ) {
49
- let mut buf = [ 0u8 ; CONFIG_SIZE * 4 + 1 ] ;
50
- assert ! ( state. nvmc. read ( 0 , & mut buf ) . is_err( ) ) ;
51
+ fn erase_unaligned ( state : & mut State ) {
52
+ assert ! ( state . nvmc . erase ( LAST_PAGE + 1 , PAGE_SIZE ) . is_err ( ) ) ;
53
+ assert ! ( state. nvmc. erase ( LAST_PAGE , PAGE_SIZE + 1 ) . is_err( ) ) ;
51
54
}
52
55
53
56
#[ test]
54
- fn erase_unaligned_from ( state : & mut State ) {
55
- assert ! ( state. nvmc. erase( 1 , 4096 ) . is_err( ) ) ;
56
- }
57
-
58
- #[ test]
59
- fn erase_unaligned_to ( state : & mut State ) {
60
- assert ! ( state. nvmc. erase( 0 , 4097 ) . is_err( ) ) ;
57
+ fn erase_outofbounds ( state : & mut State ) {
58
+ assert ! ( state
59
+ . nvmc
60
+ . erase( CONFIG_SIZE , CONFIG_SIZE + PAGE_SIZE )
61
+ . is_err( ) ) ;
62
+ assert ! ( state
63
+ . nvmc
64
+ . erase( LAST_PAGE , LAST_PAGE + 2 * PAGE_SIZE )
65
+ . is_err( ) ) ;
61
66
}
62
67
63
68
#[ test]
64
69
fn write_unaligned ( state : & mut State ) {
65
- let buf = [ 0u8 ; 1 ] ;
66
- assert ! ( state. nvmc. write( 1 , & buf) . is_err( ) ) ;
70
+ let buf = [ 0u8 ; 4 ] ;
71
+ assert ! ( state. nvmc. write( LAST_PAGE + 1 , & buf) . is_err( ) ) ;
72
+ assert ! ( state. nvmc. write( LAST_PAGE , & buf[ ..1 ] ) . is_err( ) ) ;
67
73
}
68
74
69
75
#[ test]
70
76
fn read_write_and_then_read ( state : & mut State ) {
71
- assert ! ( state. nvmc. erase( 0 , CONFIG_SIZE as u32 * 4 ) . is_ok( ) ) ;
72
- let mut read_buf = [ 0u8 ; 1 ] ;
73
- assert ! ( state. nvmc. read( 0 , & mut read_buf) . is_ok( ) ) ;
77
+ assert ! ( state. nvmc. erase( LAST_PAGE , CONFIG_SIZE ) . is_ok( ) ) ;
78
+ let mut read_buf = [ 0 ] ;
79
+ assert ! ( state. nvmc. read( LAST_PAGE , & mut read_buf) . is_ok( ) ) ;
74
80
assert_eq ! ( read_buf[ 0 ] , 0xff ) ;
75
- let write_buf = [ 1u8 ; 4 ] ;
76
- assert ! ( state. nvmc. write( 0 , & write_buf) . is_ok( ) ) ;
77
- assert ! ( state. nvmc. read( 0 , & mut read_buf) . is_ok( ) ) ;
78
- assert_eq ! ( read_buf[ 0 ] , 0x1 ) ;
81
+ let write_buf = [ 1 , 2 , 3 , 4 ] ;
82
+ assert ! ( state. nvmc. write( LAST_PAGE , & write_buf) . is_ok( ) ) ;
83
+ assert ! ( state. nvmc. read( LAST_PAGE , & mut read_buf) . is_ok( ) ) ;
84
+ assert_eq ! ( read_buf[ 0 ] , 1 ) ;
79
85
}
80
86
81
87
#[ test]
82
88
fn read_what_is_written ( state : & mut State ) {
83
- assert ! ( state. nvmc. erase( 0 , CONFIG_SIZE as u32 * 4 ) . is_ok( ) ) ;
89
+ assert ! ( state. nvmc. erase( LAST_PAGE , CONFIG_SIZE ) . is_ok( ) ) ;
84
90
let write_buf: [ u8 ; 8 ] = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ;
85
- assert ! ( state. nvmc. write( 0 , & write_buf) . is_ok( ) ) ;
91
+ assert ! ( state. nvmc. write( LAST_PAGE , & write_buf) . is_ok( ) ) ;
86
92
let mut read_buf = [ 0u8 ; 8 ] ;
87
- assert ! ( state. nvmc. read( 0 , & mut read_buf) . is_ok( ) ) ;
93
+ assert ! ( state. nvmc. read( LAST_PAGE , & mut read_buf) . is_ok( ) ) ;
88
94
assert_eq ! ( read_buf, write_buf) ;
89
- }
90
-
91
- #[ test]
92
- fn partially_read_what_is_written ( state : & mut State ) {
93
- assert ! ( state. nvmc. erase( 0 , CONFIG_SIZE as u32 * 4 ) . is_ok( ) ) ;
94
- let write_buf: [ u8 ; 4 ] = [ 1 , 2 , 3 , 4 ] ;
95
- assert ! ( state. nvmc. write( 0 , & write_buf) . is_ok( ) ) ;
96
- let mut read_buf = [ 0u8 ; 2 ] ;
97
- assert ! ( state. nvmc. read( 0 , & mut read_buf) . is_ok( ) ) ;
98
- assert_eq ! ( read_buf, write_buf[ 0 ..2 ] ) ;
95
+ let mut partial_read_buf = [ 0u8 ; 4 ] ;
96
+ assert ! ( state
97
+ . nvmc
98
+ . read( LAST_PAGE + 2 , & mut partial_read_buf)
99
+ . is_ok( ) ) ;
100
+ assert_eq ! ( partial_read_buf, write_buf[ 2 ..] [ ..4 ] ) ;
99
101
}
100
102
}
0 commit comments