Skip to content

Commit 499d17a

Browse files
authored
Implement ReadFlash (#15)
1 parent 353abbe commit 499d17a

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

ld/loader.x

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SECTIONS {
77

88
/* The KEEP is necessary to ensure that the
99
* sections don't get garbage collected by the linker.
10-
*
10+
*
1111
* Because this is not a normal binary with an entry point,
1212
* the linker would just discard all the code without the
1313
* KEEP statement here.
@@ -43,5 +43,3 @@ SECTIONS {
4343
KEEP(*(DeviceData))
4444
} > IRAM
4545
}
46-
47-

src/api.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ pub unsafe extern "C" fn ProgramPage(adr: u32, sz: u32, buf: *const u8) -> i32 {
2222
crate::ProgramPage_impl(adr, sz, buf)
2323
}
2424

25+
#[no_mangle]
26+
pub unsafe extern "C" fn ReadFlash(adr: u32, sz: u32, buf: *mut u8) -> i32 {
27+
crate::ReadFlash_impl(adr, sz, buf)
28+
}
29+
2530
#[no_mangle]
2631
pub unsafe extern "C" fn UnInit(fnc: u32) -> i32 {
2732
crate::UnInit_impl(fnc)

src/api_xtensa.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ pub unsafe extern "C" fn ProgramPage(adr: u32, sz: u32, buf: *const u8) -> i32 {
8686
)
8787
}
8888

89+
#[no_mangle]
90+
#[naked]
91+
pub unsafe extern "C" fn ReadFlash(adr: u32, sz: u32, buf: *mut u8) -> i32 {
92+
asm!(
93+
"
94+
.global ReadFlash_impl
95+
l32r a1, STACK_PTR
96+
mov.n a6, a2
97+
mov.n a7, a3
98+
mov.n a8, a4
99+
call4 ReadFlash_impl
100+
mov.n a2, a6
101+
",
102+
options(noreturn)
103+
)
104+
}
105+
89106
#[no_mangle]
90107
#[naked]
91108
pub unsafe extern "C" fn UnInit(fnc: u32) -> i32 {

src/flash.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C" {
2121
/// address (4 byte alignment), data, length
2222
fn esp_rom_spiflash_write(dest_addr: u32, data: *const u8, len: u32) -> i32;
2323
/// address (4 byte alignment), data, length
24-
// fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32;
24+
fn esp_rom_spiflash_read(src_addr: u32, data: *mut u8, len: u32) -> i32;
2525
fn esp_rom_spiflash_read_user_cmd(status: *mut u32, cmd: u8) -> i32;
2626
// fn esp_rom_spiflash_unlock() -> i32;
2727
// fn esp_rom_spiflash_lock(); // can't find in idf defs?
@@ -71,6 +71,14 @@ pub fn write_flash(address: u32, data: &[u8]) -> i32 {
7171
unsafe { esp_rom_spiflash_write(address, data.as_ptr(), len) }
7272
}
7373

74+
pub fn read_flash(address: u32, data: &mut [u8]) -> i32 {
75+
if data.is_empty() {
76+
return 0;
77+
}
78+
let len = data.len() as u32;
79+
unsafe { esp_rom_spiflash_read(address, data.as_mut_ptr(), len) }
80+
}
81+
7482
pub fn wait_for_idle() -> i32 {
7583
const SR_WIP: u32 = 1 << 0;
7684

src/main.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@ pub unsafe extern "C" fn ProgramPage_impl(adr: u32, sz: u32, buf: *const u8) ->
177177
(*DECOMPRESSOR).program(adr, input)
178178
}
179179

180+
#[no_mangle]
181+
pub unsafe extern "C" fn ReadFlash_impl(adr: u32, sz: u32, buf: *mut u8) -> i32 {
182+
if !is_inited() {
183+
return ERROR_BASE_INTERNAL - 1;
184+
};
185+
186+
if (buf as u32) % 4 != 0 {
187+
dprintln!("ERROR buf not word aligned");
188+
return ERROR_BASE_INTERNAL - 5;
189+
}
190+
191+
dprintln!("READ FLASH {} bytes @ {}", sz, adr);
192+
193+
let buffer = core::slice::from_raw_parts_mut(buf, sz as usize);
194+
crate::flash::read_flash(adr, buffer)
195+
}
196+
180197
#[no_mangle]
181198
pub unsafe extern "C" fn UnInit_impl(_fnc: u32) -> i32 {
182199
if !is_inited() {

0 commit comments

Comments
 (0)