Module kernel::hil::flash

source ·
Expand description

Interface for reading, writing, and erasing flash storage pages.

Operates on single pages. The page size is set by the associated type page. Here is an example of a page type and implementation of this trait:

use core::ops::{Index, IndexMut};

use kernel::hil;
use kernel::ErrorCode;

// Size in bytes
const PAGE_SIZE: u32 = 1024;

struct NewChipPage(pub [u8; PAGE_SIZE as usize]);

impl Default for NewChipPage {
    fn default() -> Self {
        Self {
            0: [0; PAGE_SIZE as usize],
        }
    }
}

impl NewChipPage {
    fn len(&self) -> usize {
        self.0.len()
    }
}

impl Index<usize> for NewChipPage {
    type Output = u8;

    fn index(&self, idx: usize) -> &u8 {
        &self.0[idx]
    }
}

impl IndexMut<usize> for NewChipPage {
    fn index_mut(&mut self, idx: usize) -> &mut u8 {
        &mut self.0[idx]
    }
}

impl AsMut<[u8]> for NewChipPage {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.0
    }
}

struct NewChipStruct {};

impl<'a, C> hil::flash::HasClient<'a, C> for NewChipStruct {
    fn set_client(&'a self, client: &'a C) { }
}

impl hil::flash::Flash for NewChipStruct {
    type Page = NewChipPage;

    fn read_page(&self, page_number: usize, buf: &'static mut Self::Page) -> Result<(), (ErrorCode, &'static mut Self::Page)> { Err((ErrorCode::FAIL, buf)) }
    fn write_page(&self, page_number: usize, buf: &'static mut Self::Page) -> Result<(), (ErrorCode, &'static mut Self::Page)> { Err((ErrorCode::FAIL, buf)) }
    fn erase_page(&self, page_number: usize) -> Result<(), ErrorCode> { Err(ErrorCode::FAIL) }
}

A user of this flash interface might look like:

use kernel::utilities::cells::TakeCell;
use kernel::hil;

pub struct FlashUser<'a, F: hil::flash::Flash + 'static> {
    driver: &'a F,
    buffer: TakeCell<'static, F::Page>,
}

impl<'a, F: hil::flash::Flash> FlashUser<'a, F> {
    pub fn new(driver: &'a F, buffer: &'static mut F::Page) -> FlashUser<'a, F> {
        FlashUser {
            driver: driver,
            buffer: TakeCell::new(buffer),
        }
    }
}

impl<'a, F: hil::flash::Flash> hil::flash::Client<F> for FlashUser<'a, F> {
    fn read_complete(&self, buffer: &'static mut F::Page, result: Result<(), hil::flash::Error>) {}
    fn write_complete(&self, buffer: &'static mut F::Page, result: Result<(), hil::flash::Error>) { }
    fn erase_complete(&self, result: Result<(), hil::flash::Error>) {}
}

Enums

  • Flash errors returned in the callbacks.

Traits

  • Implement Client to receive callbacks from Flash.
  • A page of writable persistent flash memory.