Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
sensor.h
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2016 Intel Corporation
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_H_
13#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_H_
14
22#include <errno.h>
23#include <stdlib.h>
24
25#include <zephyr/device.h>
27#include <zephyr/dsp/types.h>
28#include <zephyr/rtio/rtio.h>
30#include <zephyr/types.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
54};
55
109
118
125
128
131
136
139
142
149
152
189
192
197
203
208};
209
239
242
245
248
251
254
257
264
270
275};
276
285};
286
307 /* Hysteresis for trigger thresholds. */
337
340
345
351
356};
357
365typedef void (*sensor_trigger_handler_t)(const struct device *dev,
366 const struct sensor_trigger *trigger);
367
374typedef int (*sensor_attr_set_t)(const struct device *dev,
375 enum sensor_channel chan,
376 enum sensor_attribute attr,
377 const struct sensor_value *val);
378
385typedef int (*sensor_attr_get_t)(const struct device *dev,
386 enum sensor_channel chan,
387 enum sensor_attribute attr,
388 struct sensor_value *val);
389
396typedef int (*sensor_trigger_set_t)(const struct device *dev,
397 const struct sensor_trigger *trig,
405typedef int (*sensor_sample_fetch_t)(const struct device *dev,
406 enum sensor_channel chan);
413typedef int (*sensor_channel_get_t)(const struct device *dev,
414 enum sensor_channel chan,
415 struct sensor_value *val);
416
434 int (*get_frame_count)(const uint8_t *buffer, enum sensor_channel channel,
435 size_t channel_idx, uint16_t *frame_count);
436
449 int (*get_size_info)(enum sensor_channel channel, size_t *base_size, size_t *frame_size);
450
477 int (*decode)(const uint8_t *buffer, enum sensor_channel channel, size_t channel_idx,
478 uint32_t *fit, uint16_t max_count, void *data_out);
479
487 bool (*has_trigger)(const uint8_t *buffer, enum sensor_trigger_type trigger);
488};
489
520};
521
525#define SENSOR_DECODE_CONTEXT_INIT(decoder_, buffer_, channel_, channel_index_) \
526 { \
527 .decoder = (decoder_), \
528 .buffer = (buffer_), \
529 .channel = (channel_), \
530 .channel_idx = (channel_index_), \
531 .fit = 0, \
532 }
533
542static inline int sensor_decode(struct sensor_decode_context *ctx, void *out, uint16_t max_count)
543{
544 return ctx->decoder->decode(ctx->buffer, ctx->channel, ctx->channel_idx, &ctx->fit,
545 max_count, out);
546}
547
549 size_t *frame_size);
550
557typedef int (*sensor_get_decoder_t)(const struct device *dev,
558 const struct sensor_decoder_api **api);
559
570};
571
575};
576
577#define SENSOR_STREAM_TRIGGER_PREP(_trigger, _opt) \
578 { \
579 .trigger = (_trigger), .opt = (_opt), \
580 }
581/*
582 * Internal data structure used to store information about the IODevice for async reading and
583 * streaming sensor data.
584 */
586 const struct device *sensor;
587 const bool is_streaming;
588 union {
591 };
592 size_t count;
593 const size_t max;
594};
595
610#define SENSOR_DT_READ_IODEV(name, dt_node, ...) \
611 static enum sensor_channel _CONCAT(__channel_array_, name)[] = {__VA_ARGS__}; \
612 static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
613 .sensor = DEVICE_DT_GET(dt_node), \
614 .is_streaming = false, \
615 .channels = _CONCAT(__channel_array_, name), \
616 .count = ARRAY_SIZE(_CONCAT(__channel_array_, name)), \
617 .max = ARRAY_SIZE(_CONCAT(__channel_array_, name)), \
618 }; \
619 RTIO_IODEV_DEFINE(name, &__sensor_iodev_api, _CONCAT(&__sensor_read_config_, name))
620
640#define SENSOR_DT_STREAM_IODEV(name, dt_node, ...) \
641 static struct sensor_stream_trigger _CONCAT(__trigger_array_, name)[] = {__VA_ARGS__}; \
642 static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
643 .sensor = DEVICE_DT_GET(dt_node), \
644 .is_streaming = true, \
645 .triggers = _CONCAT(__trigger_array_, name), \
646 .count = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
647 .max = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
648 }; \
649 RTIO_IODEV_DEFINE(name, &__sensor_iodev_api, &_CONCAT(__sensor_read_config_, name))
650
651/* Used to submit an RTIO sqe to the sensor's iodev */
652typedef int (*sensor_submit_t)(const struct device *sensor, struct rtio_iodev_sqe *sqe);
653
654/* The default decoder API */
655extern const struct sensor_decoder_api __sensor_default_decoder;
656
657/* The default sensor iodev API */
658extern const struct rtio_iodev_api __sensor_iodev_api;
659
660__subsystem struct sensor_driver_api {
668};
669
682__syscall int sensor_attr_set(const struct device *dev,
683 enum sensor_channel chan,
684 enum sensor_attribute attr,
685 const struct sensor_value *val);
686
687static inline int z_impl_sensor_attr_set(const struct device *dev,
688 enum sensor_channel chan,
689 enum sensor_attribute attr,
690 const struct sensor_value *val)
691{
692 const struct sensor_driver_api *api =
693 (const struct sensor_driver_api *)dev->api;
694
695 if (api->attr_set == NULL) {
696 return -ENOSYS;
697 }
698
699 return api->attr_set(dev, chan, attr, val);
700}
701
714__syscall int sensor_attr_get(const struct device *dev,
715 enum sensor_channel chan,
716 enum sensor_attribute attr,
717 struct sensor_value *val);
718
719static inline int z_impl_sensor_attr_get(const struct device *dev,
720 enum sensor_channel chan,
721 enum sensor_attribute attr,
722 struct sensor_value *val)
723{
724 const struct sensor_driver_api *api =
725 (const struct sensor_driver_api *)dev->api;
726
727 if (api->attr_get == NULL) {
728 return -ENOSYS;
729 }
730
731 return api->attr_get(dev, chan, attr, val);
732}
733
756static inline int sensor_trigger_set(const struct device *dev,
757 const struct sensor_trigger *trig,
759{
760 const struct sensor_driver_api *api =
761 (const struct sensor_driver_api *)dev->api;
762
763 if (api->trigger_set == NULL) {
764 return -ENOSYS;
765 }
766
767 return api->trigger_set(dev, trig, handler);
768}
769
788__syscall int sensor_sample_fetch(const struct device *dev);
789
790static inline int z_impl_sensor_sample_fetch(const struct device *dev)
791{
792 const struct sensor_driver_api *api =
793 (const struct sensor_driver_api *)dev->api;
794
795 return api->sample_fetch(dev, SENSOR_CHAN_ALL);
796}
797
819__syscall int sensor_sample_fetch_chan(const struct device *dev,
820 enum sensor_channel type);
821
822static inline int z_impl_sensor_sample_fetch_chan(const struct device *dev,
823 enum sensor_channel type)
824{
825 const struct sensor_driver_api *api =
826 (const struct sensor_driver_api *)dev->api;
827
828 return api->sample_fetch(dev, type);
829}
830
852__syscall int sensor_channel_get(const struct device *dev,
853 enum sensor_channel chan,
854 struct sensor_value *val);
855
856static inline int z_impl_sensor_channel_get(const struct device *dev,
857 enum sensor_channel chan,
858 struct sensor_value *val)
859{
860 const struct sensor_driver_api *api =
861 (const struct sensor_driver_api *)dev->api;
862
863 return api->channel_get(dev, chan, val);
864}
865
866#if defined(CONFIG_SENSOR_ASYNC_API) || defined(__DOXYGEN__)
867
868/*
869 * Generic data structure used for encoding the sample timestamp and number of channels sampled.
870 */
871struct __attribute__((__packed__)) sensor_data_generic_header {
872 /* The timestamp at which the data was collected from the sensor */
874
875 /*
876 * The number of channels present in the frame. This will be the true number of elements in
877 * channel_info and in the q31 values that follow the header.
878 */
880
881 /* Shift value for all samples in the frame */
883
884 /* This padding is needed to make sure that the 'channels' field is aligned */
885 int8_t _padding[sizeof(enum sensor_channel) - 1];
886
887 /* Channels present in the frame */
888 enum sensor_channel channels[0];
889};
890
899#define SENSOR_CHANNEL_3_AXIS(chan) \
900 ((chan) == SENSOR_CHAN_ACCEL_XYZ || (chan) == SENSOR_CHAN_GYRO_XYZ || \
901 (chan) == SENSOR_CHAN_MAGN_XYZ)
902
911__syscall int sensor_get_decoder(const struct device *dev,
912 const struct sensor_decoder_api **decoder);
913
914static inline int z_impl_sensor_get_decoder(const struct device *dev,
915 const struct sensor_decoder_api **decoder)
916{
917 const struct sensor_driver_api *api = (const struct sensor_driver_api *)dev->api;
918
919 __ASSERT_NO_MSG(api != NULL);
920
921 if (api->get_decoder == NULL) {
922 *decoder = &__sensor_default_decoder;
923 return 0;
924 }
925
926 return api->get_decoder(dev, decoder);
927}
928
947__syscall int sensor_reconfigure_read_iodev(struct rtio_iodev *iodev, const struct device *sensor,
948 const enum sensor_channel *channels,
949 size_t num_channels);
950
951static inline int z_impl_sensor_reconfigure_read_iodev(struct rtio_iodev *iodev,
952 const struct device *sensor,
953 const enum sensor_channel *channels,
954 size_t num_channels)
955{
956 struct sensor_read_config *cfg = (struct sensor_read_config *)iodev->data;
957
958 if (cfg->max < num_channels || cfg->is_streaming) {
959 return -ENOMEM;
960 }
961
962 cfg->sensor = sensor;
963 memcpy(cfg->channels, channels, num_channels * sizeof(enum sensor_channel));
964 cfg->count = num_channels;
965 return 0;
966}
967
968static inline int sensor_stream(struct rtio_iodev *iodev, struct rtio *ctx, void *userdata,
969 struct rtio_sqe **handle)
970{
971 if (IS_ENABLED(CONFIG_USERSPACE)) {
972 struct rtio_sqe sqe;
973
975 rtio_sqe_copy_in_get_handles(ctx, &sqe, handle, 1);
976 } else {
977 struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);
978
979 if (sqe == NULL) {
980 return -ENOMEM;
981 }
982 if (handle != NULL) {
983 *handle = sqe;
984 }
986 }
987 rtio_submit(ctx, 0);
988 return 0;
989}
990
1004static inline int sensor_read(struct rtio_iodev *iodev, struct rtio *ctx, void *userdata)
1005{
1006 if (IS_ENABLED(CONFIG_USERSPACE)) {
1007 struct rtio_sqe sqe;
1008
1010 rtio_sqe_copy_in(ctx, &sqe, 1);
1011 } else {
1012 struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);
1013
1014 if (sqe == NULL) {
1015 return -ENOMEM;
1016 }
1018 }
1019 rtio_submit(ctx, 0);
1020 return 0;
1021}
1022
1035 void *userdata);
1036
1049
1050#endif /* defined(CONFIG_SENSOR_ASYNC_API) || defined(__DOXYGEN__) */
1051
1055#define SENSOR_G 9806650LL
1056
1060#define SENSOR_PI 3141592LL
1061
1070static inline int32_t sensor_ms2_to_g(const struct sensor_value *ms2)
1071{
1072 int64_t micro_ms2 = ms2->val1 * 1000000LL + ms2->val2;
1073
1074 if (micro_ms2 > 0) {
1075 return (micro_ms2 + SENSOR_G / 2) / SENSOR_G;
1076 } else {
1077 return (micro_ms2 - SENSOR_G / 2) / SENSOR_G;
1078 }
1079}
1080
1087static inline void sensor_g_to_ms2(int32_t g, struct sensor_value *ms2)
1088{
1089 ms2->val1 = ((int64_t)g * SENSOR_G) / 1000000LL;
1090 ms2->val2 = ((int64_t)g * SENSOR_G) % 1000000LL;
1091}
1092
1101static inline int32_t sensor_ms2_to_ug(const struct sensor_value *ms2)
1102{
1103 int64_t micro_ms2 = (ms2->val1 * INT64_C(1000000)) + ms2->val2;
1104
1105 return (micro_ms2 * 1000000LL) / SENSOR_G;
1106}
1107
1114static inline void sensor_ug_to_ms2(int32_t ug, struct sensor_value *ms2)
1115{
1116 ms2->val1 = ((int64_t)ug * SENSOR_G / 1000000LL) / 1000000LL;
1117 ms2->val2 = ((int64_t)ug * SENSOR_G / 1000000LL) % 1000000LL;
1118}
1119
1127static inline int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
1128{
1129 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
1130
1131 if (micro_rad_s > 0) {
1132 return (micro_rad_s * 180LL + SENSOR_PI / 2) / SENSOR_PI;
1133 } else {
1134 return (micro_rad_s * 180LL - SENSOR_PI / 2) / SENSOR_PI;
1135 }
1136}
1137
1144static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
1145{
1146 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL) / 1000000LL;
1147 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL;
1148}
1149
1161static inline int32_t sensor_rad_to_10udegrees(const struct sensor_value *rad)
1162{
1163 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
1164
1165 return (micro_rad_s * 180LL * 100000LL) / SENSOR_PI;
1166}
1167
1174static inline void sensor_10udegrees_to_rad(int32_t d, struct sensor_value *rad)
1175{
1176 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL / 100000LL) / 1000000LL;
1177 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL / 100000LL) % 1000000LL;
1178}
1179
1186static inline double sensor_value_to_double(const struct sensor_value *val)
1187{
1188 return (double)val->val1 + (double)val->val2 / 1000000;
1189}
1190
1197static inline float sensor_value_to_float(const struct sensor_value *val)
1198{
1199 return (float)val->val1 + (float)val->val2 / 1000000;
1200}
1201
1209static inline int sensor_value_from_double(struct sensor_value *val, double inp)
1210{
1211 if (inp < INT32_MIN || inp > INT32_MAX) {
1212 return -ERANGE;
1213 }
1214
1215 double val2 = (inp - (int32_t)inp) * 1000000.0;
1216
1217 if (val2 < INT32_MIN || val2 > INT32_MAX) {
1218 return -ERANGE;
1219 }
1220
1221 val->val1 = (int32_t)inp;
1222 val->val2 = (int32_t)val2;
1223
1224 return 0;
1225}
1226
1234static inline int sensor_value_from_float(struct sensor_value *val, float inp)
1235{
1236 float val2 = (inp - (int32_t)inp) * 1000000.0f;
1237
1238 if (val2 < INT32_MIN || val2 > (float)(INT32_MAX - 1)) {
1239 return -ERANGE;
1240 }
1241
1242 val->val1 = (int32_t)inp;
1243 val->val2 = (int32_t)val2;
1244
1245 return 0;
1246}
1247
1248#ifdef CONFIG_SENSOR_INFO
1249
1250struct sensor_info {
1251 const struct device *dev;
1252 const char *vendor;
1253 const char *model;
1254 const char *friendly_name;
1255};
1256
1257#define SENSOR_INFO_INITIALIZER(_dev, _vendor, _model, _friendly_name) \
1258 { \
1259 .dev = _dev, \
1260 .vendor = _vendor, \
1261 .model = _model, \
1262 .friendly_name = _friendly_name, \
1263 }
1264
1265#define SENSOR_INFO_DEFINE(name, ...) \
1266 static const STRUCT_SECTION_ITERABLE(sensor_info, name) = \
1267 SENSOR_INFO_INITIALIZER(__VA_ARGS__)
1268
1269#define SENSOR_INFO_DT_NAME(node_id) \
1270 _CONCAT(__sensor_info, DEVICE_DT_NAME_GET(node_id))
1271
1272#define SENSOR_INFO_DT_DEFINE(node_id) \
1273 SENSOR_INFO_DEFINE(SENSOR_INFO_DT_NAME(node_id), \
1274 DEVICE_DT_GET(node_id), \
1275 DT_NODE_VENDOR_OR(node_id, NULL), \
1276 DT_NODE_MODEL_OR(node_id, NULL), \
1277 DT_PROP_OR(node_id, friendly_name, NULL)) \
1278
1279#else
1280
1281#define SENSOR_INFO_DEFINE(name, ...)
1282#define SENSOR_INFO_DT_DEFINE(node_id)
1283
1284#endif /* CONFIG_SENSOR_INFO */
1285
1313#define SENSOR_DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
1314 data_ptr, cfg_ptr, level, prio, \
1315 api_ptr, ...) \
1316 DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
1317 data_ptr, cfg_ptr, level, prio, \
1318 api_ptr, __VA_ARGS__); \
1319 \
1320 SENSOR_INFO_DT_DEFINE(node_id);
1321
1331#define SENSOR_DEVICE_DT_INST_DEFINE(inst, ...) \
1332 SENSOR_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
1333
1340static inline int64_t sensor_value_to_milli(const struct sensor_value *val)
1341{
1342 return ((int64_t)val->val1 * 1000) + val->val2 / 1000;
1343}
1344
1351static inline int64_t sensor_value_to_micro(const struct sensor_value *val)
1352{
1353 return ((int64_t)val->val1 * 1000000) + val->val2;
1354}
1355
1363static inline int sensor_value_from_milli(struct sensor_value *val, int64_t milli)
1364{
1365 if (milli < ((int64_t)INT32_MIN - 1) * 1000LL ||
1366 milli > ((int64_t)INT32_MAX + 1) * 1000LL) {
1367 return -ERANGE;
1368 }
1369
1370 val->val1 = (int32_t)(milli / 1000);
1371 val->val2 = (int32_t)(milli % 1000) * 1000;
1372
1373 return 0;
1374}
1375
1383static inline int sensor_value_from_micro(struct sensor_value *val, int64_t micro)
1384{
1385 if (micro < ((int64_t)INT32_MIN - 1) * 1000000LL ||
1386 micro > ((int64_t)INT32_MAX + 1) * 1000000LL) {
1387 return -ERANGE;
1388 }
1389
1390 val->val1 = (int32_t)(micro / 1000000LL);
1391 val->val2 = (int32_t)(micro % 1000000LL);
1392
1393 return 0;
1394}
1395
1405#define SENSOR_DECODER_NAME() UTIL_CAT(DT_DRV_COMPAT, __decoder_api)
1406
1414#define SENSOR_DECODER_DT_GET(node_id) \
1415 &UTIL_CAT(DT_STRING_TOKEN_BY_IDX(node_id, compatible, 0), __decoder_api)
1416
1432#define SENSOR_DECODER_API_DT_DEFINE() \
1433 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT), (), (static)) \
1434 const STRUCT_SECTION_ITERABLE(sensor_decoder_api, SENSOR_DECODER_NAME())
1435
1436#define Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL_IDX(node_id, prop, idx) \
1437 extern const struct sensor_decoder_api UTIL_CAT( \
1438 DT_STRING_TOKEN_BY_IDX(node_id, prop, idx), __decoder_api);
1439
1440#define Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL(node_id) \
1441 COND_CODE_1(DT_NODE_HAS_PROP(node_id, compatible), \
1442 (DT_FOREACH_PROP_ELEM(node_id, compatible, \
1443 Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL_IDX)), \
1444 ())
1445
1446DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL)
1447
1448#ifdef __cplusplus
1449}
1450#endif
1451
1452#include <syscalls/sensor.h>
1453
1454#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_H_ */
irp nz macro MOVR cc d
Definition: asm-macro-32-bit-gnu.h:11
System error numbers.
#define DT_FOREACH_STATUS_OKAY_NODE(fn)
Invokes fn for every status okay node in the tree.
Definition: devicetree.h:2671
#define RTIO_PRIO_NORM
Normal priority.
Definition: rtio.h:68
static void rtio_sqe_prep_read_with_pool(struct rtio_sqe *sqe, const struct rtio_iodev *iodev, int8_t prio, void *userdata)
Prepare a read op submission with context's mempool.
Definition: rtio.h:511
static int rtio_sqe_copy_in(struct rtio *r, const struct rtio_sqe *sqes, size_t sqe_count)
Copy an array of SQEs into the queue.
Definition: rtio.h:1359
int rtio_sqe_copy_in_get_handles(struct rtio *r, const struct rtio_sqe *sqes, struct rtio_sqe **handle, size_t sqe_count)
Copy an array of SQEs into the queue and get resulting handles back.
static struct rtio_sqe * rtio_sqe_acquire(struct rtio *r)
Acquire a single submission queue event if available.
Definition: rtio.h:895
static void rtio_sqe_prep_read_multishot(struct rtio_sqe *sqe, const struct rtio_iodev *iodev, int8_t prio, void *userdata)
Definition: rtio.h:519
int rtio_submit(struct rtio *r, uint32_t wait_count)
Submit I/O requests to the underlying executor.
#define SENSOR_G
The value of gravitational constant in micro m/s^2.
Definition: sensor.h:1055
static int sensor_decode(struct sensor_decode_context *ctx, void *out, uint16_t max_count)
Decode N frames using a sensor_decode_context.
Definition: sensor.h:542
static int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
Helper function for converting radians to degrees.
Definition: sensor.h:1127
sensor_trigger_type
Sensor trigger types.
Definition: sensor.h:213
sensor_attribute
Sensor attribute types.
Definition: sensor.h:290
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.
Definition: sensor.h:374
int sensor_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder)
Get the sensor's decoder API.
int(* sensor_sample_fetch_t)(const struct device *dev, enum sensor_channel chan)
Callback API for fetching data from a sensor.
Definition: sensor.h:405
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.
Definition: sensor.h:1114
static double sensor_value_to_double(const struct sensor_value *val)
Helper function for converting struct sensor_value to double.
Definition: sensor.h:1186
static float sensor_value_to_float(const struct sensor_value *val)
Helper function for converting struct sensor_value to float.
Definition: sensor.h:1197
static int sensor_read(struct rtio_iodev *iodev, struct rtio *ctx, void *userdata)
Read data from a sensor.
Definition: sensor.h:1004
static void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
Helper function for converting degrees to radians.
Definition: sensor.h:1144
static int32_t sensor_ms2_to_ug(const struct sensor_value *ms2)
Helper function to convert acceleration from m/s^2 to micro Gs.
Definition: sensor.h:1101
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.
Definition: sensor.h:385
int sensor_natively_supported_channel_size_info(enum sensor_channel channel, size_t *base_size, size_t *frame_size)
static int sensor_value_from_float(struct sensor_value *val, float inp)
Helper function for converting float to struct sensor_value.
Definition: sensor.h:1234
void(* sensor_trigger_handler_t)(const struct device *dev, const struct sensor_trigger *trigger)
Callback API upon firing of a trigger.
Definition: sensor.h:365
static void sensor_g_to_ms2(int32_t g, struct sensor_value *ms2)
Helper function to convert acceleration from Gs to m/s^2.
Definition: sensor.h:1087
static int64_t sensor_value_to_milli(const struct sensor_value *val)
Helper function for converting struct sensor_value to integer milli units.
Definition: sensor.h:1340
#define SENSOR_PI
The value of constant PI in micros.
Definition: sensor.h:1060
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.
Definition: sensor.h:756
sensor_stream_data_opt
Options for what to do with the associated data when a trigger is consumed.
Definition: sensor.h:563
static int sensor_value_from_milli(struct sensor_value *val, int64_t milli)
Helper function for converting integer milli units to struct sensor_value.
Definition: sensor.h:1363
int sensor_reconfigure_read_iodev(struct rtio_iodev *iodev, const struct device *sensor, const enum sensor_channel *channels, size_t num_channels)
Reconfigure a reading iodev.
void(* sensor_processing_callback_t)(int result, uint8_t *buf, uint32_t buf_len, void *userdata)
Callback function used with the helper processing function.
Definition: sensor.h:1034
static int64_t sensor_value_to_micro(const struct sensor_value *val)
Helper function for converting struct sensor_value to integer micro units.
Definition: sensor.h:1351
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_sample_fetch(const struct device *dev)
Fetch a sample from the sensor and store it in an internal driver buffer.
sensor_channel
Sensor channels.
Definition: sensor.h:59
int(* sensor_get_decoder_t)(const struct device *dev, const struct sensor_decoder_api **api)
Get the decoder associate with the given device.
Definition: sensor.h:557
static void sensor_10udegrees_to_rad(int32_t d, struct sensor_value *rad)
Helper function for converting 10 micro degrees to radians.
Definition: sensor.h:1174
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.
Definition: sensor.h:413
static int32_t sensor_ms2_to_g(const struct sensor_value *ms2)
Helper function to convert acceleration from m/s^2 to Gs.
Definition: sensor.h:1070
int(* sensor_submit_t)(const struct device *sensor, struct rtio_iodev_sqe *sqe)
Definition: sensor.h:652
void sensor_processing_with_callback(struct rtio *ctx, sensor_processing_callback_t cb)
Helper function for common processing of sensor data.
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.
Definition: sensor.h:396
static int sensor_value_from_micro(struct sensor_value *val, int64_t micro)
Helper function for converting integer micro units to struct sensor_value.
Definition: sensor.h:1383
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.
static int sensor_stream(struct rtio_iodev *iodev, struct rtio *ctx, void *userdata, struct rtio_sqe **handle)
Definition: sensor.h:968
static int32_t sensor_rad_to_10udegrees(const struct sensor_value *rad)
Helper function for converting radians to 10 micro degrees.
Definition: sensor.h:1161
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_value_from_double(struct sensor_value *val, double inp)
Helper function for converting double to struct sensor_value.
Definition: sensor.h:1209
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.
@ SENSOR_TRIG_DELTA
Trigger fires when the selected channel varies significantly.
Definition: sensor.h:229
@ SENSOR_TRIG_NEAR_FAR
Trigger fires when a near/far event is detected.
Definition: sensor.h:231
@ SENSOR_TRIG_FREEFALL
Trigger fires when a free fall is detected.
Definition: sensor.h:247
@ SENSOR_TRIG_PRIV_START
This and higher values are sensor specific.
Definition: sensor.h:269
@ SENSOR_TRIG_FIFO_FULL
Trigger fires when the FIFO becomes full.
Definition: sensor.h:259
@ SENSOR_TRIG_MOTION
Trigger fires when motion is detected.
Definition: sensor.h:250
@ SENSOR_TRIG_STATIONARY
Trigger fires when no motion has been detected for a while.
Definition: sensor.h:253
@ SENSOR_TRIG_COMMON_COUNT
Number of all common sensor triggers.
Definition: sensor.h:263
@ SENSOR_TRIG_THRESHOLD
Trigger fires when channel reading transitions configured thresholds.
Definition: sensor.h:238
@ SENSOR_TRIG_MAX
Maximum value describing a sensor trigger type.
Definition: sensor.h:274
@ SENSOR_TRIG_DOUBLE_TAP
Trigger fires when a double tap is detected.
Definition: sensor.h:244
@ SENSOR_TRIG_TIMER
Timer-based trigger, useful when the sensor does not have an interrupt line.
Definition: sensor.h:218
@ SENSOR_TRIG_FIFO_WATERMARK
Trigger fires when the FIFO watermark has been reached.
Definition: sensor.h:256
@ SENSOR_TRIG_TAP
Trigger fires when a single tap is detected.
Definition: sensor.h:241
@ SENSOR_TRIG_DATA_READY
Trigger fires whenever new data is ready.
Definition: sensor.h:220
@ SENSOR_ATTR_HYSTERESIS
Definition: sensor.h:308
@ SENSOR_ATTR_FEATURE_MASK
Enable/disable sensor features.
Definition: sensor.h:328
@ SENSOR_ATTR_CALIB_TARGET
Calibration target.
Definition: sensor.h:322
@ SENSOR_ATTR_OFFSET
The sensor value returned will be altered by the amount indicated by offset: final_value = sensor_val...
Definition: sensor.h:317
@ SENSOR_ATTR_BATCH_DURATION
Hardware batch duration in ticks.
Definition: sensor.h:339
@ SENSOR_ATTR_OVERSAMPLING
Oversampling factor.
Definition: sensor.h:310
@ SENSOR_ATTR_FF_DUR
Free-fall duration represented in milliseconds.
Definition: sensor.h:336
@ SENSOR_ATTR_UPPER_THRESH
Upper threshold for trigger.
Definition: sensor.h:299
@ SENSOR_ATTR_CONFIGURATION
Configure the operating modes of a sensor.
Definition: sensor.h:324
@ SENSOR_ATTR_CALIBRATION
Set a calibration value needed by a sensor.
Definition: sensor.h:326
@ SENSOR_ATTR_COMMON_COUNT
Number of all common sensor attributes.
Definition: sensor.h:344
@ SENSOR_ATTR_ALERT
Alert threshold or alert enable/disable.
Definition: sensor.h:330
@ SENSOR_ATTR_SLOPE_TH
Threshold for any-motion (slope) trigger.
Definition: sensor.h:301
@ SENSOR_ATTR_SAMPLING_FREQUENCY
Sensor sampling frequency, i.e.
Definition: sensor.h:295
@ SENSOR_ATTR_FULL_SCALE
Sensor range, in SI units.
Definition: sensor.h:312
@ SENSOR_ATTR_LOWER_THRESH
Lower threshold for trigger.
Definition: sensor.h:297
@ SENSOR_ATTR_SLOPE_DUR
Duration for which the slope values needs to be outside the threshold for the trigger to fire.
Definition: sensor.h:306
@ SENSOR_ATTR_MAX
Maximum value describing a sensor attribute type.
Definition: sensor.h:355
@ SENSOR_ATTR_PRIV_START
This and higher values are sensor specific.
Definition: sensor.h:350
@ SENSOR_STREAM_DATA_INCLUDE
Include whatever data is associated with the trigger.
Definition: sensor.h:565
@ SENSOR_STREAM_DATA_NOP
Do nothing with the associated trigger data, it may be consumed later.
Definition: sensor.h:567
@ SENSOR_STREAM_DATA_DROP
Flush/clear whatever data is associated with the trigger.
Definition: sensor.h:569
@ SENSOR_CHAN_GAUGE_STATE_OF_HEALTH
State of health measurement in %.
Definition: sensor.h:176
@ SENSOR_CHAN_PM_1_0
1.0 micro-meters Particulate Matter, in ug/m^3
Definition: sensor.h:111
@ SENSOR_CHAN_DIE_TEMP
Device die temperature in degrees Celsius.
Definition: sensor.h:85
@ SENSOR_CHAN_PRESS
Pressure in kilopascal.
Definition: sensor.h:89
@ SENSOR_CHAN_GAUGE_TIME_TO_FULL
Time to full in minutes.
Definition: sensor.h:180
@ SENSOR_CHAN_ACCEL_XYZ
Acceleration on the X, Y and Z axes.
Definition: sensor.h:67
@ SENSOR_CHAN_MAGN_X
Magnetic field on the X axis, in Gauss.
Definition: sensor.h:77
@ SENSOR_CHAN_CURRENT
Current, in amps.
Definition: sensor.h:133
@ SENSOR_CHAN_GYRO_XYZ
Angular velocity around the X, Y and Z axes.
Definition: sensor.h:75
@ SENSOR_CHAN_VSHUNT
Current Shunt Voltage in milli-volts.
Definition: sensor.h:130
@ SENSOR_CHAN_GREEN
Illuminance in green spectrum, in lux.
Definition: sensor.h:104
@ SENSOR_CHAN_MAGN_Z
Magnetic field on the Z axis, in Gauss.
Definition: sensor.h:81
@ SENSOR_CHAN_MAGN_Y
Magnetic field on the Y axis, in Gauss.
Definition: sensor.h:79
@ SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE
Desired voltage of cell in V (nominal voltage)
Definition: sensor.h:186
@ SENSOR_CHAN_POWER
Power in watts.
Definition: sensor.h:135
@ SENSOR_CHAN_PM_2_5
2.5 micro-meters Particulate Matter, in ug/m^3
Definition: sensor.h:113
@ SENSOR_CHAN_RESISTANCE
Resistance , in Ohm.
Definition: sensor.h:138
@ SENSOR_CHAN_GAUGE_AVG_CURRENT
Average current, in amps.
Definition: sensor.h:156
@ SENSOR_CHAN_GYRO_Y
Angular velocity around the Y axis, in radians/s.
Definition: sensor.h:71
@ SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT
Desired charging current in mA.
Definition: sensor.h:188
@ SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY
Full Charge Capacity in mAh.
Definition: sensor.h:166
@ SENSOR_CHAN_ROTATION
Angular rotation, in degrees.
Definition: sensor.h:141
@ SENSOR_CHAN_AMBIENT_TEMP
Ambient temperature in degrees Celsius.
Definition: sensor.h:87
@ SENSOR_CHAN_MAGN_XYZ
Magnetic field on the X, Y and Z axes.
Definition: sensor.h:83
@ SENSOR_CHAN_GAUGE_STDBY_CURRENT
Standby current, in amps.
Definition: sensor.h:158
@ SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT
Max load current, in amps.
Definition: sensor.h:160
@ SENSOR_CHAN_ACCEL_Y
Acceleration on the Y axis, in m/s^2.
Definition: sensor.h:63
@ SENSOR_CHAN_RPM
Revolutions per minute, in RPM.
Definition: sensor.h:151
@ SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY
Full Available Capacity in mAh.
Definition: sensor.h:172
@ SENSOR_CHAN_VOLTAGE
Voltage, in volts.
Definition: sensor.h:127
@ SENSOR_CHAN_BLUE
Illuminance in blue spectrum, in lux.
Definition: sensor.h:106
@ SENSOR_CHAN_LIGHT
Illuminance in visible spectrum, in lux.
Definition: sensor.h:98
@ SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE
Design voltage of cell in V (max voltage)
Definition: sensor.h:184
@ SENSOR_CHAN_ACCEL_Z
Acceleration on the Z axis, in m/s^2.
Definition: sensor.h:65
@ SENSOR_CHAN_CO2
CO2 level, in parts per million (ppm)
Definition: sensor.h:120
@ SENSOR_CHAN_GAUGE_STATE_OF_CHARGE
State of charge measurement in %.
Definition: sensor.h:164
@ SENSOR_CHAN_GAUGE_CYCLE_COUNT
Cycle count (total number of charge/discharge cycles)
Definition: sensor.h:182
@ SENSOR_CHAN_GAUGE_TEMP
Gauge temperature
Definition: sensor.h:162
@ SENSOR_CHAN_POS_DY
Position change on the Y axis, in points.
Definition: sensor.h:146
@ SENSOR_CHAN_GYRO_Z
Angular velocity around the Z axis, in radians/s.
Definition: sensor.h:73
@ SENSOR_CHAN_POS_DX
Position change on the X axis, in points.
Definition: sensor.h:144
@ SENSOR_CHAN_GAUGE_AVG_POWER
Average power in mW.
Definition: sensor.h:174
@ SENSOR_CHAN_GAUGE_TIME_TO_EMPTY
Time to empty in minutes.
Definition: sensor.h:178
@ SENSOR_CHAN_PM_10
10 micro-meters Particulate Matter, in ug/m^3
Definition: sensor.h:115
@ SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY
Remaining Charge Capacity in mAh.
Definition: sensor.h:168
@ SENSOR_CHAN_ALL
All channels.
Definition: sensor.h:191
@ SENSOR_CHAN_GAUGE_VOLTAGE
Voltage, in volts.
Definition: sensor.h:154
@ SENSOR_CHAN_PROX
Proximity.
Definition: sensor.h:94
@ SENSOR_CHAN_COMMON_COUNT
Number of all common sensor channels.
Definition: sensor.h:196
@ SENSOR_CHAN_PRIV_START
This and higher values are sensor specific.
Definition: sensor.h:202
@ SENSOR_CHAN_GYRO_X
Angular velocity around the X axis, in radians/s.
Definition: sensor.h:69
@ SENSOR_CHAN_GAS_RES
Gas sensor resistance in ohms.
Definition: sensor.h:124
@ SENSOR_CHAN_HUMIDITY
Humidity, in percent.
Definition: sensor.h:96
@ SENSOR_CHAN_DISTANCE
Distance.
Definition: sensor.h:117
@ SENSOR_CHAN_IR
Illuminance in infra-red spectrum, in lux.
Definition: sensor.h:100
@ SENSOR_CHAN_MAX
Maximum value describing a sensor channel type.
Definition: sensor.h:207
@ SENSOR_CHAN_POS_DZ
Position change on the Z axis, in points.
Definition: sensor.h:148
@ SENSOR_CHAN_RED
Illuminance in red spectrum, in lux.
Definition: sensor.h:102
@ SENSOR_CHAN_ALTITUDE
Altitude, in meters.
Definition: sensor.h:108
@ SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY
Nominal Available Capacity in mAh.
Definition: sensor.h:170
@ SENSOR_CHAN_ACCEL_X
Acceleration on the X axis, in m/s^2.
Definition: sensor.h:61
@ SENSOR_CHAN_VOC
VOC level, in parts per billion (ppb)
Definition: sensor.h:122
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition: util_macro.h:124
#define ENOSYS
Function not implemented.
Definition: errno.h:83
#define ENOMEM
Not enough core.
Definition: errno.h:51
#define ERANGE
Result too large.
Definition: errno.h:73
#define INT64_C(x)
Definition: llvm.h:86
Real-Time IO device API for moving bytes with low effort.
#define bool
Definition: stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__INT32_TYPE__ int32_t
Definition: stdint.h:74
#define INT32_MAX
Definition: stdint.h:18
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
#define INT32_MIN
Definition: stdint.h:24
#define INT16_MAX
Definition: stdint.h:17
__INT64_TYPE__ int64_t
Definition: stdint.h:75
__INT8_TYPE__ int8_t
Definition: stdint.h:72
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
Runtime device structure (in ROM) per driver instance.
Definition: device.h:387
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:393
API that an RTIO IO device should implement.
Definition: rtio.h:429
Compute the mempool block index for a given pointer.
Definition: rtio.h:419
struct rtio_sqe sqe
Definition: rtio.h:420
An IO device with a function table for submitting requests.
Definition: rtio.h:444
void * data
Definition: rtio.h:452
A submission queue event.
Definition: rtio.h:230
void * userdata
User provided data which is returned upon operation completion.
Definition: rtio.h:250
uint8_t * buf
Buffer to use.
Definition: rtio.h:257
uint32_t buf_len
Length of buffer.
Definition: rtio.h:256
const struct rtio_iodev * iodev
Device to operation on.
Definition: rtio.h:241
An RTIO context containing what can be viewed as a pair of queues.
Definition: rtio.h:323
Definition: sensor.h:871
uint64_t timestamp_ns
Definition: sensor.h:873
int8_t shift
Definition: sensor.h:882
uint32_t num_channels
Definition: sensor.h:879
Used for iterating over the data frames via the sensor_decoder_api.
Definition: sensor.h:514
const struct sensor_decoder_api * decoder
Definition: sensor.h:515
const uint8_t * buffer
Definition: sensor.h:516
size_t channel_idx
Definition: sensor.h:518
enum sensor_channel channel
Definition: sensor.h:517
uint32_t fit
Definition: sensor.h:519
Decodes a single raw data buffer.
Definition: sensor.h:423
int(* get_size_info)(enum sensor_channel channel, size_t *base_size, size_t *frame_size)
Get the size required to decode a given channel.
Definition: sensor.h:449
int(* decode)(const uint8_t *buffer, enum sensor_channel channel, size_t channel_idx, uint32_t *fit, uint16_t max_count, void *data_out)
Decode up to max_count samples from the buffer.
Definition: sensor.h:477
bool(* has_trigger)(const uint8_t *buffer, enum sensor_trigger_type trigger)
Check if the given trigger type is present.
Definition: sensor.h:487
int(* get_frame_count)(const uint8_t *buffer, enum sensor_channel channel, size_t channel_idx, uint16_t *frame_count)
Get the number of frames in the current buffer.
Definition: sensor.h:434
Definition: sensor.h:660
sensor_get_decoder_t get_decoder
Definition: sensor.h:666
sensor_attr_set_t attr_set
Definition: sensor.h:661
sensor_attr_get_t attr_get
Definition: sensor.h:662
sensor_trigger_set_t trigger_set
Definition: sensor.h:663
sensor_sample_fetch_t sample_fetch
Definition: sensor.h:664
sensor_channel_get_t channel_get
Definition: sensor.h:665
sensor_submit_t submit
Definition: sensor.h:667
Definition: sensor.h:585
size_t count
Definition: sensor.h:592
struct sensor_stream_trigger *const triggers
Definition: sensor.h:590
const bool is_streaming
Definition: sensor.h:587
const struct device * sensor
Definition: sensor.h:586
const size_t max
Definition: sensor.h:593
enum sensor_channel *const channels
Definition: sensor.h:589
Definition: sensor.h:572
enum sensor_stream_data_opt opt
Definition: sensor.h:574
enum sensor_trigger_type trigger
Definition: sensor.h:573
Sensor trigger spec.
Definition: sensor.h:280
enum sensor_trigger_type type
Trigger type.
Definition: sensor.h:282
enum sensor_channel chan
Channel the trigger is set on.
Definition: sensor.h:284
Representation of a sensor readout value.
Definition: sensor.h:49
int32_t val2
Fractional part of the value (in one-millionth parts).
Definition: sensor.h:53
int32_t val1
Integer part of the value.
Definition: sensor.h:51