๋‚˜์นจ๋ฐ˜

I2C ๋‚˜์นจ๋ฐ˜์—์„œ ๋ฐฉํ–ฅ์„ ์ฝ๊ณ  ํŒ๋…๊ฐ’์„ ์ง๋ ฌ ํฌํŠธ์— ๊ธฐ๋กํ•˜์„ธ์š”. ์‹œ๊ฐ„์ด ์žˆ์œผ๋ฉด ์–ด๋–ป๊ฒŒ๋“  LED์— ํ‘œ์‹œํ•˜๊ฑฐ๋‚˜ ๋ฒ„ํŠผ์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

ํžŒํŠธ:

  • lsm303agr ๋ฐ microbit-v2 ํฌ๋ ˆ์ดํŠธ ๋ฐ micro:bit ํ•˜๋“œ์›จ์–ด์˜ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.
  • LSM303AGR ๊ด€์„ฑ ์ธก์ • ์žฅ์น˜๋Š” ๋‚ด๋ถ€ I2C ๋ฒ„์Šค์— ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค.
  • TWI๋Š” I2C์˜ ๋˜ ๋‹ค๋ฅธ ์ด๋ฆ„์ด๋ฏ€๋กœ I2C ๋งˆ์Šคํ„ฐ ์ฃผ๋ณ€๊ธฐ๊ธฐ๋Š” TWIM์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
  • LSM303AGR ๋“œ๋ผ์ด๋ฒ„์—๋Š” embedded_hal::blocking::i2c::WriteRead ํŠธ๋ ˆ์ž‡์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. microbit::hal::Twim ๊ตฌ์กฐ์ฒด๊ฐ€ ์ด๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹ค์–‘ํ•œ ํ•€๊ณผ ์ฃผ๋ณ€๊ธฐ๊ธฐ์— ๊ด€ํ•œ ํ•„๋“œ๊ฐ€ ์žˆ๋Š” microbit::Board ๊ตฌ์กฐ์ฒด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์›ํ•˜๋Š” ๊ฒฝ์šฐ nRF52833 ๋ฐ์ดํ„ฐ์‹œํŠธ๋ฅผ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ด ์—ฐ์Šต์—์„œ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค.

์—ฐ์Šต ํ…œํ”Œ๋ฆฟ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  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::uarte::{Baudrate, Parity, Uarte}, Board};

#[entry]
fn main() -> ! {
    let 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 (๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†์Œ):

[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 = "0.3.0"
microbit-v2 = "0.13.0"
panic-halt = "0.2.0"

Embed.toml (๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†์Œ):

[default.general]
chip = "nrf52833_xxAA"

[debug.gdb]
enabled = true

[debug.reset]
halt_afterwards = true

.cargo/config.toml (๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†์Œ):

[build]
target = "thumbv7em-none-eabihf" # Cortex-M4F

[target.'cfg(all(target_arch = "arm", target_os = "none"))']
rustflags = ["-C", "link-arg=-Tlink.x"]

๋‹ค์Œ์„ ์‚ฌ์šฉํ•˜์—ฌ Linux์—์„œ ์ง๋ ฌ ์ถœ๋ ฅ์„ ํ™•์ธํ•˜์„ธ์š”.

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

๋˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด Mac OS(๊ธฐ๊ธฐ ์ด๋ฆ„์€ ์•ฝ๊ฐ„ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ)์—์„œ ํ™•์ธํ•˜์„ธ์š”.

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

Ctrl+A Ctrl+Q๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ picocom์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.