mirror of
https://github.com/icewind1991/esp8266-flash.git
synced 2026-06-03 10:34:05 +02:00
basic flash error handling
This commit is contained in:
parent
b25148060c
commit
f50307e071
1 changed files with 37 additions and 13 deletions
50
src/lib.rs
50
src/lib.rs
|
|
@ -15,7 +15,7 @@ pub struct FlashSpi(SPI0);
|
||||||
pub struct DummyCS;
|
pub struct DummyCS;
|
||||||
|
|
||||||
impl Transfer<u8> for FlashSpi {
|
impl Transfer<u8> for FlashSpi {
|
||||||
type Error = Void;
|
type Error = ESPFlashError;
|
||||||
|
|
||||||
fn transfer<'w>(&mut self, _words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
|
fn transfer<'w>(&mut self, _words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
|
|
@ -39,6 +39,27 @@ pub struct ESPFlash {
|
||||||
spi: FlashSpi
|
spi: FlashSpi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum ESPFlashError {
|
||||||
|
Err = 1,
|
||||||
|
Timeout = 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ESPFlashError {
|
||||||
|
fn from(result: u32) -> Result<(), Self> {
|
||||||
|
match result {
|
||||||
|
0 => Ok(()),
|
||||||
|
2 => Err(ESPFlashError::Timeout),
|
||||||
|
_ => Err(ESPFlashError::Err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ESPFlashError> for Error<FlashSpi, DummyCS> {
|
||||||
|
fn from(err: ESPFlashError) -> Self {
|
||||||
|
Error::Spi(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ESPFlash {
|
impl ESPFlash {
|
||||||
pub fn new(spi: SPI0) -> Self {
|
pub fn new(spi: SPI0) -> Self {
|
||||||
// take ownership of SPI0 to ensure nobody else can mess with the spi
|
// take ownership of SPI0 to ensure nobody else can mess with the spi
|
||||||
|
|
@ -57,33 +78,36 @@ impl BlockDevice<u32, FlashSpi, DummyCS> for ESPFlash {
|
||||||
fn erase_sectors(&mut self, addr: u32, amount: usize) -> Result<(), Error<FlashSpi, DummyCS>> {
|
fn erase_sectors(&mut self, addr: u32, amount: usize) -> Result<(), Error<FlashSpi, DummyCS>> {
|
||||||
let start_sector = addr / SECTOR_SIZE;
|
let start_sector = addr / SECTOR_SIZE;
|
||||||
for i in 0..(amount as u32) {
|
for i in 0..(amount as u32) {
|
||||||
unsafe {
|
ESPFlashError::from(unsafe {
|
||||||
SPIEraseSector(start_sector + i);
|
SPIEraseSector(start_sector + i)
|
||||||
}
|
})?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn erase_all(&mut self) -> Result<(), Error<FlashSpi, DummyCS>> {
|
fn erase_all(&mut self) -> Result<(), Error<FlashSpi, DummyCS>> {
|
||||||
unsafe {
|
ESPFlashError::from(unsafe {
|
||||||
SPIEraseChip();
|
SPIEraseChip()
|
||||||
}
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_bytes(&mut self, addr: u32, data: &mut [u8]) -> Result<(), Error<FlashSpi, DummyCS>> {
|
fn write_bytes(&mut self, addr: u32, data: &mut [u8]) -> Result<(), Error<FlashSpi, DummyCS>> {
|
||||||
unsafe {
|
ESPFlashError::from(unsafe {
|
||||||
SPIWrite(addr, data.as_ptr(), data.len() as u32);
|
SPIWrite(addr, data.as_ptr(), data.len() as u32)
|
||||||
}
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Read<u32, FlashSpi, DummyCS> for ESPFlash {
|
impl Read<u32, FlashSpi, DummyCS> for ESPFlash {
|
||||||
fn read(&mut self, addr: u32, buf: &mut [u8]) -> Result<(), Error<FlashSpi, DummyCS>> {
|
fn read(&mut self, addr: u32, buf: &mut [u8]) -> Result<(), Error<FlashSpi, DummyCS>> {
|
||||||
unsafe {
|
ESPFlashError::from(unsafe {
|
||||||
SPIRead(addr, buf.as_mut_ptr() as *mut _, buf.len() as u32);
|
SPIRead(addr, buf.as_mut_ptr() as *mut _, buf.len() as u32)
|
||||||
}
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue