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

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::ReturnCode;

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

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 {

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

    fn index(&self, idx: usize) -> &u8 {

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

A user of this flash interface might look like:

use kernel::common::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, 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) {}



Flash errors returned in the callbacks.



Implement Client to receive callbacks from Flash.


A page of writable persistent flash memory.