diff --git a/firmware/src/eeprom.rs b/firmware/src/eeprom.rs index 95bcd4a..ea9ac26 100644 --- a/firmware/src/eeprom.rs +++ b/firmware/src/eeprom.rs @@ -3,10 +3,8 @@ use tm4c129x; use board; -pub const BLK_COUNT: u16 = 96; // Number of blocks -pub const BLK_U32_LEN: usize = 16; // Number of words in a block -const PRETRY: u32 = 0x00000004; // Programming Must Be Retried -const ERETRY: u32 = 0x00000008; // Erase Must Be Retried +pub const BLOCK_COUNT: u16 = 96; +pub const BLOCK_LEN: usize = 64; fn wait_done() { while cortex_m::interrupt::free(|cs| { @@ -31,56 +29,35 @@ pub fn init() { wait_done(); } -pub fn mass_erase() -> bool { - wait_done(); +pub fn read_block(buffer: &mut [u8; BLOCK_LEN], block: u16) { + assert!(block < BLOCK_COUNT); cortex_m::interrupt::free(|cs| { let eeprom = tm4c129x::EEPROM.borrow(cs); - eeprom.eedbgme.write(|w| unsafe { w.key().bits(0xE37B).me().bit(true) }); + eeprom.eeblock.write(|w| unsafe { w.block().bits(block) }); + eeprom.eeoffset.write(|w| unsafe { w.offset().bits(0) }); + for i in 0..BLOCK_LEN/4 { + let word = eeprom.eerdwrinc.read().bits(); + buffer[4*i] = word as u8; + buffer[4*i+1] = (word >> 8) as u8; + buffer[4*i+2] = (word >> 16) as u8; + buffer[4*i+3] = (word >> 24) as u8; + } }); - wait_done(); - cortex_m::interrupt::free(|cs| { - let sysctl = tm4c129x::SYSCTL.borrow(cs); - sysctl.sreeprom.modify(|_, w| w.r0().bit(true)); // Activate EEPROM reset - board::delay(16); - sysctl.sreeprom.modify(|_, w| w.r0().bit(false)); // Dectivate EEPROM reset - board::delay(16); - while !sysctl.preeprom.read().r0().bit() {} // Wait for the EEPROM to come out of reset - board::delay(16); - }); - wait_done(); - cortex_m::interrupt::free(|cs| { - let eeprom = tm4c129x::EEPROM.borrow(cs); - let eesupp2 = eeprom.eesupp.read().bits(); - eesupp2 & (PRETRY | ERETRY) == 0 - }) } -pub fn read_blk(buf: &mut [u32; BLK_U32_LEN], blk: u16) { - assert!(blk < BLK_COUNT); +pub fn write_block(buffer: &[u8; BLOCK_LEN], block: u16) { + assert!(block < BLOCK_COUNT); cortex_m::interrupt::free(|cs| { let eeprom = tm4c129x::EEPROM.borrow(cs); - eeprom.eeblock.write(|w| unsafe { w.block().bits(blk) }); + eeprom.eeblock.write(|w| unsafe { w.block().bits(block) }); eeprom.eeoffset.write(|w| unsafe { w.offset().bits(0) }); }); - for i in 0..BLK_U32_LEN { + for i in 0..BLOCK_LEN/4 { + let word = buffer[4*i] as u32 | (buffer[4*i+1] as u32) << 8 | + (buffer[4*i+2] as u32) << 16 | (buffer[4*i+3] as u32) << 24; cortex_m::interrupt::free(|cs| { let eeprom = tm4c129x::EEPROM.borrow(cs); - buf[i] = eeprom.eerdwrinc.read().bits(); - }); - } -} - -pub fn write_blk(buf: &[u32; BLK_U32_LEN], blk: u16) { - assert!(blk < BLK_COUNT); - cortex_m::interrupt::free(|cs| { - let eeprom = tm4c129x::EEPROM.borrow(cs); - eeprom.eeblock.write(|w| unsafe { w.block().bits(blk) }); - eeprom.eeoffset.write(|w| unsafe { w.offset().bits(0) }); - }); - for i in 0..BLK_U32_LEN { - cortex_m::interrupt::free(|cs| { - let eeprom = tm4c129x::EEPROM.borrow(cs); - eeprom.eerdwrinc.write(|w| unsafe { w.bits(buf[i]) }); + eeprom.eerdwrinc.write(|w| unsafe { w.bits(word) }); }); board::delay(16); wait_done();