Skip to content

Commit b7d9dbc

Browse files
authored
Merge pull request #19 from bugadani/init-fix
Set flash params on init
2 parents ff8db28 + e27ff59 commit b7d9dbc

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/flash.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ extern "C" {
33
// fn esp_rom_spiflash_write_encrypted_enable();
44
// fn esp_rom_spiflash_write_encrypted_disable();
55
// fn esp_rom_spiflash_write_encrypted(addr: u32, data: *const u8, len: u32);
6-
// fn esp_rom_spiflash_config_param();
76
// fn esp_rom_spiflash_select_qio_pins();
87
// fn esp_rom_spi_flash_auto_sus_res();
98
// fn esp_rom_spi_flash_send_resume();
@@ -27,6 +26,15 @@ extern "C" {
2726
// fn esp_rom_spiflash_lock(); // can't find in idf defs?
2827
fn esp_rom_spiflash_attach(config: u32, legacy: bool);
2928

29+
fn esp_rom_spiflash_config_param(
30+
device_id: u32,
31+
chip_size: u32,
32+
block_size: u32,
33+
sector_size: u32,
34+
page_size: u32,
35+
status_mask: u32,
36+
) -> u32;
37+
3038
#[cfg(any(
3139
feature = "esp32",
3240
feature = "esp32s2",
@@ -36,7 +44,7 @@ extern "C" {
3644
fn ets_efuse_get_spiconfig() -> u32;
3745
}
3846

39-
pub fn attach() {
47+
pub fn attach() -> i32 {
4048
#[cfg(any(
4149
feature = "esp32",
4250
feature = "esp32s2",
@@ -50,7 +58,23 @@ pub fn attach() {
5058

5159
// TODO: raise CPU frequency
5260

53-
unsafe { esp_rom_spiflash_attach(spiconfig, false) };
61+
let config_result = unsafe {
62+
esp_rom_spiflash_config_param(
63+
0,
64+
crate::properties::FLASH_SIZE, // total_size
65+
crate::properties::FLASH_BLOCK_SIZE, // block_size
66+
crate::properties::FLASH_SECTOR_SIZE, // sector_size
67+
crate::properties::PAGE_SIZE, // page_size
68+
crate::properties::FLASH_STATUS_MASK, // status_mask
69+
)
70+
};
71+
72+
if config_result == 0 {
73+
unsafe { esp_rom_spiflash_attach(spiconfig, false) };
74+
0
75+
} else {
76+
-1
77+
}
5478
}
5579

5680
pub fn erase_block(adr: u32) -> i32 {

src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,13 @@ fn is_inited() -> bool {
132132
pub unsafe extern "C" fn Init_impl(_adr: u32, _clk: u32, _fnc: u32) -> i32 {
133133
dprintln!("INIT");
134134

135-
flash::attach();
136-
137-
*DECOMPRESSOR = Decompressor::new();
138-
*INITED = INITED_MAGIC;
139-
140-
0
135+
if flash::attach() == 0 {
136+
*DECOMPRESSOR = Decompressor::new();
137+
*INITED = INITED_MAGIC;
138+
0
139+
} else {
140+
1
141+
}
141142
}
142143

143144
/// Erase the sector at the given address in flash

src/properties.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ pub const FLASH_BLOCK_SIZE: u32 = 65536;
1010
feature = "esp32h2"
1111
))]
1212
// Max of 16MB
13-
const FLASH_SIZE: u32 = 0x1000000;
13+
pub const FLASH_SIZE: u32 = 0x1000000;
1414

1515
#[cfg(any(feature = "esp32s2", feature = "esp32s3"))]
1616
// Max of 1GB
17-
const FLASH_SIZE: u32 = 0x40000000;
17+
pub const FLASH_SIZE: u32 = 0x40000000;
18+
19+
pub const FLASH_STATUS_MASK: u32 = 0xFFFF;
20+
pub const FLASH_SECTOR_SIZE: u32 = 4096;
1821

1922
#[allow(non_upper_case_globals)]
2023
#[no_mangle]

0 commit comments

Comments
 (0)