Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed I2C reading problems #150

Merged
merged 3 commits into from
Dec 14, 2019
Merged

Fixed I2C reading problems #150

merged 3 commits into from
Dec 14, 2019

Conversation

Okarin99
Copy link
Contributor

@Okarin99 Okarin99 commented Dec 6, 2019

This PR fixes some issues I had with I2C.
I wanted to query exactly 2 bytes over I2C, but I got an I2C arbitration error.
Because of this I fixed the code to follow exactly the rules described here.
Now it is working fine for me.

Example code:

#![no_std]
#![no_main]

use cortex_m::iprintln;
use cortex_m_rt::entry;
use mpu6050::*;
use stm32f1xx_hal::{delay::Delay, i2c, prelude::*, stm32};

#[allow(unused_imports)]
use panic_itm;

#[entry]
fn main() -> ! {
    let device = stm32::Peripherals::take().unwrap();
    let mut cp = cortex_m::Peripherals::take().unwrap();
    let stim = &mut cp.ITM.stim[0];

    let mut flash = device.FLASH.constrain();
    let mut rcc = device.RCC.constrain();

    let clocks = rcc.cfgr.freeze(&mut flash.acr);
    let mut afio = device.AFIO.constrain(&mut rcc.apb2);

    let mode = i2c::Mode::Standard {
        frequency: 100_000.hz(),
    };

    let mut gpiob = device.GPIOB.split(&mut rcc.apb2);

    let scl_pin = gpiob.pb8.into_alternate_open_drain(&mut gpiob.crh);
    let sda_pin = gpiob.pb9.into_alternate_open_drain(&mut gpiob.crh);

    let delay = Delay::new(cp.SYST, clocks);

    let i2c_val = i2c::BlockingI2c::i2c1(
        device.I2C1,
        (scl_pin, sda_pin),
        &mut afio.mapr,
        mode,
        clocks,
        &mut rcc.apb1,
        50000,
        10,
        50000,
        50000,
    );

    let mut mpu = Mpu6050::new(i2c_val, delay);
    mpu.init().unwrap();
    mpu.soft_calib(Steps(100)).unwrap();
    mpu.calc_variance(Steps(50)).unwrap();

    iprintln!(stim, "Calibrated with bias: {:?}", mpu.get_bias().unwrap());
    iprintln!(
        stim,
        "Calculated variance: {:?}",
        mpu.get_variance().unwrap()
    );

    loop {
        /*
        // Didn't work before
        let mut buf: [u8; 2] = [0; 2];
        mpu.read_bytes(0x41, &mut buf).unwrap();
        iprintln!(stim, "{:?}", buf);
        */

        /*
        let acc = mpu.get_acc().unwrap();
        let gyro = mpu.get_gyro().unwrap();
        let temp = mpu.get_temp().unwrap();
        iprintln!(stim, "Acc: {:?}; Gyro: {:?}; Temp: {:?}", acc, gyro, temp);
        */

        let acc = mpu.get_acc_angles_avg(Steps(200)).unwrap();
        iprintln!(stim, "Acc: {:?}", acc);
    }
}

@TheZoq2 TheZoq2 mentioned this pull request Dec 13, 2019
@TheZoq2
Copy link
Member

TheZoq2 commented Dec 14, 2019

Thanks for the PR, and sorry for the review delay :)

The code looks great!

A comment pointing to the document you based it on in the code and a changelog entry can't hurt, I can take care of that :)

@TheZoq2 TheZoq2 merged commit c55c62d into stm32-rs:master Dec 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants