Skip to content

Commit c101501

Browse files
authored
Unbreak S2 chip erase, modernize (#20)
1 parent b7d9dbc commit c101501

File tree

3 files changed

+62
-126
lines changed

3 files changed

+62
-126
lines changed

ld/esp32s2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ PROVIDE ( esp_rom_spiflash_write_encrypted_enable = SPI_Write_Encrypt_Enable );
100100
PROVIDE ( esp_rom_spiflash_config_clk = SPIClkConfig );
101101
PROVIDE ( esp_rom_spiflash_select_qio_pins = SelectSpiQIO );
102102
PROVIDE ( esp_rom_spiflash_unlock = SPIUnlock );
103+
PROVIDE ( esp_rom_spiflash_erase_chip = SPIEraseChip );
103104
PROVIDE ( esp_rom_spiflash_erase_sector = SPIEraseSector );
104105
PROVIDE ( esp_rom_spiflash_erase_block = SPIEraseBlock );
105106
PROVIDE ( esp_rom_spiflash_wait_idle = SPI_Wait_Idle );

src/api_xtensa.rs

Lines changed: 61 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::arch::asm;
1+
use core::arch::global_asm;
22

33
// Probe-rs doesn't know how to call a function on the Xtensa architecture. Due to the windowed
44
// ABI, just jumping to the function address won't work. Instead, we need to use a call<N>
@@ -19,118 +19,69 @@ static STACK_PTR: u32 = 0x3FFD_F000;
1919
// End of SRAM1 - DATA_CACHE_SIZE
2020
static STACK_PTR: u32 = 0x3FCD_0000;
2121

22-
/// Setup the device for the flashing process.
23-
#[no_mangle]
24-
#[naked]
25-
pub unsafe extern "C" fn Init(adr: u32, clk: u32, fnc: u32) -> i32 {
26-
asm!(
27-
"
28-
.global Init_impl
29-
l32r a1, STACK_PTR
30-
mov.n a6, a2
31-
mov.n a7, a3
32-
mov.n a8, a4
33-
call4 Init_impl
34-
mov.n a2, a6
35-
",
36-
options(noreturn)
37-
)
38-
}
22+
global_asm!(
23+
"
24+
Init:
25+
.global Init_impl
26+
l32r a1, STACK_PTR
27+
mov.n a6, a2
28+
mov.n a7, a3
29+
mov.n a8, a4
30+
call4 Init_impl
31+
mov.n a2, a6
32+
break 1, 15
3933
40-
/// Erase the sector at the given address in flash
41-
///
42-
/// Returns 0 on success, 1 on failure.
43-
#[no_mangle]
44-
#[naked]
45-
pub unsafe extern "C" fn EraseSector(adr: u32) -> i32 {
46-
asm!(
47-
"
48-
.global EraseSector_impl
49-
l32r a1, STACK_PTR
50-
mov.n a6, a2
51-
call4 EraseSector_impl
52-
mov.n a2, a6
53-
",
54-
options(noreturn)
55-
)
56-
}
34+
EraseSector:
35+
.global EraseSector_impl
36+
l32r a1, STACK_PTR
37+
mov.n a6, a2
38+
call4 EraseSector_impl
39+
mov.n a2, a6
40+
break 1, 15
5741
58-
#[no_mangle]
59-
#[naked]
60-
pub unsafe extern "C" fn EraseChip() -> i32 {
61-
asm!(
62-
"
63-
.global EraseChip_impl
64-
l32r a1, STACK_PTR
65-
call4 EraseChip_impl
66-
mov.n a2, a6
67-
",
68-
options(noreturn)
69-
)
70-
}
42+
EraseChip:
43+
.global EraseChip_impl
44+
l32r a1, STACK_PTR
45+
call4 EraseChip_impl
46+
mov.n a2, a6
47+
break 1, 15
7148
72-
#[no_mangle]
73-
#[naked]
74-
pub unsafe extern "C" fn ProgramPage(adr: u32, sz: u32, buf: *const u8) -> i32 {
75-
asm!(
76-
"
77-
.global ProgramPage_impl
78-
l32r a1, STACK_PTR
79-
mov.n a6, a2
80-
mov.n a7, a3
81-
mov.n a8, a4
82-
call4 ProgramPage_impl
83-
mov.n a2, a6
84-
",
85-
options(noreturn)
86-
)
87-
}
49+
ProgramPage:
50+
.global ProgramPage_impl
51+
l32r a1, STACK_PTR
52+
mov.n a6, a2
53+
mov.n a7, a3
54+
mov.n a8, a4
55+
call4 ProgramPage_impl
56+
mov.n a2, a6
57+
break 1, 15
8858
89-
#[no_mangle]
90-
#[naked]
91-
pub unsafe extern "C" fn Verify(adr: u32, sz: u32, buf: *const u8) -> i32 {
92-
asm!(
93-
"
94-
.global Verify_impl
95-
l32r a1, STACK_PTR
96-
mov.n a6, a2
97-
mov.n a7, a3
98-
mov.n a8, a4
99-
call4 Verify_impl
100-
mov.n a2, a6
101-
",
102-
options(noreturn)
103-
)
104-
}
59+
Verify:
60+
.global Verify_impl
61+
l32r a1, STACK_PTR
62+
mov.n a6, a2
63+
mov.n a7, a3
64+
mov.n a8, a4
65+
call4 Verify_impl
66+
mov.n a2, a6
67+
break 1, 15
10568
106-
#[no_mangle]
107-
#[naked]
108-
pub unsafe extern "C" fn ReadFlash(adr: u32, sz: u32, buf: *mut u8) -> i32 {
109-
asm!(
110-
"
111-
.global ReadFlash_impl
112-
l32r a1, STACK_PTR
113-
mov.n a6, a2
114-
mov.n a7, a3
115-
mov.n a8, a4
116-
call4 ReadFlash_impl
117-
mov.n a2, a6
118-
",
119-
options(noreturn)
120-
)
121-
}
69+
ReadFlash:
70+
.global ReadFlash_impl
71+
l32r a1, STACK_PTR
72+
mov.n a6, a2
73+
mov.n a7, a3
74+
mov.n a8, a4
75+
call4 ReadFlash_impl
76+
mov.n a2, a6
77+
break 1, 15
12278
123-
#[no_mangle]
124-
#[naked]
125-
pub unsafe extern "C" fn UnInit(fnc: u32) -> i32 {
126-
asm!(
127-
"
128-
.global UnInit_impl
129-
l32r a1, STACK_PTR
130-
mov.n a6, a2
131-
call4 UnInit_impl
132-
mov.n a2, a6
133-
",
134-
options(noreturn)
135-
)
136-
}
79+
UnInit:
80+
.global UnInit_impl
81+
l32r a1, STACK_PTR
82+
mov.n a6, a2
83+
call4 UnInit_impl
84+
mov.n a2, a6
85+
break 1, 15
86+
"
87+
);

src/flash.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ extern "C" {
1313
// fn esp_rom_spiflash_read_statushigh(/* esp_rom_spiflash_chip_t *spi ,*/ status: *mut u32);
1414
// fn esp_rom_spiflash_write_status(/* esp_rom_spiflash_chip_t *spi ,*/ status: *mut u32);
1515

16-
#[cfg(not(feature = "esp32s2"))]
1716
fn esp_rom_spiflash_erase_chip() -> i32;
1817
fn esp_rom_spiflash_erase_block(block_number: u32) -> i32;
1918
// fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32;
@@ -116,18 +115,3 @@ pub fn wait_for_idle() -> i32 {
116115

117116
0
118117
}
119-
120-
#[cfg(feature = "esp32s2")]
121-
unsafe fn esp_rom_spiflash_erase_chip() -> i32 {
122-
let res = wait_for_idle();
123-
if res < 0 {
124-
return res;
125-
}
126-
127-
let cmd_reg: *mut u32 = core::mem::transmute(0x3f40_2000);
128-
129-
cmd_reg.write_volatile(1 << 22);
130-
while cmd_reg.read_volatile() != 0 {}
131-
132-
0
133-
}

0 commit comments

Comments
 (0)