Skip to content

Commit

Permalink
sensors/sht4x: Converted SHT4X driver to UORB framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
linguini1 committed Jan 16, 2025
1 parent 118f827 commit e06be9b
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 372 deletions.
103 changes: 38 additions & 65 deletions Documentation/components/drivers/special/sensors/sht4x.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
=====
SHT4X
=====

Expand All @@ -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
=================================
Expand All @@ -16,92 +18,64 @@ 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 UORB
driver.

The SHT4x registration function allows the driver to be registered as a POSIX
character driver.
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>``.

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.
.. code-block:: c
The `write()` operation is not implemented for this sensor.
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);
}
Specific operations the sensor offers can be performed via the POSIX `ioctl`
operation. The supported commands are:
To debug this device, you can include the ``uorb_listener`` in your build with
debugging enabled. Running it will show the sensor measurements.

* :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`
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.

.. c:macro:: SNIOC_RESET
``SNIOC_RESET``
----------------

This will perform the SHT4X's soft reset command.
This will perform the SHT4X's soft reset command.

.. code-block:: c
err = ioctl(sensor, SNIOC_RESET);
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.

.. code-block:: c
uint32_t serialno = 0;
err = 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`.
err = orb_ioctl(sensor, SNIOC_WHO_AM_I, &serialno);
.. 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
Expand All @@ -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
Expand All @@ -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);
4 changes: 2 additions & 2 deletions boards/arm/rp2040/common/src/rp2040_common_bringup.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,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)
{
Expand Down
2 changes: 1 addition & 1 deletion drivers/sensors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 11 additions & 10 deletions drivers/sensors/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1627,19 +1627,19 @@ 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
range 0x44 0x46
---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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion drivers/sensors/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit e06be9b

Please sign in to comment.