-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmicrobit-v2.rs
74 lines (63 loc) · 2.09 KB
/
microbit-v2.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
}
}