Skip to content

Commit

Permalink
feat: add basic support for temperature sensor (tsens) for esp32c3
Browse files Browse the repository at this point in the history
  • Loading branch information
davoclavo committed Jan 1, 2025
1 parent fda487d commit 9e46483
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- More interrupts are available in `esp_hal::spi::master::SpiInterrupt`, add `enable_listen`,`interrupts` and `clear_interrupts` for ESP32/ESP32-S2 (#2833)

- The `ExtU64` and `RateExtU32` traits have been added to `esp_hal::time` (#2845)
- Added `tsens::TemperatureSensor` peripheral for ESP32C6
- Added `tsens::TemperatureSensor` peripheral for ESP32C6 and ESP32C3 (#2875)

### Changed

Expand Down
1 change: 1 addition & 0 deletions esp-hal/src/soc/esp32c3/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ crate::peripherals! {
SW_INTERRUPT <= virtual,
TIMG0 <= TIMG0,
TIMG1 <= TIMG1,
TSENS <= virtual,
TWAI0 <= TWAI0,
UART0 <= UART0,
UART1 <= UART1,
Expand Down
11 changes: 10 additions & 1 deletion esp-hal/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ impl PeripheralClockControl {
Peripheral::Systimer => {
perip_clk_en0.modify(|_, w| w.systimer_clk_en().bit(enable));
}
#[cfg(tsens)]
Peripheral::Tsens => {
perip_clk_en1.modify(|_, w| w.tsens_clk_en().bit(enable));
}
}
}

Expand Down Expand Up @@ -613,11 +617,16 @@ impl PeripheralClockControl {
perip_rst_en0.modify(|_, w| w.systimer_rst().set_bit());
perip_rst_en0.modify(|_, w| w.systimer_rst().clear_bit());
}
#[cfg(tsens)]
#[cfg(all(tsens, esp32c6))]
Peripheral::Tsens => {
perip_rst_en0.modify(|_, w| w.tsens_rst().set_bit());
perip_rst_en0.modify(|_, w| w.tsens_rst().clear_bit());
}
#[cfg(all(tsens, esp32c3))]
Peripheral::Tsens => {
perip_rst_en1.modify(|_, w| w.tsens_rst().set_bit());
perip_rst_en1.modify(|_, w| w.tsens_rst().clear_bit());
}
});
}
}
Expand Down
4 changes: 4 additions & 0 deletions esp-hal/src/tsens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl<'d> TemperatureSensor<'d> {
// Power Up
apb_saradc.tsens_ctrl().write(|w| w.pu().set_bit());

// Default to XTAL_CLK source, as it works out of the box on both esp32c6 and
// esp32c3
apb_saradc.tsens_ctrl2().write(|w| w.clk_sel().set_bit());

Self {
_guard: guard,
_peripheral: peripheral,
Expand Down
1 change: 1 addition & 0 deletions esp-metadata/devices/esp32c3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ symbols = [
"phy",
"bt",
"wifi",
"tsens",

# ROM capabilities
"rom_crc_le",
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/temperature_sensor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This example uses the internal temperature sensor to measure the chip's temperature
//!
//% CHIPS: esp32c6
//% CHIPS: esp32c6 esp32c3

#![no_std]
#![no_main]
Expand All @@ -12,6 +12,7 @@ use esp_println::println;

#[entry]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
let peripherals = esp_hal::init(esp_hal::Config::default());

let temperature_sensor = TemperatureSensor::new(peripherals.TSENS);
Expand Down

0 comments on commit 9e46483

Please sign in to comment.