Commit 754c578d authored by Yannick Thesen's avatar Yannick Thesen Committed by Fabio Baltieri
Browse files

drivers: sensor: Added driver for the Würth Elektronik WSEN-HIDS sensor.



Added driver for the WSEN-HIDS humidity and temperature sensor.

Signed-off-by: default avatarYannick Thesen <Yannick.Thesen@we-online.de>
parent ffdd4284
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -102,6 +102,7 @@ add_subdirectory_ifdef(CONFIG_VCNL4040 vcnl4040)
add_subdirectory_ifdef(CONFIG_VL53L0X		vl53l0x)
add_subdirectory_ifdef(CONFIG_TEMP_KINETIS	nxp_kinetis_temp)
add_subdirectory_ifdef(CONFIG_TACH_XEC		mchp_tach_xec)
add_subdirectory_ifdef(CONFIG_WSEN_HIDS		wsen_hids)
add_subdirectory_ifdef(CONFIG_ITDS		wsen_itds)
add_subdirectory_ifdef(CONFIG_MCUX_ACMP		mcux_acmp)
add_subdirectory_ifdef(CONFIG_TACH_NPCX		nuvoton_tach_npcx)
+2 −0
Original line number Diff line number Diff line
@@ -240,6 +240,8 @@ source "drivers/sensor/vl53l0x/Kconfig"

source "drivers/sensor/nxp_kinetis_temp/Kconfig"

source "drivers/sensor/wsen_hids/Kconfig"

source "drivers/sensor/wsen_itds/Kconfig"

source "drivers/sensor/mcux_acmp/Kconfig"
+7 −0
Original line number Diff line number Diff line
# Copyright (c) 2022 Würth Elektronik eiSos GmbH & Co. KG
# SPDX-License-Identifier: Apache-2.0

zephyr_library()

zephyr_library_sources(wsen_hids.c)
zephyr_library_sources_ifdef(CONFIG_WSEN_HIDS_TRIGGER wsen_hids_trigger.c)
+55 −0
Original line number Diff line number Diff line
# Copyright (c) 2022 Würth Elektronik eiSos GmbH & Co. KG
# SPDX-License-Identifier: Apache-2.0

menuconfig WSEN_HIDS
	bool "WSEN-HIDS humidity and temperature sensor"
	default y
	depends on DT_HAS_WE_WSEN_HIDS_ENABLED
	select I2C if $(dt_compat_on_bus,$(DT_COMPAT_WE_WSEN_HIDS),i2c)
	select SPI if $(dt_compat_on_bus,$(DT_COMPAT_WE_WSEN_HIDS),spi)
	select HAS_WESENSORS
	help
	  Enable driver for the WSEN-HIDS I2C/SPI-based humidity sensor with integrated
	  temperature sensor.

if WSEN_HIDS

choice WSEN_HIDS_TRIGGER_MODE
	prompt "Trigger mode"
	default WSEN_HIDS_TRIGGER_NONE
	help
	  Specify the type of triggering to be used by the driver.

config WSEN_HIDS_TRIGGER_NONE
	bool "No trigger"

config WSEN_HIDS_TRIGGER_GLOBAL_THREAD
	bool "Use global thread"
	depends on GPIO
	select WSEN_HIDS_TRIGGER

config WSEN_HIDS_TRIGGER_OWN_THREAD
	bool "Use own thread"
	depends on GPIO
	select WSEN_HIDS_TRIGGER

endchoice # WSEN_HIDS_TRIGGER_MODE

config WSEN_HIDS_TRIGGER
	bool

config WSEN_HIDS_THREAD_PRIORITY
	int "Thread priority"
	depends on WSEN_HIDS_TRIGGER_OWN_THREAD
	default 10
	help
	  Priority of thread used by the driver to handle interrupts.

config WSEN_HIDS_THREAD_STACK_SIZE
	int "Thread stack size"
	depends on WSEN_HIDS_TRIGGER_OWN_THREAD
	default 1024
	help
	  Stack size of thread used by the driver to handle interrupts.

endif # WSEN_HIDS
+279 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2022 Würth Elektronik eiSos GmbH & Co. KG
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#define DT_DRV_COMPAT we_wsen_hids

#include <string.h>

#include <zephyr/sys/__assert.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/logging/log.h>

#include "wsen_hids.h"

LOG_MODULE_REGISTER(WSEN_HIDS, CONFIG_SENSOR_LOG_LEVEL);

/*
 * List of supported output data rates (sensor_value struct, input to
 * sensor_attr_set()). Index into this list is used as argument for
 * HIDS_setOutputDataRate().
 */
