Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

単一のレジスタを読み取る

それでは、ここまでの理論を実践に移しましょう!

USARTペリフェラルのときと同じように、あなたが main に到達する前に、必要な初期化はすべてこちらで済ませてあります。そのため、あなたが扱う必要があるのは次のレジスタだけです:

  • CR2. コントロールレジスタ 2。
  • ISR. 割り込みおよびステータスレジスタ。
  • TXDR. 送信データレジスタ。
  • RXDR. 受信データレジスタ。

これらのレジスタは、リファレンスマニュアルの次のセクションに記載されています:

セクション 28.7 I2C レジスタ - 868 ページ - リファレンスマニュアル

I2C1 ペリフェラルを、PB6SCL)および PB7SDA)のピンと組み合わせて使用します。

今回は、センサーがボード上にあり、すでにマイクロコントローラに接続されているため、配線する必要はありません。ただし、操作しやすくするために、シリアル / Bluetooth モジュールを F3 から取り外しておくことをお勧めします。後で、このボードをかなり動かすことになります。

あなたの課題は、磁力計の IRA_REG_M レジスタの内容を読み取るプログラムを書くことです。このレジスタは読み取り専用で、常に 0b01001000 という値を含んでいます。

マイクロコントローラは I2C マスターの役割を担い、LSM303DLHC 内部の磁力計が I2C スレーブになります。

以下がスターターコードです。TODO を実装する必要があります。

#![deny(unsafe_code)]
#![no_main]
#![no_std]

#[allow(unused_imports)]
use aux14::{entry, iprint, iprintln, prelude::*};

// Slave address
const MAGNETOMETER: u16 = 0b0011_1100;

// Addresses of the magnetometer's registers
const OUT_X_H_M: u8 = 0x03;
const IRA_REG_M: u8 = 0x0A;

#[entry]
fn main() -> ! {
    let (i2c1, _delay, mut itm) = aux14::init();

    // Stage 1: Send the address of the register we want to read to the
    // magnetometer
    {
        // TODO Broadcast START

        // TODO Broadcast the MAGNETOMETER address with the R/W bit set to Write

        // TODO Send the address of the register that we want to read: IRA_REG_M
    }

    // Stage 2: Receive the contents of the register we asked for
    let byte = {
        // TODO Broadcast RESTART

        // TODO Broadcast the MAGNETOMETER address with the R/W bit set to Read

        // TODO Receive the contents of the register

        // TODO Broadcast STOP
        0
    };

    // Expected output: 0x0A - 0b01001000
    iprintln!(&mut itm.stim[0], "0x{:02X} - 0b{:08b}", IRA_REG_M, byte);

    loop {}
}

追加の助けとして、扱うことになる正確なビットフィールドは次のとおりです:

  • CR2: SADD1, RD_WRN, NBYTES, START, AUTOEND
  • ISR: TXIS, RXNE, TC
  • TXDR: TXDATA
  • RXDR: RXDATA