Brújula

Leeremos la dirección desde una brújula I2C, y registraremos las lecturas en un puerto serie. Si tienes tiempo, prueba a mostrarlo también en los LED o usa los botones de alguna forma.

Sugerencias:

  • Consulta la documentación sobre los crates lsm303agr y microbit-v2, así como el hardware de micro:bit.
  • La unidad de medición inercial LSM303AGR está conectada al bus I2C interno.
  • TWI es otro nombre para I2C, por lo que el periférico I2C maestro se llama TWIM.
  • The LSM303AGR driver needs something implementing the embedded_hal::i2c::I2c trait. The microbit::hal::Twim struct implements this.
  • Tienes una estructura microbit::Board con campos para los distintos pines y periféricos.
  • También puedes consultar la [hoja de datos nRF52833]nRF52833 datasheet si quieres, pero no debería ser necesario para este ejercicio.

Descarga la plantilla de ejercicio y busca los siguientes archivos en el directorio compass.

src/main.rs:

#![no_main] #![no_std] extern crate panic_halt as _; use core::fmt::Write; use cortex_m_rt::entry; use microbit::{hal::{Delay, uarte::{Baudrate, Parity, Uarte}}, Board}; #[entry] fn main() -> ! { let mut board = Board::take().unwrap(); // Configure serial port. let mut serial = Uarte::new( board.UARTE0, board.uart.into(), Parity::EXCLUDED, Baudrate::BAUD115200, ); // Use the system timer as a delay provider. let mut delay = Delay::new(board.SYST); // Set up the I2C controller and Inertial Measurement Unit. // TODO writeln!(serial, "Ready.").unwrap(); loop { // Read compass data and log it to the serial port. // TODO } }

Cargo.toml (you shouldn’t need to change this):

[workspace] [package] name = "compass" version = "0.1.0" edition = "2021" publish = false [dependencies] cortex-m-rt = "0.7.3" embedded-hal = "1.0.0" lsm303agr = "1.1.0" microbit-v2 = "0.15.0" panic-halt = "0.2.0"

Embed.toml (you shouldn’t need to change this):

[default.general] chip = "nrf52833_xxAA" [debug.gdb] enabled = true [debug.reset] halt_afterwards = true

.cargo/config.toml (you shouldn’t need to change this):

[build] target = "thumbv7em-none-eabihf" # Cortex-M4F [target.'cfg(all(target_arch = "arm", target_os = "none"))'] rustflags = ["-C", "link-arg=-Tlink.x"]

Consulta la salida de serie en Linux con:

picocom --baud 115200 --imap lfcrlf /dev/ttyACM0

En Mac OS debería ser algo como lo siguiente (el nombre del dispositivo puede ser algo diferente):

picocom --baud 115200 --imap lfcrlf /dev/tty.usbmodem14502

Pulsa Ctrl+A Ctrl+Q para salir de Picocom.