Zephyr API Documentation 4.0.0-rc2
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
sensor.h File Reference

Public APIs for the sensor driver. More...

#include <errno.h>
#include <stdlib.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor_data_types.h>
#include <zephyr/dsp/types.h>
#include <zephyr/rtio/rtio.h>
#include <zephyr/sys/iterable_sections.h>
#include <zephyr/types.h>
#include <zephyr/syscalls/sensor.h>

Go to the source code of this file.

Data Structures

struct  sensor_value
 Representation of a sensor readout value. More...
 
struct  sensor_trigger
 Sensor trigger spec. More...
 
struct  sensor_chan_spec
 Sensor Channel Specification. More...
 
struct  sensor_decoder_api
 Decodes a single raw data buffer. More...
 
struct  sensor_decode_context
 Used for iterating over the data frames via the sensor_decoder_api. More...
 
struct  sensor_stream_trigger
 
struct  sensor_read_config
 
struct  sensor_driver_api
 
struct  sensor_data_generic_header
 

Macros

#define SENSOR_DECODE_CONTEXT_INIT(decoder_, buffer_, channel_type_, channel_index_)
 Initialize a sensor_decode_context.
 
#define SENSOR_STREAM_TRIGGER_PREP(_trigger, _opt)
 
#define SENSOR_DT_READ_IODEV(name, dt_node, ...)
 Define a reading instance of a sensor.
 
#define SENSOR_DT_STREAM_IODEV(name, dt_node, ...)
 Define a stream instance of a sensor.
 
#define SENSOR_CHANNEL_3_AXIS(chan)
 checks if a given channel is a 3-axis channel
 
#define SENSOR_G   9806650LL
 The value of gravitational constant in micro m/s^2.
 
#define SENSOR_PI   3141592LL
 The value of constant PI in micros.
 
#define SENSOR_INFO_DEFINE(name, ...)
 
#define SENSOR_INFO_DT_DEFINE(node_id)
 
#define SENSOR_DEVICE_DT_DEFINE(node_id, init_fn, pm_device, data_ptr, cfg_ptr, level, prio, api_ptr, ...)
 Like DEVICE_DT_DEFINE() with sensor specifics.
 
#define SENSOR_DEVICE_DT_INST_DEFINE(inst, ...)
 Like SENSOR_DEVICE_DT_DEFINE() for an instance of a DT_DRV_COMPAT compatible.
 
#define SENSOR_DECODER_NAME()
 Get the decoder name for the current driver.
 
#define SENSOR_DECODER_DT_GET(node_id)
 Statically get the decoder for a given node.
 
#define SENSOR_DECODER_API_DT_DEFINE()
 Define a decoder API.
 

Typedefs

typedef void(* sensor_trigger_handler_t) (const struct device *dev, const struct sensor_trigger *trigger)
 Callback API upon firing of a trigger.
 
typedef int(* sensor_attr_set_t) (const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, const struct sensor_value *val)
 Callback API upon setting a sensor's attributes.
 
typedef int(* sensor_attr_get_t) (const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, struct sensor_value *val)
 Callback API upon getting a sensor's attributes.
 
typedef int(* sensor_trigger_set_t) (const struct device *dev, const struct sensor_trigger *trig, sensor_trigger_handler_t handler)
 Callback API for setting a sensor's trigger and handler.
 
typedef int(* sensor_sample_fetch_t) (const struct device *dev, enum sensor_channel chan)
 Callback API for fetching data from a sensor.
 
typedef int(* sensor_channel_get_t) (const struct device *dev, enum sensor_channel chan, struct sensor_value *val)
 Callback API for getting a reading from a sensor.
 
typedef int(* sensor_get_decoder_t) (const struct device *dev, const struct sensor_decoder_api **api)
 Get the decoder associate with the given device.
 
typedef void(* sensor_submit_t) (const struct device *sensor, struct rtio_iodev_sqe *sqe)
 
typedef void(* sensor_processing_callback_t) (int result, uint8_t *buf, uint32_t buf_len, void *userdata)
 Callback function used with the helper processing function.
 

Enumerations

