drivers: sensor: add driver for temperature sensor NCT75

Add a driver for the ON Semiconductor temperature sensor NCT75.

Signed-off-by: Benedikt Schmidt <benediktibk@gmail.com>
This commit is contained in:
Benedikt Schmidt 2024-03-29 08:56:57 +01:00 committed by Anas Nashif
parent c107f12832
commit 10bbf7cd57
5 changed files with 137 additions and 0 deletions

View file

@ -52,6 +52,7 @@ add_subdirectory_ifdef(CONFIG_MC3419 mc3419)
add_subdirectory_ifdef(CONFIG_MHZ19B mhz19b)
add_subdirectory_ifdef(CONFIG_MPU6050 tdk/mpu6050)
add_subdirectory_ifdef(CONFIG_MPU9250 tdk/mpu9250)
add_subdirectory_ifdef(CONFIG_NCT75 nct75)
add_subdirectory_ifdef(CONFIG_NTC_THERMISTOR ntc_thermistor)
add_subdirectory_ifdef(CONFIG_PMS7003 pms7003)
add_subdirectory_ifdef(CONFIG_QDEC_SAM qdec_sam)

View file

@ -133,6 +133,7 @@ source "drivers/sensor/mc3419/Kconfig"
source "drivers/sensor/mhz19b/Kconfig"
source "drivers/sensor/tdk/mpu6050/Kconfig"
source "drivers/sensor/tdk/mpu9250/Kconfig"
source "drivers/sensor/nct75/Kconfig"
source "drivers/sensor/ntc_thermistor/Kconfig"
source "drivers/sensor/pms7003/Kconfig"
source "drivers/sensor/qdec_sam/Kconfig"

View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_NCT75 nct75.c)

View file

@ -0,0 +1,10 @@
# Copyright (c) 2024 Benedikt Schmidt <benediktibk@gmail.com>
# SPDX-License-Identifier: Apache-2.0
config NCT75
bool "NCT75 temperature sensor"
default y
depends on DT_HAS_ONNN_NCT75_ENABLED
select I2C
help
Enable driver for the NCT75 temperature sensor.

View file

@ -0,0 +1,120 @@
/*
* Copyright (c) 2024 Benedikt Schmidt <benediktibk@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT onnn_nct75
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
LOG_MODULE_REGISTER(NCT75, CONFIG_SENSOR_LOG_LEVEL);
#define NCT75REGISTER_STOREDTEMPERATUREVALUE 0x00
#define NCT75REGISTER_CONFIGURATION 0x01
#define NCT75REGISTER_ONESHOT 0x04
#define NCT75_CONFIGURATION_ONESHOTMODE_POS 5
#define NCT75_TEMPERATURE_CONVERSION_TIME_US 48500
#define NCT75_TEMPERATURE_CONVERSION_WAIT_TIME_US (NCT75_TEMPERATURE_CONVERSION_TIME_US + 1000)
struct nct75_config {
struct i2c_dt_spec i2c;
};
struct nct75_data {
/* temperature in 1e-6 °C */
int64_t value;
};
static int nct75_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
const struct nct75_config *config = dev->config;
struct nct75_data *data = dev->data;
int result;
uint8_t write_buffer = 0;
uint16_t read_buffer;
int16_t raw_value;
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
result = i2c_reg_write_byte_dt(&config->i2c, NCT75REGISTER_ONESHOT, 0);
if (result != 0) {
LOG_ERR("%s: unable to trigger temperature one shot measurement", dev->name);
return result;
}
k_sleep(K_USEC(NCT75_TEMPERATURE_CONVERSION_WAIT_TIME_US));
write_buffer = NCT75REGISTER_STOREDTEMPERATUREVALUE;
result = i2c_write_read_dt(&config->i2c, &write_buffer, sizeof(write_buffer), &read_buffer,
sizeof(read_buffer));
if (result != 0) {
LOG_ERR("%s: unable to read temperature", dev->name);
return result;
}
raw_value = arithmetic_shift_right(sys_be16_to_cpu(read_buffer), 4);
data->value = (raw_value * 1000 * 1000) * 100 / 0x640;
return 0;
}
static int nct75_channel_get(const struct device *dev, enum sensor_channel chan,
struct sensor_value *val)
{
struct nct75_data *data = dev->data;
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
LOG_ERR("%s: requesting unsupported channel %i", dev->name, chan);
return -ENOTSUP;
}
val->val1 = data->value / (1000 * 1000);
val->val2 = data->value - val->val1 * 1000 * 1000;
return 0;
}
static const struct sensor_driver_api nct75_api = {
.sample_fetch = nct75_sample_fetch,
.channel_get = nct75_channel_get,
};
static int nct75_init(const struct device *dev)
{
const struct nct75_config *config = dev->config;
uint8_t configuration_register_value = BIT(NCT75_CONFIGURATION_ONESHOTMODE_POS);
int result;
if (!i2c_is_ready_dt(&config->i2c)) {
LOG_ERR("I2C device not ready");
return -ENODEV;
}
result = i2c_reg_write_byte_dt(&config->i2c, NCT75REGISTER_CONFIGURATION,
configuration_register_value);
if (result != 0) {
LOG_ERR("%s: unable to configure temperature sensor", dev->name);
return result;
}
return 0;
}
#define NCT75_INIT(inst) \
static const struct nct75_config nct75_##inst##_config = { \
.i2c = I2C_DT_SPEC_INST_GET(inst), \
}; \
\
static struct nct75_data nct75_##inst##_data; \
\
SENSOR_DEVICE_DT_INST_DEFINE(inst, nct75_init, NULL, &nct75_##inst##_data, \
&nct75_##inst##_config, POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, &nct75_api);
DT_INST_FOREACH_STATUS_OKAY(NCT75_INIT);