A src/eeprom.rs => src/eeprom.rs +61 -0
@@ 0,0 1,61 @@
+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 +22 -1
@@ 1,6 1,12 @@
#![no_std]
#![no_main]
+// TODO: seeds to eeprom
+//
+
+extern crate avr_device;
+
+mod eeprom;
mod animation;
mod button;
mod filled_seven_segment;
@@ 78,8 84,23 @@ fn main() -> ! {
let btn_confirm = button::Button::create(in_confirm, false);
// PERIPHERALS END
+
+ // load seeds from eeprom
+ let ep = eeprom::Eeprom::new(dp.EEPROM);
+ let mut seeds = [0u8; 3];
+ ep.read(0, &mut seeds);
+ if seeds[0] == 0 && seeds[1] == 0 && seeds[2] == 0 {
+ seeds[0] = 125;
+ seeds[1] = 139;
+ seeds[2] = 45;
+ }
+
// RNG
- let rng = rng::Rng::init(123, 111, 45);
+ let mut rng = rng::Rng::init(seeds[0], seeds[1], seeds[2]);
+
+ // write new seeds
+ let next_seeds = [rng.take_u8(), rng.take_u8(), rng.take_u8()];
+ ep.write(0, &next_seeds);
// GAME
let mut game = Game {