@@ -3,23 +3,12 @@ use std::fmt;
33use std:: time:: { Duration , Instant } ;
44
55use crate :: chips:: Chip ;
6+ use crate :: consts:: * ;
67use crate :: fel:: Fel ;
78use crate :: progress:: Progress ;
8- use crate :: spi:: { self , SpiError , SpiSession } ;
9+ use crate :: spi:: { self , Command , SpiError , SpiSession } ;
910
1011const WAIT_TIMEOUT : Duration = Duration :: from_secs ( 5 ) ;
11- const OPCODE_RDID : u8 = 0x9f ;
12- const OPCODE_GET_FEATURE : u8 = 0x0f ;
13- const OPCODE_SET_FEATURE : u8 = 0x1f ;
14- const FEATURE_PROTECT : u8 = 0xa0 ;
15- const FEATURE_STATUS : u8 = 0xc0 ;
16- const OPCODE_READ_PAGE_TO_CACHE : u8 = 0x13 ;
17- const OPCODE_READ_PAGE_FROM_CACHE : u8 = 0x03 ;
18- const OPCODE_WRITE_ENABLE : u8 = 0x06 ;
19- const OPCODE_BLOCK_ERASE : u8 = 0xd8 ;
20- const OPCODE_PROGRAM_LOAD : u8 = 0x02 ;
21- const OPCODE_PROGRAM_EXEC : u8 = 0x10 ;
22- const OPCODE_RESET : u8 = 0xff ;
2312
2413#[ derive( Debug ) ]
2514pub enum SpinandError {
@@ -125,6 +114,7 @@ pub fn write(
125114 mut progress : Option < & mut Progress > ,
126115) -> SpinandResult < ( ) > {
127116 let mut state = SpinandState :: new ( chip, fel) ?;
117+ state. erase_range ( fel, address, address + data. len ( ) as u64 , None ) ?;
128118 let mut processed = 0u64 ;
129119 let total = data. len ( ) as u64 ;
130120 println ! (
@@ -220,16 +210,13 @@ impl<'chip> SpinandState<'chip> {
220210 fn erase_block ( & mut self , fel : & Fel < ' _ > , address : u64 ) -> SpinandResult < ( ) > {
221211 let page_size = self . info . page_size as u64 ;
222212 let pa = u32:: try_from ( address / page_size) . map_err ( |_| SpinandError :: AddressOverflow ) ?;
223- self . write_enable ( fel) ?;
224- self . wait_ready ( fel) ?;
225- let tx = [
226- OPCODE_BLOCK_ERASE ,
227- ( ( pa >> 16 ) & 0xff ) as u8 ,
228- ( ( pa >> 8 ) & 0xff ) as u8 ,
229- ( pa & 0xff ) as u8 ,
230- ] ;
231- spi:: transfer ( fel, & self . session , Some ( & tx) , None ) ?;
232- self . wait_ready ( fel)
213+ let mut command = Command :: new ( ) ;
214+ command. enable_write ( ) ;
215+ command. wait_ready_nand ( ) ;
216+ command. block_erase ( pa) ;
217+ command. wait_ready_nand ( ) ;
218+ command. exec ( fel, & self . session ) ?;
219+ Ok ( ( ) )
233220 }
234221
235222 fn read_range_segment (
@@ -301,7 +288,15 @@ impl<'chip> SpinandState<'chip> {
301288 if bytes_left_in_page == 0 {
302289 break ;
303290 }
291+ let mut commands = Command :: new ( ) ;
304292 let chunk = data. len ( ) . min ( bytes_left_in_page) . min ( self . chunk_limit ( ) ) ;
293+ commands. enable_write ( ) ;
294+ commands. wait_ready_nand ( ) ;
295+ commands. program_load ( & self . session , column as u16 , & data[ ..chunk] ) ;
296+ commands. wait_ready_nand ( ) ;
297+ commands. program_exec ( page) ;
298+ commands. wait_ready_nand ( ) ;
299+ commands. exec ( fel, & self . session ) ?;
305300 self . program_load ( fel, column as u16 , & data[ ..chunk] ) ?;
306301 self . wait_ready ( fel) ?;
307302 data = & data[ chunk..] ;
@@ -317,9 +312,6 @@ impl<'chip> SpinandState<'chip> {
317312 break ;
318313 }
319314 }
320-
321- self . program_exec ( fel, page) ?;
322- self . wait_ready ( fel) ?;
323315 }
324316 Ok ( ( ) )
325317 }
@@ -480,6 +472,7 @@ impl<'chip> SpinandState<'chip> {
480472 Ok ( ( ) )
481473 }
482474
475+ #[ allow( unused) ]
483476 fn program_exec ( & mut self , fel : & Fel < ' _ > , page : u32 ) -> SpinandResult < ( ) > {
484477 let tx = [
485478 OPCODE_PROGRAM_EXEC ,
0 commit comments