aboutsummaryrefslogtreecommitdiff
path: root/tests/rp/src/bin/flash.rs
blob: 548a6ee725ad1a2d4604c734de425ed871de82f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#![no_std]
#![no_main]
#[cfg(feature = "rp2040")]
teleprobe_meta::target!(b"rpi-pico");
#[cfg(feature = "rp235xb")]
teleprobe_meta::target!(b"pimoroni-pico-plus-2");

use defmt::*;
use embassy_executor::Spawner;
use embassy_rp::flash::{Async, ERASE_SIZE, FLASH_BASE};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};

const ADDR_OFFSET: u32 = 0x8000;

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = embassy_rp::init(Default::default());
    info!("Hello World!");

    // add some delay to give an attached debug probe time to parse the
    // defmt RTT header. Reading that header might touch flash memory, which
    // interferes with flash write operations.
    // https://github.com/knurling-rs/defmt/pull/683
    Timer::after_millis(10).await;

    let mut flash = embassy_rp::flash::Flash::<_, Async, { 2 * 1024 * 1024 }>::new(p.FLASH, p.DMA_CH0);

    // Get JEDEC id
    #[cfg(feature = "rp2040")]
    {
        let jedec = defmt::unwrap!(flash.blocking_jedec_id());
        info!("jedec id: 0x{:x}", jedec);
    }

    // Get unique id
    #[cfg(feature = "rp2040")]
    {
        let mut uid = [0; 8];
        defmt::unwrap!(flash.blocking_unique_id(&mut uid));
        info!("unique id: {:?}", uid);
    }

    let mut buf = [0u8; ERASE_SIZE];
    defmt::unwrap!(flash.blocking_read(ADDR_OFFSET, &mut buf));

    info!("Addr of flash block is {:x}", ADDR_OFFSET + FLASH_BASE as u32);
    info!("Contents start with {=[u8]}", buf[0..4]);

    defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET, ADDR_OFFSET + ERASE_SIZE as u32));

    defmt::unwrap!(flash.blocking_read(ADDR_OFFSET, &mut buf));
    info!("Contents after erase starts with {=[u8]}", buf[0..4]);
    if buf.iter().any(|x| *x != 0xFF) {
        defmt::panic!("unexpected");
    }

    for b in buf.iter_mut() {
        *b = 0xDA;
    }

    defmt::unwrap!(flash.blocking_write(ADDR_OFFSET, &mut buf));

    defmt::unwrap!(flash.blocking_read(ADDR_OFFSET, &mut buf));
    info!("Contents after write starts with {=[u8]}", buf[0..4]);
    if buf.iter().any(|x| *x != 0xDA) {
        defmt::panic!("unexpected");
    }

    let mut buf = [0u32; ERASE_SIZE / 4];

    defmt::unwrap!(flash.background_read(ADDR_OFFSET, &mut buf)).await;
    info!("Contents after write starts with {=u32:x}", buf[0]);
    if buf.iter().any(|x| *x != 0xDADADADA) {
        defmt::panic!("unexpected");
    }

    info!("Test OK");
    cortex_m::asm::bkpt();
}