sensors/sht4x: Converted SHT4X driver to UORB framework.

This commit is contained in:
Matteo Golin
2025-01-15 20:30:12 -05:00
committed by Xiang Xiao
parent acd7e44cad
commit c15b900a88
7 changed files with 419 additions and 388 deletions
@@ -1,3 +1,4 @@
=====
SHT4X
=====
@@ -7,7 +8,8 @@ The SHT4x is a family of temperature and humidity sensors created by Sensirion
which operates over I2C. They include a small heating element.
The driver provided allows interfacing with the sensor over I2C. It has been
tested against the SHT41.
tested against the SHT41. This driver uses the :doc:`uorb
</components/drivers/special/sensors/sensors_uorb>` interface.
Application Programming Interface
=================================
@@ -16,42 +18,48 @@ The header file for the SHT4X driver interface can be included using:
.. code-block:: c
# include <nuttx/sensors/sht4x.h>
#include <nuttx/sensors/sht4x.h>
The SHT4x registration function allows the driver to be registered as a POSIX
character driver.
The SHT4x registration function allows the driver to be registered as a UORB
driver.
The standard POSIX `read()` operation will return the temperature and humidity
measurements in plain-text, which is useful when debugging/testing the driver
using `cat` from the shell.
The `write()` operation is not implemented for this sensor.
Specific operations the sensor offers can be performed via the POSIX `ioctl`
operation. The supported commands are:
* :c:macro:`SNIOC_RESET`
* :c:macro:`SNIOC_WHO_AM_I`
* :c:macro:`SNIOC_READ_RAW_DATA`
* :c:macro:`SNIOC_MEASURE`
* :c:macro:`SNIOC_READ_CONVERT_DATA`
* :c:macro:`SNIOC_HEAT`
* :c:macro:`SNIOC_CONFIGURE`
.. c:macro:: SNIOC_RESET
This will perform the SHT4X's soft reset command.
The SHT4x measures both ambient temperature and humidity, so registering this
driver will cause two new UORB topics to appear: ``sensor_humi<n>`` and
``sensor_temp<n>``.
.. code-block:: c
err = ioctl(sensor, SNIOC_RESET);
int err;
err = sht4x_register(i2c_master, 0, 0x44);
if (err < 0)
{
syslog(LOG_ERR, "Couldn't register SHT4X driver at 0x44: %d\n", err);
}
To debug this device, you can include the ``uorb_listener`` in your build with
debugging enabled. Running it will show the sensor measurements.
This sensor also offers some addition control commands for using the onboard
heater and checking the serial number. These control commands can be used on
either topic (humidity or temperature), since they control the device as a
whole.
``SNIOC_RESET``
----------------
This will perform the SHT4X's soft reset command.
.. code-block:: c
err = orb_ioctl(sensor, SNIOC_RESET);
if (err) {
fprintf(stderr, "SNIOC_RESET: %s\n", strerror(errno));
} else {
puts("RESET success!");
}
.. c:macro:: SNIOC_WHO_AM_I
``SNIOC_WHO_AM_I``
------------------
This command reads the serial number of the SHT4X sensor. The serial number is
returned in the argument to the command, which must be a `uint32_t` pointer.
@@ -59,49 +67,15 @@ returned in the argument to the command, which must be a `uint32_t` pointer.
.. code-block:: c
uint32_t serialno = 0;
err = ioctl(sensor, SNIOC_WHO_AM_I, &serialno);
err = orb_ioctl(sensor, SNIOC_WHO_AM_I, &serialno);
.. c:macro:: SNIOC_READ_RAW_DATA
This command allows the caller to read the raw data returned from the sensor,
without the driver performing any calculation to convert it into familiar units
(i.e. degrees Celsius for temperature).
The argument to this command must be a pointer to a `struct sht4x_raw_data_s`
structure. The raw data will be returned here.
.. code-block:: c
struct sht4x_raw_data_s raw;
err = ioctl(sensor, SNIOC_READ_RAW_DATA, &raw);
.. c:macro:: SNIOC_MEASURE
This command will measure temperature and humidity, and return it in familiar
units to the user. Temperature will be in degrees (Fahrenheit or Celsius depends
on the Kconfig options selected during compilation) and humidity will be %RH.
The argument to this command must be a pointer to a `struct sht4x_conv_data_s`.
This is where the converted data will be returned.
.. code-block:: c
struct sht4x_conv_data_s data;
err = ioctl(sensor, SNIOC_MEASURE, &data);
.. c:macro:: SNIOC_READ_CONVERT_DATA
Same as `SNIOC_MEASURE`.
.. c:macro:: SNIOC_HEAT
``SNIOC_HEAT``
--------------
This command will instruct the SHT4X to turn on its heater unit for the
specified time. Afterwards, a measurement of temperature and humidity is taken,
and the converted data is returned to the caller.
specified time.
The argument to this command must be a pointer to a `struct sht4x_conv_data_s`.
This is where the converted data will be returned. The `temperature` field of
the struct must contain a value from the `enum sht4x_heater_e`, which will
The argument to this command must be of type `enum sht4x_heater_e`, which will
indicate the duration the heater is on and the power used.
Heating commands are not allowed more than once per second to avoid damaging the
@@ -110,11 +84,10 @@ sensor. If a command is issued before this one second cool-down period is over,
.. code-block:: c
struct sht4x_conv_data_s data;
data.temp = SHT4X_HEATER_200MW_1;
err = ioctl(sensor, SNIOC_HEAT, &data);
err = orb_ioctl(sensor, SNIOC_HEAT, SHT4X_HEATER_200MW_1);
.. c:macro:: SNIOC_CONFIGURE
``SNIOC_CONFIGURE``
-------------------
This command allows the caller to configure the precision of the SHT4X sensor
used by subsequent measurement commands. By default, the sensor starts at high
@@ -124,4 +97,4 @@ The argument to this command is one of the values in `enum sht4x_precision_e`.
.. code-block:: c
err = ioctl(sensor, SNIOC_CONFIGURE, SHT4X_PREC_LOW);
err = orb_ioctl(sensor, SNIOC_CONFIGURE, SHT4X_PREC_LOW);
@@ -538,9 +538,9 @@ int rp2040_common_bringup(void)
#ifdef CONFIG_SENSORS_SHT4X
/* Try to register SHT4X device as /dev/sht4x0 at I2C0. */
/* Try to register SHT4X device on I2C0 */
ret = sht4x_register("/dev/sht4x0", rp2040_i2cbus_initialize(0),
ret = sht4x_register(rp2040_i2cbus_initialize(0), 0,
CONFIG_SHT4X_I2C_ADDR);
if (ret < 0)
{
+1 -1
View File
@@ -285,7 +285,7 @@ if(CONFIG_SENSORS)
endif()
if(CONFIG_SENSORS_SHT4X)
list(APPEND SRCS sht4x.c)
list(APPEND SRCS sht4x_uorb.c)
endif()
if(CONFIG_SENSORS_SPS30)
+11 -10
View File
@@ -1627,6 +1627,12 @@ config SHT4X_I2C_FREQUENCY
default 400000
range 1 400000
config SHT4X_THREAD_STACKSIZE
int "SHT4X worker thread stack size"
default 1024
---help---
The stack size for the worker thread that performs measurements
config SHT4X_I2C_ADDR
hex "SHT4X I2C address"
default 0x44
@@ -1634,12 +1640,6 @@ config SHT4X_I2C_ADDR
---help---
Enables debug features for the SHT4X
config SHT4X_FAHRENHEIT
bool "SHT4X use Fahrenheit"
default n
---help---
Configures read outputs of the SHT4X to be in Fahrenheit. Default uses Celsius.
config SHT4X_LIMIT_HUMIDITY
bool "SHT4X limit humidity between 0-100%"
default y
@@ -1652,11 +1652,12 @@ config SHT4X_CRC_LOOKUP
---help---
Configures the SHT4X to do CRC checks with a lookup table. Default uses a bitwise CRC calculation.
config SHT4X_DEBUG
bool "SHT4X debug support"
default n
config SENSORS_SHT4X_POLL
bool "Use polling"
default y
---help---
Enables debug features for the SHT4X
Configures the SHT4X to be polled at an interval instead of allowing user code to choose when measurements are
taken.
endif # SENSORS_SHT4X
+1 -1
View File
@@ -289,7 +289,7 @@ ifeq ($(CONFIG_SENSORS_SHT3X),y)
endif
ifeq ($(CONFIG_SENSORS_SHT4X),y)
CSRCS += sht4x.c
CSRCS += sht4x_uorb.c
endif
ifeq ($(CONFIG_SENSORS_SPS30),y)
File diff suppressed because it is too large Load Diff
+2 -15
View File
@@ -35,18 +35,6 @@
struct i2c_master_s; /* Forward reference */
struct sht4x_conv_data_s
{
int32_t temp; /* Millidegrees Celsius or Fahrenheit (depending on config) */
int16_t hum; /* Percentage relative humidity in units of 0.01 %RH */
};
struct sht4x_raw_data_s
{
uint16_t raw_temp;
uint16_t raw_hum;
};
/* Precision for reading. */
enum sht4x_precision_e
@@ -79,9 +67,9 @@ enum sht4x_heater_e
* Register the SHT4X character device as 'devpath'
*
* Input Parameters:
* devpath - The full path to the driver to register. E.g., "/dev/temp0"
* i2c - An instance of the I2C interface to use to communicate with
* the SHT4X
* devno - The device number that this device should have.
* addr - The I2C address of the SHT4X. The I2C address is one of 0x44,
* 0x45 and 0x46.
*
@@ -90,7 +78,6 @@ enum sht4x_heater_e
*
****************************************************************************/
int sht4x_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
uint8_t addr);
int sht4x_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr);
#endif /* __INCLUDE_NUTTX_SENSORS_SHT4X_H */