Zephyr API Documentation  2.7.0-rc2
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 <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
243
249
254};
255
264};
265
286 /* Hysteresis for trigger thresholds. */
310
315
321
326};
327
335typedef void (*sensor_trigger_handler_t)(const struct device *dev,
336 struct sensor_trigger *trigger);
337
344typedef int (*sensor_attr_set_t)(const struct device *dev,
345 enum sensor_channel chan,
346 enum sensor_attribute attr,
347 const struct sensor_value *val);
348
355typedef int (*sensor_attr_get_t)(const struct device *dev,
356 enum sensor_channel chan,
357 enum sensor_attribute attr,
358 struct sensor_value *val);
359
366typedef int (*sensor_trigger_set_t)(const struct device *dev,
367 const struct sensor_trigger *trig,
375typedef int (*sensor_sample_fetch_t)(const struct device *dev,
376 enum sensor_channel chan);
383typedef int (*sensor_channel_get_t)(const struct device *dev,
384 enum sensor_channel chan,
385 struct sensor_value *val);
386
387__subsystem struct sensor_driver_api {
393};
394
407__syscall int sensor_attr_set(const struct device *dev,
408 enum sensor_channel chan,
409 enum sensor_attribute attr,
410 const struct sensor_value *val);
411
412static inline int z_impl_sensor_attr_set(const struct device *dev,
413 enum sensor_channel chan,
414 enum sensor_attribute attr,
415 const struct sensor_value *val)
416{
417 const struct sensor_driver_api *api =
418 (const struct sensor_driver_api *)dev->api;
419
420 if (api->attr_set == NULL) {
421 return -ENOSYS;
422 }
423
424 return api->attr_set(dev, chan, attr, val);
425}
426
439__syscall int sensor_attr_get(const struct device *dev,
440 enum sensor_channel chan,
441 enum sensor_attribute attr,
442 struct sensor_value *val);
443
444static inline int z_impl_sensor_attr_get(const struct device *dev,
445 enum sensor_channel chan,
446 enum sensor_attribute attr,
447 struct sensor_value *val)
448{
449 const struct sensor_driver_api *api =
450 (const struct sensor_driver_api *)dev->api;
451
452 if (api->attr_get == NULL) {
453 return -ENOSYS;
454 }
455
456 return api->attr_get(dev, chan, attr, val);
457}
458
476static inline int sensor_trigger_set(const struct device *dev,
477 struct sensor_trigger *trig,
479{
480 const struct sensor_driver_api *api =
481 (const struct sensor_driver_api *)dev->api;
482
483 if (api->trigger_set == NULL) {
484 return -ENOSYS;
485 }
486
487 return api->trigger_set(dev, trig, handler);
488}
489
506__syscall int sensor_sample_fetch(const struct device *dev);
507
508static inline int z_impl_sensor_sample_fetch(const struct device *dev)
509{
510 const struct sensor_driver_api *api =
511 (const struct sensor_driver_api *)dev->api;
512
513 return api->sample_fetch(dev, SENSOR_CHAN_ALL);
514}
515
535__syscall int sensor_sample_fetch_chan(const struct device *dev,
536 enum sensor_channel type);
537
538static inline int z_impl_sensor_sample_fetch_chan(const struct device *dev,
539 enum sensor_channel type)
540{
541 const struct sensor_driver_api *api =
542 (const struct sensor_driver_api *)dev->api;
543
544 return api->sample_fetch(dev, type);
545}
546
568__syscall int sensor_channel_get(const struct device *dev,
569 enum sensor_channel chan,
570 struct sensor_value *val);
571
572static inline int z_impl_sensor_channel_get(const struct device *dev,
573 enum sensor_channel chan,
574 struct sensor_value *val)
575{
576 const struct sensor_driver_api *api =
577 (const struct sensor_driver_api *)dev->api;
578
579 return api->channel_get(dev, chan, val);
580}
581
585#define SENSOR_G 9806650LL
586
590#define SENSOR_PI 3141592LL
591
600static inline int32_t sensor_ms2_to_g(const struct sensor_value *ms2)
601{
602 int64_t micro_ms2 = ms2->val1 * 1000000LL + ms2->val2;
603
604 if (micro_ms2 > 0) {
605 return (micro_ms2 + SENSOR_G / 2) / SENSOR_G;
606 } else {
607 return (micro_ms2 - SENSOR_G / 2) / SENSOR_G;
608 }
609}
610
617static inline void sensor_g_to_ms2(int32_t g, struct sensor_value *ms2)
618{
619 ms2->val1 = ((int64_t)g * SENSOR_G) / 1000000LL;
620 ms2->val2 = ((int64_t)g * SENSOR_G) % 1000000LL;
621}
622
630static inline int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
631{
632 int64_t micro_rad_s = rad->val1 * 1000000LL + rad->val2;
633
634 if (micro_rad_s > 0) {
635 return (micro_rad_s * 180LL + SENSOR_PI / 2) / SENSOR_PI;
636 } else {
637 return (micro_rad_s * 180LL - SENSOR_PI / 2) / SENSOR_PI;
638 }
639}
640
647static inline void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
648{
649 rad->val1 = ((int64_t)d * SENSOR_PI / 180LL) / 1000000LL;
650 rad->val2 = ((int64_t)d * SENSOR_PI / 180LL) % 1000000LL;
651}
652
659static inline double sensor_value_to_double(const struct sensor_value *val)
660{
661 return (double)val->val1 + (double)val->val2 / 1000000;
662}
663
670static inline void sensor_value_from_double(struct sensor_value *val, double inp)
671{
672 val->val1 = (int32_t) inp;
673 val->val2 = (int32_t)(inp * 1000000) % 1000000;
674}
675
680#ifdef __cplusplus
681}
682#endif
683
684#include <syscalls/sensor.h>
685
686#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_H_ */
irp nz macro MOVR cc d
Definition: asm-macro-32-bit-gnu.h:11
System error numbers.
void
Definition: eswifi_shell.c:15
#define SENSOR_G
The value of gravitational constant in micro m/s^2.
Definition: sensor.h:585
static int32_t sensor_rad_to_degrees(const struct sensor_value *rad)
Helper function for converting radians to degrees.
Definition: sensor.h:630
sensor_trigger_type
Sensor trigger types.
Definition: sensor.h:203
sensor_attribute
Sensor attribute types.
Definition: sensor.h:269
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:344
static void sensor_value_from_double(struct sensor_value *val, double inp)
Helper function for converting double to struct sensor_value.
Definition: sensor.h:670
int(* sensor_sample_fetch_t)(const struct device *dev, enum sensor_channel chan)
Callback API for fetching data from a sensor.
Definition: sensor.h:375
static double sensor_value_to_double(const struct sensor_value *val)
Helper function for converting struct sensor_value to double.
Definition: sensor.h:659
static void sensor_degrees_to_rad(int32_t d, struct sensor_value *rad)
Helper function for converting degrees to radians.
Definition: sensor.h:647
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:355
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:617
#define SENSOR_PI
The value of constant PI in micros.
Definition: sensor.h:590
void(* sensor_trigger_handler_t)(const struct device *dev, struct sensor_trigger *trigger)
Callback API upon firing of a trigger.
Definition: sensor.h:335
static int sensor_trigger_set(const struct device *dev, struct sensor_trigger *trig, sensor_trigger_handler_t handler)
Activate a sensor's trigger and set the trigger handler.
Definition: sensor.h:476
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:383
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:600
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:366
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.
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:248
@ SENSOR_TRIG_COMMON_COUNT
Definition: sensor.h:242
@ SENSOR_TRIG_THRESHOLD
Definition: sensor.h:228
@ SENSOR_TRIG_MAX
Definition: sensor.h:253
@ 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:287
@ SENSOR_ATTR_FEATURE_MASK
Definition: sensor.h:307
@ SENSOR_ATTR_CALIB_TARGET
Definition: sensor.h:301
@ SENSOR_ATTR_OFFSET
Definition: sensor.h:296
@ SENSOR_ATTR_OVERSAMPLING
Definition: sensor.h:289
@ SENSOR_ATTR_UPPER_THRESH
Definition: sensor.h:278
@ SENSOR_ATTR_CONFIGURATION
Definition: sensor.h:303
@ SENSOR_ATTR_CALIBRATION
Definition: sensor.h:305
@ SENSOR_ATTR_COMMON_COUNT
Definition: sensor.h:314
@ SENSOR_ATTR_ALERT
Definition: sensor.h:309
@ SENSOR_ATTR_SLOPE_TH
Definition: sensor.h:280
@ SENSOR_ATTR_SAMPLING_FREQUENCY
Definition: sensor.h:274
@ SENSOR_ATTR_FULL_SCALE
Definition: sensor.h:291
@ SENSOR_ATTR_LOWER_THRESH
Definition: sensor.h:276
@ SENSOR_ATTR_SLOPE_DUR
Definition: sensor.h:285
@ SENSOR_ATTR_MAX
Definition: sensor.h:325
@ SENSOR_ATTR_PRIV_START
Definition: sensor.h:320
@ 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
__INT32_TYPE__ int32_t
Definition: stdint.h:44
#define INT16_MAX
Definition: stdint.h:17
__INT64_TYPE__ int64_t
Definition: stdint.h:45
Runtime device structure (in ROM) per driver instance.
Definition: device.h:367
const void * api
Definition: device.h:373
Definition: sensor.h:387
sensor_attr_set_t attr_set
Definition: sensor.h:388
sensor_attr_get_t attr_get
Definition: sensor.h:389
sensor_trigger_set_t trigger_set
Definition: sensor.h:390
sensor_sample_fetch_t sample_fetch
Definition: sensor.h:391
sensor_channel_get_t channel_get
Definition: sensor.h:392
Sensor trigger spec.
Definition: sensor.h:259
enum sensor_trigger_type type
Definition: sensor.h:261
enum sensor_channel chan
Definition: sensor.h:263
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