[][src]Module bitbang_hal::i2c

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

  1. Configure GPIO pins as Open-Drain outputs.
  2. 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

I2cBB

Bit banging I2C device

Enums

Error

I2C error