Skip to content

Commit b89cc3c

Browse files
committed
Fix and update test
1 parent 1392ec8 commit b89cc3c

File tree

4 files changed

+50
-47
lines changed

4 files changed

+50
-47
lines changed

examples/nvmc-demo/memory.x

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
MEMORY
22
{
33
/* NOTE 1 K = 1 KiB = 1024 bytes */
4-
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K - 16K
5-
/* We use 6 pages of 4 KiB each */
6-
CONFIG : ORIGIN = ORIGIN(FLASH) + LENGTH(FLASH), LENGTH = 6 * 4K
4+
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K - NUM_PAGES * 4K
5+
CONFIG : ORIGIN = ORIGIN(FLASH) + LENGTH(FLASH), LENGTH = NUM_PAGES * 4K
76
RAM : ORIGIN = 0x20000000, LENGTH = 256K
87
}
98

9+
NUM_PAGES = 6;
1010
_config = ORIGIN(CONFIG);
1111

1212
/* This is where the call stack will be allocated. */

examples/nvmc-demo/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ use hal::pac::NVMC;
1414
use panic_probe as _;
1515
use rtt_target::{rprintln, rtt_init_print};
1616

17-
const NUM_PAGES: usize = 6;
17+
const NUM_PAGES: u32 = 6;
1818
const PAGE_SIZE: u32 = 4 * 1024;
19-
const LAST_PAGE: u32 = 3 * PAGE_SIZE;
19+
const LAST_PAGE: u32 = (NUM_PAGES - 1) * PAGE_SIZE;
2020
extern "C" {
2121
#[link_name = "_config"]
22-
static mut CONFIG: [u8; NUM_PAGES * PAGE_SIZE as usize];
22+
static mut CONFIG: [u8; (NUM_PAGES * PAGE_SIZE) as usize];
2323
}
2424

2525
// To run this example:

nrf52840-hal-tests/memory.x

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
MEMORY
22
{
3-
/* NOTE 1 K = 1 KiBi = 1024 bytes */
4-
FLASH : ORIGIN = 0x00000000, LENGTH = 1020K
5-
CONFIG : ORIGIN = ORIGIN(FLASH) + LENGTH(FLASH), LENGTH = 4K /* 4K is the flash page size */
3+
/* NOTE 1 K = 1 KiB = 1024 bytes */
4+
FLASH : ORIGIN = 0x00000000, LENGTH = 1024K - NUM_PAGES * 4K
5+
CONFIG : ORIGIN = ORIGIN(FLASH) + LENGTH(FLASH), LENGTH = NUM_PAGES * 4K
66
RAM : ORIGIN = 0x20000000, LENGTH = 256K
77
}
88

9+
NUM_PAGES = 6;
910
_config = ORIGIN(CONFIG);
1011

1112
/* This is where the call stack will be allocated. */

nrf52840-hal-tests/tests/nvmc.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ use panic_probe as _;
88
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
99
use 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;
1215
extern "C" {
1316
#[link_name = "_config"]
14-
static mut CONFIG: [u32; CONFIG_SIZE];
17+
static mut CONFIG: [u8; CONFIG_SIZE as usize];
1518
}
1619

1720
struct 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

Comments
 (0)