Skip to content

Commit

Permalink
Comments and more robust checking if the EEPROM is
Browse files Browse the repository at this point in the history
 busy
  • Loading branch information
amcelroy committed May 17, 2023
1 parent 442035d commit 93f2457
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions tm4c123x-hal/src/eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ pub struct Eeprom {
}

impl Eeprom {
/// Configures a new EEPROM with a start / end address defined by the
/// user.
/// Configures a new EEPROM struct using the datasheet section 8.2.4.2
pub fn new(eeprom: EEPROM, _pc: &sysctl::PowerControl) -> Self {

let final_eeprom = Eeprom { eeprom };
Expand Down Expand Up @@ -98,7 +97,9 @@ impl Eeprom {

/// Set the block register
fn set_block(&self, block: usize) -> Result<(), EepromError> {
self.wait();
if self.is_busy() {
return Err(EepromError::Busy);
}

if block < EEPROM_NUM_BLOCKS {
unsafe {
Expand All @@ -110,6 +111,8 @@ impl Eeprom {
// Changing blocks requires a small delay, see Section 8.2.4.1 Timing Considerations
self.delay(4);

self.wait();

Ok(())
}else{
Err(EepromError::BlockOutOfBounds)
Expand All @@ -118,7 +121,9 @@ impl Eeprom {

/// Set the offset register
fn set_offset(&self, offset: usize) -> Result<(), EepromError> {
self.wait();
if self.is_busy() {
return Err(EepromError::Busy);
}

if offset < EEPROM_BLOCK_SIZE {
unsafe {
Expand All @@ -127,6 +132,8 @@ impl Eeprom {
});
}

self.wait();

Ok(())
}else{
Err(EepromError::OffsetOutOfBounds)
Expand All @@ -135,6 +142,10 @@ impl Eeprom {

/// Set the block and offset registers
fn set_block_and_offset(&self, address: &EepromAddress) -> Result<(), EepromError> {
if self.is_busy() {
return Err(EepromError::Busy);
}

self.set_block(address.block())?;
self.set_offset(address.offset())?;
Ok(())
Expand All @@ -161,7 +172,7 @@ impl Eeprom {
/// * Block 0, Offset, 1 -> Block 0, Offset 2
/// * Block 0, Offset, 15 -> Block 1, Offset 0
/// * Block 31, Offset, 15 -> Block 0, Offset 0
fn increment_offset(&mut self, starting_address: &mut EepromAddress) -> Result<(), EepromError> {
fn increment_offset(&mut self, starting_address: &mut EepromAddress) -> Result<(), EepromError> {
starting_address.increment(EEPROM_BLOCK_SIZE, EEPROM_NUM_BLOCKS);
self.set_block_and_offset(&starting_address)?;
Ok(())
Expand Down

0 comments on commit 93f2457

Please sign in to comment.