2017-08-07 12:27:44 +08:00
|
|
|
use cortex_m;
|
|
|
|
use tm4c129x;
|
|
|
|
|
2017-08-07 16:13:29 +08:00
|
|
|
use board;
|
2017-08-07 12:27:44 +08:00
|
|
|
|
2017-08-07 16:13:29 +08:00
|
|
|
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
|
2017-08-07 12:27:44 +08:00
|
|
|
|
|
|
|
fn wait_done() {
|
2017-08-07 16:13:29 +08:00
|
|
|
while cortex_m::interrupt::free(|cs| {
|
|
|
|
let eeprom = tm4c129x::EEPROM.borrow(cs);
|
|
|
|
eeprom.eedone.read().working().bit()
|
|
|
|
}) {};
|
2017-08-07 12:27:44 +08:00
|
|
|
}
|
|
|
|
|
2017-08-07 16:13:29 +08:00
|
|
|
pub fn init() {
|
2017-08-07 12:27:44 +08:00
|
|
|
cortex_m::interrupt::free(|cs| {
|
|
|
|
let sysctl = tm4c129x::SYSCTL.borrow(cs);
|
|
|
|
|
|
|
|
sysctl.rcgceeprom.modify(|_, w| w.r0().bit(true)); // Bring up EEPROM
|
2017-08-07 16:13:29 +08:00
|
|
|
board::delay(16);
|
2017-08-07 12:27:44 +08:00
|
|
|
sysctl.sreeprom.modify(|_, w| w.r0().bit(true)); // Activate EEPROM reset
|
2017-08-07 16:13:29 +08:00
|
|
|
board::delay(16);
|
2017-08-07 12:27:44 +08:00
|
|
|
sysctl.sreeprom.modify(|_, w| w.r0().bit(false)); // Dectivate EEPROM reset
|
2017-08-07 16:13:29 +08:00
|
|
|
board::delay(16);
|
2017-08-07 12:27:44 +08:00
|
|
|
while !sysctl.preeprom.read().r0().bit() {} // Wait for the EEPROM to come out of reset
|
2017-08-07 16:13:29 +08:00
|
|
|
board::delay(16);
|
2017-08-07 12:27:44 +08:00
|
|
|
});
|
2017-08-07 16:13:29 +08:00
|
|
|
wait_done();
|
2017-08-07 12:27:44 +08:00
|
|
|
}
|
|
|
|
|
2017-08-07 16:13:29 +08:00
|
|
|
pub fn mass_erase() -> bool {
|
2017-08-07 12:27:44 +08:00
|
|
|
wait_done();
|
|
|
|
cortex_m::interrupt::free(|cs| {
|
|
|
|
let eeprom = tm4c129x::EEPROM.borrow(cs);
|
|
|
|
eeprom.eedbgme.write(|w| unsafe { w.key().bits(0xE37B).me().bit(true) });
|
|
|
|
});
|
|
|
|
wait_done();
|
|
|
|
cortex_m::interrupt::free(|cs| {
|
|
|
|
let sysctl = tm4c129x::SYSCTL.borrow(cs);
|
|
|
|
sysctl.sreeprom.modify(|_, w| w.r0().bit(true)); // Activate EEPROM reset
|
2017-08-07 16:13:29 +08:00
|
|
|
board::delay(16);
|
2017-08-07 12:27:44 +08:00
|
|
|
sysctl.sreeprom.modify(|_, w| w.r0().bit(false)); // Dectivate EEPROM reset
|
2017-08-07 16:13:29 +08:00
|
|
|
board::delay(16);
|
2017-08-07 12:27:44 +08:00
|
|
|
while !sysctl.preeprom.read().r0().bit() {} // Wait for the EEPROM to come out of reset
|
2017-08-07 16:13:29 +08:00
|
|
|
board::delay(16);
|
2017-08-07 12:27:44 +08:00
|
|
|
});
|
|
|
|
wait_done();
|
|
|
|
cortex_m::interrupt::free(|cs| {
|
|
|
|
let eeprom = tm4c129x::EEPROM.borrow(cs);
|
|
|
|
let eesupp2 = eeprom.eesupp.read().bits();
|
2017-08-07 16:13:29 +08:00
|
|
|
eesupp2 & (PRETRY | ERETRY) == 0
|
|
|
|
})
|
2017-08-07 12:27:44 +08:00
|
|
|
}
|
|
|
|
|
2017-08-07 16:13:29 +08:00
|
|
|
pub fn read_blk(buf: &mut [u32; BLK_U32_LEN], blk: u16) {
|
|
|
|
assert!(blk < BLK_COUNT);
|
2017-08-07 12:27:44 +08:00
|
|
|
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) });
|
|
|
|
});
|
2017-08-07 16:13:29 +08:00
|
|
|
for i in 0..BLK_U32_LEN {
|
2017-08-07 12:27:44 +08:00
|
|
|
cortex_m::interrupt::free(|cs| {
|
|
|
|
let eeprom = tm4c129x::EEPROM.borrow(cs);
|
2017-08-07 16:13:29 +08:00
|
|
|
buf[i] = eeprom.eerdwrinc.read().bits();
|
2017-08-07 12:27:44 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 16:13:29 +08:00
|
|
|
pub fn write_blk(buf: &[u32; BLK_U32_LEN], blk: u16) {
|
|
|
|
assert!(blk < BLK_COUNT);
|
2017-08-07 12:27:44 +08:00
|
|
|
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) });
|
|
|
|
});
|
2017-08-07 16:13:29 +08:00
|
|
|
for i in 0..BLK_U32_LEN {
|
2017-08-07 12:27:44 +08:00
|
|
|
cortex_m::interrupt::free(|cs| {
|
|
|
|
let eeprom = tm4c129x::EEPROM.borrow(cs);
|
2017-08-07 16:13:29 +08:00
|
|
|
eeprom.eerdwrinc.write(|w| unsafe { w.bits(buf[i]) });
|
2017-08-07 12:27:44 +08:00
|
|
|
});
|
2017-08-07 16:13:29 +08:00
|
|
|
board::delay(16);
|
2017-08-07 12:27:44 +08:00
|
|
|
wait_done();
|
|
|
|
}
|
2017-08-07 16:13:29 +08:00
|
|
|
}
|