Skip to content

Commit 08d406e

Browse files
authored
storage: Clean up ROM function declarations (#2058)
* Clean up external function declarations * Tweak syntax
1 parent c21287a commit 08d406e

File tree

11 files changed

+144
-241
lines changed

11 files changed

+144
-241
lines changed

esp-storage/src/common.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl FlashStorage {
117117
offset: u32,
118118
bytes: &mut [u8],
119119
) -> Result<(), FlashStorageError> {
120-
check_rc(chip_specific::esp_rom_spiflash_read(
120+
check_rc(chip_specific::spiflash_read(
121121
offset,
122122
bytes.as_ptr() as *mut u32,
123123
bytes.len() as u32,
@@ -127,7 +127,7 @@ impl FlashStorage {
127127
#[inline(always)]
128128
fn unlock_once(&mut self) -> Result<(), FlashStorageError> {
129129
if !self.unlocked {
130-
if chip_specific::esp_rom_spiflash_unlock() != 0 {
130+
if chip_specific::spiflash_unlock() != 0 {
131131
return Err(FlashStorageError::CantUnlock);
132132
}
133133
self.unlocked = true;
@@ -140,7 +140,7 @@ impl FlashStorage {
140140
pub(crate) fn internal_erase(&mut self, sector: u32) -> Result<(), FlashStorageError> {
141141
self.unlock_once()?;
142142

143-
check_rc(chip_specific::esp_rom_spiflash_erase_sector(sector))
143+
check_rc(chip_specific::spiflash_erase_sector(sector))
144144
}
145145

146146
#[inline(never)]
@@ -152,7 +152,7 @@ impl FlashStorage {
152152
) -> Result<(), FlashStorageError> {
153153
self.unlock_once()?;
154154

155-
check_rc(chip_specific::esp_rom_spiflash_write(
155+
check_rc(chip_specific::spiflash_write(
156156
offset,
157157
bytes.as_ptr() as *const u32,
158158
bytes.len() as u32,

esp-storage/src/esp32.rs

Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
use crate::maybe_with_critical_section;
22

3-
const ESP_ROM_SPIFLASH_READ: u32 = 0x40062ed8;
4-
const ESP_ROM_SPIFLASH_ERASE_SECTOR: u32 = 0x40062ccc;
5-
const SPI_READ_STATUS_HIGH: u32 = 0x40062448;
6-
const SPI_READ_STATUS: u32 = 0x4006226c;
7-
const SPI_WRITE_STATUS: u32 = 0x400622f0;
8-
9-
const CACHE_FLUSH_ROM: u32 = 0x40009a14;
10-
const CACHE_READ_ENABLE_ROM: u32 = 0x40009a84;
11-
123
const SPI_BASE_REG: u32 = 0x3ff42000; // SPI peripheral 1, used for SPI flash
134
const SPI0_BASE_REG: u32 = 0x3ff43000; // SPI peripheral 0, inner state machine
145
const SPI_EXT2_REG: u32 = SPI_BASE_REG + 0xF8;
@@ -34,23 +25,17 @@ const SPI_WRSR_2B: u32 = 1 << 22;
3425
const FLASH_CHIP_ADDR: u32 = 0x3ffae270;
3526
const FLASH_DUMMY_LEN_PLUS_ADDR: u32 = 0x3ffae290;
3627

37-
#[inline(always)]
38-
#[link_section = ".rwtext"]
39-
pub(crate) fn cache_flush_rom(cpu_num: u32) {
40-
unsafe {
41-
let cache_flush_rom: unsafe extern "C" fn(u32) = core::mem::transmute(CACHE_FLUSH_ROM);
42-
cache_flush_rom(cpu_num)
43-
}
44-
}
45-
46-
#[inline(always)]
47-
#[link_section = ".rwtext"]
48-
pub(crate) fn cache_read_enable_rom(cpu_num: u32) {
49-
unsafe {
50-
let cache_read_enable_rom: unsafe extern "C" fn(u32) =
51-
core::mem::transmute(CACHE_READ_ENABLE_ROM);
52-
cache_read_enable_rom(cpu_num)
53-
}
28+
crate::rom_fn! {
29+
fn esp_rom_cache_flush(cpu_num: u32) = 0x40009a14;
30+
fn esp_rom_cache_read_enable(cpu_num: u32) = 0x40009a84;
31+
fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 = 0x40062ed8;
32+
fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 = 0x40062ccc;
33+
fn esp_rom_spi_read_status_high(
34+
flash_chip: *const EspRomSpiflashChipT,
35+
status: *mut u32
36+
) -> i32 = 0x40062448;
37+
fn esp_rom_spi_read_status(flash_chip: *const EspRomSpiflashChipT, status: *mut u32) -> i32 = 0x4006226c;
38+
fn esp_rom_spi_write_status(flash_chip: *const EspRomSpiflashChipT, status_value: u32) -> i32 = 0x400622f0;
5439
}
5540

5641
#[inline(always)]
@@ -59,33 +44,19 @@ pub(crate) fn spi_read_status_high(
5944
flash_chip: *const EspRomSpiflashChipT,
6045
status: &mut u32,
6146
) -> i32 {
62-
unsafe {
63-
let spi_read_status_high: unsafe extern "C" fn(
64-
*const EspRomSpiflashChipT,
65-
*mut u32,
66-
) -> i32 = core::mem::transmute(SPI_READ_STATUS_HIGH);
67-
spi_read_status_high(flash_chip, status as *mut u32)
68-
}
47+
esp_rom_spi_read_status_high(flash_chip, status as *mut u32)
6948
}
7049

7150
#[inline(always)]
7251
#[link_section = ".rwtext"]
7352
pub(crate) fn spi_read_status(flash_chip: *const EspRomSpiflashChipT, status: &mut u32) -> i32 {
74-
unsafe {
75-
let spi_read_status: unsafe extern "C" fn(*const EspRomSpiflashChipT, *mut u32) -> i32 =
76-
core::mem::transmute(SPI_READ_STATUS);
77-
spi_read_status(flash_chip, status as *mut u32)
78-
}
53+
esp_rom_spi_read_status(flash_chip, status as *mut u32)
7954
}
8055

8156
#[inline(always)]
8257
#[link_section = ".rwtext"]
8358
pub(crate) fn spi_write_status(flash_chip: *const EspRomSpiflashChipT, status_value: u32) -> i32 {
84-
unsafe {
85-
let spi_write_status: unsafe extern "C" fn(*const EspRomSpiflashChipT, u32) -> i32 =
86-
core::mem::transmute(SPI_WRITE_STATUS);
87-
spi_write_status(flash_chip, status_value)
88-
}
59+
esp_rom_spi_write_status(flash_chip, status_value)
8960
}
9061

9162
#[inline(always)]
@@ -98,10 +69,10 @@ fn begin() {
9869
#[inline(always)]
9970
#[link_section = ".rwtext"]
10071
fn end() {
101-
cache_flush_rom(0);
102-
cache_flush_rom(1);
103-
cache_read_enable_rom(0);
104-
cache_read_enable_rom(1);
72+
esp_rom_cache_flush(0);
73+
esp_rom_cache_flush(1);
74+
esp_rom_cache_read_enable(0);
75+
esp_rom_cache_read_enable(1);
10576
}
10677

10778
#[derive(Debug)]
@@ -117,26 +88,18 @@ pub struct EspRomSpiflashChipT {
11788

11889
#[inline(never)]
11990
#[link_section = ".rwtext"]
120-
pub(crate) fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
91+
pub(crate) fn spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
12192
maybe_with_critical_section(|| {
12293
spiflash_wait_for_ready();
123-
unsafe {
124-
let esp_rom_spiflash_read: unsafe extern "C" fn(u32, *const u32, u32) -> i32 =
125-
core::mem::transmute(ESP_ROM_SPIFLASH_READ);
126-
esp_rom_spiflash_read(src_addr, data, len)
127-
}
94+
esp_rom_spiflash_read(src_addr, data, len)
12895
})
12996
}
13097

13198
#[inline(never)]
13299
#[link_section = ".rwtext"]
133-
pub(crate) fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 {
100+
pub(crate) fn spiflash_erase_sector(sector_number: u32) -> i32 {
134101
maybe_with_critical_section(|| {
135-
let res = unsafe {
136-
let esp_rom_spiflash_erase_sector: unsafe extern "C" fn(u32) -> i32 =
137-
core::mem::transmute(ESP_ROM_SPIFLASH_ERASE_SECTOR);
138-
esp_rom_spiflash_erase_sector(sector_number)
139-
};
102+
let res = esp_rom_spiflash_erase_sector(sector_number);
140103
spiflash_wait_for_ready();
141104
res
142105
})
@@ -154,7 +117,7 @@ fn spi_write_enable() {
154117

155118
#[inline(never)]
156119
#[link_section = ".rwtext"]
157-
pub(crate) fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
120+
pub(crate) fn spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
158121
maybe_with_critical_section(|| {
159122
begin();
160123

@@ -250,7 +213,7 @@ fn spiflash_wait_for_ready() {
250213

251214
#[inline(never)]
252215
#[link_section = ".rwtext"]
253-
pub(crate) fn esp_rom_spiflash_unlock() -> i32 {
216+
pub(crate) fn spiflash_unlock() -> i32 {
254217
let flashchip = FLASH_CHIP_ADDR as *const EspRomSpiflashChipT;
255218
if unsafe { (*flashchip).device_id } >> 16 & 0xff == 0x9D {
256219
panic!("ISSI flash is not supported");

esp-storage/src/esp32c2.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
use crate::maybe_with_critical_section;
22

3-
const ESP_ROM_SPIFLASH_READ: u32 = 0x4000013c;
4-
const ESP_ROM_SPIFLASH_UNLOCK: u32 = 0x40000140;
5-
const ESP_ROM_SPIFLASH_ERASE_SECTOR: u32 = 0x40000130;
6-
const ESP_ROM_SPIFLASH_WRITE: u32 = 0x40000138;
3+
crate::rom_fn! {
4+
fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 = 0x4000013c;
5+
fn esp_rom_spiflash_unlock() -> i32 = 0x40000140;
6+
fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 = 0x40000130;
7+
fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 = 0x40000138;
8+
}
79

8-
pub(crate) fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
9-
maybe_with_critical_section(|| unsafe {
10-
let esp_rom_spiflash_read: unsafe extern "C" fn(u32, *const u32, u32) -> i32 =
11-
core::mem::transmute(ESP_ROM_SPIFLASH_READ);
12-
esp_rom_spiflash_read(src_addr, data, len)
13-
})
10+
pub(crate) fn spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
11+
maybe_with_critical_section(|| esp_rom_spiflash_read(src_addr, data, len))
1412
}
1513

16-
pub(crate) fn esp_rom_spiflash_unlock() -> i32 {
17-
maybe_with_critical_section(|| unsafe {
18-
let esp_rom_spiflash_unlock: unsafe extern "C" fn() -> i32 =
19-
core::mem::transmute(ESP_ROM_SPIFLASH_UNLOCK);
20-
esp_rom_spiflash_unlock()
21-
})
14+
pub(crate) fn spiflash_unlock() -> i32 {
15+
maybe_with_critical_section(esp_rom_spiflash_unlock)
2216
}
2317

24-
pub(crate) fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 {
25-
maybe_with_critical_section(|| unsafe {
26-
let esp_rom_spiflash_erase_sector: unsafe extern "C" fn(u32) -> i32 =
27-
core::mem::transmute(ESP_ROM_SPIFLASH_ERASE_SECTOR);
28-
esp_rom_spiflash_erase_sector(sector_number)
29-
})
18+
pub(crate) fn spiflash_erase_sector(sector_number: u32) -> i32 {
19+
maybe_with_critical_section(|| esp_rom_spiflash_erase_sector(sector_number))
3020
}
3121

32-
pub(crate) fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
33-
maybe_with_critical_section(|| unsafe {
34-
let esp_rom_spiflash_write: unsafe extern "C" fn(u32, *const u32, u32) -> i32 =
35-
core::mem::transmute(ESP_ROM_SPIFLASH_WRITE);
36-
esp_rom_spiflash_write(dest_addr, data, len)
37-
})
22+
pub(crate) fn spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
23+
maybe_with_critical_section(|| esp_rom_spiflash_write(dest_addr, data, len))
3824
}

esp-storage/src/esp32c3.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
use crate::maybe_with_critical_section;
22

3-
const ESP_ROM_SPIFLASH_READ: u32 = 0x40000130;
4-
const ESP_ROM_SPIFLASH_UNLOCK: u32 = 0x40000140;
5-
const ESP_ROM_SPIFLASH_ERASE_SECTOR: u32 = 0x40000128;
6-
const ESP_ROM_SPIFLASH_WRITE: u32 = 0x4000012c;
3+
crate::rom_fn! {
4+
fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 = 0x40000130;
5+
fn esp_rom_spiflash_unlock() -> i32 = 0x40000140;
6+
fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 = 0x40000128;
7+
fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 = 0x4000012c;
8+
}
79

8-
pub(crate) fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
9-
maybe_with_critical_section(|| unsafe {
10-
let esp_rom_spiflash_read: unsafe extern "C" fn(u32, *const u32, u32) -> i32 =
11-
core::mem::transmute(ESP_ROM_SPIFLASH_READ);
12-
esp_rom_spiflash_read(src_addr, data, len)
13-
})
10+
pub(crate) fn spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
11+
maybe_with_critical_section(|| esp_rom_spiflash_read(src_addr, data, len))
1412
}
1513

16-
pub(crate) fn esp_rom_spiflash_unlock() -> i32 {
17-
maybe_with_critical_section(|| unsafe {
18-
let esp_rom_spiflash_unlock: unsafe extern "C" fn() -> i32 =
19-
core::mem::transmute(ESP_ROM_SPIFLASH_UNLOCK);
20-
esp_rom_spiflash_unlock()
21-
})
14+
pub(crate) fn spiflash_unlock() -> i32 {
15+
maybe_with_critical_section(esp_rom_spiflash_unlock)
2216
}
2317

24-
pub(crate) fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 {
25-
maybe_with_critical_section(|| unsafe {
26-
let esp_rom_spiflash_erase_sector: unsafe extern "C" fn(u32) -> i32 =
27-
core::mem::transmute(ESP_ROM_SPIFLASH_ERASE_SECTOR);
28-
esp_rom_spiflash_erase_sector(sector_number)
29-
})
18+
pub(crate) fn spiflash_erase_sector(sector_number: u32) -> i32 {
19+
maybe_with_critical_section(|| esp_rom_spiflash_erase_sector(sector_number))
3020
}
3121

32-
pub(crate) fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
33-
maybe_with_critical_section(|| unsafe {
34-
let esp_rom_spiflash_write: unsafe extern "C" fn(u32, *const u32, u32) -> i32 =
35-
core::mem::transmute(ESP_ROM_SPIFLASH_WRITE);
36-
esp_rom_spiflash_write(dest_addr, data, len)
37-
})
22+
pub(crate) fn spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
23+
maybe_with_critical_section(|| esp_rom_spiflash_write(dest_addr, data, len))
3824
}

esp-storage/src/esp32c6.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
use crate::maybe_with_critical_section;
22

3-
const ESP_ROM_SPIFLASH_READ: u32 = 0x40000150;
4-
const ESP_ROM_SPIFLASH_UNLOCK: u32 = 0x40000154;
5-
const ESP_ROM_SPIFLASH_ERASE_SECTOR: u32 = 0x40000144;
6-
const ESP_ROM_SPIFLASH_WRITE: u32 = 0x4000014c;
3+
crate::rom_fn! {
4+
fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 = 0x40000150;
5+
fn esp_rom_spiflash_unlock() -> i32 = 0x40000154;
6+
fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 = 0x40000144;
7+
fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 = 0x4000014c;
8+
}
79

8-
pub(crate) fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
9-
maybe_with_critical_section(|| unsafe {
10-
let esp_rom_spiflash_read: unsafe extern "C" fn(u32, *const u32, u32) -> i32 =
11-
core::mem::transmute(ESP_ROM_SPIFLASH_READ);
12-
esp_rom_spiflash_read(src_addr, data, len)
13-
})
10+
pub(crate) fn spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
11+
maybe_with_critical_section(|| esp_rom_spiflash_read(src_addr, data, len))
1412
}
1513

16-
pub(crate) fn esp_rom_spiflash_unlock() -> i32 {
17-
maybe_with_critical_section(|| unsafe {
18-
let esp_rom_spiflash_unlock: unsafe extern "C" fn() -> i32 =
19-
core::mem::transmute(ESP_ROM_SPIFLASH_UNLOCK);
20-
esp_rom_spiflash_unlock()
21-
})
14+
pub(crate) fn spiflash_unlock() -> i32 {
15+
maybe_with_critical_section(esp_rom_spiflash_unlock)
2216
}
2317

24-
pub(crate) fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 {
25-
maybe_with_critical_section(|| unsafe {
26-
let esp_rom_spiflash_erase_sector: unsafe extern "C" fn(u32) -> i32 =
27-
core::mem::transmute(ESP_ROM_SPIFLASH_ERASE_SECTOR);
28-
esp_rom_spiflash_erase_sector(sector_number)
29-
})
18+
pub(crate) fn spiflash_erase_sector(sector_number: u32) -> i32 {
19+
maybe_with_critical_section(|| esp_rom_spiflash_erase_sector(sector_number))
3020
}
3121

32-
pub(crate) fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
33-
maybe_with_critical_section(|| unsafe {
34-
let esp_rom_spiflash_write: unsafe extern "C" fn(u32, *const u32, u32) -> i32 =
35-
core::mem::transmute(ESP_ROM_SPIFLASH_WRITE);
36-
esp_rom_spiflash_write(dest_addr, data, len)
37-
})
22+
pub(crate) fn spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
23+
maybe_with_critical_section(|| esp_rom_spiflash_write(dest_addr, data, len))
3824
}

esp-storage/src/esp32h2.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
use crate::maybe_with_critical_section;
22

3-
const ESP_ROM_SPIFLASH_READ: u32 = 0x4000012c;
4-
const ESP_ROM_SPIFLASH_UNLOCK: u32 = 0x40000130;
5-
const ESP_ROM_SPIFLASH_ERASE_SECTOR: u32 = 0x40000120;
6-
const ESP_ROM_SPIFLASH_WRITE: u32 = 0x40000128;
3+
crate::rom_fn! {
4+
fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 = 0x4000012c;
5+
fn esp_rom_spiflash_unlock() -> i32 = 0x40000130;
6+
fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 = 0x40000120;
7+
fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 = 0x40000128;
8+
}
79

8-
pub(crate) fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
9-
maybe_with_critical_section(|| unsafe {
10-
let esp_rom_spiflash_read: unsafe extern "C" fn(u32, *const u32, u32) -> i32 =
11-
core::mem::transmute(ESP_ROM_SPIFLASH_READ);
12-
esp_rom_spiflash_read(src_addr, data, len)
13-
})
10+
pub(crate) fn spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32 {
11+
maybe_with_critical_section(|| esp_rom_spiflash_read(src_addr, data, len))
1412
}
1513

16-
pub(crate) fn esp_rom_spiflash_unlock() -> i32 {
17-
maybe_with_critical_section(|| unsafe {
18-
let esp_rom_spiflash_unlock: unsafe extern "C" fn() -> i32 =
19-
core::mem::transmute(ESP_ROM_SPIFLASH_UNLOCK);
20-
esp_rom_spiflash_unlock()
21-
})
14+
pub(crate) fn spiflash_unlock() -> i32 {
15+
maybe_with_critical_section(esp_rom_spiflash_unlock)
2216
}
2317

24-
pub(crate) fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32 {
25-
maybe_with_critical_section(|| unsafe {
26-
let esp_rom_spiflash_erase_sector: unsafe extern "C" fn(u32) -> i32 =
27-
core::mem::transmute(ESP_ROM_SPIFLASH_ERASE_SECTOR);
28-
esp_rom_spiflash_erase_sector(sector_number)
29-
})
18+
pub(crate) fn spiflash_erase_sector(sector_number: u32) -> i32 {
19+
maybe_with_critical_section(|| esp_rom_spiflash_erase_sector(sector_number))
3020
}
3121

32-
pub(crate) fn esp_rom_spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
33-
maybe_with_critical_section(|| unsafe {
34-
let esp_rom_spiflash_write: unsafe extern "C" fn(u32, *const u32, u32) -> i32 =
35-
core::mem::transmute(ESP_ROM_SPIFLASH_WRITE);
36-
esp_rom_spiflash_write(dest_addr, data, len)
37-
})
22+
pub(crate) fn spiflash_write(dest_addr: u32, data: *const u32, len: u32) -> i32 {
23+
maybe_with_critical_section(|| esp_rom_spiflash_write(dest_addr, data, len))
3824
}

0 commit comments

Comments
 (0)