Skip to content

Commit

Permalink
Refactor EEPROM module to use new I2C write and read methods
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickdewid committed Apr 9, 2024
1 parent f47055b commit 0c683ab
Showing 1 changed file with 17 additions and 44 deletions.
61 changes: 17 additions & 44 deletions vecraft/src/eeprom.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
// use core::cell::RefCell;

// use cortex_m::{interrupt::Mutex, prelude::*};
use stm32h7xx_hal::prelude::*;
use stm32h7xx_hal::{device::I2C1, i2c::I2c};

const EEPROM_I2C_ADDRESS: u8 = 0x50;
pub const EEPROM_SIZE: u16 = 64_000;

// static EEPROM: Mutex<RefCell<Option<Eeprom>>> = Mutex::new(RefCell::new(None));

// pub fn eeprom_command<F, R>(f: F) -> R
// where
// F: FnOnce(&mut Eeprom) -> R,
// {
// cortex_m::interrupt::free(|cs| {
// let mut g_ref = EEPROM.borrow(cs).borrow_mut();
// let l_self = g_ref.as_mut().unwrap();
// f(l_self)
// })
// }

// pub fn eeprom_init(eeprom: Eeprom) {
// cortex_m::interrupt::free(|cs| {
// EEPROM.borrow(cs).replace(Some(eeprom));
// });
// }

pub struct Eeprom {
i2c: I2c<I2C1>,
}
Expand All @@ -35,31 +13,26 @@ impl Eeprom {
Self { i2c }
}

pub fn write(&mut self, address: u16, buffer: &[u8]) {
let mut i2c_buffer = [0; 32];
i2c_buffer[..2].copy_from_slice(&address.to_le_bytes());
pub fn write(&mut self, address: u16, buffer: &[u8]) -> Result<(), stm32h7xx_hal::i2c::Error> {
// let mut i2c_buffer = [0; 32];
// i2c_buffer[..2].copy_from_slice(&address.to_le_bytes());
// i2c_buffer[2..buffer.len() + 2].copy_from_slice(buffer);

let mut i2c_buffer = [0; 34];
i2c_buffer[0] = (address & 0xFF) as u8;
i2c_buffer[1] = (address >> 8) as u8;
i2c_buffer[2..buffer.len() + 2].copy_from_slice(buffer);

loop {
if self
.i2c
.write(EEPROM_I2C_ADDRESS, &i2c_buffer[..buffer.len() + 2])
.is_ok()
{
break;
}
}
self.i2c
.write(EEPROM_I2C_ADDRESS, &i2c_buffer[..buffer.len() + 2])
}

pub fn read(&mut self, address: u16, buffer: &mut [u8]) {
loop {
if self
.i2c
.write_read(EEPROM_I2C_ADDRESS, &address.to_le_bytes(), buffer)
.is_ok()
{
break;
}
}
pub fn read(
&mut self,
address: u16,
buffer: &mut [u8],
) -> Result<(), stm32h7xx_hal::i2c::Error> {
self.i2c
.write_read(EEPROM_I2C_ADDRESS, &address.to_le_bytes(), buffer)
}
}

0 comments on commit 0c683ab

Please sign in to comment.