This commit is contained in:
Alexey 2026-05-21 16:28:28 +03:00
commit a43f95d21a
4 changed files with 206 additions and 4 deletions

118
Cargo.lock generated
View file

@ -23,6 +23,12 @@ version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719"
[[package]]
name = "bitflags"
version = "2.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3"
[[package]]
name = "bytemuck"
version = "1.25.0"
@ -76,7 +82,7 @@ checksum = "e37549a379a9e0e6e576fd208ee60394ccb8be963889eebba3ffe0980364f472"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.117",
]
[[package]]
@ -180,6 +186,67 @@ dependencies = [
"stable_deref_trait",
]
[[package]]
name = "libm"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
[[package]]
name = "lsm303agr"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f24b486ceef047c043c19c24669379f1049269927430c09f0444aac4685d2c2f"
dependencies = [
"bitflags",
"embedded-hal 1.0.0",
"maybe-async-cfg",
"nb 1.1.0",
]
[[package]]
name = "manyhow"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b33efb3ca6d3b07393750d4030418d594ab1139cee518f0dc88db70fec873587"
dependencies = [
"manyhow-macros",
"proc-macro2",
"quote",
"syn 1.0.109",
"syn 2.0.117",
]
[[package]]
name = "manyhow-macros"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46fce34d199b78b6e6073abf984c9cf5fd3e9330145a93ee0738a7443e371495"
dependencies = [
"proc-macro-utils",
"proc-macro2",
"quote",
]
[[package]]
name = "maybe-async-cfg"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dbfaa67a76e2623580df07d6bb5e7956c0a4bae4b418314083a9c619bd66627"
dependencies = [
"manyhow",
"proc-macro2",
"pulldown-cmark",
"quote",
"syn 1.0.109",
]
[[package]]
name = "memchr"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
[[package]]
name = "microbit"
version = "0.1.0"
@ -189,6 +256,8 @@ dependencies = [
"embedded-hal 1.0.0",
"embedded-io",
"heapless 0.9.3",
"libm",
"lsm303agr",
"microbit-v2",
"panic-halt",
]
@ -294,6 +363,17 @@ version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
[[package]]
name = "proc-macro-utils"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eeaf08a13de400bc215877b5bdc088f241b12eb42f0a548d3390dc1c56bb7071"
dependencies = [
"proc-macro2",
"quote",
"smallvec",
]
[[package]]
name = "proc-macro2"
version = "1.0.106"
@ -303,6 +383,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "pulldown-cmark"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "679341d22c78c6c649893cbd6c3278dcbe9fc4faa62fea3a9296ae2b50c14625"
dependencies = [
"bitflags",
"memchr",
"unicase",
]
[[package]]
name = "quote"
version = "1.0.45"
@ -342,12 +433,29 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.117"
@ -371,6 +479,12 @@ version = "1.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40ce102ab67701b8526c123c1bab5cbe42d7040ccfd0f64af1a385808d2f43de"
[[package]]
name = "unicase"
version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142"
[[package]]
name = "unicode-ident"
version = "1.0.24"
@ -425,5 +539,5 @@ checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.117",
]

View file

@ -11,3 +11,5 @@ panic-halt = "1.0.0"
cortex-m = "0.7.7"
embedded-io = "0.7.1"
heapless = "0.9.3"
lsm303agr = "1.1.0"
libm = "0.2.16"

64
src/bin/i2c.rs Normal file
View file

@ -0,0 +1,64 @@
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use embedded_io::{Read, Write};
use heapless::Vec;
use lsm303agr::{AccelMode, AccelOutputDataRate, Lsm303agr, MagMode, MagOutputDataRate};
use microbit::{hal::{Timer, Twim, uarte::{self, Baudrate, Parity}}, pac::twim0::frequency::FREQUENCY_A};
use panic_halt as _;
#[entry]
fn main() -> ! {
let board = microbit::Board::take().unwrap();
let i2c = Twim::new(board.TWIM0, board.i2c_internal.into(), FREQUENCY_A::K100);
let mut timer = Timer::new(board.TIMER0);
let mut sensor = Lsm303agr::new_with_i2c(i2c);
sensor.init().unwrap();
sensor.set_accel_mode_and_odr(&mut timer, AccelMode::HighResolution, AccelOutputDataRate::Hz50).unwrap();
sensor.set_mag_mode_and_odr(&mut timer, MagMode::HighResolution, MagOutputDataRate::Hz50).unwrap();
let mut sensor = sensor.into_mag_continuous().ok().unwrap();
let tx_buf = cortex_m::singleton!(TX_BUF: [u8; 1] = [0u8; 1]).unwrap();
let rx_buf = cortex_m::singleton!(RX_BUF: [u8; 1] = [0u8; 1]).unwrap();
let serial = uarte::Uarte::new(
board.UARTE0,
board.uart.into(),
Parity::EXCLUDED,
Baudrate::BAUD115200,
);
let (mut tx, mut rx) = serial.split(tx_buf, rx_buf).unwrap();
let mut buffer: Vec<u8, 4> = Vec::new();
loop {
let mut b = [0u8;1];
rx.read(&mut b).unwrap();
if buffer.push(b[0]).is_err() {
buffer.remove(0);
buffer.push(b[0]).unwrap();
}
if buffer.ends_with(b"mag\r") {
let (x, y, z) = sensor.magnetic_field()
.unwrap()
.xyz_nt();
write!(tx, "X={x} Y={y} Z={z}\r\n")
.unwrap();
tx.flush()
.unwrap();
} else if buffer.ends_with(b"acc\r") {
let (x, y, z) = sensor.acceleration().unwrap().xyz_mg();
write!(tx, "X={x} Y={y} Z={z}\r\n").unwrap();
tx.flush().unwrap();
}
}
}

View file

@ -7,6 +7,14 @@ use heapless::Vec;
use microbit::{hal::uarte::{self, Baudrate, Parity}};
use panic_halt as _;
fn reverse(v: &mut Vec<u8, 32>) {
// remove \r\n
v.remove(v.len() - 1);
v.reverse();
let _ = v.push(b'\r');
let _ = v.push(b'\n');
}
#[entry]
fn main() -> ! {
let board = microbit::Board::take().unwrap();
@ -22,11 +30,25 @@ fn main() -> ! {
);
let (_tx, mut rx) = serial.split(tx_buf, rx_buf).unwrap();
let (mut tx, mut rx) = serial.split(tx_buf, rx_buf).unwrap();
let mut buffer: Vec<u8, 32> = Vec::new();
loop {
rx.read(buffer.as_mut_slice()).unwrap();
let mut b = [0u8;1];
rx.read(&mut b).unwrap();
if buffer.push(b[0]).is_err() {
buffer.remove(0);
buffer.push(b[0]).unwrap();
}
if buffer.ends_with(b"\r") {
reverse(&mut buffer);
let out = unsafe { str::from_utf8_unchecked(buffer.as_slice()) };
write!(tx, "{out}").unwrap();
buffer.clear();
}
tx.flush().unwrap();
}
}