From: Tilman Sauerbeck Date: Wed, 8 Jan 2020 09:48:31 +0000 (+0100) Subject: common: Implement Storage::write() in Mx25l. X-Git-Url: http://git.code-monkey.de/?a=commitdiff_plain;h=a6747c47ca99a182f6cb4c41bad0232fb2754ff7;p=gps-watch.git common: Implement Storage::write() in Mx25l. --- diff --git a/src/common/mx25l.rs b/src/common/mx25l.rs index 2cc79f0..a6ad395 100644 --- a/src/common/mx25l.rs +++ b/src/common/mx25l.rs @@ -23,7 +23,7 @@ use gpio; use spi; -use storage::Storage; +use storage::{Storage, Error}; const SECTOR_SIZE: usize = 4096; const PAGE_SIZE: usize = 256; @@ -42,10 +42,6 @@ enum Command { RDID = 0x9f, } -pub enum Error { - UnalignedAddress = 1, -} - const SR_WIP: u8 = 1 << 0; impl Mx25l { @@ -162,4 +158,25 @@ impl Storage for Mx25l { } }) } + + fn write(&mut self, address: usize, buffer: &[u8; SECTOR_SIZE]) + -> Result<(), Error> { + if let Err(e) = self.erase_sector(address) { + return Err(e); + } + + for (i, page_bytes) in buffer.chunks(PAGE_SIZE).enumerate() { + let page_address = address + (i * PAGE_SIZE); + + // XXX: Inefficient. + let mut ba = [0xff; PAGE_SIZE]; + ba.copy_from_slice(&page_bytes); + + if let Err(e) = self.program_page(page_address, &ba) { + return Err(e); + } + } + + Ok(()) + } } diff --git a/src/common/storage.rs b/src/common/storage.rs index 7f09446..784a1f6 100644 --- a/src/common/storage.rs +++ b/src/common/storage.rs @@ -21,6 +21,12 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +pub enum Error { + UnalignedAddress = 1, +} + pub trait Storage { fn read(&self, address: usize, buffer: &mut [u8]); + + fn write(&mut self, address: usize, buffer: &[u8; 4096]) -> Result<(), Error>; }