X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fcommon%2Fmx25l.rs;h=388d31172497b858c86c2ac36731156d1d16e2ed;hb=810fc22cf43b5cd504279f89e509a2a08d07481b;hp=2cc79f08f526b853c3a21a9c77965c09d8c57ff5;hpb=821707231e866e5710d6d8a0d7a68a85015de062;p=gps-watch.git diff --git a/src/common/mx25l.rs b/src/common/mx25l.rs index 2cc79f0..388d311 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; @@ -39,13 +39,10 @@ enum Command { RDSR = 0x05, WREN = 0x06, SE = 0x20, + CE = 0x60, RDID = 0x9f, } -pub enum Error { - UnalignedAddress = 1, -} - const SR_WIP: u8 = 1 << 0; impl Mx25l { @@ -76,6 +73,17 @@ impl Mx25l { }) } + pub fn erase_all(&self) { + self.write_enable(); + + self.with_selected(|| { + spi::tx8(spi::SPI0, Command::CE as u8); + }); + + while self.write_in_progress() { + } + } + pub fn erase_sector(&self, address: usize) -> Result<(), Error> { if (address & (SECTOR_SIZE - 1)) != 0 { return Err(Error::UnalignedAddress); @@ -162,4 +170,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(()) + } }