Skip to content

Commit

Permalink
i2c: Add I2C Controller implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
astapleton committed Nov 15, 2024
1 parent 4e2bed9 commit 53372b2
Show file tree
Hide file tree
Showing 6 changed files with 1,349 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ embedded-hal = "1.0.0"
defmt = { version = "0.3.8", optional = true }
paste = "1.0.15"
log = { version = "0.4.20", optional = true}
nb = "1.1.0"

[dev-dependencies]
log = { version = "0.4.20"}
Expand Down
64 changes: 64 additions & 0 deletions examples/i2c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![deny(warnings)]
#![no_main]
#![no_std]

#[macro_use]
mod utilities;
use embedded_hal::{delay::DelayNs, i2c::I2c};
use fugit::SecsDurationU32;
use stm32h5xx_hal::{delay::Delay, pac, prelude::*};

use cortex_m_rt::entry;

use log::info;

#[entry]
fn main() -> ! {
utilities::logger::init();
log::set_max_level(log::LevelFilter::Debug);
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();

// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = pwr.freeze();

// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc.sys_ck(100.MHz()).freeze(pwrcfg, &dp.SBS);

let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);

let mut delay = Delay::new(cp.SYST, &ccdr.clocks);
let duration = SecsDurationU32::secs(1).to_millis();

// Configure the SCL and the SDA pin for our I2C bus
let scl = gpiob.pb5.into_alternate_open_drain();
let sda = gpiob.pb3.into_alternate_open_drain();

info!("");
info!("stm32h5xx-hal example - I2C");
info!("");

let mut i2c =
dp.I2C2
.i2c((scl, sda), 100.kHz(), ccdr.peripheral.I2C2, &ccdr.clocks);

// The STM32H503 NUCLEO board does not have any I2C peripherals, so put in the address of
// whatever peripheral you connect
let device_addr: u8 = 0x18;
// This implements a typical 8-bit register read operation, writing the register address and
// then issuing a repeat start to read the register value. Tweak these settings to read the
// desired amount of bytes from the register address.
let register_addr = 0x02;
let write = &[register_addr];
let mut read = [0u8; 1];

loop {
i2c.write_read(device_addr, write, &mut read).unwrap();
info!("Read reg {register_addr}: {read:X?}");
delay.delay_ms(duration);
}
}
Loading

0 comments on commit 53372b2

Please sign in to comment.