Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
sensor.h
Go to the documentation of this file.
1/*
2* Copyright (c) 2016 Intel Corporation
3*
4* SPDX-License-Identifier: Apache-2.0
5*/
6#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_H_
7#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_H_
8
14
27
28#include <errno.h>
29#include <stdlib.h>
30
31#include <zephyr/device.h>
33#include <zephyr/dsp/types.h>
34#include <zephyr/rtio/rtio.h>
36#include <zephyr/sys/util.h>
37#include <zephyr/types.h>
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
62
118
143
146
159
162
165
170
173
176
185
188
191
234
237
240
243
248
254
259};
260
269#define SENSOR_CHANNEL_3_AXIS(chan) \
270 ((chan) == SENSOR_CHAN_ACCEL_XYZ || (chan) == SENSOR_CHAN_GYRO_XYZ || \
271 (chan) == SENSOR_CHAN_MAGN_XYZ || (chan) == SENSOR_CHAN_POS_DXYZ)
272
281#define SENSOR_CHANNEL_IS_ACCEL(chan) \
282 ((chan) == SENSOR_CHAN_ACCEL_XYZ || (chan) == SENSOR_CHAN_ACCEL_X || \
283 (chan) == SENSOR_CHAN_ACCEL_Y || (chan) == SENSOR_CHAN_ACCEL_Z)
284
293#define SENSOR_CHANNEL_IS_GYRO(chan) \
294 ((chan) == SENSOR_CHAN_GYRO_XYZ || (chan) == SENSOR_CHAN_GYRO_X || \
295 (chan) == SENSOR_CHAN_GYRO_Y || (chan) == SENSOR_CHAN_GYRO_Z)
296
370
380
456
463typedef void (*sensor_trigger_handler_t)(const struct device *dev,
464 const struct sensor_trigger *trigger);
465
470
475typedef int (*sensor_attr_set_t)(const struct device *dev,
476 enum sensor_channel chan,
477 enum sensor_attribute attr,
478 const struct sensor_value *val);
479
484typedef int (*sensor_attr_get_t)(const struct device *dev,
485 enum sensor_channel chan,
486 enum sensor_attribute attr,
487 struct sensor_value *val);
488
493typedef int (*sensor_trigger_set_t)(const struct device *dev,
494 const struct sensor_trigger *trig,
496
501typedef int (*sensor_sample_fetch_t)(const struct device *dev,
502 enum sensor_channel chan);
503
508typedef int (*sensor_channel_get_t)(const struct device *dev,
509 enum sensor_channel chan,
510 struct sensor_value *val);
511
512/* Forward declaration */
513struct sensor_decoder_api;
514
519typedef int (*sensor_get_decoder_t)(const struct device *dev,
520 const struct sensor_decoder_api **api);
521
530typedef void (*sensor_submit_t)(const struct device *sensor, struct rtio_iodev_sqe *sqe);
531
565
568
581
583/* Ensure sensor_chan_spec is sensibly sized to pass by value */
584BUILD_ASSERT(sizeof(struct sensor_chan_spec) <= sizeof(uintptr_t),
585 "sensor_chan_spec size should be equal or less than the size of a machine word");
587
596static inline bool sensor_chan_spec_eq(struct sensor_chan_spec chan_spec0,
597 struct sensor_chan_spec chan_spec1)
598{
599 return chan_spec0.chan_type == chan_spec1.chan_type &&
600 chan_spec0.chan_idx == chan_spec1.chan_idx;
601}
602
620 int (*get_frame_count)(const uint8_t *buffer, struct sensor_chan_spec chan_spec,
621 uint16_t *frame_count);
622
635 int (*get_size_info)(struct sensor_chan_spec channel, size_t *base_size,
636 size_t *frame_size);
637
662 int (*decode)(const uint8_t *buffer, struct sensor_chan_spec chan_spec, uint32_t *fit,
663 uint16_t max_count, void *data_out);
664
672 bool (*has_trigger)(const uint8_t *buffer, enum sensor_trigger_type trigger);
673};
674
705
709#define SENSOR_DECODE_CONTEXT_INIT(decoder_, buffer_, channel_type_, channel_index_) \
710 { \
711 .decoder = (decoder_), \
712 .buffer = (buffer_), \
713 .channel = {.chan_type = (channel_type_), .chan_idx = (channel_index_)}, \
714 .fit = 0, \
715 }
716
725static inline int sensor_decode(struct sensor_decode_context *ctx, void *out, uint16_t max_count)
726{
727 return ctx->decoder->decode(ctx->buffer, ctx->channel, &ctx->fit, max_count, out);
728}
729
731 size_t *frame_size);
732
744
749
750#define SENSOR_STREAM_TRIGGER_PREP(_trigger, _opt) \
751 { \
752 .trigger = (_trigger), .opt = (_opt), \
753 }
754
755/*
756 * Internal data structure used to store information about the IODevice for async reading and
757 * streaming sensor data.
758 */
760 const struct device *sensor;
761 const bool is_streaming;
762 union {
765 };
766 size_t count;
767 const size_t max;
768};
769
785#define SENSOR_DT_READ_IODEV(name, dt_node, ...) \
786 static struct sensor_chan_spec _CONCAT(__channel_array_, name)[] = {__VA_ARGS__}; \
787 static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
788 .sensor = DEVICE_DT_GET(dt_node), \
789 .is_streaming = false, \
790 .channels = _CONCAT(__channel_array_, name), \
791 .count = ARRAY_SIZE(_CONCAT(__channel_array_, name)), \
792 .max = ARRAY_SIZE(_CONCAT(__channel_array_, name)), \
793 }; \
794 RTIO_IODEV_DEFINE(name, &__sensor_iodev_api, _CONCAT(&__sensor_read_config_, name))
795
815#define SENSOR_DT_STREAM_IODEV(name, dt_node, ...) \
816 static struct sensor_stream_trigger _CONCAT(__trigger_array_, name)[] = {__VA_ARGS__}; \
817 static struct sensor_read_config _CONCAT(__sensor_read_config_, name) = { \
818 .sensor = DEVICE_DT_GET(dt_node), \
819 .is_streaming = true, \
820 .triggers = _CONCAT(__trigger_array_, name), \
821 .count = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
822 .max = ARRAY_SIZE(_CONCAT(__trigger_array_, name)), \
823 }; \
824 RTIO_IODEV_DEFINE(name, &__sensor_iodev_api, &_CONCAT(__sensor_read_config_, name))
825
826/* The default decoder API */
827extern const struct sensor_decoder_api __sensor_default_decoder;
828
829/* The default sensor iodev API */
830extern const struct rtio_iodev_api __sensor_iodev_api;
831
844__syscall int sensor_attr_set(const struct device *dev,
845 enum sensor_channel chan,
846 enum sensor_attribute attr,
847 const struct sensor_value *val);
848
849static inline int z_impl_sensor_attr_set(const struct device *dev,
850 enum sensor_channel chan,
851 enum sensor_attribute attr,
852 const struct sensor_value *val)
853{
854 const struct sensor_driver_api *api = DEVICE_API_GET(sensor, dev);
855
856 if (api->attr_set == NULL) {
857 return -ENOSYS;
858 }
859
860 return api->attr_set(dev, chan, attr, val);
861}
862
875__syscall int sensor_attr_get(const struct device *dev,
876 enum sensor_channel chan,
877 enum sensor_attribute attr,
878 struct sensor_value *val);
879
880static inline int z_impl_sensor_attr_get(const struct device *dev,
881 enum sensor_channel chan,
882 enum sensor_attribute attr,
883 struct sensor_value *val)
884{
885 const struct sensor_driver_api *api = DEVICE_API_GET(sensor, dev);
886
887 if (api->attr_get == NULL) {
888 return -ENOSYS;
889 }
890
891 return api->attr_get(dev, chan, attr, val);
892}
893
916static inline int sensor_trigger_set(const struct device *dev,
917 const struct sensor_trigger *trig,
919{
920 const struct sensor_driver_api *api = DEVICE_API_GET(sensor, dev);
921
922 if (api->trigger_set == NULL) {
923 return -ENOSYS;
924 }
925
926 return api->trigger_set(dev, trig, handler);
927}
928
947__syscall int sensor_sample_fetch(const struct device *dev);
948
949static inline int z_impl_sensor_sample_fetch(const struct device *dev)
950{
951 return DEVICE_API_GET(sensor, dev)->sample_fetch(dev, SENSOR_CHAN_ALL);
952}
953
975__syscall int sensor_sample_fetch_chan(const struct device *dev,
976 enum sensor_channel type);
977
978static inline int z_impl_sensor_sample_fetch_chan(const struct device *dev,
979 enum sensor_channel type)
980{
981 return DEVICE_API_GET(sensor, dev)->sample_fetch(dev, type);
982}
983
1005__syscall int sensor_channel_get(const struct device *dev,
1006 enum sensor_channel chan,
1007 struct sensor_value *val);
1008
1009static inline int z_impl_sensor_channel_get(const struct device *dev,
1010 enum sensor_channel chan,
1011 struct sensor_value *val)
1012{
1013 return DEVICE_API_GET(sensor, dev)->channel_get(dev, chan, val);
1014}
1015
1016#if defined(CONFIG_SENSOR_ASYNC_API) || defined(__DOXYGEN__)
1017
1018/*
1019 * Generic data structure used for encoding the sample timestamp and number of channels sampled.
1020 */
1021struct __attribute__((__packed__)) sensor_data_generic_header {
1024
1025 /*
1026 ** The number of channels present in the frame.
1027 * This will be the true number of elements in channel_info and in the q31 values that
1028 * follow the header.
1029 */
1031
1034
1035 /* This padding is needed to make sure that the 'channels' field is aligned */
1036 int8_t _padding[sizeof(struct sensor_chan_spec) - 1];
1037
1040};
1041
1050__syscall int sensor_get_decoder(const struct device *dev,
1051 const struct sensor_decoder_api **decoder);
1052
1053static inline int z_impl_sensor_get_decoder(const struct device *dev,
1054 const struct sensor_decoder_api **decoder)
1055{
1056 const struct sensor_driver_api *api = DEVICE_API_GET(sensor, dev);
1057
1058 __ASSERT_NO_MSG(api != NULL);
1059
1060 if (api->get_decoder == NULL) {
1061 *decoder = &__sensor_default_decoder;
1062 return 0;
1063 }
1064
1065 return api->get_decoder(dev, decoder);
1066}
1067
1086__syscall int sensor_reconfigure_read_iodev(const struct rtio_iodev *iodev,
1087 const struct device *sensor,
1088 const struct sensor_chan_spec *channels,
1089 size_t num_channels);
1090
1091static inline int z_impl_sensor_reconfigure_read_iodev(const struct rtio_iodev *iodev,
1092 const struct device *sensor,
1093 const struct sensor_chan_spec *channels,
1094 size_t num_channels)
1095{
1096 struct sensor_read_config *cfg = (struct sensor_read_config *)iodev->data;
1097
1098 if (cfg->max < num_channels || cfg->is_streaming) {
1099 return -ENOMEM;
1100 }
1101
1102 cfg->sensor = sensor;
1103 memcpy(cfg->channels, channels, num_channels * sizeof(struct sensor_chan_spec));
1104 cfg->count = num_channels;
1105 return 0;
1106}
1107
1108static inline int sensor_stream(const struct rtio_iodev *iodev, struct rtio *ctx, void *userdata,
1109 struct rtio_sqe **handle)
1110{
1111 if (IS_ENABLED(CONFIG_USERSPACE)) {
1112 struct rtio_sqe sqe;
1113
1115 rtio_sqe_copy_in_get_handles(ctx, &sqe, handle, 1);
1116 } else {
1117 struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);
1118
1119 if (sqe == NULL) {
1120 return -ENOMEM;
1121 }
1122 if (handle != NULL) {
1123 *handle = sqe;
1124 }
1126 }
1127 rtio_submit(ctx, 0);
1128 return 0;
1129}
1130
1145static inline int sensor_read(const struct rtio_iodev *iodev, struct rtio *ctx, uint8_t *buf,
1146 size_t buf_len)
1147{
1148 if (IS_ENABLED(CONFIG_USERSPACE)) {
1149 struct rtio_sqe sqe;
1150
1152 rtio_sqe_copy_in(ctx, &sqe, 1);
1153 } else {
1154 struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);
1155
1156 if (sqe == NULL) {
1157 return -ENOMEM;
1158 }
1160 }
1161 rtio_submit(ctx, 0);
1162
1163 struct rtio_cqe *cqe = rtio_cqe_consume_block(ctx);
1164 int res = cqe->result;
1165
1166 __ASSERT(cqe->userdata == buf,
1167 "consumed non-matching completion for sensor read into buffer %p\n", buf);
1168
1169 rtio_cqe_release(ctx, cqe);
1170
1171 return res;
1172}
1173
1187static inline int sensor_read_async_mempool(const struct rtio_iodev *iodev, struct rtio *ctx,
1188 void *userdata)
1189{
1190 if (IS_ENABLED(CONFIG_USERSPACE)) {
1191 struct rtio_sqe sqe;
1192
1194 rtio_sqe_copy_in(ctx, &sqe, 1);
1195 } else {
1196 struct rtio_sqe *sqe = rtio_sqe_acquire(ctx);
1197
1198 if (sqe == NULL) {
1199 return -ENOMEM;
1200 }
1202 }
1203 rtio_submit(ctx, 0);
1204 return 0;
1205}
1206
1218 void *userdata);
1219
1232
1233#endif /* defined(CONFIG_SENSOR_ASYNC_API) || defined(__DOXYGEN__) */
1234
1238#define SENSOR_G 9806650LL
1239
1243#define SENSOR_PI 3141592LL
1244
1253static inline int32_t sensor_ms2_to_g(const struct sensor_value *ms2)
1254{
1255 int64_t micro_ms2 = ms2->val1 * 1000000LL + ms2->val2;
1256
1257 if (micro_ms2 > 0) {
1258 return (micro_ms2 + SENSOR_G / 2) / SENSOR_G;
1259 } else {
1260 return (micro_ms2 - SENSOR_G / 2) / SENSOR_G;
1261 }
1262}
1263
1270static inline void sensor_g_to_ms2(int32_t g, struct sensor_value *ms2)
1271{
1272 ms2->val1 = ((int64_t)g * SENSOR_G) / 1000000LL;
1273 ms2->val2 = ((int64_t)g * SENSOR_G) % 1000000LL;
1274}
1275
1284static inline int32_t sensor_ms2_to_mg(const struct sensor_value *ms2)
1285{
1286 int64_t nano_ms2 = (ms2->val1 * 1000000LL + ms2->val2) * 1000LL;
1287
1288 if (nano_ms2 > 0) {
1289 return (nano_ms2 + SENSOR_G / 2) / SENSOR_G;
1290 } else {
1291 return (nano_ms2 - SENSOR_G / 2) / SENSOR_G;
1292 }
1293}
1294
1303static inline int32_t sensor_ms2_to_ug(const struct sensor_value *ms2)
1304{
1305 int64_t micro_ms2 = (ms2->val1 * INT64_C(1000000)) + ms2->val2;
1306
1307 return (micro_ms2 * 1000000LL) / SENSOR_G;
1308}
1309
1316static inline void sensor_ug_to_ms2(int32_t ug, struct sensor_value *ms2)
1317{
1318 ms2->val1 = ((int64_t)ug * SENSOR_G / 1000000LL) / 1000000LL;
1319 ms2->val2 = ((int64_t)ug * SENSOR_G / 1000000LL) % 1000000LL;
1320}
1321
1329static inline int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
1330{
1331 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
1332
1333 if (micro_rad_s > 0) {
1334 return (micro_rad_s * 180LL + SENSOR_PI / 2) / SENSOR_PI;
1335 } else {
1336 return (micro_rad_s * 180LL - SENSOR_PI / 2) / SENSOR_PI;
1337 }
1338}
1339
1346static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
1347{
1348 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL) / 1000000LL;
1349 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL;
1350}
1351
1363static inline int32_t sensor_rad_to_10udegrees(const struct sensor_value *rad)
1364{
1365 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
1366
1367 return (micro_rad_s * 180LL * 100000LL) / SENSOR_PI;
1368}
1369
1376static inline void sensor_10udegrees_to_rad(int32_t d, struct sensor_value *rad)
1377{
1378 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL / 100000LL) / 1000000LL;
1379 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL / 100000LL) % 1000000LL;
1380}
1381
1388static inline double sensor_value_to_double(const struct sensor_value *val)
1389{
1390 return (double)val->val1 + (double)val->val2 / 1000000;
1391}
1392
1399static inline float sensor_value_to_float(const struct sensor_value *val)
1400{
1401 return (float)val->val1 + (float)val->val2 / 1000000;
1402}
1403
1414static inline int sensor_value_to_fixed_point(uint32_t *val, struct sensor_value *inp, uint8_t m,
1415 uint8_t n, bool is_signed)
1416{
1417 uint8_t num_bits = m + n + (is_signed ? 1U : 0U);
1418
1419 if (m > 31 || !IN_RANGE(n, 1, 32) || !IN_RANGE(num_bits, 1, 32)) {
1420 return -EINVAL;
1421 }
1422
1423 int64_t raw = ((int64_t)inp->val1 << n) + ((int64_t)inp->val2 << n) / 1000000LL;
1424
1425 if (is_signed) {
1426 if (!IN_RANGE(raw, -(int64_t)BIT(num_bits - 1), (int64_t)BIT_MASK(num_bits - 1))) {
1427 return -ERANGE;
1428 }
1429
1430 *val = (uint32_t)(raw & BIT64_MASK(num_bits));
1431 } else {
1432 if (!IN_RANGE(raw, 0, (int64_t)BIT64_MASK(num_bits))) {
1433 return -ERANGE;
1434 }
1435
1436 *val = (uint32_t)raw;
1437 }
1438
1439 return 0;
1440}
1441
1449static inline int sensor_value_from_double(struct sensor_value *val, double inp)
1450{
1451 if (inp < (double)INT32_MIN || inp > (double)INT32_MAX) {
1452 return -ERANGE;
1453 }
1454
1455 int32_t val1 = (int32_t)inp;
1456 int32_t val2 = (int32_t)((inp - (double)val1) * 1000000.0);
1457
1458 val->val1 = val1;
1459 val->val2 = val2;
1460
1461 return 0;
1462}
1463
1471static inline int sensor_value_from_float(struct sensor_value *val, float inp)
1472{
1473 if (inp < (float)INT32_MIN || inp >= (float)INT32_MAX) {
1474 return -ERANGE;
1475 }
1476
1477 int32_t val1 = (int32_t)inp;
1478 int32_t val2 = (int32_t)((inp - (float)val1) * 1000000.0f);
1479
1480 val->val1 = val1;
1481 val->val2 = val2;
1482
1483 return 0;
1484}
1485
1496static inline int sensor_value_from_fixed_point(struct sensor_value *val, uint32_t inp, uint8_t m,
1497 uint8_t n, bool is_signed)
1498{
1499 uint8_t num_bits = m + n + (is_signed ? 1U : 0U);
1500
1501 if (m > 31 || !IN_RANGE(n, 1, 32) || !IN_RANGE(num_bits, 1, 32)) {
1502 return -EINVAL;
1503 }
1504
1505 inp &= BIT64_MASK(num_bits);
1506
1507 int64_t inp_int64 = is_signed ? sign_extend_64((uint64_t)inp, m + n) : (int64_t)inp;
1508
1509 val->val1 = arithmetic_shift_right(inp_int64 + (inp_int64 < 0 ? BIT64_MASK(n) : 0), n);
1510
1511 inp_int64 -= ((int64_t)val->val1 << n);
1512 inp_int64 *= 1000000LL;
1513
1514 val->val2 = arithmetic_shift_right(inp_int64 + (inp_int64 < 0 ? BIT64_MASK(n) : 0), n);
1515
1516 return 0;
1517}
1518
1519#ifdef CONFIG_SENSOR_INFO
1520
1521struct sensor_info {
1522 const struct device *dev;
1523 const char *vendor;
1524 const char *model;
1525 const char *friendly_name;
1526};
1527
1528#define SENSOR_INFO_INITIALIZER(_dev, _vendor, _model, _friendly_name) \
1529 { \
1530 .dev = _dev, \
1531 .vendor = _vendor, \
1532 .model = _model, \
1533 .friendly_name = _friendly_name, \
1534 }
1535
1536#define SENSOR_INFO_DEFINE(name, ...) \
1537 static const STRUCT_SECTION_ITERABLE(sensor_info, name) = \
1538 SENSOR_INFO_INITIALIZER(__VA_ARGS__)
1539
1540#define SENSOR_INFO_DT_NAME(node_id) \
1541 _CONCAT(__sensor_info, DEVICE_DT_NAME_GET(node_id))
1542
1543#define SENSOR_INFO_DT_DEFINE(node_id) \
1544 SENSOR_INFO_DEFINE(SENSOR_INFO_DT_NAME(node_id), \
1545 DEVICE_DT_GET(node_id), \
1546 DT_NODE_VENDOR_OR(node_id, NULL), \
1547 DT_NODE_MODEL_OR(node_id, NULL), \
1548 DT_PROP_OR(node_id, friendly_name, NULL)) \
1549
1550#else
1551
1552#define SENSOR_INFO_DEFINE(name, ...)
1553#define SENSOR_INFO_DT_DEFINE(node_id)
1554
1555#endif /* CONFIG_SENSOR_INFO */
1556
1584#define SENSOR_DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
1585 data_ptr, cfg_ptr, level, prio, \
1586 api_ptr, ...) \
1587 DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
1588 data_ptr, cfg_ptr, level, prio, \
1589 api_ptr, __VA_ARGS__); \
1590 \
1591 SENSOR_INFO_DT_DEFINE(node_id);
1592
1602#define SENSOR_DEVICE_DT_INST_DEFINE(inst, ...) \
1603 SENSOR_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
1604
1611static inline int64_t sensor_value_to_deci(const struct sensor_value *val)
1612{
1613 return ((int64_t)val->val1 * 10) + val->val2 / 100000;
1614}
1615
1622static inline int64_t sensor_value_to_centi(const struct sensor_value *val)
1623{
1624 return ((int64_t)val->val1 * 100) + val->val2 / 10000;
1625}
1626
1633static inline int64_t sensor_value_to_milli(const struct sensor_value *val)
1634{
1635 return ((int64_t)val->val1 * 1000) + val->val2 / 1000;
1636}
1637
1644static inline int64_t sensor_value_to_micro(const struct sensor_value *val)
1645{
1646 return ((int64_t)val->val1 * 1000000) + val->val2;
1647}
1648
1656static inline int sensor_value_from_milli(struct sensor_value *val, int64_t milli)
1657{
1658 if (milli < ((int64_t)INT32_MIN - 1) * 1000LL ||
1659 milli > ((int64_t)INT32_MAX + 1) * 1000LL) {
1660 return -ERANGE;
1661 }
1662
1663 val->val1 = (int32_t)(milli / 1000);
1664 val->val2 = (int32_t)(milli % 1000) * 1000;
1665
1666 return 0;
1667}
1668
1676static inline int sensor_value_from_micro(struct sensor_value *val, int64_t micro)
1677{
1678 if (micro < ((int64_t)INT32_MIN - 1) * 1000000LL ||
1679 micro > ((int64_t)INT32_MAX + 1) * 1000000LL) {
1680 return -ERANGE;
1681 }
1682
1683 val->val1 = (int32_t)(micro / 1000000LL);
1684 val->val2 = (int32_t)(micro % 1000000LL);
1685
1686 return 0;
1687}
1688
1697static inline int sensor_value_add(struct sensor_value *inp1, struct sensor_value *inp2,
1698 struct sensor_value *out)
1699{
1700 int64_t val1 = (int64_t)inp1->val1 + (int64_t)inp2->val1;
1701 int32_t val2 = inp1->val2 + inp2->val2;
1702
1703 if (val2 >= 1000000LL || (val1 < 0 && val2 > 0)) {
1704 val1 += 1;
1705 val2 -= 1000000LL;
1706 } else if (val2 <= -1000000LL || (val1 > 0 && val2 < 0)) {
1707 val1 -= 1;
1708 val2 += 1000000LL;
1709 }
1710
1711 if (!IN_RANGE(val1, INT32_MIN, INT32_MAX)) {
1712 return -ERANGE;
1713 }
1714
1715 out->val1 = val1;
1716 out->val2 = val2;
1717
1718 return 0;
1719}
1720
1729static inline int sensor_value_subtract(struct sensor_value *inp1, struct sensor_value *inp2,
1730 struct sensor_value *out)
1731{
1732 int64_t val1 = (int64_t)inp1->val1 - (int64_t)inp2->val1;
1733 int32_t val2 = inp1->val2 - inp2->val2;
1734
1735 if (val2 >= 1000000LL || (val1 < 0 && val2 > 0)) {
1736 val1 += 1;
1737 val2 -= 1000000LL;
1738 } else if (val2 <= -1000000LL || (val1 > 0 && val2 < 0)) {
1739 val1 -= 1;
1740 val2 += 1000000LL;
1741 }
1742
1743 if (!IN_RANGE(val1, INT32_MIN, INT32_MAX)) {
1744 return -ERANGE;
1745 }
1746
1747 out->val1 = val1;
1748 out->val2 = val2;
1749
1750 return 0;
1751}
1752
1761static inline int sensor_value_multiply(struct sensor_value *inp1, struct sensor_value *inp2,
1762 struct sensor_value *out)
1763{
1764 int64_t val1 = (int64_t)inp1->val1 * inp2->val1;
1765 int64_t val2 = (int64_t)inp1->val1 * inp2->val2 + (int64_t)inp1->val2 * inp2->val1 +
1766 ((int64_t)inp1->val2 * inp2->val2) / 1000000LL;
1767
1768 val1 += val2 / 1000000LL;
1769 val2 %= 1000000LL;
1770
1771 if (!IN_RANGE(val1, INT32_MIN, INT32_MAX)) {
1772 return -ERANGE;
1773 }
1774
1775 out->val1 = val1;
1776 out->val2 = val2;
1777
1778 return 0;
1779}
1780
1789static inline int sensor_value_scale(struct sensor_value *inp, int32_t scalar,
1790 struct sensor_value *out)
1791{
1792 int64_t val1 = (int64_t)inp->val1 * scalar;
1793 int64_t val2 = (int64_t)inp->val2 * scalar;
1794
1795 val1 += val2 / 1000000LL;
1796 val2 %= 1000000LL;
1797
1798 if (!IN_RANGE(val1, INT32_MIN, INT32_MAX)) {
1799 return -ERANGE;
1800 }
1801
1802 out->val1 = val1;
1803 out->val2 = val2;
1804
1805 return 0;
1806}
1807
1811
1817#define SENSOR_DECODER_NAME() UTIL_CAT(DT_DRV_COMPAT, __decoder_api)
1818
1826#define SENSOR_DECODER_DT_GET(node_id) \
1827 &UTIL_CAT(DT_STRING_TOKEN_BY_IDX(node_id, compatible, 0), __decoder_api)
1828
1844#define SENSOR_DECODER_API_DT_DEFINE() \
1845 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT), (), (static)) \
1846 const STRUCT_SECTION_ITERABLE(sensor_decoder_api, SENSOR_DECODER_NAME())
1847
1848#define Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL_IDX(node_id, prop, idx) \
1849 extern const struct sensor_decoder_api UTIL_CAT( \
1850 DT_STRING_TOKEN_BY_IDX(node_id, prop, idx), __decoder_api);
1851
1852#define Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL(node_id) \
1853 COND_CODE_1(DT_NODE_HAS_PROP(node_id, compatible), \
1854 (DT_FOREACH_PROP_ELEM(node_id, compatible, \
1855 Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL_IDX)), \
1856 ())
1857
1858DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_SENSOR_DECODER_DECLARE_INTERNAL)
1859
1860#ifdef __cplusplus
1861}
1862#endif
1863
1864#include <zephyr/syscalls/sensor.h>
1865
1866#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_H_ */
irp nz macro MOVR cc d
Definition asm-macro-32-bit-gnu.h:14
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1425
System error numbers.
#define DT_FOREACH_STATUS_OKAY_NODE(fn)
Invokes fn for every status okay node in the tree.
Definition devicetree.h:3296
#define RTIO_PRIO_NORM
Normal priority.
Definition sqe.h:55
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 sqe.h:470
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:933
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 sqe.h:453
static struct rtio_sqe * rtio_sqe_acquire(struct rtio *r)
Acquire a single submission queue event if available.
Definition rtio.h:338
static void rtio_sqe_prep_read_multishot(struct rtio_sqe *sqe, const struct rtio_iodev *iodev, int8_t prio, void *userdata)
Definition sqe.h:478
static void rtio_cqe_release(struct rtio *r, struct rtio_cqe *cqe)
Release consumed completion queue event.
Definition rtio.h:512
static struct rtio_cqe * rtio_cqe_consume_block(struct rtio *r)
Wait for and consume a single completion queue event.
Definition rtio.h:488
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:1238
int(* sensor_attr_get_t)(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, struct sensor_value *val)
Callback API to get a sensor attribute.
Definition sensor.h:484
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:725
static int sensor_stream(const struct rtio_iodev *iodev, struct rtio *ctx, void *userdata, struct rtio_sqe **handle)
Definition sensor.h:1108
static int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
Helper function for converting radians to degrees.
Definition sensor.h:1329
sensor_trigger_type
Sensor trigger types.
Definition sensor.h:300
sensor_attribute
Sensor attribute types.
Definition sensor.h:384
int sensor_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder)
Get the sensor's decoder API.
int(* sensor_get_decoder_t)(const struct device *dev, const struct sensor_decoder_api **api)
Callback API to get the sensor decoder implementation.
Definition sensor.h:519
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:1316
static double sensor_value_to_double(const struct sensor_value *val)
Helper function for converting struct sensor_value to double.
Definition sensor.h:1388
static float sensor_value_to_float(const struct sensor_value *val)
Helper function for converting struct sensor_value to float.
Definition sensor.h:1399
int sensor_natively_supported_channel_size_info(struct sensor_chan_spec channel, size_t *base_size, size_t *frame_size)
int(* sensor_attr_set_t)(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, const struct sensor_value *val)
@def_driverbackendgroup{Sensor,sensor_interface}
Definition sensor.h:475
static void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
Helper function for converting degrees to radians.
Definition sensor.h:1346
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:1303
int(* sensor_channel_get_t)(const struct device *dev, enum sensor_channel chan, struct sensor_value *val)
Callback API to read a sensor channel from the driver buffer.
Definition sensor.h:508
static int sensor_value_from_float(struct sensor_value *val, float inp)
Helper function for converting float to struct sensor_value.
Definition sensor.h:1471
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:1270
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:1633
#define SENSOR_PI
The value of constant PI in micros.
Definition sensor.h:1243
int sensor_reconfigure_read_iodev(const 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_value_from_fixed_point(struct sensor_value *val, uint32_t inp, uint8_t m, uint8_t n, bool is_signed)
Helper function for converting fixed-point to struct sensor_value.
Definition sensor.h:1496
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:916
sensor_stream_data_opt
Options for what to do with the associated data when a trigger is consumed.
Definition sensor.h:736
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:1656
void(* sensor_trigger_handler_t)(const struct device *dev, const struct sensor_trigger *trigger)
Callback API upon firing of a trigger.
Definition sensor.h:463
static int sensor_value_scale(struct sensor_value *inp, int32_t scalar, struct sensor_value *out)
Helper function for scaling a struct sensor_value.
Definition sensor.h:1789
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:1644
int sensor_channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val)
Get a reading from a sensor device.
static int32_t sensor_ms2_to_mg(const struct sensor_value *ms2)
Helper function to convert acceleration from m/s^2 to milli Gs.
Definition sensor.h:1284
int sensor_sample_fetch(const struct device *dev)
Fetch a sample from the sensor and store it in an internal driver buffer.
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:1217
sensor_channel
Sensor channels.
Definition sensor.h:66
static int sensor_value_to_fixed_point(uint32_t *val, struct sensor_value *inp, uint8_t m, uint8_t n, bool is_signed)
Helper function for converting struct sensor_value to fixed-point.
Definition sensor.h:1414
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:1376
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:1253
void sensor_processing_with_callback(struct rtio *ctx, sensor_processing_callback_t cb)
Helper function for common processing of sensor data.
static int64_t sensor_value_to_deci(const struct sensor_value *val)
Helper function for converting struct sensor_value to integer deci units.
Definition sensor.h:1611
static int sensor_value_add(struct sensor_value *inp1, struct sensor_value *inp2, struct sensor_value *out)
Helper function for adding two struct sensor_value.
Definition sensor.h:1697
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:1676
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_value_subtract(struct sensor_value *inp1, struct sensor_value *inp2, struct sensor_value *out)
Helper function for subtracting two struct sensor_value.
Definition sensor.h:1729
int(* sensor_sample_fetch_t)(const struct device *dev, enum sensor_channel chan)
Callback API to fetch a sensor sample into the driver buffer.
Definition sensor.h:501
static int sensor_value_multiply(struct sensor_value *inp1, struct sensor_value *inp2, struct sensor_value *out)
Helper function for multiplying two struct sensor_value.
Definition sensor.h:1761
static int sensor_read(const 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:1145
void(* sensor_submit_t)(const struct device *sensor, struct rtio_iodev_sqe *sqe)
Callback API to service an RTIO submission for a sensor device.
Definition sensor.h:530
int(* sensor_trigger_set_t)(const struct device *dev, const struct sensor_trigger *trig, sensor_trigger_handler_t handler)
Callback API to set a sensor trigger and handler.
Definition sensor.h:493
static int32_t sensor_rad_to_10udegrees(const struct sensor_value *rad)
Helper function for converting radians to 10 micro degrees.
Definition sensor.h:1363
static int64_t sensor_value_to_centi(const struct sensor_value *val)
Helper function for converting struct sensor_value to integer centi units.
Definition sensor.h:1622
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:596
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:1449
static int sensor_read_async_mempool(const struct rtio_iodev *iodev, struct rtio *ctx, void *userdata)
One shot non-blocking read with pool allocated buffer.
Definition sensor.h:1187
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_OVERFLOW
Trigger fires when data overflows.
Definition sensor.h:352
@ SENSOR_TRIG_DELTA
Trigger fires when the selected channel varies significantly.
Definition sensor.h:316
@ SENSOR_TRIG_NEAR_FAR
Trigger fires when a near/far event is detected.
Definition sensor.h:318
@ SENSOR_TRIG_FREEFALL
Trigger fires when a free fall is detected.
Definition sensor.h:334
@ SENSOR_TRIG_PRIV_START
This and higher values are sensor specific.
Definition sensor.h:363
@ SENSOR_TRIG_FIFO_FULL
Trigger fires when the FIFO becomes full.
Definition sensor.h:346
@ SENSOR_TRIG_MOTION
Trigger fires when motion is detected.
Definition sensor.h:337
@ SENSOR_TRIG_STATIONARY
Trigger fires when no motion has been detected for a while.
Definition sensor.h:340
@ SENSOR_TRIG_COMMON_COUNT
Number of all common sensor triggers.
Definition sensor.h:357
@ SENSOR_TRIG_THRESHOLD
Trigger fires when channel reading transitions configured thresholds.
Definition sensor.h:325
@ SENSOR_TRIG_MAX
Maximum value describing a sensor trigger type.
Definition sensor.h:368
@ SENSOR_TRIG_DOUBLE_TAP
Trigger fires when a double tap is detected.
Definition sensor.h:331
@ SENSOR_TRIG_TILT
Trigger fires when a tilt is detected.
Definition sensor.h:349
@ SENSOR_TRIG_TIMER
Timer-based trigger, useful when the sensor does not have an interrupt line.
Definition sensor.h:305
@ SENSOR_TRIG_FIFO_WATERMARK
Trigger fires when the FIFO watermark has been reached.
Definition sensor.h:343
@ SENSOR_TRIG_TAP
Trigger fires when a single tap is detected.
Definition sensor.h:328
@ SENSOR_TRIG_DATA_READY
Trigger fires whenever new data is ready.
Definition sensor.h:307
@ SENSOR_ATTR_HYSTERESIS
Definition sensor.h:402
@ SENSOR_ATTR_FEATURE_MASK
Enable/disable sensor features.
Definition sensor.h:422
@ SENSOR_ATTR_CALIB_TARGET
Calibration target.
Definition sensor.h:416
@ SENSOR_ATTR_OFFSET
The sensor value returned will be altered by the amount indicated by offset: final_value = sensor_val...
Definition sensor.h:411
@ SENSOR_ATTR_BATCH_DURATION
Hardware batch duration in ticks.
Definition sensor.h:433
@ SENSOR_ATTR_OVERSAMPLING
Oversampling factor.
Definition sensor.h:404
@ SENSOR_ATTR_FF_DUR
Free-fall duration represented in milliseconds.
Definition sensor.h:430
@ SENSOR_ATTR_UPPER_THRESH
Upper threshold for trigger.
Definition sensor.h:393
@ SENSOR_ATTR_CONFIGURATION
Configure the operating modes of a sensor.
Definition sensor.h:418
@ SENSOR_ATTR_RESOLUTION
Configure the resolution of a sensor.
Definition sensor.h:437
@ SENSOR_ATTR_CALIBRATION
Set a calibration value needed by a sensor.
Definition sensor.h:420
@ SENSOR_ATTR_CHIP_ID
Chip ID of a sensor.
Definition sensor.h:439
@ SENSOR_ATTR_COMMON_COUNT
Number of all common sensor attributes.
Definition sensor.h:443
@ SENSOR_ATTR_ALERT
Alert threshold or alert enable/disable.
Definition sensor.h:424
@ SENSOR_ATTR_SLOPE_TH
Threshold for any-motion (slope) trigger.
Definition sensor.h:395
@ SENSOR_ATTR_GAIN
Configure the gain of a sensor.
Definition sensor.h:435
@ SENSOR_ATTR_SAMPLING_FREQUENCY
Sensor sampling frequency, i.e.
Definition sensor.h:389
@ SENSOR_ATTR_FULL_SCALE
Sensor range, in SI units.
Definition sensor.h:406
@ SENSOR_ATTR_LOWER_THRESH
Lower threshold for trigger.
Definition sensor.h:391
@ SENSOR_ATTR_SLOPE_DUR
Duration for which the slope values needs to be outside the threshold for the trigger to fire.
Definition sensor.h:400
@ SENSOR_ATTR_MAX
Maximum value describing a sensor attribute type.
Definition sensor.h:454
@ SENSOR_ATTR_PRIV_START
This and higher values are sensor specific.
Definition sensor.h:449
@ SENSOR_STREAM_DATA_INCLUDE
Include whatever data is associated with the trigger.
Definition sensor.h:738
@ SENSOR_STREAM_DATA_NOP
Do nothing with the associated trigger data, it may be consumed later.
Definition sensor.h:740
@ SENSOR_STREAM_DATA_DROP
Flush/clear whatever data is associated with the trigger.
Definition sensor.h:742
@ SENSOR_CHAN_GAUGE_STATE_OF_HEALTH
State of health measurement in %.
Definition sensor.h:215
@ SENSOR_CHAN_PM_1_0_CF
PM1.0 concentration (standard particle, CF=1), in µg/m³
Definition sensor.h:120
@ SENSOR_CHAN_PM_1_0
1.0 micro-meters Particulate Matter, in ug/m^3
Definition sensor.h:126
@ SENSOR_CHAN_DIE_TEMP
Device die temperature in degrees Celsius.
Definition sensor.h:92
@ SENSOR_CHAN_PRESS
Pressure in kilopascal.
Definition sensor.h:96
@ SENSOR_CHAN_GAUGE_TIME_TO_FULL
Time to full in minutes.
Definition sensor.h:219
@ SENSOR_CHAN_ACCEL_XYZ
Acceleration on the X, Y and Z axes.
Definition sensor.h:74
@ SENSOR_CHAN_MAGN_X
Magnetic field on the X axis, in G.
Definition sensor.h:84
@ SENSOR_CHAN_O2
O2 level, in parts per million (ppm).
Definition sensor.h:150
@ SENSOR_CHAN_CURRENT
Current, in amps.
Definition sensor.h:167
@ SENSOR_CHAN_GYRO_XYZ
Angular velocity around the X, Y and Z axes.
Definition sensor.h:82
@ SENSOR_CHAN_VSHUNT
Current Shunt Voltage in milli-volts.
Definition sensor.h:164
@ SENSOR_CHAN_GREEN
Illuminance in green spectrum, in lux.
Definition sensor.h:113
@ SENSOR_CHAN_GRAVITY_VECTOR
Gravity Vector (X/Y/Z components in m/s^2).
Definition sensor.h:231
@ SENSOR_CHAN_MAGN_Z
Magnetic field on the Z axis, in G.
Definition sensor.h:88
@ SENSOR_CHAN_MAGN_Y
Magnetic field on the Y axis, in G.
Definition sensor.h:86
@ SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE
Desired voltage of cell in V (nominal voltage).
Definition sensor.h:225
@ SENSOR_CHAN_POWER
Power in watts.
Definition sensor.h:169
@ SENSOR_CHAN_PM_2_5
2.5 micro-meters Particulate Matter, in ug/m^3
Definition sensor.h:128
@ SENSOR_CHAN_RESISTANCE
Resistance , in Ohm.
Definition sensor.h:172
@ SENSOR_CHAN_GAME_ROTATION_VECTOR
Game Rotation Vector (unit quaternion components X/Y/Z/W).
Definition sensor.h:229
@ SENSOR_CHAN_AMBIENT_LIGHT
Ambient illuminance in visible spectrum, in lux.
Definition sensor.h:105
@ SENSOR_CHAN_PM_0_5_COUNT
Number of particles ≥ 0.5 µm per 0.1 liter of air.
Definition sensor.h:134
@ SENSOR_CHAN_GAUGE_AVG_CURRENT
Average current, in amps (negative=discharging).
Definition sensor.h:195
@ SENSOR_CHAN_ENCODER_COUNT
Raw quadrature decoder count, in counts.
Definition sensor.h:236
@ SENSOR_CHAN_GYRO_Y
Angular velocity around the Y axis, in rad/s.
Definition sensor.h:78
@ SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT
Desired charging current in mA.
Definition sensor.h:227
@ SENSOR_CHAN_FREQUENCY
Frequency, in Hz.
Definition sensor.h:190
@ SENSOR_CHAN_PM_2_5_CF
PM2.5 concentration (standard particle, CF=1), in µg/m³
Definition sensor.h:122
@ SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY
Full Charge Capacity in mAh.
Definition sensor.h:205
@ SENSOR_CHAN_ROTATION
Angular rotation, in degrees.
Definition sensor.h:175
@ SENSOR_CHAN_AMBIENT_TEMP
Ambient temperature in degrees Celsius.
Definition sensor.h:94
@ SENSOR_CHAN_MAGN_XYZ
Magnetic field on the X, Y and Z axes.
Definition sensor.h:90
@ SENSOR_CHAN_GAUGE_STDBY_CURRENT
Standby current, in amps (negative=discharging).
Definition sensor.h:197
@ SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT
Max load current, in amps (negative=discharging).
Definition sensor.h:199
@ SENSOR_CHAN_ACCEL_Y
Acceleration on the Y axis, in m/s^2.
Definition sensor.h:70
@ SENSOR_CHAN_RPM
Revolutions per minute, in RPM.
Definition sensor.h:187
@ SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY
Full Available Capacity in mAh.
Definition sensor.h:211
@ SENSOR_CHAN_VOLTAGE
Voltage, in volts.
Definition sensor.h:161
@ SENSOR_CHAN_FLOW_RATE
Flow rate in liters per minute.
Definition sensor.h:156
@ SENSOR_CHAN_VOLUME
Flow volume in liters.
Definition sensor.h:158
@ SENSOR_CHAN_BLUE
Illuminance in blue spectrum, in lux.
Definition sensor.h:115
@ SENSOR_CHAN_LIGHT
Illuminance in visible spectrum, in lux.
Definition sensor.h:107
@ SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE
Design voltage of cell in V (max voltage).
Definition sensor.h:223
@ SENSOR_CHAN_PM_5_COUNT
Number of particles ≥ 5.0 µm per 0.1 liter of air.
Definition sensor.h:140
@ SENSOR_CHAN_ACCEL_Z
Acceleration on the Z axis, in m/s^2.
Definition sensor.h:72
@ SENSOR_CHAN_CO2
CO2 level, in parts per million (ppm).
Definition sensor.h:148
@ SENSOR_CHAN_GAUGE_STATE_OF_CHARGE
State of charge measurement in %.
Definition sensor.h:203
@ SENSOR_CHAN_POS_DXYZ
Position change on the X, Y and Z axis, in points.
Definition sensor.h:184
@ SENSOR_CHAN_GBIAS_XYZ
Gyroscope bias (X/Y/Z components in rad/s).
Definition sensor.h:233
@ SENSOR_CHAN_GAUGE_CYCLE_COUNT
Cycle count (total number of charge/discharge cycles).
Definition sensor.h:221
@ SENSOR_CHAN_GAUGE_TEMP
Gauge temperature.
Definition sensor.h:201
@ SENSOR_CHAN_POS_DY
Position change on the Y axis, in points.
Definition sensor.h:180
@ SENSOR_CHAN_GYRO_Z
Angular velocity around the Z axis, in rad/s.
Definition sensor.h:80
@ SENSOR_CHAN_PM_10_CF
PM10 concentration (standard particle, CF=1), in µg/m³
Definition sensor.h:124
@ SENSOR_CHAN_POS_DX
Position change on the X axis, in points.
Definition sensor.h:178
@ SENSOR_CHAN_GAUGE_AVG_POWER
Average power in mW.
Definition sensor.h:213
@ SENSOR_CHAN_GAUGE_TIME_TO_EMPTY
Time to empty in minutes.
Definition sensor.h:217
@ SENSOR_CHAN_PM_10
10 micro-meters Particulate Matter, in ug/m^3
Definition sensor.h:130
@ SENSOR_CHAN_ENCODER_REVOLUTIONS
Number of revolutions for quadrature decoder.
Definition sensor.h:239
@ SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY
Remaining Charge Capacity in mAh.
Definition sensor.h:207
@ SENSOR_CHAN_PM_1_0_COUNT
Number of particles ≥ 1.0 µm per 0.1 liter of air.
Definition sensor.h:136
@ SENSOR_CHAN_PM_0_3_COUNT
Number of particles ≥ 0.3 µm per 0.1 liter of air.
Definition sensor.h:132
@ SENSOR_CHAN_PM_10_COUNT
Number of particles ≥ 10.0 µm per 0.1 liter of air.
Definition sensor.h:142
@ SENSOR_CHAN_ALL
All channels.
Definition sensor.h:242
@ SENSOR_CHAN_GAUGE_VOLTAGE
Voltage, in volts.
Definition sensor.h:193
@ SENSOR_CHAN_PROX
Proximity.
Definition sensor.h:101
@ SENSOR_CHAN_COMMON_COUNT
Number of all common sensor channels.
Definition sensor.h:247
@ SENSOR_CHAN_PRIV_START
This and higher values are sensor specific.
Definition sensor.h:253
@ SENSOR_CHAN_GYRO_X
Angular velocity around the X axis, in rad/s.
Definition sensor.h:76
@ SENSOR_CHAN_GAS_RES
Gas sensor resistance in ohms.
Definition sensor.h:154
@ SENSOR_CHAN_HUMIDITY
Humidity, in percent.
Definition sensor.h:103
@ SENSOR_CHAN_DISTANCE
Distance.
Definition sensor.h:145
@ SENSOR_CHAN_IR
Illuminance in infra-red spectrum, in lux.
Definition sensor.h:109
@ SENSOR_CHAN_MAX
Maximum value describing a sensor channel type.
Definition sensor.h:258
@ SENSOR_CHAN_PM_2_5_COUNT
Number of particles ≥ 2.5 µm per 0.1 liter of air.
Definition sensor.h:138
@ SENSOR_CHAN_POS_DZ
Position change on the Z axis, in points.
Definition sensor.h:182
@ SENSOR_CHAN_RED
Illuminance in red spectrum, in lux.
Definition sensor.h:111
@ SENSOR_CHAN_ALTITUDE
Altitude, in meters.
Definition sensor.h:117
@ SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY
Nominal Available Capacity in mAh.
Definition sensor.h:209
@ SENSOR_CHAN_ACCEL_X
Acceleration on the X axis, in m/s^2.
Definition sensor.h:68
@ SENSOR_CHAN_VOC
VOC level, in parts per billion (ppb).
Definition sensor.h:152
static int64_t sign_extend_64(uint64_t value, uint8_t index)
Sign extend a 64 bit value using the index bit as sign bit.
Definition util.h:780
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:154
#define BIT64_MASK(n)
64-bit bit mask with bits 0 through n-1 (inclusive) set, or 0 if n is 0.
Definition util_macro.h:80
static int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
Arithmetic shift right.
Definition util.h:617
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
#define BIT_MASK(n)
Bit mask with bits 0 through n-1 (inclusive) set, or 0 if n is 0.
Definition util_macro.h:74
#define IN_RANGE(val, min, max)
Checks if a value is within range.
Definition util.h:558
#define EINVAL
Invalid argument.
Definition errno.h:61
#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 NULL
Definition iar_missing_defs.h:20
#define BUILD_ASSERT(EXPR, MSG...)
Definition llvm.h:51
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:513
A completion queue event.
Definition cqe.h:83
void * userdata
Associated userdata with operation.
Definition cqe.h:87
int32_t result
Result from operation.
Definition cqe.h:86
API that an RTIO IO device should implement.
Definition iodev.h:33
IO device submission queue entry.
Definition sqe.h:394
struct rtio_sqe sqe
Definition sqe.h:395
An IO device with a function table for submitting requests.
Definition iodev.h:48
void * data
Definition iodev.h:53
A submission queue event.
Definition sqe.h:304
void * userdata
User provided data which is returned upon operation completion.
Definition sqe.h:322
uint32_t buf_len
Length of buffer.
Definition sqe.h:328
const struct rtio_iodev * iodev
Device to operation on.
Definition sqe.h:313
const uint8_t * buf
Buffer to write from.
Definition sqe.h:329
An RTIO context containing what can be viewed as a pair of queues.
Definition rtio.h:70
Sensor Channel Specification.
Definition sensor.h:577
uint16_t chan_idx
A sensor channel index.
Definition sensor.h:579
uint16_t chan_type
A sensor channel type.
Definition sensor.h:578
Definition sensor.h:1021
uint64_t timestamp_ns
The timestamp at which the data was collected from the sensor.
Definition sensor.h:1023
int8_t shift
Shift value for all samples in the frame.
Definition sensor.h:1033
uint32_t num_channels
Definition sensor.h:1030
struct sensor_chan_spec channels[0]
Channels present in the frame.
Definition sensor.h:1039
Used for iterating over the data frames via the sensor_decoder_api.
Definition sensor.h:699
const struct sensor_decoder_api * decoder
Definition sensor.h:700
struct sensor_chan_spec channel
Definition sensor.h:702
const uint8_t * buffer
Definition sensor.h:701
uint32_t fit
Definition sensor.h:703
Decodes a single raw data buffer.
Definition sensor.h:609
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:635
int(* get_frame_count)(const uint8_t *buffer, struct sensor_chan_spec chan_spec, uint16_t *frame_count)
Get the frame_count for a specified chan_spec from the buffer.
Definition sensor.h:620
int(* decode)(const uint8_t *buffer, struct sensor_chan_spec chan_spec, uint32_t *fit, uint16_t max_count, void *data_out)
Decode up to max_count frames specified by chan_spec from the buffer.
Definition sensor.h:662
bool(* has_trigger)(const uint8_t *buffer, enum sensor_trigger_type trigger)
Check if the given trigger type is present.
Definition sensor.h:672
@driver_ops{Sensor}
Definition sensor.h:535
sensor_get_decoder_t get_decoder
@driver_ops_optional Get the sensor's decoder API.
Definition sensor.h:559
sensor_attr_set_t attr_set
@driver_ops_optional Set an attribute for a sensor.
Definition sensor.h:539
sensor_attr_get_t attr_get
@driver_ops_optional Get an attribute for a sensor.
Definition sensor.h:543
sensor_trigger_set_t trigger_set
@driver_ops_optional Activate a sensor's trigger and set the trigger handler.
Definition sensor.h:547
sensor_sample_fetch_t sample_fetch
@driver_ops_mandatory Fetch a sample from the sensor and store it in an internal driver buffer.
Definition sensor.h:551
sensor_channel_get_t channel_get
@driver_ops_mandatory Get a reading from a sensor device.
Definition sensor.h:555
sensor_submit_t submit
@driver_ops_optional Handler for RTIO submissions to this sensor.
Definition sensor.h:563
Definition sensor.h:759
struct sensor_chan_spec *const channels
Definition sensor.h:763
size_t count
Definition sensor.h:766
struct sensor_stream_trigger *const triggers
Definition sensor.h:764
const bool is_streaming
Definition sensor.h:761
const struct device * sensor
Definition sensor.h:760
const size_t max
Definition sensor.h:767
Definition sensor.h:745
enum sensor_stream_data_opt opt
Definition sensor.h:747
enum sensor_trigger_type trigger
Definition sensor.h:746
Sensor trigger spec.
Definition sensor.h:374
enum sensor_trigger_type type
Trigger type.
Definition sensor.h:376
enum sensor_channel chan
Channel the trigger is set on.
Definition sensor.h:378
Representation of a sensor readout value.
Definition sensor.h:56
int32_t val2
Fractional part of the value (in one-millionth parts).
Definition sensor.h:60
int32_t val1
Integer part of the value.
Definition sensor.h:58
Iterable sections helpers.
Misc utilities.
#define INT64_C(x)
Definition xcc.h:119