Skip to content

Commit 835bd85

Browse files
authored
Merge pull request #164 from Piroro-hs/i2c-fix-clock-i2c3
Fix I2C clock and support I2C3
2 parents 8fa675e + dee229c commit 835bd85

File tree

4 files changed

+438
-331
lines changed

4 files changed

+438
-331
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222
devices. ([#99](https://github.com/stm32-rs/stm32f3xx-hal/pull/99))
2323
- Support for I2C transfer of more than 255 bytes, and 0 byte write ([#154](https://github.com/stm32-rs/stm32f3xx-hal/pull/154))
2424
- Support for HSE bypass and CSS ([#156](https://github.com/stm32-rs/stm32f3xx-hal/pull/156))
25+
- Impls for missing I2C pin definitions ([#164](https://github.com/stm32-rs/stm32f3xx-hal/pull/164))
26+
- Support I2C3 ([#164](https://github.com/stm32-rs/stm32f3xx-hal/pull/164))
2527

2628
### Changed
2729

@@ -33,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3335

3436
- Fixed [#151][] not being able to generate 72 MHz HCLK for stm32f303xc devices
3537
([#152](https://github.com/stm32-rs/stm32f3xx-hal/pull/152))
38+
- Wrong I2C clock source ([#164](https://github.com/stm32-rs/stm32f3xx-hal/pull/164))
3639

3740
[#151]: https://github.com/stm32-rs/stm32f3xx-hal/issues/151
3841

@@ -47,6 +50,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4750
not mean, that we guarantee any MSRV policy. It is rather for documentation
4851
purposes and if a new useful feature arises, we will increase the MSRV.
4952
([#170](https://github.com/stm32-rs/stm32f3xx-hal/pull/170))
53+
- Removed I2C2 support for `stm32f303x6`, `stm32f303x8` and `stm32f328` targets. ([#164](https://github.com/stm32-rs/stm32f3xx-hal/pull/164))
54+
- `I2c::i2c1` and `I2c::i2c2` functions are renamed to `I2c::new`. ([#164](https://github.com/stm32-rs/stm32f3xx-hal/pull/164))
5055

5156
## [v0.5.0] - 2020-07-21
5257

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,7 @@ required-features = ["stm32f303"]
134134
[[example]]
135135
name = "adc"
136136
required-features = ["stm32f303"]
137+
138+
[[example]]
139+
name = "i2c_scanner"
140+
required-features = ["stm32f303xc"]

examples/i2c_scanner.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! Example of using I2C.
2+
//! Scans available I2C devices on bus and print the result.
3+
//! Appropriate pull-up registers should be installed on I2C bus.
4+
//! Target board: STM32F3DISCOVERY
5+
6+
#![no_std]
7+
#![no_main]
8+
9+
use core::ops::Range;
10+
11+
use panic_semihosting as _;
12+
13+
use cortex_m::asm;
14+
use cortex_m_rt::entry;
15+
use cortex_m_semihosting::{hprint, hprintln};
16+
17+
use stm32f3xx_hal::{self as hal, pac, prelude::*};
18+
19+
const VALID_ADDR_RANGE: Range<u8> = 0x08..0x78;
20+
21+
#[entry]
22+
fn main() -> ! {
23+
let dp = pac::Peripherals::take().unwrap();
24+
25+
let mut flash = dp.FLASH.constrain();
26+
let mut rcc = dp.RCC.constrain();
27+
let clocks = rcc.cfgr.freeze(&mut flash.acr);
28+
29+
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
30+
31+
// Configure I2C1
32+
let pins = (
33+
gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl), // SCL
34+
gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl), // SDA
35+
);
36+
let mut i2c = hal::i2c::I2c::new(dp.I2C1, pins, 100.khz(), clocks, &mut rcc.apb1);
37+
38+
hprintln!("Start i2c scanning...").expect("Error using hprintln.");
39+
hprintln!().unwrap();
40+
41+
for addr in 0x00_u8..0x80 {
42+
// Write the empty array and check the slave response.
43+
if VALID_ADDR_RANGE.contains(&addr) && i2c.write(addr, &[]).is_ok() {
44+
hprint!("{:02x}", addr).unwrap();
45+
} else {
46+
hprint!("..").unwrap();
47+
}
48+
if addr % 0x10 == 0x0F {
49+
hprintln!().unwrap();
50+
} else {
51+
hprint!(" ").unwrap();
52+
}
53+
}
54+
55+
hprintln!().unwrap();
56+
hprintln!("Done!").unwrap();
57+
58+
loop {
59+
asm::wfi();
60+
}
61+
}

0 commit comments

Comments
 (0)