minor improvements to driver

- updated docs
- added types
This commit is contained in:
Djeeberjr 2025-08-08 14:42:04 +02:00
parent c6d7ca79d0
commit 60f5cc12e4

View File

@ -1,14 +1,19 @@
#![no_std] #![no_std]
//! The `MemoryAddress` are two bit.
//! For the 16Kb version 11 bit are used.
//! For the 256Kb version 16 bit are used.
//! For the 64Kb version 16 bit are used.
use core::result::Result; use core::result::Result;
use embedded_hal::i2c::{I2c, SevenBitAddress}; use embedded_hal::i2c::{I2c, SevenBitAddress};
mod async_hal;
mod embedded_io; mod embedded_io;
const DEVICE_ADDRESS: u8 = 0b10100000;
const DEVICE_ADDRESS_CODE: u8 = 0b00000000; /// [High,Low]
const DEVICE_W: u8 = 0b00000000; type MemoryAddress = [u8; 2];
const DEVICE_R: u8 = 0b00000001;
pub struct MB85RC<T: I2c<SevenBitAddress>> { pub struct MB85RC<T: I2c<SevenBitAddress>> {
i2c: T, i2c: T,
@ -22,10 +27,6 @@ impl<T: I2c> MB85RC<T> {
/// The Device ID command reads fixed Device ID. The size of Device ID is 3 bytes and consists of manufacturer /// The Device ID command reads fixed Device ID. The size of Device ID is 3 bytes and consists of manufacturer
/// ID and product ID. /// ID and product ID.
/// # Arguments
/// * `self` - A mutable reference to the MB85RC instance.
/// # Returns
/// * `Result<[u8; 3], Error>` - Device ID is 3 bytes and consists of manufacturer ID and product ID
pub fn get_device_id(&mut self) -> Result<[u8; 3], T::Error> { pub fn get_device_id(&mut self) -> Result<[u8; 3], T::Error> {
let mut buffer: [u8; 3] = [0, 0, 0]; let mut buffer: [u8; 3] = [0, 0, 0];
let reserved_slave_address = 0x7C; // Reserved Slave ID F9H without last bit, because wrte address adds this bit let reserved_slave_address = 0x7C; // Reserved Slave ID F9H without last bit, because wrte address adds this bit
@ -37,13 +38,7 @@ impl<T: I2c> MB85RC<T> {
} }
/// Write bit on the specified memory address /// Write bit on the specified memory address
/// # Arguments pub fn byte_write(&mut self, memory_address: &MemoryAddress, data: u8) -> Result<(), T::Error> {
/// * `self` - A mutable reference to the MB85RC instance.
/// * `memory_address` - The memory address to write to.
/// * `data` - The data byte to write.
/// # Returns
/// * `Result<u8, T::Error>`
pub fn byte_write(&mut self, memory_address: [u8; 2], data: u8) -> Result<(), T::Error> {
let payload = [memory_address[0], memory_address[1], data]; let payload = [memory_address[0], memory_address[1], data];
self.i2c.write(self.address, &payload) self.i2c.write(self.address, &payload)
} }
@ -54,32 +49,21 @@ impl<T: I2c> MB85RC<T> {
/// of the memory address that was written first. Because FRAM performs the high-speed write operations, the /// of the memory address that was written first. Because FRAM performs the high-speed write operations, the
/// data will be written to FRAM right after the ACK response finished. /// data will be written to FRAM right after the ACK response finished.
/// array 32KB /// array 32KB
/// # Arguments
/// * `self` - A mutable reference to the MB85RC instance.
/// * `memory_address` - The memory address to write to.
/// * `data` - The data bytes to write max 32KB.
/// # Returns
/// * `Result<(), T::Error>`
pub fn write_page( pub fn write_page(
&mut self, &mut self,
memory_address: [u8; 2], memory_address: &MemoryAddress,
data: &mut [u8; 32000], data: &[u8],
) -> Result<(), T::Error> { ) -> Result<(), T::Error> {
let mut payload = [0u8; 32002]; let mut payload = [0u8; 32002];
payload[0] = memory_address[0]; payload[0] = memory_address[0];
payload[1] = memory_address[1]; payload[1] = memory_address[1];
payload[2..].copy_from_slice(data); payload[2..].copy_from_slice(data);
self.i2c.write(self.address, &payload) self.i2c.write(self.address, &payload[..2 + data.len()])
} }
/// The one byte of data from the memory address saved in the memory address buffer can be read out /// The one byte of data from the memory address saved in the memory address buffer can be read out
/// synchronously /// synchronously
/// # Arguments pub fn random_read(&mut self, memory_address: &MemoryAddress) -> Result<u8, T::Error> {
/// * `self` - A mutable reference to the MB85RC instance.
/// * `memory_address` - The memory address to read from.
/// # Returns
/// * `Result<u8, Error>` - The byte read from the specified
pub fn random_read(&mut self, memory_address: &[u8; 2]) -> Result<u8, T::Error> {
let mut buffer: [u8; 1] = [0]; let mut buffer: [u8; 1] = [0];
self.i2c self.i2c
.write_read(self.address, memory_address, &mut buffer)?; .write_read(self.address, memory_address, &mut buffer)?;
@ -93,16 +77,10 @@ impl<T: I2c> MB85RC<T> {
/// command. If the end of the memory address space is reached, the internal read /// command. If the end of the memory address space is reached, the internal read
/// address automatically rolls over to the first memory address (0x0000) and continues /// address automatically rolls over to the first memory address (0x0000) and continues
/// reading. /// reading.
/// # Arguments pub fn sequential_read(
/// * `self` - A mutable reference to the MB85RC instance.
/// * `memory_address` - The memory address to read from.
/// * `buffer` - buffer to write the payload data
/// # Return
/// * `Result<&'a mut [u8], T::Error>` - Pointer to the array with the read data
pub fn sequential_read<'a>(
&mut self, &mut self,
memory_address: &[u8; 2], memory_address: &MemoryAddress,
buffer: &'a mut [u8], buffer: &mut [u8],
) -> Result<(), T::Error> { ) -> Result<(), T::Error> {
self.i2c.write_read(self.address, memory_address, buffer) self.i2c.write_read(self.address, memory_address, buffer)
} }