Skip to content

Commit

Permalink
eeprom: Add workaround for #406
Browse files Browse the repository at this point in the history
The smart logic that replaces a write of 0xff with an erase is removed,
and any erase operation is replaced by a write of 0xff. This is done
because the erase function is borked, as documented here:
  #406
  • Loading branch information
tronje committed Nov 24, 2023
1 parent a448bb5 commit 16191b1
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions avr-hal-generic/src/eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ where
#[inline]
pub fn erase_byte(&mut self, offset: u16) {
assert!(offset < Self::CAPACITY);
self.p.raw_erase_byte(offset)
// Write 0xff here because the erase function is borked.
// See also: https://github.com/Rahix/avr-hal/issues/406
self.p.raw_write_byte(offset, 0xff)
}

pub fn read(&self, offset: u16, buf: &mut [u8]) -> Result<(), OutOfBoundsError> {
Expand Down Expand Up @@ -179,26 +181,12 @@ macro_rules! impl_eeprom_common {

// Check if any bits are changed to '1' in the new value.
if (diff_mask & data) != 0 {
// Now we know that _some_ bits need to be erased to '1'.

// Check if any bits in the new value are '0'.
if data != 0xff {
// Now we know that some bits need to be programmed to '0' also.
self.eedr.write(|w| w.bits(data)); // Set EEPROM data register.

{
let $periph_ewmode_var = &self;
$set_erasewrite_mode
}
self.eecr.write(|w| w.eepe().set_bit()); // Start Erase+Write operation.
} else {
// Now we know that all bits should be erased.
{
let $periph_emode_var = &self;
$set_erase_mode
}
self.eecr.write(|w| w.eepe().set_bit()); // Start Erase-only operation.
self.eedr.write(|w| w.bits(data)); // Set EEPROM data register.
{
let $periph_ewmode_var = &self;
$set_erasewrite_mode
}
self.eecr.write(|w| w.eepe().set_bit()); // Start Erase+Write operation.
}
//Now we know that _no_ bits need to be erased to '1'.
else {
Expand Down

0 comments on commit 16191b1

Please sign in to comment.