Skip to content

Commit

Permalink
Add an I2C/MPU6050 example (the timing is still broken).
Browse files Browse the repository at this point in the history
  • Loading branch information
mgottschlag committed Aug 29, 2021
1 parent 7b7d5d1 commit 00e9745
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ cortex-m-log = { version = "0.7", features = ["log-integration"] }
cfg-if = "0.1.10"
rtt-target = { version = "0.3.0", features = ["cortex-m"] }
panic-rtt-target = { version = "0.1.1", features = ["cortex-m"] }
mpu6050 = "0.1.4"

[features]
default = ["rt"]
Expand Down
49 changes: 49 additions & 0 deletions examples/i2c-mpu6050.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![deny(warnings)]
#![deny(unsafe_code)]
#![no_main]
#![no_std]

use hal::i2c::Config;
use hal::prelude::*;
use hal::stm32;
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;
use log::info;
use mpu6050::*;

#[macro_use]
mod utils;

#[entry]
fn main() -> ! {
utils::logger::init();

let dp = stm32::Peripherals::take().expect("cannot take peripherals");
let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals");

let mut rcc = dp.RCC.constrain();
let gpiob = dp.GPIOB.split(&mut rcc);

let sda = gpiob.pb9.into_alternate_open_drain();
let scl = gpiob.pb8.into_alternate_open_drain();

//let i2c = dp.I2C1.i2c(sda, scl, Config::new(100.khz()), &mut rcc);
/*let i2c = dp
.I2C1
.i2c(sda, scl, Config::with_timing(0xf020_151b), &mut rcc);*/
let i2c = dp
.I2C1
.i2c(sda, scl, Config::with_timing(0x3042_0f13), &mut rcc);

let mut mpu = Mpu6050::new(i2c);
let mut delay = cp.SYST.delay(&rcc.clocks);
mpu.init(&mut delay).expect("cannot initialize the MPU6050");

loop {
let acc = mpu.get_acc().expect("cannot read accelerometer");
let gyro = mpu.get_gyro().expect("cannot read gyro");
let temp = mpu.get_temp().expect("cannot read temperature");
info!("acc: {:?}, gyro: {:?}, temp: {:?}C", acc, gyro, temp);
}
}

0 comments on commit 00e9745

Please sign in to comment.