Skip to content

Commit

Permalink
feat: SD card async feature
Browse files Browse the repository at this point in the history
Signed-off-by: Lachezar Lechev <[email protected]>
  • Loading branch information
elpiel committed Jun 26, 2024
1 parent 52dd077 commit 9f2aa81
Show file tree
Hide file tree
Showing 5 changed files with 675 additions and 66 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ The format is based on [Keep a Changelog] and this project adheres to [Semantic

- Updated to `heapless` ^0.8

### Added
- __Breaking Change__: SdCard Blocking and Async modes and a news AsyncBlockDevice Trait under the `asynch` feature

## [Version 0.7.0] - 2024-02-04

### Changed
Expand Down
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ version = "0.7.0"
byteorder = {version = "1", default-features = false}
defmt = {version = "0.3", optional = true}
embedded-hal = "1.0.0"
embedded-hal-async = { version = "1.0.0", optional = true }
heapless = "^0.8"
log = {version = "0.4", default-features = false, optional = true}

futures = { version = "0.3", default-features = false, features = ["async-await", "executor"] }

[dev-dependencies]
chrono = "0.4"
embedded-hal-bus = "0.1.0"
Expand All @@ -26,6 +29,8 @@ hex-literal = "0.4.1"
sha2 = "0.10"

[features]
default = ["log"]
default = ["log", "asynch"]
defmt-log = ["dep:defmt"]
log = ["dep:log"]
# Enable Async support for SdCard and AsyncBlockDevice trait
asynch = ["dep:embedded-hal-async"]
18 changes: 18 additions & 0 deletions src/blockdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ pub trait BlockDevice {
fn num_blocks(&self) -> Result<BlockCount, Self::Error>;
}

/// Represents a block device - a device which can read and write blocks (or
/// sectors). Only supports devices which are <= 2 TiB in size.
pub trait AsyncBlockDevice {
/// The errors that the `BlockDevice` can return. Must be debug formattable.
type Error: core::fmt::Debug;
/// Read one or more blocks, starting at the given block index.
async fn read(
&self,
blocks: &mut [Block],
start_block_idx: BlockIdx,
reason: &str,
) -> Result<(), Self::Error>;
/// Write one or more blocks, starting at the given block index.
async fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
/// Determine how many blocks this device can hold.
async fn num_blocks(&self) -> Result<BlockCount, Self::Error>;
}

impl Block {
/// All our blocks are a fixed length of 512 bytes. We do not support
/// 'Advanced Format' Hard Drives with 4 KiB blocks, nor weird old
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@

#![cfg_attr(not(test), no_std)]
#![deny(missing_docs)]
#![cfg_attr(feature = "asynch", feature(async_fn_traits))]
// Since we don't have MSRV we always enable this feature when `asynch` is enabled
#![cfg_attr(feature = "asynch", feature(async_fn_in_trait))]
#![cfg_attr(feature = "asynch", allow(stable_features))]
#![cfg_attr(feature = "asynch", allow(async_fn_in_trait))]
#![cfg_attr(feature = "asynch", feature(async_closure))]

// ****************************************************************************
//
Expand Down
Loading

0 comments on commit 9f2aa81

Please sign in to comment.