static const struct sensor_value hids_odr_list[] = {
	{.val1 = 0, .val2 = 0},
	{.val1 = 1, .val2 = 0},
	{.val1 = 7, .val2 = 0},
	{.val1 = 12, .val2 = 5 * 100000},
};

static int hids_sample_fetch(const struct device *dev, enum sensor_channel channel)
{
	struct hids_data *data = dev->data;
	int16_t raw_humidity;
	int16_t raw_temp;

	__ASSERT_NO_MSG(channel == SENSOR_CHAN_ALL);

	if (HIDS_getRawValues(&data->sensor_interface, &raw_humidity, &raw_temp) != WE_SUCCESS) {
		LOG_ERR("Failed to %s sample.", "fetch data");
		return -EIO;
	}

	if (HIDS_convertHumidity_uint16(&data->sensor_interface, raw_humidity, &data->humidity) !=
	    WE_SUCCESS) {
		LOG_ERR("Failed to %s sample.", "convert humidity");
		return -EIO;
	}

	if (HIDS_convertTemperature_int16(&data->sensor_interface, raw_temp, &data->temperature) !=
	    WE_SUCCESS) {
		LOG_ERR("Failed to %s sample.", "convert temperature");
		return -EIO;
	}

	return 0;
}

static int hids_channel_get(const struct device *dev, enum sensor_channel channel,
			    struct sensor_value *value)
{
	struct hids_data *data = dev->data;
	int32_t value_converted;

	if (channel == SENSOR_CHAN_AMBIENT_TEMP) {
		value_converted = (int32_t)data->temperature;

		/* Convert temperature from 0.01 degrees Celsius to degrees Celsius */
		value->val1 = value_converted / 100;
		value->val2 = (value_converted % 100) * (1000000 / 100);
	} else if (channel == SENSOR_CHAN_HUMIDITY) {
		value_converted = (int32_t)data->humidity;

		/* Convert humidity from 0.01 percent to percent */
		value->val1 = value_converted / 100;
		value->val2 = (value_converted % 100) * (1000000 / 100);
	} else {
		return -ENOTSUP;
	}

	return 0;
}

/* Set output data rate. See hids_odr_list for allowed values. */
static int hids_odr_set(const struct device *dev, const struct sensor_value *odr)
{
	struct hids_data *data = dev->data;
	int odr_index;

	for (odr_index = 0; odr_index < ARRAY_SIZE(hids_odr_list); odr_index++) {
		if (odr->val1 == hids_odr_list[odr_index].val1 &&
		    odr->val2 == hids_odr_list[odr_index].val2) {
			break;
		}
	}

	if (odr_index == ARRAY_SIZE(hids_odr_list)) {
		/* ODR not allowed (was not found in hids_odr_list) */
		LOG_ERR("Bad sampling frequency %d.%d", odr->val1, odr->val2);
		return -EINVAL;
	}

	if (HIDS_setOutputDataRate(&data->sensor_interface, (HIDS_outputDataRate_t)odr_index) !=
	    WE_SUCCESS) {
		LOG_ERR("Failed to set output data rate");
		return -EIO;
	}

	return 0;
}

static int hids_attr_set(const struct device *dev, enum sensor_channel chan,
			 enum sensor_attribute attr, const struct sensor_value *val)
{
	if (chan != SENSOR_CHAN_ALL) {
		LOG_WRN("attr_set() is not supported on channel %d.", chan);
		return -ENOTSUP;
	}

	if (attr == SENSOR_ATTR_SAMPLING_FREQUENCY) {
		return hids_odr_set(dev, val);
	} else {
		return -ENOTSUP;
	}
}

static const struct sensor_driver_api hids_driver_api = {
	.attr_set = hids_attr_set,
#if CONFIG_WSEN_HIDS_TRIGGER
	.trigger_set = hids_trigger_set,
#endif
	.sample_fetch = hids_sample_fetch,
	.channel_get = hids_channel_get,
};

