drivers: sensor: STM32 die temperature driver
STM32 internal temperature sensor driver. This sensor can be used to measure the temperature of the CPU and its surroundings. Signed-off-by: Eug Krashtan <eug.krashtan@gmail.com>
This commit is contained in:
parent
c045cbd336
commit
05d798e3a9
7 changed files with 207 additions and 0 deletions
|
@ -74,6 +74,7 @@ add_subdirectory_ifdef(CONFIG_SI7055 si7055)
|
||||||
add_subdirectory_ifdef(CONFIG_SI7060 si7060)
|
add_subdirectory_ifdef(CONFIG_SI7060 si7060)
|
||||||
add_subdirectory_ifdef(CONFIG_SM351LT sm351lt)
|
add_subdirectory_ifdef(CONFIG_SM351LT sm351lt)
|
||||||
add_subdirectory_ifdef(CONFIG_HAS_STMEMSC stmemsc)
|
add_subdirectory_ifdef(CONFIG_HAS_STMEMSC stmemsc)
|
||||||
|
add_subdirectory_ifdef(CONFIG_STM32_TEMP stm32_temp)
|
||||||
add_subdirectory_ifdef(CONFIG_STTS751 stts751)
|
add_subdirectory_ifdef(CONFIG_STTS751 stts751)
|
||||||
add_subdirectory_ifdef(CONFIG_SX9500 sx9500)
|
add_subdirectory_ifdef(CONFIG_SX9500 sx9500)
|
||||||
add_subdirectory_ifdef(CONFIG_TH02 th02)
|
add_subdirectory_ifdef(CONFIG_TH02 th02)
|
||||||
|
|
|
@ -186,6 +186,8 @@ source "drivers/sensor/si7060/Kconfig"
|
||||||
|
|
||||||
source "drivers/sensor/sm351lt/Kconfig"
|
source "drivers/sensor/sm351lt/Kconfig"
|
||||||
|
|
||||||
|
source "drivers/sensor/stm32_temp/Kconfig"
|
||||||
|
|
||||||
source "drivers/sensor/stts751/Kconfig"
|
source "drivers/sensor/stts751/Kconfig"
|
||||||
|
|
||||||
source "drivers/sensor/sx9500/Kconfig"
|
source "drivers/sensor/sx9500/Kconfig"
|
||||||
|
|
5
drivers/sensor/stm32_temp/CMakeLists.txt
Normal file
5
drivers/sensor/stm32_temp/CMakeLists.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
zephyr_library()
|
||||||
|
|
||||||
|
zephyr_library_sources_ifdef(CONFIG_STM32_TEMP stm32_temp.c)
|
10
drivers/sensor/stm32_temp/Kconfig
Normal file
10
drivers/sensor/stm32_temp/Kconfig
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# STM32 temperature sensor configuration options
|
||||||
|
|
||||||
|
# Copyright (c) 2021 Eug Krashtan
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
config STM32_TEMP
|
||||||
|
bool "STM32 Temperature Sensor"
|
||||||
|
depends on (ADC && SOC_FAMILY_STM32)
|
||||||
|
help
|
||||||
|
Enable driver for STM32 temperature sensor.
|
144
drivers/sensor/stm32_temp/stm32_temp.c
Normal file
144
drivers/sensor/stm32_temp/stm32_temp.c
Normal file
|
@ -0,0 +1,144 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021 Eug Krashtan
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <device.h>
|
||||||
|
#include <drivers/sensor.h>
|
||||||
|
#include <drivers/adc.h>
|
||||||
|
#include <logging/log.h>
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(stm32_temp, CONFIG_SENSOR_LOG_LEVEL);
|
||||||
|
|
||||||
|
#define DT_DRV_COMPAT st_stm32_temp
|
||||||
|
|
||||||
|
struct stm32_temp_data {
|
||||||
|
const struct device *adc;
|
||||||
|
uint8_t channel;
|
||||||
|
struct adc_channel_cfg adc_cfg;
|
||||||
|
struct adc_sequence adc_seq;
|
||||||
|
struct k_mutex mutex;
|
||||||
|
int16_t sample_buffer;
|
||||||
|
int32_t mv; /* Sensor value in millivolts */
|
||||||
|
};
|
||||||
|
|
||||||
|
struct stm32_temp_config {
|
||||||
|
int avgslope;
|
||||||
|
int v25_mv;
|
||||||
|
int tsv_mv;
|
||||||
|
bool is_ntc;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int stm32_temp_sample_fetch(const struct device *dev,
|
||||||
|
enum sensor_channel chan)
|
||||||
|
{
|
||||||
|
const struct stm32_temp_config *cfg = dev->config;
|
||||||
|
struct stm32_temp_data *data = dev->data;
|
||||||
|
struct adc_sequence *sp = &data->adc_seq;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_DIE_TEMP) {
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
k_mutex_lock(&data->mutex, K_FOREVER);
|
||||||
|
|
||||||
|
rc = adc_read(data->adc, sp);
|
||||||
|
sp->calibrate = false;
|
||||||
|
if (rc == 0) {
|
||||||
|
data->mv = data->sample_buffer * cfg->tsv_mv / 0x0FFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
k_mutex_unlock(&data->mutex);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int stm32_temp_channel_get(const struct device *dev,
|
||||||
|
enum sensor_channel chan,
|
||||||
|
struct sensor_value *val)
|
||||||
|
{
|
||||||
|
struct stm32_temp_data *data = dev->data;
|
||||||
|
const struct stm32_temp_config *cfg = dev->config;
|
||||||
|
float temp;
|
||||||
|
|
||||||
|
if (chan != SENSOR_CHAN_DIE_TEMP) {
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cfg->is_ntc) {
|
||||||
|
temp = (float)(cfg->v25_mv - data->mv);
|
||||||
|
} else {
|
||||||
|
temp = (float)(data->mv - cfg->v25_mv);
|
||||||
|
}
|
||||||
|
temp = (temp/cfg->avgslope)*10;
|
||||||
|
temp += 25;
|
||||||
|
sensor_value_from_double(val, temp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct sensor_driver_api stm32_temp_driver_api = {
|
||||||
|
.sample_fetch = stm32_temp_sample_fetch,
|
||||||
|
.channel_get = stm32_temp_channel_get,
|
||||||
|
};
|
||||||
|
|
||||||
|
static int stm32_temp_init(const struct device *dev)
|
||||||
|
{
|
||||||
|
struct stm32_temp_data *data = dev->data;
|
||||||
|
struct adc_channel_cfg *accp = &data->adc_cfg;
|
||||||
|
struct adc_sequence *asp = &data->adc_seq;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
k_mutex_init(&data->mutex);
|
||||||
|
|
||||||
|
if (!device_is_ready(data->adc)) {
|
||||||
|
LOG_ERR("Device %s is not ready", data->adc->name);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
*accp = (struct adc_channel_cfg){
|
||||||
|
.gain = ADC_GAIN_1,
|
||||||
|
.reference = ADC_REF_INTERNAL,
|
||||||
|
.acquisition_time = ADC_ACQ_TIME_MAX,
|
||||||
|
.channel_id = data->channel,
|
||||||
|
.differential = 0
|
||||||
|
};
|
||||||
|
rc = adc_channel_setup(data->adc, accp);
|
||||||
|
LOG_DBG("Setup AIN%u got %d", data->channel, rc);
|
||||||
|
|
||||||
|
*asp = (struct adc_sequence){
|
||||||
|
.channels = BIT(data->channel),
|
||||||
|
.buffer = &data->sample_buffer,
|
||||||
|
.buffer_size = sizeof(data->sample_buffer),
|
||||||
|
.resolution = 12,
|
||||||
|
.calibrate = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define STM32_TEMP_INST(idx) \
|
||||||
|
static struct stm32_temp_data inst_##idx##_data = { \
|
||||||
|
.adc = DEVICE_DT_GET(DT_IO_CHANNELS_CTLR( \
|
||||||
|
DT_INST(idx, st_stm32_temp))), \
|
||||||
|
.channel = DT_IO_CHANNELS_INPUT( \
|
||||||
|
DT_INST(idx, st_stm32_temp)) \
|
||||||
|
}; \
|
||||||
|
static const struct stm32_temp_config inst_##idx##_config = { \
|
||||||
|
.avgslope = DT_INST_PROP(idx, avgslope), \
|
||||||
|
.v25_mv = DT_INST_PROP(idx, v25), \
|
||||||
|
.tsv_mv = DT_INST_PROP(idx, ts_voltage_mv), \
|
||||||
|
.is_ntc = DT_INST_PROP(idx, ntc) \
|
||||||
|
}; \
|
||||||
|
DEVICE_DT_INST_DEFINE(idx, \
|
||||||
|
stm32_temp_init, \
|
||||||
|
NULL, \
|
||||||
|
&inst_##idx##_data, \
|
||||||
|
&inst_##idx##_config, \
|
||||||
|
POST_KERNEL, \
|
||||||
|
CONFIG_SENSOR_INIT_PRIORITY, \
|
||||||
|
&stm32_temp_driver_api);
|
||||||
|
|
||||||
|
DT_INST_FOREACH_STATUS_OKAY(STM32_TEMP_INST)
|
44
dts/bindings/sensor/st,stm32-temp.yaml
Normal file
44
dts/bindings/sensor/st,stm32-temp.yaml
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
# Copyright (c) 2021, Eug Krashtan
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
description: STM32 family TEMP node
|
||||||
|
|
||||||
|
compatible: "st,stm32-temp"
|
||||||
|
|
||||||
|
include: base.yaml
|
||||||
|
|
||||||
|
properties:
|
||||||
|
label:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
io-channels:
|
||||||
|
required: true
|
||||||
|
description: ADC channel for temperature sensor
|
||||||
|
|
||||||
|
ts-voltage-mv:
|
||||||
|
type: int
|
||||||
|
default: 3300
|
||||||
|
description: Temperature sensor voltage in millivolts
|
||||||
|
|
||||||
|
avgslope:
|
||||||
|
type: int
|
||||||
|
default: 25
|
||||||
|
description: >
|
||||||
|
Average slope of T-V chart (in mV/C x10) according to
|
||||||
|
datasheet "Electrical characteristics/Operating conditions"
|
||||||
|
STM32F1 Table 5.3.19 (min 4 mV/C, max 4.6, default 4.3)
|
||||||
|
STM32F4 Table 6.3.21 default 2.5
|
||||||
|
|
||||||
|
v25:
|
||||||
|
type: int
|
||||||
|
default: 760
|
||||||
|
description: >
|
||||||
|
Voltage of temperature sensor at 25C in mV according to
|
||||||
|
datasheet "Electrical characteristics/Operating conditions"
|
||||||
|
STM32F1 Table 5.3.19 (min 1340, max 1520, default 1430)
|
||||||
|
STM32F4 Table 6.3.21 default 760
|
||||||
|
|
||||||
|
ntc:
|
||||||
|
type: boolean
|
||||||
|
description: >
|
||||||
|
Negative Temperature Coefficient. True if STM32F1
|
|
@ -37,6 +37,7 @@ CONFIG_SI7006=y
|
||||||
CONFIG_SI7055=y
|
CONFIG_SI7055=y
|
||||||
CONFIG_SI7060=y
|
CONFIG_SI7060=y
|
||||||
CONFIG_SM351LT=y
|
CONFIG_SM351LT=y
|
||||||
|
CONFIG_STM32_TEMP=y
|
||||||
CONFIG_SX9500=y
|
CONFIG_SX9500=y
|
||||||
CONFIG_TACH_NPCX=y
|
CONFIG_TACH_NPCX=y
|
||||||
CONFIG_TEMP_NRF5=y
|
CONFIG_TEMP_NRF5=y
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue