-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi2c_bme280.rs
53 lines (40 loc) · 1.39 KB
/
i2c_bme280.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Target board: NUCLEO-F042K6
//
// Connections:
// D4 - SDA
// D5 - SCL
use avatar_probe_rs::open_probe;
use cortex_m::interrupt;
use stm32f0xx_hal::prelude::*;
use stm32f0xx_hal::stm32;
use stm32f0xx_hal::i2c::I2c;
use std::time::Duration;
use std::thread;
use bme280::BME280;
use linux_embedded_hal::Delay;
fn main() {
let interface = open_probe();
vcell::set_memory_interface(interface);
let mut p = stm32::Peripherals::take().unwrap();
interrupt::free(|cs| {
let mut rcc = p.RCC.configure().freeze(&mut p.FLASH);
let gpiob = p.GPIOB.split(&mut rcc);
// Configure pins for I2C
let sda = gpiob.pb7.into_alternate_af1(cs);
let scl = gpiob.pb6.into_alternate_af1(cs);
// Configure I2C with 100kHz rate
let i2c = I2c::i2c1(p.I2C1, (scl, sda), 100.khz(), &mut rcc);
let mut bme280 = BME280::new_primary(i2c, Delay);
// initialize the sensor
bme280.init().unwrap();
loop {
// measure temperature, pressure, and humidity
let measurements = bme280.measure().unwrap();
println!();
println!("Relative Humidity = {}%", measurements.humidity);
println!("Temperature = {} deg C", measurements.temperature);
println!("Pressure = {} pascals", measurements.pressure);
thread::sleep(Duration::from_millis(1000));
}
});
}