drivers: esp32: temp: CPU die temperature sensor

Support for the measuring the CPU die temperature
for the ESP32 targets S2,C3. The ESP32 support
was ommited due to lack of offset calibration.

Signed-off-by: Marek Matej <marek.matej@espressif.com>
This commit is contained in:
Marek Matej 2022-11-30 14:27:30 +01:00 committed by Carles Cufí
parent c646a5576a
commit 45d55205db
8 changed files with 155 additions and 0 deletions

View File

@ -112,6 +112,7 @@ add_subdirectory_ifdef(CONFIG_ADC_CMP_NPCX nuvoton_adc_cmp_npcx)
add_subdirectory_ifdef(CONFIG_TACH_IT8XXX2 ite_tach_it8xxx2)
add_subdirectory_ifdef(CONFIG_VCMP_IT8XXX2 ite_vcmp_it8xxx2)
add_subdirectory_ifdef(CONFIG_PCNT_ESP32 pcnt_esp32)
add_subdirectory_ifdef(CONFIG_ESP32_TEMP esp32_temp)
if(CONFIG_USERSPACE OR CONFIG_SENSOR_SHELL OR CONFIG_SENSOR_SHELL_BATTERY)
# The above if() is needed or else CMake would complain about

View File

@ -263,4 +263,6 @@ source "drivers/sensor/ite_vcmp_it8xxx2/Kconfig"
source "drivers/sensor/pcnt_esp32/Kconfig"
source "drivers/sensor/esp32_temp/Kconfig"
endif # SENSOR

View File

@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources(esp32_temp.c)

View File

@ -0,0 +1,10 @@
# Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0
config ESP32_TEMP
bool "ESP32 Temperature Sensor"
default y
depends on DT_HAS_ESPRESSIF_ESP32_TEMP_ENABLED
depends on !SOC_ESP32
help
Enable driver for temperature sensor on certain ESP targets.

View File

@ -0,0 +1,100 @@
/*
* Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT espressif_esp32_temp
#include <driver/temp_sensor.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(esp32_temp, CONFIG_SENSOR_LOG_LEVEL);
#if CONFIG_SOC_ESP32
#error "Temperature sensor not supported on ESP32"
#endif /* CONFIG_IDF_TARGET_ESP32 */
struct esp32_temp_data {
struct k_mutex mutex;
temp_sensor_config_t temp_sensor;
float temp_out;
};
struct esp32_temp_config {
temp_sensor_dac_offset_t range;
};
static int esp32_temp_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
struct esp32_temp_data *data = dev->data;
int rc = 0;
k_mutex_lock(&data->mutex, K_FOREVER);
if (temp_sensor_read_celsius(&data->temp_out) != ESP_OK) {
LOG_ERR("Temperature read error!");
rc = -EFAULT;
goto unlock;
}
unlock:
k_mutex_unlock(&data->mutex);
return rc;
}
static int esp32_temp_channel_get(const struct device *dev, enum sensor_channel chan,
struct sensor_value *val)
{
struct esp32_temp_data *data = dev->data;
const struct esp32_temp_config *cfg = dev->config;
if (chan != SENSOR_CHAN_DIE_TEMP) {
return -ENOTSUP;
}
return sensor_value_from_double(val, data->temp_out);
}
static const struct sensor_driver_api esp32_temp_driver_api = {
.sample_fetch = esp32_temp_sample_fetch,
.channel_get = esp32_temp_channel_get,
};
static int esp32_temp_init(const struct device *dev)
{
struct esp32_temp_data *data = dev->data;
const struct esp32_temp_config *conf = dev->config;
k_mutex_init(&data->mutex);
temp_sensor_get_config(&data->temp_sensor);
data->temp_sensor.dac_offset = conf->range;
temp_sensor_set_config(data->temp_sensor);
temp_sensor_start();
LOG_DBG("Temperature sensor started. Offset %d, clk_div %d",
data->temp_sensor.dac_offset, data->temp_sensor.clk_div);
return 0;
}
#define ESP32_TEMP_DEFINE(inst) \
static struct esp32_temp_data esp32_temp_dev_data_##inst = { \
.temp_sensor = TSENS_CONFIG_DEFAULT(), \
}; \
\
static const struct esp32_temp_config esp32_temp_dev_config_##inst = { \
.range = (temp_sensor_dac_offset_t) DT_INST_PROP(inst, range), \
}; \
\
SENSOR_DEVICE_DT_INST_DEFINE(inst, esp32_temp_init, NULL, \
&esp32_temp_dev_data_##inst, &esp32_temp_dev_config_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
&esp32_temp_driver_api); \
DT_INST_FOREACH_STATUS_OKAY(ESP32_TEMP_DEFINE)

View File

@ -0,0 +1,24 @@
# Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0
description: ESP32 family temperature sensor node
compatible: "espressif,esp32-temp"
include: sensor-device.yaml
properties:
range:
type: int
description: |
The temperature sensor is available on the ESP32-S2, ESP32-C3. Note
that it is unavailable on the ESP32 due to missing offset calibration.
Temperature range is defined by the temperature offset which is used
during calculation of the output temperature from the measured value.
default: 2
enum:
- 0 # measure range: 50°C ~ 125°C, error < 3°C
- 1 # measure range: 20°C ~ 100°C, error < 2°C
- 2 # measure range:-10°C ~ 80°C, error < 1°C
- 3 # measure range:-30°C ~ 50°C, error < 2°C
- 4 # measure range:-40°C ~ 20°C, error < 3°C

View File

@ -224,6 +224,13 @@
status = "disabled";
};
coretemp: coretemp@0x60040058 {
compatible = "espressif,esp32-temp";
friendly-name = "coretemp";
reg = <0x60040058 0x4>;
status = "disabled";
};
};
};

View File

@ -262,6 +262,12 @@
interrupt-parent = <&intc>;
clocks = <&rtc ESP32_PERIPH_SARADC_MODULE>;
#io-channel-cells = <1>;
};
coretemp: coretemp@3f408800 {
compatible = "espressif,esp32-temp";
friendly-name = "coretemp";
reg = <0x3f408800 0x4>;
status = "disabled";
};
};