Zephyr Project API  3.2.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. */
315
320
326
331};
332
340typedef void (*sensor_trigger_handler_t)(const struct device *dev,
341 const struct sensor_trigger *trigger);
342
349typedef int (*sensor_attr_set_t)(const struct device *dev,
350 enum sensor_channel chan,
351 enum sensor_attribute attr,
352 const struct sensor_value *val);
353
360typedef int (*sensor_attr_get_t)(const struct device *dev,
361 enum sensor_channel chan,
362 enum sensor_attribute attr,
363 struct sensor_value *val);
364
371typedef int (*sensor_trigger_set_t)(const struct device *dev,
372 const struct sensor_trigger *trig,
380typedef int (*sensor_sample_fetch_t)(const struct device *dev,
381 enum sensor_channel chan);
388typedef int (*sensor_channel_get_t)(const struct device *dev,
389 enum sensor_channel chan,
390 struct sensor_value *val);
391
392__subsystem struct sensor_driver_api {
398};
399
412__syscall int sensor_attr_set(const struct device *dev,
413 enum sensor_channel chan,
414 enum sensor_attribute attr,
415 const struct sensor_value *val);
416
417static inline int z_impl_sensor_attr_set(const struct device *dev,
418 enum sensor_channel chan,
419 enum sensor_attribute attr,
420 const struct sensor_value *val)
421{
422 const struct sensor_driver_api *api =
423 (const struct sensor_driver_api *)dev->api;
424
425 if (api->attr_set == NULL) {
426 return -ENOSYS;
427 }
428
429 return api->attr_set(dev, chan, attr, val);
430}
431
444__syscall int sensor_attr_get(const struct device *dev,
445 enum sensor_channel chan,
446 enum sensor_attribute attr,
447 struct sensor_value *val);
448
449static inline int z_impl_sensor_attr_get(const struct device *dev,
450 enum sensor_channel chan,
451 enum sensor_attribute attr,
452 struct sensor_value *val)
453{
454 const struct sensor_driver_api *api =
455 (const struct sensor_driver_api *)dev->api;
456
457 if (api->attr_get == NULL) {
458 return -ENOSYS;
459 }
460
461 return api->attr_get(dev, chan, attr, val);
462}
463
481static inline int sensor_trigger_set(const struct device *dev,
482 const struct sensor_trigger *trig,
484{
485 const struct sensor_driver_api *api =
486 (const struct sensor_driver_api *)dev->api;
487
488 if (api->trigger_set == NULL) {
489 return -ENOSYS;
490 }
491
492 return api->trigger_set(dev, trig, handler);
493}
494
511__syscall int sensor_sample_fetch(const struct device *dev);
512
513static inline int z_impl_sensor_sample_fetch(const struct device *dev)
514{
515 const struct sensor_driver_api *api =
516 (const struct sensor_driver_api *)dev->api;
517
518 return api->sample_fetch(dev, SENSOR_CHAN_ALL);
519}
520
540__syscall int sensor_sample_fetch_chan(const struct device *dev,
541 enum sensor_channel type);
542
543static inline int z_impl_sensor_sample_fetch_chan(const struct device *dev,
544 enum sensor_channel type)
545{
546 const struct sensor_driver_api *api =
547 (const struct sensor_driver_api *)dev->api;
548
549 return api->sample_fetch(dev, type);
550}
551
573__syscall int sensor_channel_get(const struct device *dev,
574 enum sensor_channel chan,
575 struct sensor_value *val);
576
577static inline int z_impl_sensor_channel_get(const struct device *dev,
578 enum sensor_channel chan,
579 struct sensor_value *val)
580{
581 const struct sensor_driver_api *api =
582 (const struct sensor_driver_api *)dev->api;
583
584 return api->channel_get(dev, chan, val);
585}
586
590#define SENSOR_G 9806650LL
591
595#define SENSOR_PI 3141592LL
596
605static inline int32_t sensor_ms2_to_g(const struct sensor_value *ms2)
606{
607 int64_t micro_ms2 = ms2->val1 * 1000000LL + ms2->val2;
608
609 if (micro_ms2 > 0) {
610 return (micro_ms2 + SENSOR_G / 2) / SENSOR_G;
611 } else {
612 return (micro_ms2 - SENSOR_G / 2) / SENSOR_G;
613 }
614}
615
622static inline void sensor_g_to_ms2(int32_t g, struct sensor_value *ms2)
623{
624 ms2->val1 = ((int64_t)g * SENSOR_G) / 1000000LL;
625 ms2->val2 = ((int64_t)g * SENSOR_G) % 1000000LL;
626}
627
635static inline int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
636{
637 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
638
639 if (micro_rad_s > 0) {
640 return (micro_rad_s * 180LL + SENSOR_PI / 2) / SENSOR_PI;
641 } else {
642 return (micro_rad_s * 180LL - SENSOR_PI / 2) / SENSOR_PI;
643 }
644}
645
652static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
653{
654 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL) / 1000000LL;
655 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL;
656}
657
664static inline double sensor_value_to_double(const struct sensor_value *val)
665{
666 return (double)val->val1 + (double)val->val2 / 1000000;
667}
668
676static inline int sensor_value_from_double(struct sensor_value *val, double inp)
677{
678 if (inp < INT32_MIN || inp > INT32_MAX) {
679 return -ERANGE;
680 }
681
682 double val2 = (inp - (int32_t)inp) * 1000000.0;
683
684 if (val2 < INT32_MIN || val2 > INT32_MAX) {
685 return -ERANGE;
686 }
687
688 val->val1 = (int32_t)inp;
689 val->val2 = (int32_t)val2;
690
691 return 0;
692}
693
698#ifdef __cplusplus
699}
700#endif
701
702#include <syscalls/sensor.h>
703
704#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:590
static int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
Helper function for converting radians to degrees.
Definition: sensor.h:635
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:349
int(* sensor_sample_fetch_t)(const struct device *dev, enum sensor_channel chan)
Callback API for fetching data from a sensor.
Definition: sensor.h:380
static double sensor_value_to_double(const struct sensor_value *val)
Helper function for converting struct sensor_value to double.
Definition: sensor.h:664
static void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
Helper function for converting degrees to radians.
Definition: sensor.h:652
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:360
void(* sensor_trigger_handler_t)(const struct device *dev, const struct sensor_trigger *trigger)
Callback API upon firing of a trigger.
Definition: sensor.h:340
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:622
#define SENSOR_PI
The value of constant PI in micros.
Definition: sensor.h:595
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:481
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
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:388
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:605
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:371
int sensor_sample_fetch_chan(const struct device *dev, enum sensor_channel type)
Fetch a sample from the sensor and store it in an internal driver buffer.
int sensor_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:676
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_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:319
@ 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:330
@ SENSOR_ATTR_PRIV_START
Definition: sensor.h:325
@ 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:435
const void * api
Definition: device.h:441
Definition: sensor.h:392
sensor_attr_set_t attr_set
Definition: sensor.h:393
sensor_attr_get_t attr_get
Definition: sensor.h:394
sensor_trigger_set_t trigger_set
Definition: sensor.h:395
sensor_sample_fetch_t sample_fetch
Definition: sensor.h:396
sensor_channel_get_t channel_get
Definition: sensor.h:397
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