From: Tilman Sauerbeck Date: Wed, 8 Jan 2020 09:46:18 +0000 (+0100) Subject: common: Implement Mx25l::program_page(). X-Git-Url: http://git.code-monkey.de/?a=commitdiff_plain;h=821707231e866e5710d6d8a0d7a68a85015de062;p=gps-watch.git common: Implement Mx25l::program_page(). --- diff --git a/src/common/mx25l.rs b/src/common/mx25l.rs index 2c498ce..2cc79f0 100644 --- a/src/common/mx25l.rs +++ b/src/common/mx25l.rs @@ -26,6 +26,7 @@ use spi; use storage::Storage; const SECTOR_SIZE: usize = 4096; +const PAGE_SIZE: usize = 256; pub struct Mx25l { cs_gpio: u32, @@ -33,6 +34,7 @@ pub struct Mx25l { } enum Command { + PP = 0x02, READ = 0x03, RDSR = 0x05, WREN = 0x06, @@ -95,6 +97,34 @@ impl Mx25l { Ok(()) } + pub fn program_page(&self, address: usize, buffer: &[u8; PAGE_SIZE]) + -> Result<(), Error> { + if (address & (PAGE_SIZE - 1)) != 0 { + return Err(Error::UnalignedAddress); + } + + if buffer.iter().all(|&b| b == 0xff) { + return Ok(()); + } + + self.with_selected(|| { + spi::tx8(spi::SPI0, Command::PP as u8); + + spi::tx8(spi::SPI0, (address >> 16) as u8); + spi::tx8(spi::SPI0, (address >> 8) as u8); + spi::tx8(spi::SPI0, (address >> 0) as u8); + + for &b in buffer.iter() { + spi::tx8(spi::SPI0, b); + } + }); + + while self.write_in_progress() { + } + + Ok(()) + } + fn write_enable(&self) { self.with_selected(|| { spi::tx8(spi::SPI0, Command::WREN as u8);