Skip to content

Commit 2145d46

Browse files
committed
Add async version of nor flash
Add async version of the nor_flash traits, that can be used in an async context. In the same way as embedded_hal_async in relation to embedded_hal, the traits are stored in a separate crate, with the intention of moving to the main crate once the async GAT feature is part of Rust stable.
1 parent 262845d commit 2145d46

File tree

3 files changed

+91
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)