drivers/sensors: add initial support for fixed-point data for sensors

new sensor framework can now select between float data type and
fixed-point data type

Signed-off-by: raiden00pl <raiden00@railab.me>
This commit is contained in:
raiden00pl
2025-11-29 21:37:11 +01:00
committed by Xiang Xiao
parent ae7525d195
commit 39378e1d46
30 changed files with 529 additions and 202 deletions
@@ -48,11 +48,68 @@ sensor events are sent to the ring buffer in the upper layer.
:width: 800px
:align: center
Problems to solve
=================
Sensor data type
================
The current implementation uses the ``float`` type which may make it difficult
to use on platforms without FPU support.
The framework supports two types of sensor data:
#. ``float`` if ``CONFIG_SENSORS_USE_FLOAT=y``, not recommended for
targets without FPU.
#. ``b16_t`` if ``CONFIG_SENSORS_USE_B16=y``, recommended for targets without
FPU.
Currently all sensors support ``float``, support for ``b16_t`` is work-in-progress.
.. warning::
Using fixed-point math instead of floating point may cause sensors measurements
to saturate where they wouldn't before. Verify that fixed-point data types are
sufficient for your application. Just because a driver has fixed-point support
**does not mean** it can support the sensor's full range.
To create generic drivers that support both data types, you should use the dedicated
sensor data type:
.. code:: C
/* Data type for sensors */
#ifdef CONFIG_SENSORS_USE_B16
typedef b16_t sensor_data_t;
#else
typedef float sensor_data_t;
#endif
Mathematical operations on this type should only be performed using dedicated
macros:
- ``sensor_data_ftof(f1)`` - convert float to sensor data number.
Should be used only for compile-time constants.
- ``sensor_data_itof(i)`` - convert int to sensor data number.
- ``sensor_data_inv(i)`` - invert int and convert to sensor data number
- ``sensor_data_add(f1, f2)`` - add two sensor data numbers
- ``sensor_data_sub(f1, f2)`` - subtract two sensor data numbers
- ``sensor_data_subi(f1, i)`` - subtract int from sensor data number
- ``sensor_data_mul(f1, f2)`` - multiply two sensor data numbers
- ``sensor_data_muli(f1, i)`` - multiply sensor data with int
- ``sensor_data_div(f1, f2)`` - divide two sensor data numbers
- ``sensor_data_divi(f1, i)`` - divide sensor data with int
- ``sensor_data_abs(f1)`` - get absolute value for sensor data number
- ``sensor_data_sqrt(f1)`` - get sqrt for sensro data number
- ``sensor_data_usat(f1)`` - unsigned saturation for sensor data.
**Code**
========
@@ -313,9 +370,9 @@ being ``sensor_device_info_s``.
struct sensor_device_info_s
{
uint32_t version;
float power;
float max_range;
float resolution;
sensor_data_t power;
sensor_data_t max_range;
sensor_data_t resolution;
int32_t min_delay;
int32_t max_delay;
uint32_t fifo_reserved_event_count;
+18
View File
@@ -17,6 +17,24 @@ config USENSOR
---help---
Allow application to register user sensor by /dev/usensor.
choice
prompt "Sensor framework data type"
default SENSORS_USE_FLOAT
config SENSORS_USE_FLOAT
bool "Use float as data type for sensors"
---help---
Use float as data type for sensors.
This option is recommended for targets with FPU support.
config SENSORS_USE_B16
bool "Use b16_t as data type for sensors"
---help---
Use b16_t (fixed-point type) as data type for sensors.
This option is recommended for targets without FPU support.
endchoice # Sensor framework data type
config SENSORS_RPMSG
bool "Sensor RPMSG Support"
default n
+6
View File
@@ -43,6 +43,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define ADXL362_SPI_FREQUENCY 1000000
#define ADXL362_SPI_MODE SPIDEV_MODE0
+6
View File
@@ -49,6 +49,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define BME680_ADDR 0x76 /* I2C Slave Address */
#define BME680_FREQ CONFIG_BME680_I2C_FREQUENCY
#define BME680_DEVID 0x61
+6
View File
@@ -49,6 +49,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define BME688_ADDR 0x76 /* I2C Slave Address */
#define BME688_FREQ CONFIG_BME688_I2C_FREQUENCY
#define BME688_DEVID 0x61
+6
View File
@@ -36,6 +36,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define BMI088_DEFAULT_INTERVAL 10000 /* Default conversion interval. */
/****************************************************************************
+6
View File
@@ -36,6 +36,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define BMI160_DEFAULT_INTERVAL 10000 /* Default conversion interval. */
/****************************************************************************
+6
View File
@@ -49,6 +49,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define CONSTANTS_ONE_G 9.8f
/****************************************************************************
+6
View File
@@ -43,6 +43,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define BMM150_CHIPID_VAL 0x32
#define BMM150_CHIPID 0x40
+6
View File
@@ -37,6 +37,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define BMP180_MIN_INTERVAL 30000
/****************************************************************************
+6
View File
@@ -45,6 +45,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#ifdef CONFIG_BMP280_I2C_ADDR_76
#define BMP280_ADDR 0x76
#else
+6
View File
@@ -50,6 +50,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#ifndef CONFIG_ENDIAN_BIG
# define ds18b20_leuint64(x) (x)
#endif
+10
View File
@@ -46,6 +46,16 @@
#include <nuttx/sensors/gnss.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
/****************************************************************************
* Private Types
****************************************************************************/
+6
View File
@@ -45,6 +45,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define FS3000_ADDR 0x28
/****************************************************************************
+6
View File
@@ -43,6 +43,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define GNSS_PATH_FMT "/dev/ttyGNSS%d"
#define GNSS_PARSE_BUFFERSIZE 256
+10
View File
@@ -40,6 +40,16 @@
#include <nuttx/sensors/goldfish_gnss.h>
#include <nuttx/sensors/gnss.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
/****************************************************************************
* Private Types
****************************************************************************/
+6
View File
@@ -45,6 +45,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define GOLDFISH_LIST_SENSOR_CMD "list-sensors"
/****************************************************************************
+6
View File
@@ -47,6 +47,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define HYT271_TEMPDATA_MASK 0x3fff
#define HYT271_HUMIDATA_MASK 0x3fff
+10
View File
@@ -46,6 +46,16 @@
#if defined(CONFIG_SPI) && defined(CONFIG_SENSORS_L3GD20)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
/****************************************************************************
* Private Types
****************************************************************************/
+6
View File
@@ -58,6 +58,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#ifndef CONFIG_SENSORS_L86_XXX_THREAD_STACKSIZE
#define CONFIG_SENSORS_L86_XXX_THREAD_STACKSIZE 10000
#endif
+6
View File
@@ -49,6 +49,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
/* The value that should be in the "who am I" register */
#define WHO_AM_I_VAL 0x40
+6
View File
@@ -46,6 +46,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
/* The value that should be in the WHO_AM_I register. */
#define WHO_AM_I_VAL 0x6c
+6
View File
@@ -49,6 +49,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define CONSTANTS_ONE_G 9.8f
/****************************************************************************
+6
View File
@@ -45,6 +45,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define LTR308_ADDR 0x53
#define DEVID 0xB1
+6
View File
@@ -47,6 +47,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define REG_THERMO_HOT_JUNC 0x0 /* Thermocouple Hot-Junction, T H */
#define REG_JUNC_TEMP_DELTA 0x1 /* Junctions Temperature Delta, TΔ */
#define REG_COLD_JUNC_TEMP 0x2 /* Cold-Junction Temperature, T C */
+6
View File
@@ -51,6 +51,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define MPU9250_AKM_DEV_ID 0x48 /* Magnetometer device ID */
#define MIN(x, y) (x) > (y) ? (y) : (x)
+6
View File
@@ -52,6 +52,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define MS56XX_CMD_RESET 0x1e
#define MS56XX_CMD_START_ADC_READ 0x00
#define MS56XX_CMD_CONV_D1_OSR_256 0x40 /* D1 = uncompensated pressure */
+6
View File
@@ -47,6 +47,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define SHT4X_CRC_INIT 0xFF /* Initial value of the calculated CRC. */
#define SHT4X_CRC_POLY 0x31 /* CRC calculation polynomial. */
+6
View File
@@ -43,6 +43,12 @@
* Pre-processor Definitions
****************************************************************************/
/* Only float data type supported now */
#ifdef CONFIG_SENSORS_USE_B16
# error fixed-point data type not supported yet
#endif
#define WTGAHRS2_ACCEL_IDX 0
#define WTGAHRS2_GYRO_IDX 1
#define WTGAHRS2_MAG_IDX 2
+273 -195
View File
File diff suppressed because it is too large Load Diff