-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathi2c_scanner.rs
61 lines (47 loc) · 1.37 KB
/
i2c_scanner.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
54
55
56
57
58
59
60
61
//! Example of using I2C.
//! Scans available I2C devices on bus and print the result.
#![no_std]
#![no_main]
use core::ops::Range;
use panic_semihosting as _;
use cortex_m_rt::entry;
use cortex_m_semihosting::{hprint, hprintln};
use stm32f7xx_hal::{self as hal, gpio::GpioExt, pac, prelude::*};
const VALID_ADDR_RANGE: Range<u8> = 0x08..0x78;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze();
let gpiob = dp.GPIOB.split();
// Configure I2C1
let scl = gpiob.pb8.into_alternate_open_drain::<4>();
let sda = gpiob.pb7.into_alternate_open_drain::<4>();
let mut i2c = hal::i2c::BlockingI2c::i2c1(
dp.I2C1,
(scl, sda),
hal::i2c::Mode::fast(100_000.Hz()),
&clocks,
&mut rcc.apb1,
50_000,
);
hprintln!("Start i2c scanning...");
hprintln!();
for addr in 0x00_u8..0x80 {
// Write the empty array and check the slave response.
let byte: [u8; 1] = [0; 1];
if VALID_ADDR_RANGE.contains(&addr) && i2c.write(addr, &byte).is_ok() {
hprint!("{:02x}", addr);
} else {
hprint!("..");
}
if addr % 0x10 == 0x0F {
hprintln!();
} else {
hprint!(" ");
}
}
hprintln!();
hprintln!("Done!");
loop {}
}