ionpak-thermostat/firmware/src/eeprom.rs

66 lines
2.1 KiB
Rust
Raw Normal View History

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
pub const BLOCK_COUNT: u16 = 96;
pub const BLOCK_LEN: usize = 64;
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
}
pub fn read_block(buffer: &mut [u8; BLOCK_LEN], block: u16) {
assert!(block < BLOCK_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(block) });
2017-08-07 12:27:44 +08:00
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;
}
2017-08-07 12:27:44 +08:00
});
}
pub fn write_block(buffer: &[u8; BLOCK_LEN], block: u16) {
assert!(block < BLOCK_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(block) });
2017-08-07 12:27:44 +08:00
eeprom.eeoffset.write(|w| unsafe { w.offset().bits(0) });
});
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;
2017-08-07 12:27:44 +08:00
cortex_m::interrupt::free(|cs| {
let eeprom = tm4c129x::EEPROM.borrow(cs);
eeprom.eerdwrinc.write(|w| unsafe { w.bits(word) });
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
}