Skip to content

Commit df4227b

Browse files
committed
add async support using bisync crate
This moves the existing API into a `blocking` module and duplicates the API into a new `asynchronous` module, using `async fn` where applicable. Fixes #50
1 parent e7eef2a commit df4227b

36 files changed

+886
-804
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog] and this project adheres to [Semantic
88

99
### Changed
1010

11+
- __Breaking Change__: Existing API moved into `blocking` module. Adjust your imports from `embedded_sdmmc` to `embedded_sdmmc::blocking` to keep old code building.
1112
- __Breaking Change__: `VolumeManager` now uses interior-mutability (with a `RefCell`) and so most methods are now `&self`. This also makes it easier to open multiple `File`, `Directory` or `Volume` objects at once.
1213
- __Breaking Change__: The `VolumeManager`, `File`, `Directory` and `Volume` no longer implement `Send` or `Sync.
1314
- `VolumeManager` uses an interior block cache of 512 bytes, increasing its size by about 520 bytes but hugely reducing stack space required at run-time.
@@ -17,6 +18,7 @@ The format is based on [Keep a Changelog] and this project adheres to [Semantic
1718

1819
### Added
1920

21+
- Async API in `asynchronous` module
2022
- `File` now implements the `embedded-io` `Read`, `Write` and `Seek` traits.
2123
- New `iterate_dir_lfn` method on `VolumeManager` and `Directory` - provides decoded Long File Names as `Option<&str>`
2224

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ version = "0.8.0"
1414
rust-version = "1.76"
1515

1616
[dependencies]
17+
bisync = "0.2.3"
1718
byteorder = {version = "1", default-features = false}
1819
defmt = {version = "0.3", optional = true}
1920
embedded-hal = "1.0.0"
21+
embedded-hal-async = "1.0.0"
2022
embedded-io = "0.6.1"
23+
embedded-io-async = "0.6.1"
2124
heapless = "^0.8"
2225
log = {version = "0.4", default-features = false, optional = true}
2326

examples/append_file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ use linux::*;
2222

2323
const FILE_TO_APPEND: &str = "README.TXT";
2424

25-
use embedded_sdmmc::{Error, Mode, VolumeIdx};
25+
use embedded_sdmmc::blocking::{Error, Mode, VolumeIdx};
2626

27-
type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
27+
type VolumeManager = embedded_sdmmc::blocking::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
2828

29-
fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
29+
fn main() -> Result<(), Error<std::io::Error>> {
3030
env_logger::init();
3131
let mut args = std::env::args().skip(1);
3232
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());

examples/big_dir.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ extern crate embedded_sdmmc;
33
mod linux;
44
use linux::*;
55

6-
use embedded_sdmmc::Error;
6+
use embedded_sdmmc::blocking::{Error, Mode, VolumeIdx};
77

8-
type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
8+
type VolumeManager = embedded_sdmmc::blocking::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
99

10-
fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
10+
fn main() -> Result<(), Error<std::io::Error>> {
1111
env_logger::init();
1212
let mut args = std::env::args().skip(1);
1313
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());
1414
let print_blocks = args.find(|x| x == "-v").map(|_| true).unwrap_or(false);
1515
let lbd = LinuxBlockDevice::new(filename, print_blocks).map_err(Error::DeviceError)?;
1616
let volume_mgr: VolumeManager = VolumeManager::new_with_limits(lbd, Clock, 0xAA00_0000);
1717
let volume = volume_mgr
18-
.open_volume(embedded_sdmmc::VolumeIdx(1))
18+
.open_volume(VolumeIdx(1))
1919
.unwrap();
2020
println!("Volume: {:?}", volume);
2121
let root_dir = volume.open_root_dir().unwrap();
@@ -28,7 +28,7 @@ fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
2828
let file = root_dir
2929
.open_file_in_dir(
3030
file_name.as_str(),
31-
embedded_sdmmc::Mode::ReadWriteCreateOrTruncate,
31+
Mode::ReadWriteCreateOrTruncate,
3232
)
3333
.unwrap();
3434
let buf = b"hello world, from rust";

examples/create_file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ use linux::*;
2222

2323
const FILE_TO_CREATE: &str = "CREATE.TXT";
2424

25-
use embedded_sdmmc::{Error, Mode, VolumeIdx};
25+
use embedded_sdmmc::blocking::{Error, Mode, VolumeIdx};
2626

27-
type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
27+
type VolumeManager = embedded_sdmmc::blocking::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
2828

29-
fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
29+
fn main() -> Result<(), Error<std::io::Error>> {
3030
env_logger::init();
3131
let mut args = std::env::args().skip(1);
3232
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());

examples/delete_file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ use linux::*;
2525

2626
const FILE_TO_DELETE: &str = "README.TXT";
2727

28-
use embedded_sdmmc::{Error, VolumeIdx};
28+
use embedded_sdmmc::blocking::{Error, VolumeIdx};
2929

30-
type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
30+
type VolumeManager = embedded_sdmmc::blocking::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
3131

32-
fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
32+
fn main() -> Result<(), Error<std::io::Error>> {
3333
env_logger::init();
3434
let mut args = std::env::args().skip(1);
3535
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());

examples/linux/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Helpers for using embedded-sdmmc on Linux
22
33
use chrono::Timelike;
4-
use embedded_sdmmc::{Block, BlockCount, BlockDevice, BlockIdx, TimeSource, Timestamp};
4+
use embedded_sdmmc::blocking::{Block, BlockCount, BlockDevice, BlockIdx, TimeSource, Timestamp};
55
use std::cell::RefCell;
66
use std::fs::{File, OpenOptions};
77
use std::io::prelude::*;

examples/list_dir.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
mod linux;
3636
use linux::*;
3737

38-
use embedded_sdmmc::{ShortFileName, VolumeIdx};
38+
use embedded_sdmmc::blocking::{ShortFileName, VolumeIdx};
3939

40-
type Error = embedded_sdmmc::Error<std::io::Error>;
40+
type Error = embedded_sdmmc::blocking::Error<std::io::Error>;
4141

42-
type Directory<'a> = embedded_sdmmc::Directory<'a, LinuxBlockDevice, Clock, 8, 4, 4>;
43-
type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
42+
type Directory<'a> = embedded_sdmmc::blocking::Directory<'a, LinuxBlockDevice, Clock, 8, 4, 4>;
43+
type VolumeManager = embedded_sdmmc::blocking::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
4444

4545
fn main() -> Result<(), Error> {
4646
env_logger::init();

examples/read_file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ use linux::*;
3939

4040
const FILE_TO_READ: &str = "README.TXT";
4141

42-
use embedded_sdmmc::{Error, Mode, VolumeIdx};
42+
use embedded_sdmmc::blocking::{Error, Mode, VolumeIdx};
4343

44-
type VolumeManager = embedded_sdmmc::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
44+
type VolumeManager = embedded_sdmmc::blocking::VolumeManager<LinuxBlockDevice, Clock, 8, 4, 4>;
4545

46-
fn main() -> Result<(), embedded_sdmmc::Error<std::io::Error>> {
46+
fn main() -> Result<(), Error<std::io::Error>> {
4747
env_logger::init();
4848
let mut args = std::env::args().skip(1);
4949
let filename = args.next().unwrap_or_else(|| "/dev/mmcblk0".into());

examples/readme_test.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ impl embedded_hal::delay::DelayNs for FakeDelayer {
8080

8181
struct FakeTimesource();
8282

83-
impl embedded_sdmmc::TimeSource for FakeTimesource {
84-
fn get_timestamp(&self) -> embedded_sdmmc::Timestamp {
85-
embedded_sdmmc::Timestamp {
83+
impl embedded_sdmmc::blocking::TimeSource for FakeTimesource {
84+
fn get_timestamp(&self) -> embedded_sdmmc::blocking::Timestamp {
85+
embedded_sdmmc::blocking::Timestamp {
8686
year_since_1970: 0,
8787
zero_indexed_month: 0,
8888
zero_indexed_day: 0,
@@ -95,18 +95,18 @@ impl embedded_sdmmc::TimeSource for FakeTimesource {
9595

9696
#[derive(Debug, Clone)]
9797
enum Error {
98-
Filesystem(embedded_sdmmc::Error<embedded_sdmmc::SdCardError>),
99-
Disk(embedded_sdmmc::SdCardError),
98+
Filesystem(embedded_sdmmc::blocking::Error<embedded_sdmmc::blocking::SdCardError>),
99+
Disk(embedded_sdmmc::blocking::SdCardError),
100100
}
101101

102-
impl From<embedded_sdmmc::Error<embedded_sdmmc::SdCardError>> for Error {
103-
fn from(value: embedded_sdmmc::Error<embedded_sdmmc::SdCardError>) -> Error {
102+
impl From<embedded_sdmmc::blocking::Error<embedded_sdmmc::blocking::SdCardError>> for Error {
103+
fn from(value: embedded_sdmmc::blocking::Error<embedded_sdmmc::blocking::SdCardError>) -> Error {
104104
Error::Filesystem(value)
105105
}
106106
}
107107

108-
impl From<embedded_sdmmc::SdCardError> for Error {
109-
fn from(value: embedded_sdmmc::SdCardError) -> Error {
108+
impl From<embedded_sdmmc::blocking::SdCardError> for Error {
109+
fn from(value: embedded_sdmmc::blocking::SdCardError) -> Error {
110110
Error::Disk(value)
111111
}
112112
}
@@ -120,21 +120,21 @@ fn main() -> Result<(), Error> {
120120
// END Fake stuff that will be replaced with real peripherals
121121

122122
// Build an SD Card interface out of an SPI device, a chip-select pin and the delay object
123-
let sdcard = embedded_sdmmc::SdCard::new(sdmmc_spi, delay);
123+
let sdcard = embedded_sdmmc::blocking::SdCard::new(sdmmc_spi, delay);
124124
// Get the card size (this also triggers card initialisation because it's not been done yet)
125125
println!("Card size is {} bytes", sdcard.num_bytes()?);
126126
// Now let's look for volumes (also known as partitions) on our block device.
127127
// To do this we need a Volume Manager. It will take ownership of the block device.
128-
let volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, time_source);
128+
let volume_mgr = embedded_sdmmc::blocking::VolumeManager::new(sdcard, time_source);
129129
// Try and access Volume 0 (i.e. the first partition).
130130
// The volume object holds information about the filesystem on that volume.
131-
let volume0 = volume_mgr.open_volume(embedded_sdmmc::VolumeIdx(0))?;
131+
let volume0 = volume_mgr.open_volume(embedded_sdmmc::blocking::VolumeIdx(0))?;
132132
println!("Volume 0: {:?}", volume0);
133133
// Open the root directory (mutably borrows from the volume).
134134
let root_dir = volume0.open_root_dir()?;
135135
// Open a file called "MY_FILE.TXT" in the root directory
136136
// This mutably borrows the directory.
137-
let my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadOnly)?;
137+
let my_file = root_dir.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::blocking::Mode::ReadOnly)?;
138138
// Print the contents of the file, assuming it's in ISO-8859-1 encoding
139139
while !my_file.is_eof() {
140140
let mut buffer = [0u8; 32];

0 commit comments

Comments
 (0)