@@ -8,10 +8,13 @@ use panic_probe as _;
88use embedded_storage:: nor_flash:: { NorFlash , ReadNorFlash } ;
99use nrf52840_hal:: { nvmc:: Nvmc , pac} ;
1010
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 ;
1215extern "C" {
1316 #[ link_name = "_config" ]
14- static mut CONFIG : [ u32 ; CONFIG_SIZE ] ;
17+ static mut CONFIG : [ u8 ; CONFIG_SIZE as usize ] ;
1518}
1619
1720struct State {
@@ -35,66 +38,65 @@ mod tests {
3538
3639 #[ test]
3740 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 ) ;
3942 }
4043
4144 #[ 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( ) ) ;
4548 }
4649
4750 #[ 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( ) ) ;
5154 }
5255
5356 #[ 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( ) ) ;
6166 }
6267
6368 #[ test]
6469 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( ) ) ;
6773 }
6874
6975 #[ test]
7076 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( ) ) ;
7480 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 ) ;
7985 }
8086
8187 #[ test]
8288 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( ) ) ;
8490 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( ) ) ;
8692 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( ) ) ;
8894 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 ] ) ;
99101 }
100102}
0 commit comments