Skip to content

Commit

Permalink
Merge pull request #4 from xgroleau/feat/async-support
Browse files Browse the repository at this point in the history
Feat/async support
  • Loading branch information
xgroleau authored Apr 18, 2022
2 parents 6314bb6 + fb35754 commit e52a46a
Show file tree
Hide file tree
Showing 7 changed files with 501 additions and 34 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exclude = [
[dependencies]
# TODO: Migrate to embedded-hal repo
embedded-hal = { version = "1.0.0-alpha.7", git = "https://github.com/embassy-rs/embedded-hal", branch = "embassy2" }
embedded-hal-async = { version = "0.0.1", git = "https://github.com/embassy-rs/embedded-hal", branch = "embassy2" }
bit = "0.1.1"
defmt = { version = "0.3", optional = true }
embedded-storage = "0.3.0"
Expand All @@ -29,7 +30,7 @@ embedded-storage = "0.3.0"
cortex-m = "0.7.3"
cortex-m-rt = "0.7.0"
embassy = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "nightly", "unstable-traits"] }
embassy-nrf = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-traits"] }
embassy-nrf = { version = "0.1.0", git = "https://github.com/embassy-rs/embassy", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-traits", "nightly"] }

defmt = "0.3"
defmt-rtt = "0.3"
Expand Down
73 changes: 73 additions & 0 deletions examples/asynchronous.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use defmt::info;
use defmt_rtt as _;
use embassy::{
executor::Spawner,
time::{Duration, Timer},
};
use embassy_nrf::{
gpio::{Level, Output, OutputDrive},
interrupt,
peripherals::{P0_17, TWISPI0},
spim::{self, Spim},
Peripherals,
};
use embedded_hal_async::spi::ExclusiveDevice;
use mx25r::{
address::{Address, Page, Sector},
asynchronous::AsyncMX25R6435F,
error::Error,
};
use panic_probe as _;

type DkMX25R<'a> = AsyncMX25R6435F<ExclusiveDevice<Spim<'a, TWISPI0>, Output<'a, P0_17>>>;

async fn wait_wip(mx25r: &mut DkMX25R<'_>) {
while let Err(Error::Busy) = mx25r.poll_wip().await {
Timer::after(Duration::from_millis(100)).await;
}
}

#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
let mut spi_config = spim::Config::default();
spi_config.frequency = spim::Frequency::M16;

let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0);

// See https://infocenter.nordicsemi.com/index.jsp?topic=%2Fug_nrf52840_dk%2FUG%2Fdk%2Fhw_external_memory.html
let spi = Spim::new(p.TWISPI0, irq, p.P0_19, p.P0_21, p.P0_20, spi_config);
let cs = Output::new(p.P0_17, Level::High, OutputDrive::Standard);
let spi_dev = ExclusiveDevice::new(spi, cs);

let mut memory = AsyncMX25R6435F::new(spi_dev);

let mut buff = [0];
let page = Page(0);
let sector = Sector(0);
let addr = Address::from_page(sector, page);

memory.read(addr, &mut buff).await.unwrap();
info!("Value before erase {}", buff);

info!("Erasing first sector");
memory.erase_sector(sector).await.unwrap();
wait_wip(&mut memory).await;

memory.read(addr, &mut buff).await.unwrap();
assert_eq!(buff[0], 0xff);

info!("Writing 42");
memory.write_page(sector, page, &[42]).await.unwrap();
wait_wip(&mut memory).await;

memory.read(addr, &mut buff).await.unwrap();
assert_eq!(buff[0], 42);

// Exit
info!("Example completed");
cortex_m::asm::bkpt();
}
5 changes: 2 additions & 3 deletions examples/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let cs = Output::new(p.P0_17, Level::High, OutputDrive::Standard);
let spi_dev = ExclusiveDevice::new(spi, cs);


let mut memory = MX25R6435F::new(spi_dev);

let mut buff = [0];
Expand All @@ -58,14 +57,14 @@ async fn main(_spawner: Spawner, p: Peripherals) {
memory.erase_sector(sector).unwrap();
wait_wip(&mut memory).await;

memory.read_fast(addr, &mut buff).unwrap();
memory.read(addr, &mut buff).unwrap();
assert_eq!(buff[0], 0xff);

info!("Writing 42");
memory.write_page(sector, page, &[42]).unwrap();
wait_wip(&mut memory).await;

memory.read_fast(addr, &mut buff).unwrap();
memory.read(addr, &mut buff).unwrap();
assert_eq!(buff[0], 42);

// Exit
Expand Down
Loading

0 comments on commit e52a46a

Please sign in to comment.