From a4e8cc525093104253daf58ce6c9555c12f99230 Mon Sep 17 00:00:00 2001 From: Philipp_EndevourOS Date: Thu, 7 Aug 2025 01:46:11 +0200 Subject: [PATCH] finished write_page and sequential_read function --- src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0e12f75..80a9333 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ impl MB85RC { /// # Arguments /// * `self` - A mutable reference to the MB85RC instance. /// # Returns - /// * `Result<[u8; 3], Infallible>` - + /// * `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> { 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 @@ -39,9 +39,12 @@ impl MB85RC { /// * `self` - A mutable reference to the MB85RC instance. /// * `memory_address` - The memory address to write to. /// * `data` - The data byte to write. + /// # Returns + /// * `Result` pub fn byte_write(&mut self, memory_address: [u8; 2], data: u8) -> Result { let payload = [memory_address[0], memory_address[1], data]; self.i2c.write(self.address, &payload)?; + Ok(data) } @@ -51,16 +54,33 @@ impl MB85RC { /// 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. /// array 32KB - pub fn write_page(memory_address: [u8; 2], buf: &mut [u8]) -> Result<(), T::Error> { - todo!() + /// # 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( + &mut self, + memory_address: [u8; 2], + data: &mut [u8; 32000], + ) -> Result<(), T::Error> { + let mut payload = [0u8; 32002]; + payload[0] = memory_address[0]; + payload[1] = memory_address[1]; + payload[2..].copy_from_slice(data); + self.i2c.write(self.address, &payload)?; + + Ok(()) } /// The one byte of data from the memory address saved in the memory address buffer can be read out /// synchronously /// # Arguments + /// * `self` - A mutable reference to the MB85RC instance. /// * `memory_address` - The memory address to read from. /// # Returns - /// * `Result` - The byte read from the specified + /// * `Result` - The byte read from the specified pub fn random_read(&mut self, memory_address: &[u8; 2]) -> Result { let mut buffer: [u8; 1] = [0]; self.i2c @@ -75,11 +95,19 @@ impl MB85RC { /// 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 /// reading. - pub fn sequential_read( + /// # Arguments + /// * `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, - memory_address: [u8; 2], - buf: &mut [u8], - ) -> Result { - todo!() + memory_address: &[u8; 2], + buffer: &'a mut [u8], + ) -> Result<&'a mut [u8], T::Error> { + self.i2c.write_read(self.address, memory_address, buffer)?; + + Ok(buffer) } }