enum  sensor_channel {
  SENSOR_CHAN_ACCEL_X , SENSOR_CHAN_ACCEL_Y , SENSOR_CHAN_ACCEL_Z , SENSOR_CHAN_ACCEL_XYZ ,
  SENSOR_CHAN_GYRO_X , SENSOR_CHAN_GYRO_Y , SENSOR_CHAN_GYRO_Z , SENSOR_CHAN_GYRO_XYZ ,
  SENSOR_CHAN_MAGN_X , SENSOR_CHAN_MAGN_Y , SENSOR_CHAN_MAGN_Z , SENSOR_CHAN_MAGN_XYZ ,
  SENSOR_CHAN_DIE_TEMP , SENSOR_CHAN_AMBIENT_TEMP , SENSOR_CHAN_PRESS , SENSOR_CHAN_PROX ,
  SENSOR_CHAN_HUMIDITY , SENSOR_CHAN_LIGHT , SENSOR_CHAN_IR , SENSOR_CHAN_RED ,
  SENSOR_CHAN_GREEN , SENSOR_CHAN_BLUE , SENSOR_CHAN_ALTITUDE , SENSOR_CHAN_PM_1_0 ,
  SENSOR_CHAN_PM_2_5 , SENSOR_CHAN_PM_10 , SENSOR_CHAN_DISTANCE , SENSOR_CHAN_CO2 ,
  SENSOR_CHAN_O2 , SENSOR_CHAN_VOC , SENSOR_CHAN_GAS_RES , SENSOR_CHAN_VOLTAGE ,
  SENSOR_CHAN_VSHUNT , SENSOR_CHAN_CURRENT , SENSOR_CHAN_POWER , SENSOR_CHAN_RESISTANCE ,
  SENSOR_CHAN_ROTATION , SENSOR_CHAN_POS_DX , SENSOR_CHAN_POS_DY , SENSOR_CHAN_POS_DZ ,
  SENSOR_CHAN_POS_DXYZ , SENSOR_CHAN_RPM , SENSOR_CHAN_GAUGE_VOLTAGE , SENSOR_CHAN_GAUGE_AVG_CURRENT ,
  SENSOR_CHAN_GAUGE_STDBY_CURRENT , SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT , SENSOR_CHAN_GAUGE_TEMP , SENSOR_CHAN_GAUGE_STATE_OF_CHARGE ,
  SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY , SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY , SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY , SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY ,
  SENSOR_CHAN_GAUGE_AVG_POWER , SENSOR_CHAN_GAUGE_STATE_OF_HEALTH , SENSOR_CHAN_GAUGE_TIME_TO_EMPTY , SENSOR_CHAN_GAUGE_TIME_TO_FULL ,
  SENSOR_CHAN_GAUGE_CYCLE_COUNT , SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE , SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE , SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT ,
  SENSOR_CHAN_ALL , SENSOR_CHAN_COMMON_COUNT , SENSOR_CHAN_PRIV_START = SENSOR_CHAN_COMMON_COUNT , SENSOR_CHAN_MAX = INT16_MAX
}
 Sensor channels. More...
 
enum  sensor_trigger_type {
  SENSOR_TRIG_TIMER , SENSOR_TRIG_DATA_READY , SENSOR_TRIG_DELTA , SENSOR_TRIG_NEAR_FAR ,
  SENSOR_TRIG_THRESHOLD , SENSOR_TRIG_TAP , SENSOR_TRIG_DOUBLE_TAP , SENSOR_TRIG_FREEFALL ,
  SENSOR_TRIG_MOTION , SENSOR_TRIG_STATIONARY , SENSOR_TRIG_FIFO_WATERMARK , SENSOR_TRIG_FIFO_FULL ,
  SENSOR_TRIG_COMMON_COUNT , SENSOR_TRIG_PRIV_START = SENSOR_TRIG_COMMON_COUNT , SENSOR_TRIG_MAX = INT16_MAX
}
 Sensor trigger types. More...
 
