~ruther/avr-guess-the-number

fb0bec9071d83f6b0ad2a5094391e779c64c29ee — František Boháček 2 years ago bf678fc
feat: use eeprom from avr-hal
2 files changed, 1 insertions(+), 63 deletions(-)

D src/eeprom.rs
M src/entrypoint.rs
D src/eeprom.rs => src/eeprom.rs +0 -61
@@ 1,61 0,0 @@
pub struct Eeprom {
    eeprom: avr_device::atmega8::EEPROM,
}

impl Eeprom {
    pub fn new(ep: avr_device::atmega8::EEPROM) -> Eeprom {
        Eeprom {
            eeprom: ep
        }
    }

    fn wait_until_write(&self) {
        while self.eeprom.eecr.read().eewe().bit_is_set() {}
    }

    pub fn read(&self, address: u16, data: &mut [u8]) {
        let mut address = address;
        for i in 0..data.len() {
            let byte = self.read_byte(address);
            data[i] = byte;
            address += 1;
        }
    }

    pub fn write(&self, address: u16, data: &[u8]) {
        let mut address = address;
        for byte in data.iter() {
            self.write_byte(address, *byte);
            address += 1;
        }
    }

    pub fn read_byte(&self, address: u16) -> u8 {
        self.wait_until_write();

        self.eeprom.eear.write(|w| unsafe {
            w.bits(address)
        });

        self.eeprom.eecr.write(|w| w.eere().set_bit());
        self.eeprom.eedr.read().bits()
    }

    pub fn write_byte(&self, address: u16, data: u8) {
        self.wait_until_write();

        self.eeprom.eear.write(|w| unsafe {
            w.bits(address)
        });
        self.eeprom.eedr.write(|w| unsafe {
            w.bits(data)
        });

        self.eeprom.eecr.write(|w|
            w
                .eemwe().set_bit()
                .eewe().clear_bit());

        self.eeprom.eecr.write(|w|  w.eewe().set_bit());
    }
}

M src/entrypoint.rs => src/entrypoint.rs +1 -2
@@ 6,7 6,6 @@

extern crate avr_device;

mod eeprom;
mod animation;
mod button;
mod filled_seven_segment;


@@ 86,7 85,7 @@ fn main() -> ! {


    // load seeds from eeprom
    let ep = eeprom::Eeprom::new(dp.EEPROM);
    let mut ep = atmega_hal::Eeprom::new(dp.EEPROM);
    let mut seeds = [0u8; 3];
    ep.read(0, &mut seeds);
    if seeds[0] == 0 && seeds[1] == 0 && seeds[2] == 0 {

Do not follow this link