static int hids_init(const struct device *dev)
{
	const struct hids_config *config = dev->config;
	struct hids_data *data = dev->data;
	uint8_t device_id;

	/* Initialize WE sensor interface */
	WE_sensorInterfaceType_t interface_type = data->sensor_interface.interfaceType;

	HIDS_getDefaultInterface(&data->sensor_interface);
	data->sensor_interface.interfaceType = interface_type;

	switch (data->sensor_interface.interfaceType) {
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
	case WE_i2c:
		data->sensor_interface.handle = (void *)&config->bus_cfg.i2c;
		break;
#endif
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
	case WE_spi:
		data->sensor_interface.handle = (void *)&config->bus_cfg.spi;
		break;
#endif
	default:
		LOG_ERR("Invalid interface type");
		return -EINVAL;
	}

	/* First communication test - check device ID */
	if (HIDS_getDeviceID(&data->sensor_interface, &device_id) != WE_SUCCESS) {
		LOG_ERR("Failed to read device ID.");
		return -EIO;
	}

	if (device_id != HIDS_DEVICE_ID_VALUE) {
		LOG_ERR("Invalid device ID 0x%x.", device_id);
		return -EINVAL;
	}

	if (HIDS_setOutputDataRate(&data->sensor_interface, config->odr) != WE_SUCCESS) {
		LOG_ERR("Failed to set output data rate.");
		return -EIO;
	}

	if (HIDS_enableBlockDataUpdate(&data->sensor_interface, HIDS_enable) != WE_SUCCESS) {
		LOG_ERR("Failed to enable block data update.");
		return -EIO;
	}

	if (HIDS_setPowerMode(&data->sensor_interface, HIDS_activeMode) != WE_SUCCESS) {
		LOG_ERR("Failed to set power mode.");
		return -EIO;
	}

	if (HIDS_readCalibrationData(&data->sensor_interface) != WE_SUCCESS) {
		LOG_ERR("Failed to read calibration data.");
		return -EIO;
	}

#if CONFIG_WSEN_HIDS_TRIGGER
	int status = hids_init_interrupt(dev);

	if (status < 0) {
		LOG_ERR("Failed to initialize data-ready interrupt.");
		return status;
	}
#endif

	return 0;
}

#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
#warning "HIDS driver enabled without any devices"
#endif

/*
 * Device creation macros
 */

#define HIDS_DEVICE_INIT(inst)                                                         \
	DEVICE_DT_INST_DEFINE(inst,                                                    \
			      hids_init,                                               \
			      NULL,                                                    \
			      &hids_data_##inst,                                       \
			      &hids_config_##inst,                                     \
			      POST_KERNEL,                                             \
			      CONFIG_SENSOR_INIT_PRIORITY,                             \
			      &hids_driver_api);

#ifdef CONFIG_WSEN_HIDS_TRIGGER
#define HIDS_CFG_IRQ(inst) .gpio_drdy = GPIO_DT_SPEC_INST_GET(inst, drdy_gpios)
#else
#define HIDS_CFG_IRQ(inst)
#endif /* CONFIG_WSEN_HIDS_TRIGGER */

#define HIDS_CONFIG_COMMON(inst)                                                       \
	.odr = (HIDS_outputDataRate_t)(DT_INST_ENUM_IDX(inst, odr) + 1),               \
	COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios),                           \
			(HIDS_CFG_IRQ(inst)), ())

/*
 * Instantiation macros used when device is on SPI bus.
 */

#define HIDS_SPI_OPERATION (SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_MODE_CPOL | SPI_MODE_CPHA)

#define HIDS_CONFIG_SPI(inst)                                                          \
	{                                                                              \
		.bus_cfg = {                                                           \
			.spi = SPI_DT_SPEC_INST_GET(inst,                              \
							HIDS_SPI_OPERATION,            \
							0),                            \
		},                                                                     \
		HIDS_CONFIG_COMMON(inst)                                               \
	}

/*
 * Instantiation macros used when device is on I2C bus.
 */

#define HIDS_CONFIG_I2C(inst)                                                          \
	{                                                                              \
		.bus_cfg = {                                                           \
			.i2c = I2C_DT_SPEC_INST_GET(inst),                             \
		},                                                                     \
		HIDS_CONFIG_COMMON(inst)                                               \
	}

/*
 * Main instantiation macro. Use of COND_CODE_1() selects the right
 * bus-specific macro at preprocessor time.
 */
#define HIDS_DEFINE(inst)                                                              \
	static struct hids_data hids_data_##inst =                                     \
		COND_CODE_1(DT_INST_ON_BUS(inst, i2c),                                 \
			    ({ .sensor_interface = { .interfaceType = WE_i2c } }), ()) \
		COND_CODE_1(DT_INST_ON_BUS(inst, spi),                                 \
			    ({ .sensor_interface = { .interfaceType = WE_spi } }), ());\
	static const struct hids_config hids_config_##inst =                           \
		COND_CODE_1(DT_INST_ON_BUS(inst, i2c), (HIDS_CONFIG_I2C(inst)), ())      \
		COND_CODE_1(DT_INST_ON_BUS(inst, spi), (HIDS_CONFIG_SPI(inst)), ());     \
	HIDS_DEVICE_INIT(inst)

DT_INST_FOREACH_STATUS_OKAY(HIDS_DEFINE)
Loading