-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement embedded-hal-async traits using maybe-async-cfg (#31)
* implement embedded-hal-async traits using maybe-async-cfg * Run rustfmt * Have async variant of Magnetometer<_, MagContinuous>::magnetic_field return Poll::pending instead of nb::WouldBlock in case no data is ready * Add example for microbit V2 on embassy * Update to rust 1.75 for CI build, 1.79 for clippy * Always enable async feature of embedded-hal-mock * Clarify changelog entry and comment in linker script Co-authored-by: Diego Barrios Romero <eldruin@gmail.com> * Add reference to microbit-v2 example in README * Add reference to microbit-v2 example in rustdoc --------- Co-authored-by: Diego Barrios Romero <eldruin@gmail.com>
- Loading branch information
Showing
14 changed files
with
408 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## Settings for the micro:bit V2 example | ||
|
||
[target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
runner = "probe-rs run --chip nRF52833_xxAA --catch-reset --catch-hardfault --allow-erase-all" | ||
|
||
rustflags = [ | ||
"-C", "link-arg=-Tlink.x", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
//! Example of using the async feature of the LSM303AGR driver. | ||
//! Uses [embassy](https://embassy.dev) for the HAL and the executor. | ||
//! | ||
//! Make sure [probe-rs](https://probe.rs) is installed. | ||
//! | ||
//! Run me using | ||
//! | ||
//! ```sh | ||
//! cargo run --example microbit-v2 --target thumbv7em-none-eabihf --features async,microbit-example | ||
//! ``` | ||
use embassy_nrf::{self as hal, twim::Twim}; | ||
use embassy_time::Delay; | ||
use embedded_hal_async::delay::DelayNs; | ||
use hal::twim; | ||
use lsm303agr::Lsm303agr; | ||
use rtt_target::{rprintln, rtt_init_print}; | ||
|
||
use panic_rtt_target as _; // Panic handler | ||
|
||
hal::bind_interrupts!(struct Irqs { | ||
SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0 => twim::InterruptHandler<hal::peripherals::TWISPI0>; | ||
}); | ||
|
||
#[embassy_executor::main] | ||
async fn main(_s: embassy_executor::Spawner) -> ! { | ||
// Init RTT control block | ||
rtt_init_print!(); | ||
|
||
let _cp = cortex_m::Peripherals::take().unwrap(); | ||
// Use ``dp` to get a handle to the peripherals | ||
let dp = hal::init(Default::default()); | ||
|
||
rprintln!("Starting"); | ||
|
||
let config = twim::Config::default(); | ||
let twim0 = Twim::new(dp.TWISPI0, Irqs, dp.P0_16, dp.P0_08, config); | ||
|
||
let mut sensor = Lsm303agr::new_with_i2c(twim0); | ||
let id = sensor.magnetometer_id().await.unwrap(); | ||
rprintln!("{:#02x?}", id); | ||
|
||
sensor.init().await.unwrap(); | ||
sensor | ||
.set_mag_mode_and_odr( | ||
&mut Delay, | ||
lsm303agr::MagMode::HighResolution, | ||
lsm303agr::MagOutputDataRate::Hz10, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let Ok(mut sensor) = sensor.into_mag_continuous().await else { | ||
panic!("Error enabling continuous mode") | ||
}; | ||
sensor.mag_enable_low_pass_filter().await.unwrap(); | ||
loop { | ||
if sensor.mag_status().await.unwrap().xyz_new_data() { | ||
let data = sensor.magnetic_field().await.unwrap(); | ||
rprintln!( | ||
"Magnetic field: x {} y {} z {}", | ||
data.x_nt(), | ||
data.y_nt(), | ||
data.z_nt() | ||
); | ||
} else { | ||
rprintln!("No data") | ||
} | ||
Delay.delay_ms(200).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Linker script for the nRF52833 MCU on the microbit:v2 necessary for the example. Please disregard it otherwise. | ||
MEMORY | ||
{ | ||
/* NOTE K = KiBi = 1024 bytes */ | ||
FLASH : ORIGIN = 0x00000000, LENGTH = 512K | ||
RAM : ORIGIN = 0x20000000, LENGTH = 128K | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.