Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I2C Driver Refactoring #233

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
593 changes: 367 additions & 226 deletions esp-hal-common/src/i2c.rs

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions esp32-hal/examples/i2c_bmp180_calibration_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Read calibration data from BMP180 sensor
//!
//! This example dumps the calibration data from a BMP180 sensor
//!
//! The following wiring is assumed:
//! - SDA => GPIO32
//! - SCL => GPIO33

#![no_std]
#![no_main]

use esp32_hal::{
clock::ClockControl,
gpio::IO,
i2c::I2C,
pac::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
};
use esp_backtrace as _;
use esp_println::println;
use xtensa_lx_rt::entry;

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let mut system = peripherals.DPORT.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut wdt = timer_group0.wdt;
let mut rtc = Rtc::new(peripherals.RTC_CNTL);

// Disable watchdog timer
wdt.disable();
rtc.rwdt.disable();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

// Create a new peripheral object with the described wiring
// and standard I2C clock speed
let mut i2c = I2C::new(
peripherals.I2C0,
io.pins.gpio32,
io.pins.gpio33,
100u32.kHz(),
&mut system.peripheral_clock_control,
&clocks,
)
.unwrap();

loop {
let mut data = [0u8; 22];
i2c.write_read(0x77, &[0xaa], &mut data).ok();

println!("{:02x?}", data);
}
}
60 changes: 60 additions & 0 deletions esp32c2-hal/examples/i2c_bmp180_calibration_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Read calibration data from BMP180 sensor
//!
//! This example dumps the calibration data from a BMP180 sensor
//!
//! The following wiring is assumed:
//! - SDA => GPIO1
//! - SCL => GPIO2

#![no_std]
#![no_main]

use esp32c2_hal::{
clock::ClockControl,
gpio::IO,
i2c::I2C,
pac::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
};
use esp_backtrace as _;
use esp_println::println;
use riscv_rt::entry;

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut wdt0 = timer_group0.wdt;

// Disable watchdog timers
rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

// Create a new peripheral object with the described wiring
// and standard I2C clock speed
let mut i2c = I2C::new(
peripherals.I2C0,
io.pins.gpio1,
io.pins.gpio2,
100u32.kHz(),
&mut system.peripheral_clock_control,
&clocks,
)
.unwrap();

loop {
let mut data = [0u8; 22];
i2c.write_read(0x77, &[0xaa], &mut data).ok();

println!("{:02x?}", data);
}
}
2 changes: 1 addition & 1 deletion esp32c3-hal/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn main() {
add_defaults();
}

#[cfg(not(any(feature = "mcu-boot",feature = "direct-boot")))]
#[cfg(not(any(feature = "mcu-boot", feature = "direct-boot")))]
fn main() {
check_opt_level();

Expand Down
63 changes: 63 additions & 0 deletions esp32c3-hal/examples/i2c_bmp180_calibration_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! Read calibration data from BMP180 sensor
//!
//! This example dumps the calibration data from a BMP180 sensor
//!
//! The following wiring is assumed:
//! - SDA => GPIO1
//! - SCL => GPIO2

#![no_std]
#![no_main]

use esp32c3_hal::{
clock::ClockControl,
gpio::IO,
i2c::I2C,
pac::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
};
use esp_backtrace as _;
use esp_println::println;
use riscv_rt::entry;

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks);
let mut wdt1 = timer_group1.wdt;

// Disable watchdog timers
rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

// Create a new peripheral object with the described wiring
// and standard I2C clock speed
let mut i2c = I2C::new(
peripherals.I2C0,
io.pins.gpio1,
io.pins.gpio2,
100u32.kHz(),
&mut system.peripheral_clock_control,
&clocks,
)
.unwrap();

loop {
let mut data = [0u8; 22];
i2c.write_read(0x77, &[0xaa], &mut data).ok();

println!("{:02x?}", data);
}
}
7 changes: 2 additions & 5 deletions esp32c3-hal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_std]

use core::arch::{asm, global_asm};

#[cfg(feature = "mcu-boot")]
use core::mem::size_of;

Expand Down Expand Up @@ -34,7 +33,6 @@ pub use esp_hal_common::{
Serial,
UsbSerialJtag,
};

#[cfg(feature = "direct-boot")]
use riscv_rt::pre_init;

Expand Down Expand Up @@ -361,9 +359,8 @@ unsafe fn configure_mmu() {
let autoload = cache_suspend_icache();
cache_invalidate_icache_all();

/* Clear the MMU entries that are already set up, so the new app only has
* the mappings it creates.
*/
// Clear the MMU entries that are already set up, so the new app only has
// the mappings it creates.

const FLASH_MMU_TABLE: *mut u32 = 0x600c_5000 as *mut u32;
const ICACHE_MMU_SIZE: usize = 0x200;
Expand Down
59 changes: 59 additions & 0 deletions esp32s2-hal/examples/i2c_bmp180_calibration_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Read calibration data from BMP180 sensor
//!
//! This example dumps the calibration data from a BMP180 sensor
//!
//! The following wiring is assumed:
//! - SDA => GPIO35
//! - SCL => GPIO36

#![no_std]
#![no_main]

use esp32s2_hal::{
clock::ClockControl,
gpio::IO,
i2c::I2C,
pac::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
};
use esp_backtrace as _;
use esp_println::println;
use xtensa_lx_rt::entry;

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut wdt = timer_group0.wdt;
let mut rtc = Rtc::new(peripherals.RTC_CNTL);

// Disable watchdog timer
wdt.disable();
rtc.rwdt.disable();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

// Create a new peripheral object with the described wiring
// and standard I2C clock speed
let mut i2c = I2C::new(
peripherals.I2C0,
io.pins.gpio35,
io.pins.gpio36,
100u32.kHz(),
&mut system.peripheral_clock_control,
&clocks,
)
.unwrap();

loop {
let mut data = [0u8; 22];
i2c.write_read(0x77, &[0xaa], &mut data).ok();

println!("{:02x?}", data);
}
}
59 changes: 59 additions & 0 deletions esp32s3-hal/examples/i2c_bmp180_calibration_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Read calibration data from BMP180 sensor
//!
//! This example dumps the calibration data from a BMP180 sensor
//!
//! The following wiring is assumed:
//! - SDA => GPIO1
//! - SCL => GPIO2

#![no_std]
#![no_main]

use esp32s3_hal::{
clock::ClockControl,
gpio::IO,
i2c::I2C,
pac::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
};
use esp_backtrace as _;
use esp_println::println;
use xtensa_lx_rt::entry;

#[entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut wdt = timer_group0.wdt;
let mut rtc = Rtc::new(peripherals.RTC_CNTL);

// Disable watchdog timer
wdt.disable();
rtc.rwdt.disable();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

// Create a new peripheral object with the described wiring
// and standard I2C clock speed
let mut i2c = I2C::new(
peripherals.I2C0,
io.pins.gpio1,
io.pins.gpio2,
100u32.kHz(),
&mut system.peripheral_clock_control,
&clocks,
)
.unwrap();

loop {
let mut data = [0u8; 22];
i2c.write_read(0x77, &[0xaa], &mut data).ok();

println!("{:02x?}", data);
}
}