-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EEPROM module for reading and writing data to EEPROM
- Loading branch information
1 parent
619ae4e
commit f47055b
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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>, | ||
} | ||
|
||
impl Eeprom { | ||
pub fn new(i2c: I2c<I2C1>) -> Self { | ||
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()); | ||
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; | ||
} | ||
} | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters