Unit 8.1 - The Rust embedded ecosystem
Exercise 8.1.1: LSM303AGR ID
Use our newly gained knowledge to get our first application running and read out the ID of the LSM303AGR accelerometer. We can communicate with the LSM303AGR using the I2C that is present on the micro:bit board. Note that the nRF52833 supports I2C with its TWIM (Two-Wire Interface Master) peripheral.
To get started we'll setup the i2c peripheral on our development kit and read out the ID register of the LSM303AGR accelerometer.
The starting point can be found in the file at exercises/8-embedded/1-embedded-ecosystem/src/main.rs
in the repository.
Try to run the existing project and then fill in the functionality as instructed by the comments.
To use that project, you can use the following commands from inside that folder using the terminal:
cargo build
: Builds the projectcargo run
: Builds the project, flashes it to the device and listens for any logs which it will display in the terminal. (This uses theprobe-rs run
tool)
In both cases you can add the --release
flag to turn on optimizations.
Some pointers to help you get started
- You can find the documentation on the HAL here on docs.embassy.dev. This website houses the docs for embassy for every available chip. Normally you'd search at docs.rs, but that only shows one possible configuration of the HAL.
- To find out how to configure I2C for the nRF52833: embassy-nrf TWIM demo example.
- You can find the LSM303AGR data sheet here: https://www.st.com/resource/en/datasheet/lsm303agr.pdf. You can find the accelerometer device ID in the
WHO_AM_I_A
register, at register address0x0F
. You'll need0x19
to address the accelerometer itself. - Use the
Twim::blocking_write_read
method to first write the device address, then write the register address, and then read its contents into a buffer.