Skip to content

Commit 22b65fd

Browse files
authored
Inflate miniz-compressed stream (#11)
* Decompress a deflated stream using tinfl * Clean up * Add instruction to readme
1 parent 844a7aa commit 22b65fd

File tree

7 files changed

+486
-128
lines changed

7 files changed

+486
-128
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ $ target-gen elf target/riscv32imc-unknown-none-elf/release/esp-flashloader outp
6363
- Next `!Ram` block corresponds to data bus for internal SRAM, see Internal Memory Address Mapping of TRM
6464
- Next `!Nvm` corresponds to instruction bus for external memory, see External Memory Address Mapping of TRM
6565
- Next `!Nvm` corresponds to data bus for external memory, see External Memory Address Mapping of TRM
66-
3. Add `load_address` under `flash_algorithms` and assing the IRAM `ORIGIN` value (step 3).
66+
3. Add `load_address` under `flash_algorithms` and assign the IRAM `ORIGIN` value (step 3).
67+
4. Add `transfer_encoding: Miniz` under `load_address`
6768
9. Upstream the new updates to probe-rs.

ld/esp32c3.x

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,27 @@ PROVIDE( Enable_QMode = 0x40000228 );
8282
PROVIDE( ets_efuse_get_spiconfig = 0x4000071c );
8383

8484

85-
PROVIDE( uart_tx_one_char = 0x40000068 );
85+
PROVIDE( uart_tx_one_char = 0x40000068 );
86+
87+
/***************************************
88+
Group miniz
89+
***************************************/
90+
91+
/* Functions */
92+
mz_adler32 = 0x400000c0;
93+
mz_crc32 = 0x400000c4;
94+
mz_free = 0x400000c8;
95+
tdefl_compress = 0x400000cc;
96+
tdefl_compress_buffer = 0x400000d0;
97+
tdefl_compress_mem_to_heap = 0x400000d4;
98+
tdefl_compress_mem_to_mem = 0x400000d8;
99+
tdefl_compress_mem_to_output = 0x400000dc;
100+
tdefl_get_adler32 = 0x400000e0;
101+
tdefl_get_prev_return_status = 0x400000e4;
102+
tdefl_init = 0x400000e8;
103+
tdefl_write_image_to_png_file_in_memory = 0x400000ec;
104+
tdefl_write_image_to_png_file_in_memory_ex = 0x400000f0;
105+
tinfl_decompress = 0x400000f4;
106+
tinfl_decompress_mem_to_callback = 0x400000f8;
107+
tinfl_decompress_mem_to_heap = 0x400000fc;
108+
tinfl_decompress_mem_to_mem = 0x40000100;

ld/loader.x

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ SECTIONS {
2626

2727
*(.bss .bss.*)
2828
*(.sbss .sbss.*)
29-
29+
3030
. = ALIGN(4);
3131
} > IRAM
3232

3333
/* Description of the flash algorithm */
34-
DeviceData :
35-
{
34+
DeviceData : {
3635
/* The device data content is only for external tools,
3736
* and usually not referenced by the code.
3837
*

src/flash.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
extern "C" {
2+
3+
// fn esp_rom_spiflash_write_encrypted_enable();
4+
// fn esp_rom_spiflash_write_encrypted_disable();
5+
// fn esp_rom_spiflash_write_encrypted(addr: u32, data: *const u8, len: u32);
6+
// fn esp_rom_spiflash_config_param();
7+
// fn esp_rom_spiflash_select_qio_pins();
8+
// fn esp_rom_spi_flash_auto_sus_res();
9+
// fn esp_rom_spi_flash_send_resume();
10+
// fn esp_rom_spi_flash_update_id();
11+
// fn esp_rom_spiflash_config_clk();
12+
// fn esp_rom_spiflash_config_readmode();
13+
// fn esp_rom_spiflash_read_status(/* esp_rom_spiflash_chip_t *spi ,*/ status: *mut u32);
14+
// fn esp_rom_spiflash_read_statushigh(/* esp_rom_spiflash_chip_t *spi ,*/ status: *mut u32);
15+
// fn esp_rom_spiflash_write_status(/* esp_rom_spiflash_chip_t *spi ,*/ status: *mut u32);
16+
17+
fn esp_rom_spiflash_erase_chip() -> i32;
18+
fn esp_rom_spiflash_erase_block(block_number: u32) -> i32;
19+
// fn esp_rom_spiflash_erase_sector(sector_number: u32) -> i32;
20+
/// address (4 byte alignment), data, length
21+
fn esp_rom_spiflash_write(dest_addr: u32, data: *const u8, len: u32) -> i32;
22+
/// address (4 byte alignment), data, length
23+
// fn esp_rom_spiflash_read(src_addr: u32, data: *const u32, len: u32) -> i32;
24+
fn esp_rom_spiflash_read_user_cmd(status: *mut u32, cmd: u8) -> i32;
25+
// fn esp_rom_spiflash_unlock() -> i32;
26+
// fn esp_rom_spiflash_lock(); // can't find in idf defs?
27+
fn esp_rom_spiflash_attach(config: u32, legacy: bool);
28+
29+
#[cfg(feature = "esp32c3")]
30+
fn ets_efuse_get_spiconfig() -> u32;
31+
}
32+
33+
pub fn attach() {
34+
#[cfg(feature = "esp32c3")]
35+
let spiconfig: u32 = unsafe { ets_efuse_get_spiconfig() };
36+
#[cfg(any(feature = "esp32c2", feature = "esp32c6", feature = "esp32h2"))]
37+
let spiconfig: u32 = 0;
38+
39+
// TODO: raise CPU frequency
40+
41+
unsafe { esp_rom_spiflash_attach(spiconfig, false) };
42+
}
43+
44+
pub fn erase_block(adr: u32) -> i32 {
45+
crate::dprintln!("ERASE @ {}", adr);
46+
47+
unsafe { esp_rom_spiflash_erase_block(adr / crate::properties::FLASH_BLOCK_SIZE) }
48+
}
49+
50+
pub fn erase_chip() -> i32 {
51+
unsafe { esp_rom_spiflash_erase_chip() }
52+
}
53+
54+
pub fn write_flash(address: u32, data: &[u8]) -> i32 {
55+
if data.is_empty() {
56+
return 0;
57+
}
58+
let len = data.len() as u32;
59+
unsafe { esp_rom_spiflash_write(address, data.as_ptr(), len) }
60+
}
61+
62+
pub fn wait_for_idle() -> i32 {
63+
const SR_WIP: u32 = 1 << 0;
64+
65+
let mut status = SR_WIP;
66+
while status & SR_WIP != 0 {
67+
let res = unsafe { esp_rom_spiflash_read_user_cmd(&mut status, 0x05) };
68+
if res != 0 {
69+
return res;
70+
}
71+
}
72+
73+
0
74+
}

0 commit comments

Comments
 (0)