Module kernel::hil::flash [] [src]

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:

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

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

impl NewChipPage {
    pub const fn new() -> NewChipPage {
        NewChipPage([0; PAGE_SIZE as usize])
    }

    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
    }
}

Then a basic implementation of this trait should look like:

impl hil::flash::HasClient 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) -> ReturnCode { }
    fn write_page(&self, page_number: usize, buf: &'static mut Self::Page) -> ReturnCode { }
    fn erase_page(&self, page_number: usize) -> ReturnCode { }
}

A user of this flash interface might look like:

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

impl<'a, F: hil::flash::Flash + 'a> 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 + 'a> hil::flash::Client<F> for FlashUser<'a, F> {
    fn read_complete(&self, buffer: &'static mut F::Page, error: hil::flash::Error) {}
    fn write_complete(&self, buffer: &'static mut F::Page, error: hil::flash::Error) { }
    fn erase_complete(&self, error: hil::flash::Error) {}
}

Reexports

use returncode::ReturnCode;

Enums

Error

Flash errors returned in the callbacks.

Traits

Client

Implement Client to receive callbacks from Flash.

Flash

A page of writable persistent flash memory.

HasClient