enum  sensor_attribute {
  SENSOR_ATTR_SAMPLING_FREQUENCY , SENSOR_ATTR_LOWER_THRESH , SENSOR_ATTR_UPPER_THRESH , SENSOR_ATTR_SLOPE_TH ,
  SENSOR_ATTR_SLOPE_DUR , SENSOR_ATTR_HYSTERESIS , SENSOR_ATTR_OVERSAMPLING , SENSOR_ATTR_FULL_SCALE ,
  SENSOR_ATTR_OFFSET , SENSOR_ATTR_CALIB_TARGET , SENSOR_ATTR_CONFIGURATION , SENSOR_ATTR_CALIBRATION ,
  SENSOR_ATTR_FEATURE_MASK , SENSOR_ATTR_ALERT , SENSOR_ATTR_FF_DUR , SENSOR_ATTR_BATCH_DURATION ,
  SENSOR_ATTR_GAIN , SENSOR_ATTR_RESOLUTION , SENSOR_ATTR_COMMON_COUNT , SENSOR_ATTR_PRIV_START = SENSOR_ATTR_COMMON_COUNT ,
  SENSOR_ATTR_MAX = INT16_MAX
}
 Sensor attribute types. More...
 
enum  sensor_stream_data_opt { SENSOR_STREAM_DATA_INCLUDE = 0 , SENSOR_STREAM_DATA_NOP = 1 , SENSOR_STREAM_DATA_DROP = 2 }
 Options for what to do with the associated data when a trigger is consumed. More...
 

Functions

static bool sensor_chan_spec_eq (struct sensor_chan_spec chan_spec0, struct sensor_chan_spec chan_spec1)
 Check if channel specs are equivalent.
 
static int sensor_decode (struct sensor_decode_context *ctx, void *out, uint16_t max_count)
 Decode N frames using a sensor_decode_context.
 
int sensor_natively_supported_channel_size_info (struct sensor_chan_spec channel, size_t *base_size, size_t *frame_size)
 
int sensor_attr_set (const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, const struct sensor_value *val)
 Set an attribute for a sensor.
 
int sensor_attr_get (const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, struct sensor_value *val)
 Get an attribute for a sensor.
 
static int sensor_trigger_set (const struct device *dev, const struct sensor_trigger *trig, sensor_trigger_handler_t handler)
 Activate a sensor's trigger and set the trigger handler.
 
int sensor_sample_fetch (const struct device *dev)
 Fetch a sample from the sensor and store it in an internal driver buffer.
 
int sensor_sample_fetch_chan (const struct device *dev, enum sensor_channel type)
 Fetch a sample from the sensor and store it in an internal driver buffer.
 
int sensor_channel_get (const struct device *dev, enum sensor_channel chan, struct sensor_value *val)
 Get a reading from a sensor device.
 
int sensor_get_decoder (const struct device *dev, const struct sensor_decoder_api **decoder)
 Get the sensor's decoder API.
 
int sensor_reconfigure_read_iodev (struct rtio_iodev *iodev, const struct device *sensor, const struct sensor_chan_spec *channels, size_t num_channels)
 Reconfigure a reading iodev.
 
static int sensor_stream (struct rtio_iodev *iodev, struct rtio *ctx, void *userdata, struct rtio_sqe **handle)
 
static int sensor_read (struct rtio_iodev *iodev, struct rtio *ctx, uint8_t *buf, size_t buf_len)
 Blocking one shot read of samples from a sensor into a buffer.
 
static int sensor_read_async_mempool (struct rtio_iodev *iodev, struct rtio *ctx, void *userdata)
 One shot non-blocking read with pool allocated buffer.
 
void sensor_processing_with_callback (struct rtio *ctx, sensor_processing_callback_t cb)
 Helper function for common processing of sensor data.
 
static int32_t sensor_ms2_to_g (const struct sensor_value *ms2)
 Helper function to convert acceleration from m/s^2 to Gs.
 
static void sensor_g_to_ms2 (int32_t g, struct sensor_value *ms2)
 Helper function to convert acceleration from Gs to m/s^2.
 
static int32_t sensor_ms2_to_ug (const struct sensor_value *ms2)
 Helper function to convert acceleration from m/s^2 to micro Gs.
 
