Skip to content

Commit f40c906

Browse files
authored
Merge pull request #24 from embassy-rs/e-h-pr
Add async version of nor flash
2 parents 262845d + 51e59a6 commit f40c906

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

embedded-storage-async/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "embedded-storage-async"
3+
version = "0.2.0"
4+
authors = [
5+
"Mathias Koch <[email protected]>",
6+
]
7+
edition = "2018"
8+
description = "A Storage Abstraction Layer for Embedded Systems"
9+
license = "MIT OR Apache-2.0"
10+
repository = "https://github.com/rust-embedded-community/embedded-storage"
11+
homepage = "https://github.com/rust-embedded-community/embedded-storage"
12+
documentation = "https://docs.rs/embedded-storage"
13+
readme = "README.md"
14+
keywords = ["storage"]
15+
categories = ["embedded", "hardware-support", "no-std"]
16+
17+
[dependencies]
18+
embedded-storage = { path = "../" }

embedded-storage-async/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! # embedded-storage-async - An async Storage Abstraction Layer for Embedded Systems
2+
//!
3+
//! Storage traits to allow on and off board storage devices to read and write
4+
//! data asynchronously.
5+
6+
#![no_std]
7+
#![feature(generic_associated_types)]
8+
9+
pub mod nor_flash;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use core::future::Future;
2+
use embedded_storage::nor_flash::ErrorType;
3+
4+
/// Read only NOR flash trait.
5+
pub trait AsyncReadNorFlash: ErrorType {
6+
/// The minumum number of bytes the storage peripheral can read
7+
const READ_SIZE: usize;
8+
9+
type ReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
10+
where
11+
Self: 'a;
12+
13+
/// Read a slice of data from the storage peripheral, starting the read
14+
/// operation at the given address offset, and reading `bytes.len()` bytes.
15+
///
16+
/// # Errors
17+
///
18+
/// Returns an error if the arguments are not aligned or out of bounds. The implementation
19+
/// can use the [`check_read`] helper function.
20+
fn read<'a>(&'a mut self, offset: u32, bytes: &'a mut [u8]) -> Self::ReadFuture<'a>;
21+
22+
/// The capacity of the peripheral in bytes.
23+
fn capacity(&self) -> usize;
24+
}
25+
26+
/// NOR flash trait.
27+
pub trait AsyncNorFlash: AsyncReadNorFlash {
28+
/// The minumum number of bytes the storage peripheral can write
29+
const WRITE_SIZE: usize;
30+
31+
/// The minumum number of bytes the storage peripheral can erase
32+
const ERASE_SIZE: usize;
33+
34+
type EraseFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
35+
where
36+
Self: 'a;
37+
38+
/// Erase the given storage range, clearing all data within `[from..to]`.
39+
/// The given range will contain all 1s afterwards.
40+
///
41+
/// If power is lost during erase, contents of the page are undefined.
42+
///
43+
/// # Errors
44+
///
45+
/// Returns an error if the arguments are not aligned or out of bounds (the case where `to >
46+
/// from` is considered out of bounds). The implementation can use the [`check_erase`]
47+
/// helper function.
48+
fn erase<'a>(&'a mut self, from: u32, to: u32) -> Self::EraseFuture<'a>;
49+
50+
type WriteFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
51+
where
52+
Self: 'a;
53+
/// If power is lost during write, the contents of the written words are undefined,
54+
/// but the rest of the page is guaranteed to be unchanged.
55+
/// It is not allowed to write to the same word twice.
56+
///
57+
/// # Errors
58+
///
59+
/// Returns an error if the arguments are not aligned or out of bounds. The implementation
60+
/// can use the [`check_write`] helper function.
61+
fn write<'a>(&'a mut self, offset: u32, bytes: &'a [u8]) -> Self::WriteFuture<'a>;
62+
}

src/nor_flash.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ impl NorFlashError for core::convert::Infallible {
1515
}
1616
}
1717

18+
/// A trait that NorFlash implementations can use to share an error type.
19+
pub trait ErrorType {
20+
/// Errors returned by this NOR flash.
21+
type Error: NorFlashError;
22+
}
23+
1824
/// NOR flash error kinds.
1925
///
2026
/// NOR flash implementations must map their error to those generic error kinds through the
@@ -49,10 +55,7 @@ impl core::fmt::Display for NorFlashErrorKind {
4955
}
5056

5157
/// Read only NOR flash trait.
52-
pub trait ReadNorFlash {
53-
/// Errors returned by this NOR flash.
54-
type Error: NorFlashError;
55-
58+
pub trait ReadNorFlash: ErrorType {
5659
/// The minumum number of bytes the storage peripheral can read
5760
const READ_SIZE: usize;
5861

0 commit comments

Comments
 (0)