ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_driver_sensor.h
Go to the documentation of this file.
1 #ifndef RUUVI_DRIVER_SENSOR_H
2 #define RUUVI_DRIVER_SENSOR_H
58 #include "ruuvi_driver_error.h"
59 #include <stdbool.h>
60 #include <stddef.h>
61 #include <stdint.h>
62 
63 #define RD_SENSOR_INVALID_VALUE RD_FLOAT_INVALID
64 #define RD_SENSOR_INVALID_TIMSTAMP RD_UINT64_INVALID
65 
66 // Constants for sensor configuration and status
67 #define RD_SENSOR_CFG_DEFAULT (0U)
68 #define RD_SENSOR_CFG_CUSTOM_1 (0xC9U)
69 #define RD_SENSOR_CFG_CUSTOM_2 (0xCAU)
70 #define RD_SENSOR_CFG_CUSTOM_3 (0xCBU)
71 #define RD_SENSOR_CFG_CUSTOM_4 (0xCCU)
72 #define RD_SENSOR_CFG_CUSTOM_5 (0xCDU)
73 #define RD_SENSOR_CFG_CUSTOM_6 (0xCEU)
74 #define RD_SENSOR_ERR_INVALID (0xE0U)
75 #define RD_SENSOR_ERR_NOT_IMPLEMENTED (0xE1U)
76 #define RD_SENSOR_ERR_NOT_SUPPORTED (0xE2U)
77 #define RD_SENSOR_CFG_MIN (0xF0U)
78 #define RD_SENSOR_CFG_MAX (0xF1U)
79 #define RD_SENSOR_CFG_SLEEP (0xF2U)
80 #define RD_SENSOR_CFG_SINGLE (0xF3U)
81 #define RD_SENSOR_CFG_CONTINUOUS (0xF4U)
82 #define RD_SENSOR_CFG_NO_CHANGE (0xFFU)
83 
84 // DSP functions, complemented by DSP parameter
85 #define RD_SENSOR_DSP_LAST (0U)
86 #define RD_SENSOR_DSP_LOW_PASS (1U<<1U)
87 #define RD_SENSOR_DSP_HIGH_PASS (1U<<2U)
88 #define RD_SENSOR_DSP_OS (1U<<3U)
89 
91 #define RD_GPIO_TO_HANDLE(handle) ((((handle) >> 3U) & 0xE0U) + ((handle) & 0x1FU))
93 #define RD_HANDLE_TO_GPIO(handle) ((((handle) & 0xE0U) << 3U) + ((handle) & 0x1FU))
95 #define RD_HANDLE_UNUSED (0xFFU)
96 
97 #ifndef UNUSED_VARIABLE
98 # define UNUSED_VARIABLE(X) ((void)(X))
99 #endif
100 
104 typedef struct __attribute__ ( (packed, aligned (4)))
105 {
106  uint8_t samplerate;
107  uint8_t resolution;
108  uint8_t scale;
109  uint8_t dsp_function;
110  uint8_t dsp_parameter;
111  uint8_t mode;
112  uint8_t reserved0;
113  uint8_t reserved1;
114 }
116 
120 typedef enum
121 {
122  RD_BUS_NONE = 0U,
123  RD_BUS_SPI = 1U,
124  RD_BUS_I2C = 2U,
125  RD_BUS_UART = 3U,
126  RD_BUS_PDM = 4U,
127  RD_BUS_FAIL = 5U
129 
133 typedef struct
134 {
135  unsigned int acceleration_x_g : 1;
136  unsigned int acceleration_y_g : 1;
137  unsigned int acceleration_z_g : 1;
138  unsigned int co2_ppm : 1;
139  unsigned int gyro_x_dps : 1;
140  unsigned int gyro_y_dps : 1;
141  unsigned int gyro_z_dps : 1;
142  unsigned int humidity_rh : 1;
144  unsigned int luminosity : 1;
145  unsigned int magnetometer_x_g : 1;
146  unsigned int magnetometer_y_g : 1;
147  unsigned int magnetometer_z_g : 1;
148  unsigned int pm_1_ugm3 : 1;
149  unsigned int pm_2_ugm3 : 1;
150  unsigned int pm_4_ugm3 : 1;
151  unsigned int pm_10_ugm3 : 1;
152  unsigned int pressure_pa : 1;
153  unsigned int spl_dbz : 1;
154  unsigned int temperature_c : 1;
155  unsigned int voc_ppm : 1;
156  unsigned int voltage_v : 1;
157  unsigned int voltage_ratio : 1;
158  unsigned int reserved: 10;
160 
170 #define RD_SENSOR_ACC_X_FIELD ((rd_sensor_data_fields_t){.datas.acceleration_x_g=1})
172 #define RD_SENSOR_ACC_Y_FIELD ((rd_sensor_data_fields_t){.datas.acceleration_y_g=1})
174 #define RD_SENSOR_ACC_Z_FIELD ((rd_sensor_data_fields_t){.datas.acceleration_z_g=1})
176 #define RD_SENSOR_GYR_X_FIELD ((rd_sensor_data_fields_t){.datas.gyro_x_dps=1})
178 #define RD_SENSOR_GYR_Y_FIELD ((rd_sensor_data_fields_t){.datas.gyro_y_dps=1})
180 #define RD_SENSOR_GYR_Z_FIELD ((rd_sensor_data_fields_t){.datas.gyro_z_dps=1})
182 #define RD_SENSOR_HUMI_FIELD ((rd_sensor_data_fields_t){.datas.humidity_rh=1})
184 #define RD_SENSOR_PRES_FIELD ((rd_sensor_data_fields_t){.datas.pressure_pa=1})
186 #define RD_SENSOR_TEMP_FIELD ((rd_sensor_data_fields_t){.datas.temperature_c=1})
187 
188 
189 
201 typedef union // -V2514
202 {
203  uint32_t bitfield;
206 
214 typedef struct rd_sensor_data_t
215 {
216  uint64_t timestamp_ms;
221  float * data;
223 
225 typedef struct rd_sensor_t rd_sensor_t;
226 
244  p_sensor, const rd_bus_t bus, const uint8_t handle);
245 
256 typedef rd_status_t (*rd_sensor_setup_fp) (uint8_t * parameter);
257 
273 typedef rd_status_t (*rd_sensor_dsp_fp) (uint8_t * dsp_function,
274  uint8_t * dsp_parameter);
275 
291 typedef rd_status_t (*rd_sensor_data_fp) (rd_sensor_data_t * const p_data);
292 
305  const rd_sensor_t * const p_sensor,
306  rd_sensor_configuration_t * const p_configuration);
307 
321 typedef rd_status_t (*rd_sensor_fifo_read_fp) (size_t * const num_elements,
322  rd_sensor_data_t * const data);
323 
333 typedef rd_status_t (*rd_sensor_fifo_enable_fp) (const bool enable);
334 
361 typedef rd_status_t (*rd_sensor_level_interrupt_use_fp) (const bool enable,
362  float * limit_g);
363 
370 typedef uint64_t (*rd_sensor_timestamp_fp) (void);
371 
377 typedef struct rd_sensor_t
378 {
380  const char * name;
382  void * p_ctx;
423 } rd_sensor_t;
424 
429  sensor, rd_sensor_configuration_t * config);
430 
435  sensor, rd_sensor_configuration_t * config);
436 
445  const rd_sensor_timestamp_fp timestamp_fp);
446 
452 uint64_t rd_sensor_timestamp_get (void);
453 
464 void rd_sensor_initialize (rd_sensor_t * const p_sensor);
465 
472 void rd_sensor_uninitialize (rd_sensor_t * const p_sensor);
473 
480 bool rd_sensor_is_init (const rd_sensor_t * const sensor);
481 
513 void rd_sensor_data_populate (rd_sensor_data_t * const target,
514  const rd_sensor_data_t * const provided,
515  const rd_sensor_data_fields_t requested);
516 
525 float rd_sensor_data_parse (const rd_sensor_data_t * const provided,
526  const rd_sensor_data_fields_t requested);
527 
534 uint8_t rd_sensor_data_fieldcount (const rd_sensor_data_t * const target);
535 
550 void rd_sensor_data_set (rd_sensor_data_t * const target,
551  const rd_sensor_data_fields_t field,
552  const float value);
553 
561 rd_status_t validate_default_input_set (uint8_t * const input, const uint8_t mode);
562 
571 rd_status_t validate_default_input_get (uint8_t * const input);
572 
599 bool rd_sensor_has_valid_data (const rd_sensor_data_t * const target,
600  const uint8_t index);
601 
622  const uint8_t index);
623 
625 #endif
uint32_t rd_status_t
bitfield for representing errors
rd_status_t(* rd_sensor_init_fp)(rd_sensor_t *const p_sensor, const rd_bus_t bus, const uint8_t handle)
Initialize and uninitialize sensor. Init and uninit will setup sensor with function pointers....
bool rd_sensor_is_init(const rd_sensor_t *const sensor)
Check if given sensor structure is already initialized.
rd_status_t(* rd_sensor_level_interrupt_use_fp)(const bool enable, float *limit_g)
Enable level interrupt on sensor.
bool rd_sensor_has_valid_data(const rd_sensor_data_t *const target, const uint8_t index)
Check if sensor has valid data at given index.
rd_status_t(* rd_sensor_dsp_fp)(uint8_t *dsp_function, uint8_t *dsp_parameter)
Configure sensor digital signal processing. Takes DSP function and a DSP parameter as input,...
rd_status_t(* rd_sensor_data_fp)(rd_sensor_data_t *const p_data)
Read latest data from sensor registers Return latest data from sensor. Does not take a new sample,...
uint8_t rd_sensor_data_fieldcount(const rd_sensor_data_t *const target)
Count number of floats required for this data structure.
void rd_sensor_uninitialize(rd_sensor_t *const p_sensor)
Mark sensor as uninitialized by calling the generic initialization. Will not clear the name of the se...
rd_sensor_data_bitfield_t rd_sensor_field_type(const rd_sensor_data_t *const target, const uint8_t index)
Check the type of data at given index.
rd_status_t rd_sensor_timestamp_function_set(const rd_sensor_timestamp_fp timestamp_fp)
Setup timestamping. Set to NULL to disable timestamps.
rd_status_t rd_sensor_configuration_get(const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
Implementation of ref rd_configuration_fp.
void rd_sensor_initialize(rd_sensor_t *const p_sensor)
Initialize sensor struct with non-null pointers which return RD_ERROR_NOT_INITIALIZED.
rd_status_t(* rd_sensor_fifo_read_fp)(size_t *const num_elements, rd_sensor_data_t *const data)
Read First-in-first-out (FIFO) buffer in sensor. Reads up to num_elements data points from FIFO and p...
rd_status_t validate_default_input_set(uint8_t *const input, const uint8_t mode)
Validate that given setting can be set on a sensor which supports only default value.
rd_status_t(* rd_sensor_setup_fp)(uint8_t *parameter)
Setup a parameter of a sensor. The function will modify the pointed data to the actual value which wa...
void rd_sensor_data_populate(rd_sensor_data_t *const target, const rd_sensor_data_t *const provided, const rd_sensor_data_fields_t requested)
Populate given target data with data provided by sensor as requested.
rd_status_t(* rd_sensor_fifo_enable_fp)(const bool enable)
Enable FIFO or FIFO interrupt full interrupt on sensor. FIFO interrupt Triggers an interrupt once FIF...
rd_status_t(* rd_configuration_fp)(const rd_sensor_t *const p_sensor, rd_sensor_configuration_t *const p_configuration)
Convenience function to write/read entire configuration in one call. Modifies input parameters to act...
rd_status_t rd_sensor_configuration_set(const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
Implementation of ref rd_configuration_fp.
rd_bus_t
Type of bus sensor uses.
void rd_sensor_data_set(rd_sensor_data_t *const target, const rd_sensor_data_fields_t field, const float value)
Set a desired value to target data.
struct rd_sensor_t rd_sensor_t
Forward declare type definition of sensor structure.
uint64_t rd_sensor_timestamp_get(void)
Calls the timestamp function and returns its value.
rd_status_t validate_default_input_get(uint8_t *const input)
Validate and get input when only allowed value is default.
struct rd_sensor_data_t rd_sensor_data_t
Generic sensor data struct.
float rd_sensor_data_parse(const rd_sensor_data_t *const provided, const rd_sensor_data_fields_t requested)
Parse data from provided struct.
uint64_t(* rd_sensor_timestamp_fp)(void)
Return number of milliseconds since the start of RTC.
@ RD_BUS_SPI
SPI bus.
@ RD_BUS_NONE
No bus, internal to IC.
@ RD_BUS_FAIL
Test behaviour on invalid bus with this value.
@ RD_BUS_UART
UART bus.
@ RD_BUS_PDM
PDM bus.
@ RD_BUS_I2C
I2C bus.
Ruuvi error codes and error check function.
All sensors must implement configuration functions which accept this struct.
uint8_t reserved1
Reserved for future use.
uint8_t resolution
Resolution, in bits.
uint8_t mode
Mode, RD_SENSOR_SLEEP, _SINGLE, _CONTINOUS.
uint8_t reserved0
Reserved for future use.
uint8_t samplerate
Samplerate, in Hz.
uint8_t scale
Scale, in relevant Si-unit.
uint8_t dsp_parameter
Parameter to DSP functions.
uint8_t dsp_function
DSP function, one of RD_SENSOR_DSP_*.
Bitfield to describe related sensor data.
unsigned int luminosity
Light level, dimensionless. Comparable only between identical devices.
unsigned int reserved
Reserved bits, force remainder of bitfield to 0.
unsigned int humidity_rh
Relative humidity, %.
unsigned int magnetometer_y_g
Magnetic flux along Y-axis, Gauss.
unsigned int gyro_z_dps
Rotation along Z-axis, degrees per second.
unsigned int acceleration_x_g
Acceleration along X-axis, gravities.
unsigned int pm_10_ugm3
Coarse particulate matter, microgram per m^3.
unsigned int pm_4_ugm3
Medium particulate matter, microgram per m^3.
unsigned int magnetometer_z_g
Magnetic flux along Z-axis, Gauss.
unsigned int pm_1_ugm3
Ultra-fine particulate matter, microgram per m^3.
unsigned int temperature_c
Temperature, celcius.
unsigned int voltage_v
Voltage, volts.
unsigned int gyro_y_dps
Rotation along Y-axis, degrees per second.
unsigned int gyro_x_dps
Rotation along X-axis, degrees per second.
unsigned int pressure_pa
Pressure, pascals.
unsigned int co2_ppm
CO2, Parts per million.
unsigned int pm_2_ugm3
Fine particulate matter, microgram per m^3.
unsigned int magnetometer_x_g
Magnetic flux along X-axis, Gauss.
unsigned int acceleration_z_g
Acceleration along Z-axis, gravities.
unsigned int acceleration_y_g
Acceleration along Y-axis, gravities.
unsigned int voltage_ratio
Voltage, ratio to maximum.
unsigned int spl_dbz
Unweighted sound pressure level.
unsigned int voc_ppm
Volatile organic compounds, parts per million.
Generic sensor data struct.
float * data
Data of sensor. Must contain as many elements as fields has bits set.
uint64_t timestamp_ms
Timestamp of the event, rd_sensor_timestamp_get.
rd_sensor_data_fields_t valid
Listing of valid data in this sample.
rd_sensor_data_fields_t fields
Description of datafields which may be contained in this sample.
Interface to sensor. Some sensors can implement additional functions. The additional functions are de...
rd_sensor_data_fp data_get
rd_sensor_data_fp
rd_configuration_fp configuration_set
rd_configuration_fp
rd_sensor_setup_fp samplerate_get
rd_sensor_setup_fp
rd_sensor_setup_fp resolution_get
rd_sensor_setup_fp
rd_sensor_init_fp uninit
rd_sensor_init_fp
rd_configuration_fp configuration_get
rd_configuration_fp
void * p_ctx
handle for sensor internal context
rd_sensor_dsp_fp dsp_get
rd_sensor_dsp_fp
rd_sensor_data_fields_t provides
Description of data fields the sensor is able to provide.
rd_sensor_dsp_fp dsp_set
rd_sensor_dsp_fp
rd_sensor_setup_fp mode_set
rd_sensor_setup_fp
const char * name
Sensor human-readable name. Should be at most 8 bytes long.
rd_sensor_fifo_read_fp fifo_read
rd_sensor_level_interrupt_use_fp
rd_sensor_fifo_enable_fp fifo_interrupt_enable
rd_sensor_level_interrupt_use_fp
rd_sensor_level_interrupt_use_fp level_interrupt_set
rd_sensor_level_interrupt_use_fp
rd_sensor_setup_fp mode_get
rd_sensor_setup_fp
rd_sensor_setup_fp samplerate_set
rd_sensor_setup_fp
rd_sensor_fifo_enable_fp fifo_enable
rd_sensor_fifo_enable_fp
rd_sensor_init_fp init
rd_sensor_init_fp
rd_sensor_setup_fp scale_set
rd_sensor_setup_fp
rd_sensor_setup_fp resolution_set
rd_sensor_setup_fp
rd_sensor_setup_fp scale_get
rd_sensor_setup_fp
Union to access sensor data.
uint32_t bitfield
Bitfield used to access sensor data.
rd_sensor_data_bitfield_t datas
Structured data field.