Zephyr Project API  3.3.0
A Scalable Open Source RTOS
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 <zephyr/types.h>
23#include <zephyr/device.h>
24#include <errno.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
48};
49
103
112
119
126
129
132
139
142
179
182
187
193
198};
199
229
232
235
238
241
248
254
259};
260
269};
270
291 /* Hysteresis for trigger thresholds. */
325
331
336};
337
345typedef void (*sensor_trigger_handler_t)(const struct device *dev,
346 const struct sensor_trigger *trigger);
347
354typedef int (*sensor_attr_set_t)(const struct device *dev,
355 enum sensor_channel chan,
356 enum sensor_attribute attr,
357 const struct sensor_value *val);
358
365typedef int (*sensor_attr_get_t)(const struct device *dev,
366 enum sensor_channel chan,
367 enum sensor_attribute attr,
368 struct sensor_value *val);
369
376typedef int (*sensor_trigger_set_t)(const struct device *dev,
377 const struct sensor_trigger *trig,
385typedef int (*sensor_sample_fetch_t)(const struct device *dev,
386 enum sensor_channel chan);
393typedef int (*sensor_channel_get_t)(const struct device *dev,
394 enum sensor_channel chan,
395 struct sensor_value *val);
396
397__subsystem struct sensor_driver_api {
403};
404
417__syscall int sensor_attr_set(const struct device *dev,
418 enum sensor_channel chan,
419 enum sensor_attribute attr,
420 const struct sensor_value *val);
421
422static inline int z_impl_sensor_attr_set(const struct device *dev,
423 enum sensor_channel chan,
424 enum sensor_attribute attr,
425 const struct sensor_value *val)
426{
427 const struct sensor_driver_api *api =
428 (const struct sensor_driver_api *)dev->api;
429
430 if (api->attr_set == NULL) {
431 return -ENOSYS;
432 }
433
434 return api->attr_set(dev, chan, attr, val);
435}
436
449__syscall int sensor_attr_get(const struct device *dev,
450 enum sensor_channel chan,
451 enum sensor_attribute attr,
452 struct sensor_value *val);
453
454static inline int z_impl_sensor_attr_get(const struct device *dev,
455 enum sensor_channel chan,
456 enum sensor_attribute attr,
457 struct sensor_value *val)
458{
459 const struct sensor_driver_api *api =
460 (const struct sensor_driver_api *)dev->api;
461
462 if (api->attr_get == NULL) {
463 return -ENOSYS;
464 }
465
466 return api->attr_get(dev, chan, attr, val);
467}
468
486static inline int sensor_trigger_set(const struct device *dev,
487 const struct sensor_trigger *trig,
489{
490 const struct sensor_driver_api *api =
491 (const struct sensor_driver_api *)dev->api;
492
493 if (api->trigger_set == NULL) {
494 return -ENOSYS;
495 }
496
497 return api->trigger_set(dev, trig, handler);
498}
499
516__syscall int sensor_sample_fetch(const struct device *dev);
517
518static inline int z_impl_sensor_sample_fetch(const struct device *dev)
519{
520 const struct sensor_driver_api *api =
521 (const struct sensor_driver_api *)dev->api;
522
523 return api->sample_fetch(dev, SENSOR_CHAN_ALL);
524}
525
545__syscall int sensor_sample_fetch_chan(const struct device *dev,
546 enum sensor_channel type);
547
548static inline int z_impl_sensor_sample_fetch_chan(const struct device *dev,
549 enum sensor_channel type)
550{
551 const struct sensor_driver_api *api =
552 (const struct sensor_driver_api *)dev->api;
553
554 return api->sample_fetch(dev, type);
555}
556
578__syscall int sensor_channel_get(const struct device *dev,
579 enum sensor_channel chan,
580 struct sensor_value *val);
581
582static inline int z_impl_sensor_channel_get(const struct device *dev,
583 enum sensor_channel chan,
584 struct sensor_value *val)
585{
586 const struct sensor_driver_api *api =
587 (const struct sensor_driver_api *)dev->api;
588
589 return api->channel_get(dev, chan, val);
590}
591
595#define SENSOR_G 9806650LL
596
600#define SENSOR_PI 3141592LL
601
610static inline int32_t sensor_ms2_to_g(const struct sensor_value *ms2)
611{
612 int64_t micro_ms2 = ms2->val1 * 1000000LL + ms2->val2;
613
614 if (micro_ms2 > 0) {
615 return (micro_ms2 + SENSOR_G / 2) / SENSOR_G;
616 } else {
617 return (micro_ms2 - SENSOR_G / 2) / SENSOR_G;
618 }
619}
620
627static inline void sensor_g_to_ms2(int32_t g, struct sensor_value *ms2)
628{
629 ms2->val1 = ((int64_t)g * SENSOR_G) / 1000000LL;
630 ms2->val2 = ((int64_t)g * SENSOR_G) % 1000000LL;
631}
632
641static inline int32_t sensor_ms2_to_ug(const struct sensor_value *ms2)
642{
643 int64_t micro_ms2 = ms2->val1 * 1000000LL + ms2->val2;
644
645 return (micro_ms2 * 1000000LL) / SENSOR_G;
646}
647
654static inline void sensor_ug_to_ms2(int32_t ug, struct sensor_value *ms2)
655{
656 ms2->val1 = ((int64_t)ug * SENSOR_G / 1000000LL) / 1000000LL;
657 ms2->val2 = ((int64_t)ug * SENSOR_G / 1000000LL) % 1000000LL;
658}
659
667static inline int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
668{
669 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
670
671 if (micro_rad_s > 0) {
672 return (micro_rad_s * 180LL + SENSOR_PI / 2) / SENSOR_PI;
673 } else {
674 return (micro_rad_s * 180LL - SENSOR_PI / 2) / SENSOR_PI;
675 }
676}
677
684static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
685{
686 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL) / 1000000LL;
687 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL;
688}
689
701static inline int32_t sensor_rad_to_10udegrees(const struct sensor_value *rad)
702{
703 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
704
705 return (micro_rad_s * 180LL * 100000LL) / SENSOR_PI;
706}
707
714static inline void sensor_10udegrees_to_rad(int32_t d, struct sensor_value *rad)
715{
716 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL / 100000LL) / 1000000LL;
717 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL / 100000LL) % 1000000LL;
718}
719
726static inline double sensor_value_to_double(const struct sensor_value *val)
727{
728 return (double)val->val1 + (double)val->val2 / 1000000;
729}
730
738static inline int sensor_value_from_double(struct sensor_value *val, double inp)
739{
740 if (inp < INT32_MIN || inp > INT32_MAX) {
741 return -ERANGE;
742 }
743
744 double val2 = (inp - (int32_t)inp) * 1000000.0;
745
746 if (val2 < INT32_MIN || val2 > INT32_MAX) {
747 return -ERANGE;
748 }
749
750 val->val1 = (int32_t)inp;
751 val->val2 = (int32_t)val2;
752
753 return 0;
754}
755
756#ifdef CONFIG_SENSOR_INFO
757
758struct sensor_info {
759 const struct device *dev;
760 const char *vendor;
761 const char *model;
762 const char *friendly_name;
763};
764
765#define SENSOR_INFO_INITIALIZER(_dev, _vendor, _model, _friendly_name) \
766 { \
767 .dev = _dev, \
768 .vendor = _vendor, \
769 .model = _model, \
770 .friendly_name = _friendly_name, \
771 }
772
773#define SENSOR_INFO_DEFINE(name, ...) \
774 static const STRUCT_SECTION_ITERABLE(sensor_info, name) = \
775 SENSOR_INFO_INITIALIZER(__VA_ARGS__)
776
777#define SENSOR_INFO_DT_NAME(node_id) \
778 _CONCAT(__sensor_info, DEVICE_DT_NAME_GET(node_id))
779
780#define SENSOR_INFO_DT_DEFINE(node_id) \
781 SENSOR_INFO_DEFINE(SENSOR_INFO_DT_NAME(node_id), \
782 DEVICE_DT_GET(node_id), \
783 DT_NODE_VENDOR_OR(node_id, NULL), \
784 DT_NODE_MODEL_OR(node_id, NULL), \
785 DT_PROP_OR(node_id, friendly_name, NULL)) \
786
787#else
788
789#define SENSOR_INFO_DEFINE(name, ...)
790#define SENSOR_INFO_DT_DEFINE(node_id)
791
792#endif /* CONFIG_SENSOR_INFO */
793
821#define SENSOR_DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
822 data_ptr, cfg_ptr, level, prio, \
823 api_ptr, ...) \
824 DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
825 data_ptr, cfg_ptr, level, prio, \
826 api_ptr, __VA_ARGS__); \
827 \
828 SENSOR_INFO_DT_DEFINE(node_id);
829
839#define SENSOR_DEVICE_DT_INST_DEFINE(inst, ...) \
840 SENSOR_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
841
849{
850 return ((int64_t)val->val1 * 1000) + val->val2 / 1000;
851}
852
860{
861 return ((int64_t)val->val1 * 1000000) + val->val2;
862}
863
868#ifdef __cplusplus
869}
870#endif
871
872#include <syscalls/sensor.h>
873
874#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_H_ */
irp nz macro MOVR cc d
Definition: asm-macro-32-bit-gnu.h:11
System error numbers.
#define SENSOR_G
The value of gravitational constant in micro m/s^2.
Definition: sensor.h:595
static int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
Helper function for converting radians to degrees.
Definition: sensor.h:667
sensor_trigger_type
Sensor trigger types.
Definition: sensor.h:203
sensor_attribute
Sensor attribute types.
Definition: sensor.h:274
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:354
int(* sensor_sample_fetch_t)(const struct device *dev, enum sensor_channel chan)
Callback API for fetching data from a sensor.
Definition: sensor.h:385
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:654
static double sensor_value_to_double(const struct sensor_value *val)
Helper function for converting struct sensor_value to double.
Definition: sensor.h:726
static void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
Helper function for converting degrees to radians.
Definition: sensor.h:684
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:641
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:365
void(* sensor_trigger_handler_t)(const struct device *dev, const struct sensor_trigger *trigger)
Callback API upon firing of a trigger.
Definition: sensor.h:345
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:627
#define SENSOR_PI
The value of constant PI in micros.
Definition: sensor.h:600
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:486
static int64_t sensor_value_to_milli(struct sensor_value *val)
Helper function for converting struct sensor_value to integer milli units.
Definition: sensor.h:848
static int64_t sensor_value_to_micro(struct sensor_value *val)
Helper function for converting struct sensor_value to integer micro units.
Definition: sensor.h:859
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:53
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:714
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:393
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:610
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:376
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 int32_t sensor_rad_to_10udegrees(const struct sensor_value *rad)
Helper function for converting radians to 10 micro degrees.
Definition: sensor.h:701
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:738
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
Definition: sensor.h:219
@ SENSOR_TRIG_NEAR_FAR
Definition: sensor.h:221
@ SENSOR_TRIG_FREEFALL
Definition: sensor.h:237
@ SENSOR_TRIG_PRIV_START
Definition: sensor.h:253
@ SENSOR_TRIG_MOTION
Definition: sensor.h:240
@ SENSOR_TRIG_STATIONARY
Definition: sensor.h:243
@ SENSOR_TRIG_COMMON_COUNT
Definition: sensor.h:247
@ SENSOR_TRIG_THRESHOLD
Definition: sensor.h:228
@ SENSOR_TRIG_MAX
Definition: sensor.h:258
@ SENSOR_TRIG_DOUBLE_TAP
Definition: sensor.h:234
@ SENSOR_TRIG_TIMER
Definition: sensor.h:208
@ SENSOR_TRIG_TAP
Definition: sensor.h:231
@ SENSOR_TRIG_DATA_READY
Definition: sensor.h:210
@ SENSOR_ATTR_HYSTERESIS
Definition: sensor.h:292
@ SENSOR_ATTR_FEATURE_MASK
Definition: sensor.h:312
@ SENSOR_ATTR_CALIB_TARGET
Definition: sensor.h:306
@ SENSOR_ATTR_OFFSET
Definition: sensor.h:301
@ SENSOR_ATTR_OVERSAMPLING
Definition: sensor.h:294
@ SENSOR_ATTR_FF_DUR
Definition: sensor.h:320
@ SENSOR_ATTR_UPPER_THRESH
Definition: sensor.h:283
@ SENSOR_ATTR_CONFIGURATION
Definition: sensor.h:308
@ SENSOR_ATTR_CALIBRATION
Definition: sensor.h:310
@ SENSOR_ATTR_COMMON_COUNT
Definition: sensor.h:324
@ SENSOR_ATTR_ALERT
Definition: sensor.h:314
@ SENSOR_ATTR_SLOPE_TH
Definition: sensor.h:285
@ SENSOR_ATTR_SAMPLING_FREQUENCY
Definition: sensor.h:279
@ SENSOR_ATTR_FULL_SCALE
Definition: sensor.h:296
@ SENSOR_ATTR_LOWER_THRESH
Definition: sensor.h:281
@ SENSOR_ATTR_SLOPE_DUR
Definition: sensor.h:290
@ SENSOR_ATTR_MAX
Definition: sensor.h:335
@ SENSOR_ATTR_PRIV_START
Definition: sensor.h:330
@ SENSOR_CHAN_GAUGE_STATE_OF_HEALTH
Definition: sensor.h:166
@ SENSOR_CHAN_PM_1_0
Definition: sensor.h:105
@ SENSOR_CHAN_DIE_TEMP
Definition: sensor.h:79
@ SENSOR_CHAN_PRESS
Definition: sensor.h:83
@ SENSOR_CHAN_GAUGE_TIME_TO_FULL
Definition: sensor.h:170
@ SENSOR_CHAN_ACCEL_XYZ
Definition: sensor.h:61
@ SENSOR_CHAN_MAGN_X
Definition: sensor.h:71
@ SENSOR_CHAN_CURRENT
Definition: sensor.h:123
@ SENSOR_CHAN_GYRO_XYZ
Definition: sensor.h:69
@ SENSOR_CHAN_GREEN
Definition: sensor.h:98
@ SENSOR_CHAN_MAGN_Z
Definition: sensor.h:75
@ SENSOR_CHAN_MAGN_Y
Definition: sensor.h:73
@ SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE
Definition: sensor.h:176
@ SENSOR_CHAN_POWER
Definition: sensor.h:125
@ SENSOR_CHAN_PM_2_5
Definition: sensor.h:107
@ SENSOR_CHAN_RESISTANCE
Definition: sensor.h:128
@ SENSOR_CHAN_GAUGE_AVG_CURRENT
Definition: sensor.h:146
@ SENSOR_CHAN_GYRO_Y
Definition: sensor.h:65
@ SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT
Definition: sensor.h:178
@ SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY
Definition: sensor.h:156
@ SENSOR_CHAN_ROTATION
Definition: sensor.h:131
@ SENSOR_CHAN_AMBIENT_TEMP
Definition: sensor.h:81
@ SENSOR_CHAN_MAGN_XYZ
Definition: sensor.h:77
@ SENSOR_CHAN_GAUGE_STDBY_CURRENT
Definition: sensor.h:148
@ SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT
Definition: sensor.h:150
@ SENSOR_CHAN_ACCEL_Y
Definition: sensor.h:57
@ SENSOR_CHAN_RPM
Definition: sensor.h:141
@ SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY
Definition: sensor.h:162
@ SENSOR_CHAN_VOLTAGE
Definition: sensor.h:121
@ SENSOR_CHAN_BLUE
Definition: sensor.h:100
@ SENSOR_CHAN_LIGHT
Definition: sensor.h:92
@ SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE
Definition: sensor.h:174
@ SENSOR_CHAN_ACCEL_Z
Definition: sensor.h:59
@ SENSOR_CHAN_CO2
Definition: sensor.h:114
@ SENSOR_CHAN_GAUGE_STATE_OF_CHARGE
Definition: sensor.h:154
@ SENSOR_CHAN_GAUGE_CYCLE_COUNT
Definition: sensor.h:172
@ SENSOR_CHAN_GAUGE_TEMP
Definition: sensor.h:152
@ SENSOR_CHAN_POS_DY
Definition: sensor.h:136
@ SENSOR_CHAN_GYRO_Z
Definition: sensor.h:67
@ SENSOR_CHAN_POS_DX
Definition: sensor.h:134
@ SENSOR_CHAN_GAUGE_AVG_POWER
Definition: sensor.h:164
@ SENSOR_CHAN_GAUGE_TIME_TO_EMPTY
Definition: sensor.h:168
@ SENSOR_CHAN_PM_10
Definition: sensor.h:109
@ SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY
Definition: sensor.h:158
@ SENSOR_CHAN_ALL
Definition: sensor.h:181
@ SENSOR_CHAN_GAUGE_VOLTAGE
Definition: sensor.h:144
@ SENSOR_CHAN_PROX
Definition: sensor.h:88
@ SENSOR_CHAN_COMMON_COUNT
Definition: sensor.h:186
@ SENSOR_CHAN_PRIV_START
Definition: sensor.h:192
@ SENSOR_CHAN_GYRO_X
Definition: sensor.h:63
@ SENSOR_CHAN_GAS_RES
Definition: sensor.h:118
@ SENSOR_CHAN_HUMIDITY
Definition: sensor.h:90
@ SENSOR_CHAN_DISTANCE
Definition: sensor.h:111
@ SENSOR_CHAN_IR
Definition: sensor.h:94
@ SENSOR_CHAN_MAX
Definition: sensor.h:197
@ SENSOR_CHAN_POS_DZ
Definition: sensor.h:138
@ SENSOR_CHAN_RED
Definition: sensor.h:96
@ SENSOR_CHAN_ALTITUDE
Definition: sensor.h:102
@ SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY
Definition: sensor.h:160
@ SENSOR_CHAN_ACCEL_X
Definition: sensor.h:55
@ SENSOR_CHAN_VOC
Definition: sensor.h:116
#define ENOSYS
Definition: errno.h:83
#define ERANGE
Definition: errno.h:73
__INT32_TYPE__ int32_t
Definition: stdint.h:74
#define INT32_MAX
Definition: stdint.h:18
#define INT16_MAX
Definition: stdint.h:17
__INT64_TYPE__ int64_t
Definition: stdint.h:75
Runtime device structure (in ROM) per driver instance.
Definition: device.h:378
const void * api
Definition: device.h:384
Definition: sensor.h:397
sensor_attr_set_t attr_set
Definition: sensor.h:398
sensor_attr_get_t attr_get
Definition: sensor.h:399
sensor_trigger_set_t trigger_set
Definition: sensor.h:400
sensor_sample_fetch_t sample_fetch
Definition: sensor.h:401
sensor_channel_get_t channel_get
Definition: sensor.h:402
Sensor trigger spec.
Definition: sensor.h:264
enum sensor_trigger_type type
Definition: sensor.h:266
enum sensor_channel chan
Definition: sensor.h:268
Representation of a sensor readout value.
Definition: sensor.h:43
int32_t val2
Definition: sensor.h:47
int32_t val1
Definition: sensor.h:45
static void handler(struct k_timer *timer)
Definition: main.c:19