static void sensor_ug_to_ms2 (int32_t ug, struct sensor_value *ms2)
 Helper function to convert acceleration from micro Gs to m/s^2.
 
static int32_t sensor_rad_to_degrees (const struct sensor_value *rad)
 Helper function for converting radians to degrees.
 
static void sensor_degrees_to_rad (int32_t d, struct sensor_value *rad)
 Helper function for converting degrees to radians.
 
static int32_t sensor_rad_to_10udegrees (const struct sensor_value *rad)
 Helper function for converting radians to 10 micro degrees.
 
static void sensor_10udegrees_to_rad (int32_t d, struct sensor_value *rad)
 Helper function for converting 10 micro degrees to radians.
 
static double sensor_value_to_double (const struct sensor_value *val)
 Helper function for converting struct sensor_value to double.
 
static float sensor_value_to_float (const struct sensor_value *val)
 Helper function for converting struct sensor_value to float.
 
static int sensor_value_from_double (struct sensor_value *val, double inp)
 Helper function for converting double to struct sensor_value.
 
static int sensor_value_from_float (struct sensor_value *val, float inp)
 Helper function for converting float to struct sensor_value.
 
static int64_t sensor_value_to_milli (const struct sensor_value *val)
 Helper function for converting struct sensor_value to integer milli units.
 
static int64_t sensor_value_to_micro (const struct sensor_value *val)
 Helper function for converting struct sensor_value to integer micro units.
 
static int sensor_value_from_milli (struct sensor_value *val, int64_t milli)
 Helper function for converting integer milli units to struct sensor_value.
 
static int sensor_value_from_micro (struct sensor_value *val, int64_t micro)
 Helper function for converting integer micro units to struct sensor_value.
 

Detailed Description

Public APIs for the sensor driver.

Macro Definition Documentation

◆ SENSOR_DECODER_API_DT_DEFINE

#define SENSOR_DECODER_API_DT_DEFINE ( )
Value:
COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT), (), (static)) \
#define DT_HAS_COMPAT_STATUS_OKAY(compat)
Does the devicetree have a status okay node with a compatible?
Definition devicetree.h:3604
#define STRUCT_SECTION_ITERABLE(struct_type, varname)
Defines a new element for an iterable section.
Definition iterable_sections.h:216
#define COND_CODE_1(_flag, _if_1_code, _else_code)
Insert code depending on whether _flag expands to 1 or not.
Definition util_macro.h:195
#define SENSOR_DECODER_NAME()
Get the decoder name for the current driver.
Definition sensor.h:1490
Decodes a single raw data buffer.
Definition sensor.h:466

Define a decoder API.

This macro should be created once per compatible string of a sensor and will create a statically referenceable decoder API.

.get_frame_count = my_driver_get_frame_count,
.get_timestamp = my_driver_get_timestamp,
.get_shift = my_driver_get_shift,
.decode = my_driver_decode,
};
#define SENSOR_DECODER_API_DT_DEFINE()
Define a decoder API.
Definition sensor.h:1517

◆ SENSOR_DECODER_DT_GET

#define SENSOR_DECODER_DT_GET ( node_id)
Value:
&UTIL_CAT(DT_STRING_TOKEN_BY_IDX(node_id, compatible, 0), __decoder_api)
#define DT_STRING_TOKEN_BY_IDX(node_id, prop, idx)
Get an element out of a string-array property as a token.
Definition devicetree.h:1294
#define UTIL_CAT(a,...)
Definition util_internal.h:104

Statically get the decoder for a given node.

static const sensor_decoder_api *decoder = SENSOR_DECODER_DT_GET(DT_ALIAS(accel));
#define DT_ALIAS(alias)
Get a node identifier from /aliases.
Definition devicetree.h:236
#define SENSOR_DECODER_DT_GET(node_id)
Statically get the decoder for a given node.
Definition sensor.h:1499

◆ SENSOR_DECODER_NAME

#define SENSOR_DECODER_NAME ( )
Value:
UTIL_CAT(DT_DRV_COMPAT, __decoder_api)

Get the decoder name for the current driver.

This function depends on DT_DRV_COMPAT being defined.