Module bitbang_hal::i2c
source · Expand description
§Synchronous implementation of embedded-hal I2C traits based on GPIO bitbang
This implementation consumes the following hardware resources:
- A periodic timer to mark clock cycles
- Two GPIO pins for SDA and SCL lines.
Note that the current implementation does not support I2C clock stretching.
§Hardware requirements
- Configure GPIO pins as Open-Drain outputs.
- Configure timer frequency to be twice the desired I2C clock frequency.
§Blue Pill example
Here is a sample code for LM75A I2C temperature sensor on Blue Pill or any other stm32f1xx board:
use stm32f1xx_hal as hal;
use hal::{prelude::*, timer::Timer, stm32};
use lm75::{Lm75, SlaveAddr};
use bitbang_hal;
// ...
let pdev = stm32::Peripherals::take().unwrap();
let mut flash = pdev.FLASH.constrain();
let mut rcc = pdev.RCC.constrain();
let mut gpioa = pdev.GPIOA.split(&mut rcc.apb2);
let clocks = rcc
.cfgr
.use_hse(8.mhz())
.sysclk(32.mhz())
.pclk1(16.mhz())
.freeze(&mut flash.acr);
let tmr = Timer::tim3(pdev.TIM3, &clocks, &mut rcc.apb1).start_count_down(200.khz());
let scl = gpioa.pa1.into_open_drain_output(&mut gpioa.crl);
let sda = gpioa.pa2.into_open_drain_output(&mut gpioa.crl);
let i2c = bitbang_hal::i2c::I2cBB::new(scl, sda, tmr);
let mut sensor = Lm75::new(i2c, SlaveAddr::default());
let temp = sensor.read_temperature().unwrap();
//...
Structs§
- Bit banging I2C device
Enums§
- I2C error