99
1010use panic_never as _;
1111
12- const FLASH_SECTOR_SIZE : u32 = 4096 ;
12+ const FLASH_BLOCK_SIZE : u32 = 65536 ;
1313
1414#[ cfg( not( any( target_arch = "xtensa" , target_arch = "riscv32" ) ) ) ]
1515compile_error ! ( "specify the target with `--target`" ) ;
@@ -54,12 +54,10 @@ macro_rules! dprintln {
5454#[ allow( unused) ]
5555extern "C" {
5656
57- // fn esp_rom_spiflash_wait_idle(/* esp_rom_spiflash_chip_t *spi */);
5857 // fn esp_rom_spiflash_write_encrypted_enable();
5958 // fn esp_rom_spiflash_write_encrypted_disable();
6059 // fn esp_rom_spiflash_write_encrypted(addr: u32, data: *const u8, len: u32);
6160 // fn esp_rom_spiflash_config_param();
62- // fn esp_rom_spiflash_read_user_cmd();
6361 // fn esp_rom_spiflash_select_qio_pins();
6462 // fn esp_rom_spi_flash_auto_sus_res();
6563 // fn esp_rom_spi_flash_send_resume();
@@ -77,6 +75,7 @@ extern "C" {
7775 fn esp_rom_spiflash_write ( dest_addr : u32 , data : * const u8 , len : u32 ) -> i32 ;
7876 /// address (4 byte alignment), data, length
7977 fn esp_rom_spiflash_read ( src_addr : u32 , data : * const u32 , len : u32 ) -> i32 ;
78+ fn esp_rom_spiflash_read_user_cmd ( status : * mut u32 , cmd : u8 ) -> i32 ;
8079 fn esp_rom_spiflash_unlock ( ) -> i32 ;
8180 // fn esp_rom_spiflash_lock(); // can't find in idf defs?
8281 fn esp_rom_spiflash_attach ( config : u32 , legacy : bool ) ;
@@ -87,6 +86,20 @@ extern "C" {
8786
8887}
8988
89+ unsafe fn wait_for_idle ( ) -> i32 {
90+ const SR_WIP : u32 = 1 << 0 ;
91+
92+ let mut status = SR_WIP ;
93+ while status & SR_WIP != 0 {
94+ let res = esp_rom_spiflash_read_user_cmd ( & mut status, 0x05 ) ;
95+ if res != 0 {
96+ return res;
97+ }
98+ }
99+
100+ 0
101+ }
102+
90103/// Setup the device for the
91104#[ no_mangle]
92105#[ inline( never) ]
@@ -103,11 +116,6 @@ pub unsafe extern "C" fn Init(_adr: u32, _clk: u32, _fnc: u32) -> i32 {
103116
104117 esp_rom_spiflash_attach ( spiconfig, false ) ;
105118
106- let res = esp_rom_spiflash_unlock ( ) ;
107- if res != 0 {
108- return res;
109- }
110-
111119 INITD = true ;
112120 }
113121
@@ -121,12 +129,7 @@ pub unsafe extern "C" fn Init(_adr: u32, _clk: u32, _fnc: u32) -> i32 {
121129#[ inline( never) ]
122130pub unsafe extern "C" fn EraseSector ( adr : u32 ) -> i32 {
123131 dprintln ! ( "ERASE @ {}" , adr) ;
124- let res = esp_rom_spiflash_erase_sector ( adr / FLASH_SECTOR_SIZE ) ;
125- if res != 0 {
126- return res;
127- }
128-
129- 0
132+ esp_rom_spiflash_erase_block ( adr / FLASH_BLOCK_SIZE )
130133}
131134
132135#[ no_mangle]
@@ -146,18 +149,13 @@ pub unsafe extern "C" fn ProgramPage(adr: u32, sz: u32, buf: *const u8) -> i32 {
146149
147150 dprintln ! ( "PROGRAM {} bytes @ {}" , sz, adr) ;
148151
149- let res = esp_rom_spiflash_write ( adr, buf, sz) ;
150- if res != 0 {
151- return res;
152- }
153-
154- 0
152+ esp_rom_spiflash_write ( adr, buf, sz)
155153}
156154
157155#[ no_mangle]
158156#[ inline( never) ]
159- pub extern "C" fn UnInit ( _fnc : u32 ) -> i32 {
160- 0
157+ pub unsafe extern "C" fn UnInit ( _fnc : u32 ) -> i32 {
158+ wait_for_idle ( )
161159}
162160
163161#[ allow( non_upper_case_globals) ]
@@ -171,7 +169,7 @@ pub static FlashDevice: FlashDeviceDescription = FlashDeviceDescription {
171169 dev_addr : 0x0 ,
172170 device_size : 0x4000000 , /* Max of 64MB */
173171 // TODO change per variant?
174- page_size : 2048 ,
172+ page_size : 16384 ,
175173 _reserved : 0 ,
176174 empty : 0xFF ,
177175 program_time_out : 1000 ,
@@ -188,7 +186,7 @@ const fn sectors() -> [FlashSector; 512] {
188186 let mut sectors = [ FlashSector :: default ( ) ; 512 ] ;
189187
190188 sectors[ 0 ] = FlashSector {
191- size : 0x1000 , // 4k
189+ size : 0x10000 , // 64k
192190 address : 0x0 ,
193191 } ;
194192 sectors[ 1 ] = SECTOR_END ;
0 commit comments