Zephyr Project API 3.7.0
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
24#include <errno.h>
25#include <stdlib.h>
26
27#include <zephyr/device.h>
29#include <zephyr/dsp/types.h>
30#include <zephyr/rtio/rtio.h>
32#include <zephyr/types.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
57
111
120
129
132
135
140
143
146
155
158
195
198
203
209
214};
215
282
292
363
371typedef void (*sensor_trigger_handler_t)(const struct device *dev,
372 const struct sensor_trigger *trigger);
373
380typedef int (*sensor_attr_set_t)(const struct device *dev,
381 enum sensor_channel chan,
382 enum sensor_attribute attr,
383 const struct sensor_value *val);
384
391typedef int (*sensor_attr_get_t)(const struct device *dev,
392 enum sensor_channel chan,
393 enum sensor_attribute attr,
394 struct sensor_value *val);
395
402typedef int (*sensor_trigger_set_t)(const struct device *dev,
403 const struct sensor_trigger *trig,
411typedef int (*sensor_sample_fetch_t)(const struct device *dev,
412 enum sensor_channel chan);
419typedef int (*sensor_channel_get_t)(const struct device *dev,
420 enum sensor_channel chan,
421 struct sensor_value *val);
422
435
437/* Ensure sensor_chan_spec is sensibly sized to pass by value */
438BUILD_ASSERT(sizeof(struct sensor_chan_spec) <= sizeof(uintptr_t),
439 "sensor_chan_spec size should be equal or less than the size of a machine word");
450static inline bool sensor_chan_spec_eq(struct sensor_chan_spec chan_spec0,
451 struct sensor_chan_spec chan_spec1)
452{
453 return chan_spec0.chan_type == chan_spec1.chan_type &&
454 chan_spec0.chan_idx == chan_spec1.chan_idx;
455}
456
473 int (*get_frame_count)(const uint8_t *buffer, struct sensor_chan_spec channel,
474 uint16_t *frame_count);
475
488 int (*get_size_info)(struct sensor_chan_spec channel, size_t *base_size,
489 size_t *frame_size);
490
516 int (*decode)(const uint8_t *buffer, struct sensor_chan_spec channel, uint32_t *fit,
517 uint16_t max_count, void *data_out);
518
527};
528
559
563#define SENSOR_DECODE_CONTEXT_INIT(decoder_, buffer_, channel_type_, channel_index_) \
564 { \
565 .decoder = (decoder_), \
566 .buffer = (buffer_), \
567 .channel = {.chan_type = (channel_type_), .chan_idx = (channel_index_)}, \
568 .fit = 0, \
569 }
570
579static inline int sensor_decode(struct sensor_decode_context *ctx, void *out, uint16_t max_count)
580{
581 return ctx->decoder->decode(ctx->buffer, ctx->channel, &ctx->fit, max_count, out);
582}
583
585 size_t *frame_size);
586
593typedef int (*sensor_get_decoder_t)(const struct device *dev,
594 const struct sensor_decoder_api **api);
595
607
612
613#define SENSOR_STREAM_TRIGGER_PREP(_trigger, _opt) \
614 { \
615 .trigger = (_trigger), .opt = (_opt), \
616 }
617
618/*
619 * Internal data structure used to store information about the IODevice for async reading and
620 * streaming sensor data.
621 */
623 const struct device *sensor;
624 const bool is_streaming;
625 union {
628 };
629 size_t count;
630 const size_t max;
631};
632
648#define SENSOR_DT_READ_IODEV(name, dt_node, ...) \
649 static struct sensor_chan_spec _CONCAT(__channel_array_, name)[] = {__VA_ARGS__}; \
650 static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
651 .sensor = DEVICE_DT_GET(dt_node), \
652 .is_streaming = false, \
653 .channels = _CONCAT(__channel_array_, name), \
654 .count = ARRAY_SIZE(_CONCAT(__channel_array_, name)), \
655 .max = ARRAY_SIZE(_CONCAT(__channel_array_, name)), \
656 }; \
657 RTIO_IODEV_DEFINE(name, &__sensor_iodev_api, _CONCAT(&__sensor_read_config_, name))
658
678#define SENSOR_DT_STREAM_IODEV(name, dt_node, ...) \
679 static struct sensor_stream_trigger _CONCAT(__trigger_array_, name)[] = {__VA_ARGS__}; \
680 static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
681 .sensor = DEVICE_DT_GET(dt_node), \
682 .is_streaming = true, \
683 .triggers = _CONCAT(__trigger_array_, name), \
684 .count = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
685 .max = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
686 }; \
687 RTIO_IODEV_DEFINE(name, &__sensor_iodev_api, &_CONCAT(__sensor_read_config_, name))
688
689/* Used to submit an RTIO sqe to the sensor's iodev */
690typedef void (*sensor_submit_t)(const struct device *sensor, struct rtio_iodev_sqe *sqe);
691
692/* The default decoder API */
693extern const struct sensor_decoder_api __sensor_default_decoder;
694
695/* The default sensor iodev API */
696extern const struct rtio_iodev_api __sensor_iodev_api;
697
707
720__syscall int sensor_attr_set(const struct device *dev,
721 enum sensor_channel chan,
722 enum sensor_attribute attr,
723 const struct sensor_value *val);
724
725static inline int z_impl_sensor_attr_set(const struct device *dev,
726 enum sensor_channel chan,
727 enum sensor_attribute attr,
728 const struct sensor_value *val)
729{
730 const struct sensor_driver_api *api =
731 (const struct sensor_driver_api *)dev->api;
732
733 if (api->attr_set == NULL) {
734 return -ENOSYS;
735 }
736
737 return api->attr_set(dev, chan, attr, val);
738}
739
752__syscall int sensor_attr_get(const struct device *dev,
753 enum sensor_channel chan,
754 enum sensor_attribute attr,
755 struct sensor_value *val);
756
757static inline int z_impl_sensor_attr_get(const struct device *dev,
758 enum sensor_channel chan,
759 enum sensor_attribute attr,
760 struct sensor_value *val)
761{
762 const struct sensor_driver_api *api =
763 (const struct sensor_driver_api *)dev->api;
764
765 if (api->attr_get == NULL) {
766 return -ENOSYS;
767 }
768
769 return api->attr_get(dev, chan, attr, val);
770}
771
794static inline int sensor_trigger_set(const struct device *dev,
795 const struct sensor_trigger *trig,
797{
798 const struct sensor_driver_api *api =
799 (const struct sensor_driver_api *)dev->api;
800
801 if (api->trigger_set == NULL) {
802 return -ENOSYS;
803 }
804
805 return api->trigger_set(dev, trig, handler);
806}
807
826__syscall int sensor_sample_fetch(const struct device *dev);
827
828static inline int z_impl_sensor_sample_fetch(const struct device *dev)
829{
830 const struct sensor_driver_api *api =
831 (const struct sensor_driver_api *)dev->api;
832
833 return api->sample_fetch(dev, SENSOR_CHAN_ALL);
834}
835
857__syscall int sensor_sample_fetch_chan(const struct device *dev,
858 enum sensor_channel type);
859
860static inline int z_impl_sensor_sample_fetch_chan(const struct device *dev,
861 enum sensor_channel type)
862{
863 const struct sensor_driver_api *api =
864 (const struct sensor_driver_api *)dev->api;
865
866 return api->sample_fetch(dev, type);
867}
868
890__syscall int sensor_channel_get(const struct device *dev,
891 enum sensor_channel chan,
892 struct sensor_value *val);
893
894static inline int z_impl_sensor_channel_get(const struct device *dev,
895 enum sensor_channel chan,
896 struct sensor_value *val)
897{
898 const struct sensor_driver_api *api =
899 (const struct sensor_driver_api *)dev->api;
900
901 return api->channel_get(dev, chan, val);
902}
903
904#if defined(CONFIG_SENSOR_ASYNC_API) || defined(__DOXYGEN__)
905
906/*
907 * Generic data structure used for encoding the sample timestamp and number of channels sampled.
908 */
909struct __attribute__((__packed__)) sensor_data_generic_header {
910 /* The timestamp at which the data was collected from the sensor */
912
913 /*
914 * The number of channels present in the frame. This will be the true number of elements in
915 * channel_info and in the q31 values that follow the header.
916 */
918
919 /* Shift value for all samples in the frame */
921
922 /* This padding is needed to make sure that the 'channels' field is aligned */
923 int8_t _padding[sizeof(struct sensor_chan_spec) - 1];
924
925 /* Channels present in the frame */
926 struct sensor_chan_spec channels[0];
927};
928
937#define SENSOR_CHANNEL_3_AXIS(chan) \
938 ((chan) == SENSOR_CHAN_ACCEL_XYZ || (chan) == SENSOR_CHAN_GYRO_XYZ || \
939 (chan) == SENSOR_CHAN_MAGN_XYZ || (chan) == SENSOR_CHAN_POS_DXYZ)
940
949__syscall int sensor_get_decoder(const struct device *dev,
950 const struct sensor_decoder_api **decoder);
951
952static inline int z_impl_sensor_get_decoder(const struct device *dev,
953 const struct sensor_decoder_api **decoder)
954{
955 const struct sensor_driver_api *api = (const struct sensor_driver_api *)dev->api;
956
957 __ASSERT_NO_MSG(api != NULL);
958
959 if (api->get_decoder == NULL) {
960 *decoder = &__sensor_default_decoder;
961 return 0;
962 }
963
964 return api->get_decoder(dev, decoder);
965}
966
985__syscall int sensor_reconfigure_read_iodev(struct rtio_iodev *iodev, const struct device *sensor,
986 const struct sensor_chan_spec *channels,
987 size_t num_channels);
988
989static inline int z_impl_sensor_reconfigure_read_iodev(struct rtio_iodev *iodev,
990 const struct device *sensor,
991 const struct sensor_chan_spec *channels,
992 size_t num_channels)
993{
994 struct sensor_read_config *cfg = (struct sensor_read_config *)iodev->data;
995
996 if (cfg->max < num_channels || cfg->is_streaming) {
997 return -ENOMEM;
998 }
999
1000 cfg->sensor = sensor;
1001 memcpy(cfg->channels, channels, num_channels * sizeof(struct sensor_chan_spec));
1002 cfg->count = num_channels;
1003 return 0;
1004}
1005
1006static inline int sensor_stream(struct rtio_iodev *iodev, struct rtio *ctx, void *userdata,
1007 struct rtio_sqe **handle)
1008{
1009 if (IS_ENABLED(CONFIG_USERSPACE)) {
1010 struct rtio_sqe sqe;
1011
1013 rtio_sqe_copy_in_get_handles(ctx, &sqe, handle, 1);
1014 } else {
1015 struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);
1016
1017 if (sqe == NULL) {
1018 return -ENOMEM;
1019 }
1020 if (handle != NULL) {
1021 *handle = sqe;
1022 }
1024 }
1025 rtio_submit(ctx, 0);
1026 return 0;
1027}
1028
1043static inline int sensor_read(struct rtio_iodev *iodev, struct rtio *ctx, uint8_t *buf,
1044 size_t buf_len)
1045{
1046 if (IS_ENABLED(CONFIG_USERSPACE)) {
1047 struct rtio_sqe sqe;
1048
1050 rtio_sqe_copy_in(ctx, &sqe, 1);
1051 } else {
1052 struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);
1053
1054 if (sqe == NULL) {
1055 return -ENOMEM;
1056 }
1058 }
1059 rtio_submit(ctx, 0);
1060
1061 struct rtio_cqe *cqe = rtio_cqe_consume_block(ctx);
1062 int res = cqe->result;
1063
1064 __ASSERT(cqe->userdata != buf,
1065 "consumed non-matching completion for sensor read into buffer %p\n", buf);
1066
1067 rtio_cqe_release(ctx, cqe);
1068
1069 return res;
1070}
1071
1085static inline int sensor_read_async_mempool(struct rtio_iodev *iodev, struct rtio *ctx,
1086 void *userdata)
1087{
1088 if (IS_ENABLED(CONFIG_USERSPACE)) {
1089 struct rtio_sqe sqe;
1090
1092 rtio_sqe_copy_in(ctx, &sqe, 1);
1093 } else {
1094 struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);
1095
1096 if (sqe == NULL) {
1097 return -ENOMEM;
1098 }
1100 }
1101 rtio_submit(ctx, 0);
1102 return 0;
1103}
1104
1117 void *userdata);
1118
1131
1132#endif /* defined(CONFIG_SENSOR_ASYNC_API) || defined(__DOXYGEN__) */
1133
1137#define SENSOR_G 9806650LL
1138
1142#define SENSOR_PI 3141592LL
1143
1152static inline int32_t sensor_ms2_to_g(const struct sensor_value *ms2)
1153{
1154 int64_t micro_ms2 = ms2->val1 * 1000000LL + ms2->val2;
1155
1156 if (micro_ms2 > 0) {
1157 return (micro_ms2 + SENSOR_G / 2) / SENSOR_G;
1158 } else {
1159 return (micro_ms2 - SENSOR_G / 2) / SENSOR_G;
1160 }
1161}
1162
1169static inline void sensor_g_to_ms2(int32_t g, struct sensor_value *ms2)
1170{
1171 ms2->val1 = ((int64_t)g * SENSOR_G) / 1000000LL;
1172 ms2->val2 = ((int64_t)g * SENSOR_G) % 1000000LL;
1173}
1174
1183static inline int32_t sensor_ms2_to_ug(const struct sensor_value *ms2)
1184{
1185 int64_t micro_ms2 = (ms2->val1 * INT64_C(1000000)) + ms2->val2;
1186
1187 return (micro_ms2 * 1000000LL) / SENSOR_G;
1188}
1189
1196static inline void sensor_ug_to_ms2(int32_t ug, struct sensor_value *ms2)
1197{
1198 ms2->val1 = ((int64_t)ug * SENSOR_G / 1000000LL) / 1000000LL;
1199 ms2->val2 = ((int64_t)ug * SENSOR_G / 1000000LL) % 1000000LL;
1200}
1201
1209static inline int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
1210{
1211 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
1212
1213 if (micro_rad_s > 0) {
1214 return (micro_rad_s * 180LL + SENSOR_PI / 2) / SENSOR_PI;
1215 } else {
1216 return (micro_rad_s * 180LL - SENSOR_PI / 2) / SENSOR_PI;
1217 }
1218}
1219
1226static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
1227{
1228 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL) / 1000000LL;
1229 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL;
1230}
1231
1243static inline int32_t sensor_rad_to_10udegrees(const struct sensor_value *rad)
1244{
1245 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
1246
1247 return (micro_rad_s * 180LL * 100000LL) / SENSOR_PI;
1248}
1249
1256static inline void sensor_10udegrees_to_rad(int32_t d, struct sensor_value *rad)
1257{
1258 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL / 100000LL) / 1000000LL;
1259 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL / 100000LL) % 1000000LL;
1260}
1261
1268static inline double sensor_value_to_double(const struct sensor_value *val)
1269{
1270 return (double)val->val1 + (double)val->val2 / 1000000;
1271}
1272
1279static inline float sensor_value_to_float(const struct sensor_value *val)
1280{
1281 return (float)val->val1 + (float)val->val2 / 1000000;
1282}
1283
1291static inline int sensor_value_from_double(struct sensor_value *val, double inp)
1292{
1293 if (inp < INT32_MIN || inp > INT32_MAX) {
1294 return -ERANGE;
1295 }
1296
1297 double val2 = (inp - (int32_t)inp) * 1000000.0;
1298
1299 if (val2 < INT32_MIN || val2 > INT32_MAX) {
1300 return -ERANGE;
1301 }
1302
1303 val->val1 = (int32_t)inp;
1304 val->val2 = (int32_t)val2;
1305
1306 return 0;
1307}
1308
1316static inline int sensor_value_from_float(struct sensor_value *val, float inp)
1317{
1318 float val2 = (inp - (int32_t)inp) * 1000000.0f;
1319
1320 if (val2 < INT32_MIN || val2 > (float)(INT32_MAX - 1)) {
1321 return -ERANGE;
1322 }
1323
1324 val->val1 = (int32_t)inp;
1325 val->val2 = (int32_t)val2;
1326
1327 return 0;
1328}
1329
1330#ifdef CONFIG_SENSOR_INFO
1331
1332struct sensor_info {
1333 const struct device *dev;
1334 const char *vendor;
1335 const char *model;
1336 const char *friendly_name;
1337};
1338
1339#define SENSOR_INFO_INITIALIZER(_dev, _vendor, _model, _friendly_name) \
1340 { \
1341 .dev = _dev, \
1342 .vendor = _vendor, \
1343 .model = _model, \
1344 .friendly_name = _friendly_name, \
1345 }
1346
1347#define SENSOR_INFO_DEFINE(name, ...) \
1348 static const STRUCT_SECTION_ITERABLE(sensor_info, name) = \
1349 SENSOR_INFO_INITIALIZER(__VA_ARGS__)
1350
1351#define SENSOR_INFO_DT_NAME(node_id) \
1352 _CONCAT(__sensor_info, DEVICE_DT_NAME_GET(node_id))
1353
1354#define SENSOR_INFO_DT_DEFINE(node_id) \
1355 SENSOR_INFO_DEFINE(SENSOR_INFO_DT_NAME(node_id), \
1356 DEVICE_DT_GET(node_id), \
1357 DT_NODE_VENDOR_OR(node_id, NULL), \
1358 DT_NODE_MODEL_OR(node_id, NULL), \
1359 DT_PROP_OR(node_id, friendly_name, NULL)) \
1360
1361#else
1362
1363#define SENSOR_INFO_DEFINE(name, ...)
1364#define SENSOR_INFO_DT_DEFINE(node_id)
1365
1366#endif /* CONFIG_SENSOR_INFO */
1367
1395#define SENSOR_DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
1396 data_ptr, cfg_ptr, level, prio, \
1397 api_ptr, ...) \
1398 DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
1399 data_ptr, cfg_ptr, level, prio, \
1400 api_ptr, __VA_ARGS__); \
1401 \
1402 SENSOR_INFO_DT_DEFINE(node_id);
1403
1413#define SENSOR_DEVICE_DT_INST_DEFINE(inst, ...) \
1414 SENSOR_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
1415
1422static inline int64_t sensor_value_to_milli(const struct sensor_value *val)
1423{
1424 return ((int64_t)val->val1 * 1000) + val->val2 / 1000;
1425}
1426
1433static inline int64_t sensor_value_to_micro(const struct sensor_value *val)
1434{
1435 return ((int64_t)val->val1 * 1000000) + val->val2;
1436}
1437
1445static inline int sensor_value_from_milli(struct sensor_value *val, int64_t milli)
1446{
1447 if (milli < ((int64_t)INT32_MIN - 1) * 1000LL ||
1448 milli > ((int64_t)INT32_MAX + 1) * 1000LL) {
1449 return -ERANGE;
1450 }
1451
1452 val->val1 = (int32_t)(milli / 1000);
1453 val->val2 = (int32_t)(milli % 1000) * 1000;
1454
1455 return 0;
1456}
1457
1465static inline int sensor_value_from_micro(struct sensor_value *val, int64_t micro)
1466{
1467 if (micro < ((int64_t)INT32_MIN - 1) * 1000000LL ||
1468 micro > ((int64_t)INT32_MAX + 1) * 1000000LL) {
1469 return -ERANGE;
1470 }
1471
1472 val->val1 = (int32_t)(micro / 1000000LL);
1473 val->val2 = (int32_t)(micro % 1000000LL);
1474
1475 return 0;
1476}
1477
1487#define SENSOR_DECODER_NAME() UTIL_CAT(DT_DRV_COMPAT, __decoder_api)
1488
1496#define SENSOR_DECODER_DT_GET(node_id) \
1497 &UTIL_CAT(DT_STRING_TOKEN_BY_IDX(node_id, compatible, 0), __decoder_api)
1498
1514#define SENSOR_DECODER_API_DT_DEFINE() \
1515 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT), (), (static)) \
1516 const STRUCT_SECTION_ITERABLE(sensor_decoder_api, SENSOR_DECODER_NAME())
1517
1518#define Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL_IDX(node_id, prop, idx) \
1519 extern const struct sensor_decoder_api UTIL_CAT( \
1520 DT_STRING_TOKEN_BY_IDX(node_id, prop, idx), __decoder_api);
1521
1522#define Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL(node_id) \
1523 COND_CODE_1(DT_NODE_HAS_PROP(node_id, compatible), \
1524 (DT_FOREACH_PROP_ELEM(node_id, compatible, \
1525 Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL_IDX)), \
1526 ())
1527
1528DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL)
1529
1530#ifdef __cplusplus
1531}
1532#endif
1533
1534#include <zephyr/syscalls/sensor.h>
1535
1536#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:2785
#define RTIO_PRIO_NORM
Normal priority.
Definition rtio.h:70
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:517
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:1347
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 void rtio_sqe_prep_read(struct rtio_sqe *sqe, const struct rtio_iodev *iodev, int8_t prio, uint8_t *buf, uint32_t len, void *userdata)
Prepare a read op submission.
Definition rtio.h:496
static struct rtio_sqe * rtio_sqe_acquire(struct rtio *r)
Acquire a single submission queue event if available.
Definition rtio.h:901
static void rtio_sqe_prep_read_multishot(struct rtio_sqe *sqe, const struct rtio_iodev *iodev, int8_t prio, void *userdata)
Definition rtio.h:525
static void rtio_cqe_release(struct rtio *r, struct rtio_cqe *cqe)
Release consumed completion queue event.
Definition rtio.h:1020
static struct rtio_cqe * rtio_cqe_consume_block(struct rtio *r)
Wait for and consume a single completion queue event.
Definition rtio.h:996
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:1137
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:579
static int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
Helper function for converting radians to degrees.
Definition sensor.h:1209
sensor_trigger_type
Sensor trigger types.
Definition sensor.h:219
sensor_attribute
Sensor attribute types.
Definition sensor.h:296
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:380
int sensor_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder)
Get the sensor's decoder API.
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.
Definition sensor.h:1043
int(* sensor_sample_fetch_t)(const struct device *dev, enum sensor_channel chan)
Callback API for fetching data from a sensor.
Definition sensor.h:411
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:1196
static double sensor_value_to_double(const struct sensor_value *val)
Helper function for converting struct sensor_value to double.
Definition sensor.h:1268
static float sensor_value_to_float(const struct sensor_value *val)
Helper function for converting struct sensor_value to float.
Definition sensor.h:1279
int sensor_natively_supported_channel_size_info(struct sensor_chan_spec channel, size_t *base_size, size_t *frame_size)
static void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
Helper function for converting degrees to radians.
Definition sensor.h:1226
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:1183
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:391
static int sensor_value_from_float(struct sensor_value *val, float inp)
Helper function for converting float to struct sensor_value.
Definition sensor.h:1316
void(* sensor_trigger_handler_t)(const struct device *dev, const struct sensor_trigger *trigger)
Callback API upon firing of a trigger.
Definition sensor.h:371
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:1169
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:1422
#define SENSOR_PI
The value of constant PI in micros.
Definition sensor.h:1142
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:794
sensor_stream_data_opt
Options for what to do with the associated data when a trigger is consumed.
Definition sensor.h:599
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:1445
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:1116
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:1433
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:61
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:593
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:1256
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:419
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:1152
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_read_async_mempool(struct rtio_iodev *iodev, struct rtio *ctx, void *userdata)
One shot non-blocking read with pool allocated buffer.
Definition sensor.h:1085
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:402
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:1465
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:1006
void(* sensor_submit_t)(const struct device *sensor, struct rtio_iodev_sqe *sqe)
Definition sensor.h:690
static int32_t sensor_rad_to_10udegrees(const struct sensor_value *rad)
Helper function for converting radians to 10 micro degrees.
Definition sensor.h:1243
static bool sensor_chan_spec_eq(struct sensor_chan_spec chan_spec0, struct sensor_chan_spec chan_spec1)
Check if channel specs are equivalent.
Definition sensor.h:450
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:1291
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:235
@ SENSOR_TRIG_NEAR_FAR
Trigger fires when a near/far event is detected.
Definition sensor.h:237
@ SENSOR_TRIG_FREEFALL
Trigger fires when a free fall is detected.
Definition sensor.h:253
@ SENSOR_TRIG_PRIV_START
This and higher values are sensor specific.
Definition sensor.h:275
@ SENSOR_TRIG_FIFO_FULL
Trigger fires when the FIFO becomes full.
Definition sensor.h:265
@ SENSOR_TRIG_MOTION
Trigger fires when motion is detected.
Definition sensor.h:256
@ SENSOR_TRIG_STATIONARY
Trigger fires when no motion has been detected for a while.
Definition sensor.h:259
@ SENSOR_TRIG_COMMON_COUNT
Number of all common sensor triggers.
Definition sensor.h:269
@ SENSOR_TRIG_THRESHOLD
Trigger fires when channel reading transitions configured thresholds.
Definition sensor.h:244
@ SENSOR_TRIG_MAX
Maximum value describing a sensor trigger type.
Definition sensor.h:280
@ SENSOR_TRIG_DOUBLE_TAP
Trigger fires when a double tap is detected.
Definition sensor.h:250
@ SENSOR_TRIG_TIMER
Timer-based trigger, useful when the sensor does not have an interrupt line.
Definition sensor.h:224
@ SENSOR_TRIG_FIFO_WATERMARK
Trigger fires when the FIFO watermark has been reached.
Definition sensor.h:262
@ SENSOR_TRIG_TAP
Trigger fires when a single tap is detected.
Definition sensor.h:247
@ SENSOR_TRIG_DATA_READY
Trigger fires whenever new data is ready.
Definition sensor.h:226
@ SENSOR_ATTR_HYSTERESIS
Definition sensor.h:314
@ SENSOR_ATTR_FEATURE_MASK
Enable/disable sensor features.
Definition sensor.h:334
@ SENSOR_ATTR_CALIB_TARGET
Calibration target.
Definition sensor.h:328
@ SENSOR_ATTR_OFFSET
The sensor value returned will be altered by the amount indicated by offset: final_value = sensor_val...
Definition sensor.h:323
@ SENSOR_ATTR_BATCH_DURATION
Hardware batch duration in ticks.
Definition sensor.h:345
@ SENSOR_ATTR_OVERSAMPLING
Oversampling factor.
Definition sensor.h:316
@ SENSOR_ATTR_FF_DUR
Free-fall duration represented in milliseconds.
Definition sensor.h:342
@ SENSOR_ATTR_UPPER_THRESH
Upper threshold for trigger.
Definition sensor.h:305
@ SENSOR_ATTR_CONFIGURATION
Configure the operating modes of a sensor.
Definition sensor.h:330
@ SENSOR_ATTR_CALIBRATION
Set a calibration value needed by a sensor.
Definition sensor.h:332
@ SENSOR_ATTR_COMMON_COUNT
Number of all common sensor attributes.
Definition sensor.h:350
@ SENSOR_ATTR_ALERT
Alert threshold or alert enable/disable.
Definition sensor.h:336
@ SENSOR_ATTR_SLOPE_TH
Threshold for any-motion (slope) trigger.
Definition sensor.h:307
@ SENSOR_ATTR_SAMPLING_FREQUENCY
Sensor sampling frequency, i.e.
Definition sensor.h:301
@ SENSOR_ATTR_FULL_SCALE
Sensor range, in SI units.
Definition sensor.h:318
@ SENSOR_ATTR_LOWER_THRESH
Lower threshold for trigger.
Definition sensor.h:303
@ SENSOR_ATTR_SLOPE_DUR
Duration for which the slope values needs to be outside the threshold for the trigger to fire.
Definition sensor.h:312
@ SENSOR_ATTR_MAX
Maximum value describing a sensor attribute type.
Definition sensor.h:361
@ SENSOR_ATTR_PRIV_START
This and higher values are sensor specific.
Definition sensor.h:356
@ SENSOR_STREAM_DATA_INCLUDE
Include whatever data is associated with the trigger.
Definition sensor.h:601
@ SENSOR_STREAM_DATA_NOP
Do nothing with the associated trigger data, it may be consumed later.
Definition sensor.h:603
@ SENSOR_STREAM_DATA_DROP
Flush/clear whatever data is associated with the trigger.
Definition sensor.h:605
@ SENSOR_CHAN_GAUGE_STATE_OF_HEALTH
State of health measurement in %.
Definition sensor.h:182
@ SENSOR_CHAN_PM_1_0
1.0 micro-meters Particulate Matter, in ug/m^3
Definition sensor.h:113
@ SENSOR_CHAN_DIE_TEMP
Device die temperature in degrees Celsius.
Definition sensor.h:87
@ SENSOR_CHAN_PRESS
Pressure in kilopascal.
Definition sensor.h:91
@ SENSOR_CHAN_GAUGE_TIME_TO_FULL
Time to full in minutes.
Definition sensor.h:186
@ SENSOR_CHAN_ACCEL_XYZ
Acceleration on the X, Y and Z axes.
Definition sensor.h:69
@ SENSOR_CHAN_MAGN_X
Magnetic field on the X axis, in Gauss.
Definition sensor.h:79
@ SENSOR_CHAN_O2
O2 level, in parts per million (ppm)
Definition sensor.h:124
@ SENSOR_CHAN_CURRENT
Current, in amps.
Definition sensor.h:137
@ SENSOR_CHAN_GYRO_XYZ
Angular velocity around the X, Y and Z axes.
Definition sensor.h:77
@ SENSOR_CHAN_VSHUNT
Current Shunt Voltage in milli-volts.
Definition sensor.h:134
@ SENSOR_CHAN_GREEN
Illuminance in green spectrum, in lux.
Definition sensor.h:106
@ SENSOR_CHAN_MAGN_Z
Magnetic field on the Z axis, in Gauss.
Definition sensor.h:83
@ SENSOR_CHAN_MAGN_Y
Magnetic field on the Y axis, in Gauss.
Definition sensor.h:81
@ SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE
Desired voltage of cell in V (nominal voltage)
Definition sensor.h:192
@ SENSOR_CHAN_POWER
Power in watts.
Definition sensor.h:139
@ SENSOR_CHAN_PM_2_5
2.5 micro-meters Particulate Matter, in ug/m^3
Definition sensor.h:115
@ SENSOR_CHAN_RESISTANCE
Resistance , in Ohm.
Definition sensor.h:142
@ SENSOR_CHAN_GAUGE_AVG_CURRENT
Average current, in amps.
Definition sensor.h:162
@ SENSOR_CHAN_GYRO_Y
Angular velocity around the Y axis, in radians/s.
Definition sensor.h:73
@ SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT
Desired charging current in mA.
Definition sensor.h:194
@ SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY
Full Charge Capacity in mAh.
Definition sensor.h:172
@ SENSOR_CHAN_ROTATION
Angular rotation, in degrees.
Definition sensor.h:145
@ SENSOR_CHAN_AMBIENT_TEMP
Ambient temperature in degrees Celsius.
Definition sensor.h:89
@ SENSOR_CHAN_MAGN_XYZ
Magnetic field on the X, Y and Z axes.
Definition sensor.h:85
@ SENSOR_CHAN_GAUGE_STDBY_CURRENT
Standby current, in amps.
Definition sensor.h:164
@ SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT
Max load current, in amps.
Definition sensor.h:166
@ SENSOR_CHAN_ACCEL_Y
Acceleration on the Y axis, in m/s^2.
Definition sensor.h:65
@ SENSOR_CHAN_RPM
Revolutions per minute, in RPM.
Definition sensor.h:157
@ SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY
Full Available Capacity in mAh.
Definition sensor.h:178
@ SENSOR_CHAN_VOLTAGE
Voltage, in volts.
Definition sensor.h:131
@ SENSOR_CHAN_BLUE
Illuminance in blue spectrum, in lux.
Definition sensor.h:108
@ SENSOR_CHAN_LIGHT
Illuminance in visible spectrum, in lux.
Definition sensor.h:100
@ SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE
Design voltage of cell in V (max voltage)
Definition sensor.h:190
@ SENSOR_CHAN_ACCEL_Z
Acceleration on the Z axis, in m/s^2.
Definition sensor.h:67
@ SENSOR_CHAN_CO2
CO2 level, in parts per million (ppm)
Definition sensor.h:122
@ SENSOR_CHAN_GAUGE_STATE_OF_CHARGE
State of charge measurement in %.
Definition sensor.h:170
@ SENSOR_CHAN_POS_DXYZ
Position change on the X, Y and Z axis, in points.
Definition sensor.h:154
@ SENSOR_CHAN_GAUGE_CYCLE_COUNT
Cycle count (total number of charge/discharge cycles)
Definition sensor.h:188
@ SENSOR_CHAN_GAUGE_TEMP
Gauge temperature
Definition sensor.h:168
@ SENSOR_CHAN_POS_DY
Position change on the Y axis, in points.
Definition sensor.h:150
@ SENSOR_CHAN_GYRO_Z
Angular velocity around the Z axis, in radians/s.
Definition sensor.h:75
@ SENSOR_CHAN_POS_DX
Position change on the X axis, in points.
Definition sensor.h:148
@ SENSOR_CHAN_GAUGE_AVG_POWER
Average power in mW.
Definition sensor.h:180
@ SENSOR_CHAN_GAUGE_TIME_TO_EMPTY
Time to empty in minutes.
Definition sensor.h:184
@ SENSOR_CHAN_PM_10
10 micro-meters Particulate Matter, in ug/m^3
Definition sensor.h:117
@ SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY
Remaining Charge Capacity in mAh.
Definition sensor.h:174
@ SENSOR_CHAN_ALL
All channels.
Definition sensor.h:197
@ SENSOR_CHAN_GAUGE_VOLTAGE
Voltage, in volts.
Definition sensor.h:160
@ SENSOR_CHAN_PROX
Proximity.
Definition sensor.h:96
@ SENSOR_CHAN_COMMON_COUNT
Number of all common sensor channels.
Definition sensor.h:202
@ SENSOR_CHAN_PRIV_START
This and higher values are sensor specific.
Definition sensor.h:208
@ SENSOR_CHAN_GYRO_X
Angular velocity around the X axis, in radians/s.
Definition sensor.h:71
@ SENSOR_CHAN_GAS_RES
Gas sensor resistance in ohms.
Definition sensor.h:128
@ SENSOR_CHAN_HUMIDITY
Humidity, in percent.
Definition sensor.h:98
@ SENSOR_CHAN_DISTANCE
Distance.
Definition sensor.h:119
@ SENSOR_CHAN_IR
Illuminance in infra-red spectrum, in lux.
Definition sensor.h:102
@ SENSOR_CHAN_MAX
Maximum value describing a sensor channel type.
Definition sensor.h:213
@ SENSOR_CHAN_POS_DZ
Position change on the Z axis, in points.
Definition sensor.h:152
@ SENSOR_CHAN_RED
Illuminance in red spectrum, in lux.
Definition sensor.h:104
@ SENSOR_CHAN_ALTITUDE
Altitude, in meters.
Definition sensor.h:110
@ SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY
Nominal Available Capacity in mAh.
Definition sensor.h:176
@ SENSOR_CHAN_ACCEL_X
Acceleration on the X axis, in m/s^2.
Definition sensor.h:63
@ SENSOR_CHAN_VOC
VOC level, in parts per billion (ppb)
Definition sensor.h:126
#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:82
#define ENOMEM
Not enough core.
Definition errno.h:50
#define ERANGE
Result too large.
Definition errno.h:72
Size of off_t must be equal or less than size of size_t
Definition retained_mem.h:28
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
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
__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:403
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:409
Definition errno.c:37
A completion queue event.
Definition rtio.h:294
void * userdata
Associated userdata with operation.
Definition rtio.h:298
int32_t result
Result from operation.
Definition rtio.h:297
API that an RTIO IO device should implement.
Definition rtio.h:433
Compute the mempool block index for a given pointer.
Definition rtio.h:423
struct rtio_sqe sqe
Definition rtio.h:424
An IO device with a function table for submitting requests.
Definition rtio.h:448
void * data
Definition rtio.h:453
A submission queue event.
Definition rtio.h:232
void * userdata
User provided data which is returned upon operation completion.
Definition rtio.h:252
uint8_t * buf
Buffer to use.
Definition rtio.h:259
uint32_t buf_len
Length of buffer.
Definition rtio.h:258
const struct rtio_iodev * iodev
Device to operation on.
Definition rtio.h:243
An RTIO context containing what can be viewed as a pair of queues.
Definition rtio.h:327
Sensor Channel Specification.
Definition sensor.h:431
uint16_t chan_idx
A sensor channel index.
Definition sensor.h:433
uint16_t chan_type
A sensor channel type.
Definition sensor.h:432
Definition sensor.h:909
uint64_t timestamp_ns
Definition sensor.h:911
int8_t shift
Definition sensor.h:920
uint32_t num_channels
Definition sensor.h:917
Used for iterating over the data frames via the sensor_decoder_api.
Definition sensor.h:553
const struct sensor_decoder_api * decoder
Definition sensor.h:554
struct sensor_chan_spec channel
Definition sensor.h:556
const uint8_t * buffer
Definition sensor.h:555
uint32_t fit
Definition sensor.h:557
Decodes a single raw data buffer.
Definition sensor.h:463
int(* get_size_info)(struct sensor_chan_spec channel, size_t *base_size, size_t *frame_size)
Get the size required to decode a given channel.
Definition sensor.h:488
int(* get_frame_count)(const uint8_t *buffer, struct sensor_chan_spec channel, uint16_t *frame_count)
Get the number of frames in the current buffer.
Definition sensor.h:473
int(* decode)(const uint8_t *buffer, struct sensor_chan_spec channel, uint32_t *fit, uint16_t max_count, void *data_out)
Decode up to max_count samples from the buffer.
Definition sensor.h:516
bool(* has_trigger)(const uint8_t *buffer, enum sensor_trigger_type trigger)
Check if the given trigger type is present.
Definition sensor.h:526
Definition sensor.h:698
sensor_get_decoder_t get_decoder
Definition sensor.h:704
sensor_attr_set_t attr_set
Definition sensor.h:699
sensor_attr_get_t attr_get
Definition sensor.h:700
sensor_trigger_set_t trigger_set
Definition sensor.h:701
sensor_sample_fetch_t sample_fetch
Definition sensor.h:702
sensor_channel_get_t channel_get
Definition sensor.h:703
sensor_submit_t submit
Definition sensor.h:705
Definition sensor.h:622
struct sensor_chan_spec *const channels
Definition sensor.h:626
size_t count
Definition sensor.h:629
struct sensor_stream_trigger *const triggers
Definition sensor.h:627
const bool is_streaming
Definition sensor.h:624
const struct device * sensor
Definition sensor.h:623
const size_t max
Definition sensor.h:630
Definition sensor.h:608
enum sensor_stream_data_opt opt
Definition sensor.h:610
enum sensor_trigger_type trigger
Definition sensor.h:609
Sensor trigger spec.
Definition sensor.h:286
enum sensor_trigger_type type
Trigger type.
Definition sensor.h:288
enum sensor_channel chan
Channel the trigger is set on.
Definition sensor.h:290
Representation of a sensor readout value.
Definition sensor.h:51
int32_t val2
Fractional part of the value (in one-millionth parts).
Definition sensor.h:55
int32_t val1
Integer part of the value.
Definition sensor.h:53
static ZTEST_BMEM char buffer[8]
Definition test_mbox_api.c:433
static void handler(struct k_timer *timer)
Definition main.c:19
#define INT64_C(x)
Definition xcc.h:90