Skip to content

Commit

Permalink
Back to use i2c async
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiojmendes committed Aug 24, 2024
1 parent ea00c2b commit 5651314
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ embassy-time = { version = "0.3.2", features = [
embassy-nrf = { version = "0.2.0", features = [
"defmt",
"gpiote",
"time",
"time-driver-rtc1",
"nrf52840",
] }
Expand Down
13 changes: 6 additions & 7 deletions src/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use embassy_time::{Duration, Timer};

const MCP9808_ADDRESS: u8 = 0x18;
static SAMPLE_RATE: Duration = Duration::from_secs(30);
static I2C_TIMEOUT: Duration = Duration::from_millis(2000);

static SHARED: Signal<ThreadModeRawMutex, i16> = Signal::new();

Expand All @@ -18,7 +17,7 @@ pub async fn init(mut twi: Twim<'static, impl twim::Instance>) -> ! {
let mut set_resolution = true;
loop {
if set_resolution {
if let Err(e) = setup_temp_reader(&mut twi) {
if let Err(e) = setup_temp_reader(&mut twi).await {
defmt::error!("Error setting sensor resolution {}", e)
} else {
set_resolution = false;
Expand All @@ -37,20 +36,20 @@ pub async fn init(mut twi: Twim<'static, impl twim::Instance>) -> ! {
}
}

fn setup_temp_reader(twi: &mut Twim<'_, impl twim::Instance>) -> Result<(), twim::Error> {
async fn setup_temp_reader(twi: &mut Twim<'_, impl twim::Instance>) -> Result<(), twim::Error> {
defmt::info!("Set resolution");
twi.blocking_write_timeout(MCP9808_ADDRESS, &[0x08, 0x00], I2C_TIMEOUT)?;
twi.write(MCP9808_ADDRESS, &[0x08, 0x00]).await?;
Ok(())
}

async fn read_temperature(twi: &mut Twim<'_, impl twim::Instance>) -> Result<i16, twim::Error> {
// Temp Sensor
// Wake up
twi.blocking_write_timeout(MCP9808_ADDRESS, &[0x01, 0x00, 0x00], I2C_TIMEOUT)?;
twi.write(MCP9808_ADDRESS, &[0x01, 0x00, 0x00]).await?;
Timer::after_millis(200).await;
// Read
let mut buf = [0u8; 2];
twi.blocking_write_read_timeout(MCP9808_ADDRESS, &[0x05], &mut buf, I2C_TIMEOUT)?;
twi.write_read(MCP9808_ADDRESS, &[0x05], &mut buf).await?;
// Conversion code based on the datasheet
// https://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf pg25
let [upper, lower] = buf;
Expand All @@ -63,7 +62,7 @@ async fn read_temperature(twi: &mut Twim<'_, impl twim::Instance>) -> Result<i16
lower
);
// Shutdown
twi.blocking_write_timeout(MCP9808_ADDRESS, &[0x01, 0x01, 0x00], I2C_TIMEOUT)?;
twi.write(MCP9808_ADDRESS, &[0x01, 0x01, 0x00]).await?;
Ok(temperature)
}

Expand Down

0 comments on commit 5651314

Please sign in to comment.