diff --git a/drivers/sensor/CMakeLists.txt b/drivers/sensor/CMakeLists.txt index 9d416131000..bc328cea574 100644 --- a/drivers/sensor/CMakeLists.txt +++ b/drivers/sensor/CMakeLists.txt @@ -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 diff --git a/drivers/sensor/Kconfig b/drivers/sensor/Kconfig index 201d2f20e64..08c4b0da0e7 100644 --- a/drivers/sensor/Kconfig +++ b/drivers/sensor/Kconfig @@ -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 diff --git a/drivers/sensor/esp32_temp/CMakeLists.txt b/drivers/sensor/esp32_temp/CMakeLists.txt new file mode 100644 index 00000000000..9c643145f71 --- /dev/null +++ b/drivers/sensor/esp32_temp/CMakeLists.txt @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library() + +zephyr_library_sources(esp32_temp.c) diff --git a/drivers/sensor/esp32_temp/Kconfig b/drivers/sensor/esp32_temp/Kconfig new file mode 100644 index 00000000000..93568b416a9 --- /dev/null +++ b/drivers/sensor/esp32_temp/Kconfig @@ -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. diff --git a/drivers/sensor/esp32_temp/esp32_temp.c b/drivers/sensor/esp32_temp/esp32_temp.c new file mode 100644 index 00000000000..1ceb13bde08 --- /dev/null +++ b/drivers/sensor/esp32_temp/esp32_temp.c @@ -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 + +#include +#include +#include +#include + +#include +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) diff --git a/dts/bindings/sensor/espressif,esp32-temp.yaml b/dts/bindings/sensor/espressif,esp32-temp.yaml new file mode 100644 index 00000000000..0acaf9e928f --- /dev/null +++ b/dts/bindings/sensor/espressif,esp32-temp.yaml @@ -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 diff --git a/dts/riscv/espressif/esp32c3.dtsi b/dts/riscv/espressif/esp32c3.dtsi index d8251e6137c..a31c13c80f0 100644 --- a/dts/riscv/espressif/esp32c3.dtsi +++ b/dts/riscv/espressif/esp32c3.dtsi @@ -224,6 +224,13 @@ status = "disabled"; }; + coretemp: coretemp@0x60040058 { + compatible = "espressif,esp32-temp"; + friendly-name = "coretemp"; + reg = <0x60040058 0x4>; + status = "disabled"; + }; + }; }; diff --git a/dts/xtensa/espressif/esp32s2.dtsi b/dts/xtensa/espressif/esp32s2.dtsi index cbfc1bae306..5607d9bd8d9 100644 --- a/dts/xtensa/espressif/esp32s2.dtsi +++ b/dts/xtensa/espressif/esp32s2.dtsi @@ -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"; }; };