-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflash.rs
More file actions
120 lines (101 loc) · 3.48 KB
/
Copy pathflash.rs
File metadata and controls
120 lines (101 loc) · 3.48 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Derived from the official example:
https://github.com/esp-rs/esp-hal/blob/main/examples/peripheral/flash_read_write/src/main.rs
This example accesses the built-in flash storage
prints the capacity in bytes
and prints out the partition table.
*/
#![no_std]
#![no_main]
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
use embedded_storage::{ReadStorage, Storage};
use esp_backtrace as _;
use esp_bootloader_esp_idf::partitions;
use esp_hal::main;
use esp_println::println;
use esp_storage::FlashStorage;
// #[panic_handler]
// fn panic(_: &core::panic::PanicInfo) -> ! {
// loop {}
// }
//
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
#[main]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
let peripherals = esp_hal::init(esp_hal::Config::default());
let mut flash = FlashStorage::new(peripherals.FLASH);
println!("Flash size = {}", flash.capacity());
let mut pt_mem = [0u8; partitions::PARTITION_TABLE_MAX_LEN];
let pt = partitions::read_partition_table(&mut flash, &mut pt_mem).unwrap();
for i in 0..pt.len() {
let raw = pt.get_partition(i).unwrap();
println!("{:?}", raw);
}
println!();
// The app descriptor (if present) is contained in the first 256 bytes
// of an app image, right after the image header (24 bytes) and the first
// section header (8 bytes)
let mut app_desc = [0u8; 256];
pt.find_partition(partitions::PartitionType::App(
partitions::AppPartitionSubType::Factory,
))
.unwrap()
.unwrap()
.as_embedded_storage(&mut flash)
.read(32, &mut app_desc)
.unwrap();
println!("App descriptor dump {:02x?}", app_desc);
println!();
let nvs = pt
.find_partition(partitions::PartitionType::Data(
partitions::DataPartitionSubType::Nvs,
))
.unwrap()
.unwrap();
let mut nvs_partition = nvs.as_embedded_storage(&mut flash);
let mut bytes = [0u8; 32];
println!("NVS partition size = {}", nvs_partition.capacity());
println!();
let offset_in_nvs_partition = 0;
nvs_partition
.read(offset_in_nvs_partition, &mut bytes)
.unwrap();
println!(
"Read from {:x}: {:02x?}",
offset_in_nvs_partition,
&bytes[..32]
);
bytes[0x00] = bytes[0x00].wrapping_add(1);
bytes[0x01] = bytes[0x01].wrapping_add(2);
bytes[0x02] = bytes[0x02].wrapping_add(3);
bytes[0x03] = bytes[0x03].wrapping_add(4);
bytes[0x04] = bytes[0x04].wrapping_add(1);
bytes[0x05] = bytes[0x05].wrapping_add(2);
bytes[0x06] = bytes[0x06].wrapping_add(3);
bytes[0x07] = bytes[0x07].wrapping_add(4);
nvs_partition
.write(offset_in_nvs_partition, &bytes)
.unwrap();
println!(
"Written to {:x}: {:02x?}",
offset_in_nvs_partition,
&bytes[..32]
);
let mut reread_bytes = [0u8; 32];
nvs_partition.read(0, &mut reread_bytes).unwrap();
println!(
"Read from {:x}: {:02x?}",
offset_in_nvs_partition,
&reread_bytes[..32]
);
println!();
println!("Reset (CTRL-R in espflash) to re-read the persisted data.");
loop { }
}