ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_driver_sensor.h File Reference

Ruuvi sensor interface Lifecycle: Beta More...

#include "ruuvi_driver_error.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  rd_sensor_configuration_t
 All sensors must implement configuration functions which accept this struct. More...
 
struct  rd_sensor_data_bitfield_t
 Bitfield to describe related sensor data. More...
 
union  rd_sensor_data_fields_t
 Union to access sensor data. More...
 
struct  rd_sensor_data_t
 Generic sensor data struct. More...
 
struct  rd_sensor_t
 Interface to sensor. Some sensors can implement additional functions. The additional functions are defined in the interface of the sensor. More...
 

Macros

#define RD_SENSOR_INVALID_VALUE   RD_FLOAT_INVALID
 Signal this sensor value is erroneous. More...
 
#define RD_SENSOR_INVALID_TIMSTAMP   RD_UINT64_INVALID
 Signal this timestamp value is erroneous. More...
 
#define RD_SENSOR_CFG_DEFAULT   (0U)
 Default value, always valid for the sensor. More...
 
#define RD_SENSOR_CFG_CUSTOM_1   (0xC9U)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_2   (0xCAU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_3   (0xCBU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_4   (0xCCU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_5   (0xCDU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_CFG_CUSTOM_6   (0xCEU)
 Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values. More...
 
#define RD_SENSOR_ERR_INVALID   (0xE0U)
 Error code, given parameter is invalid. More...
 
#define RD_SENSOR_ERR_NOT_IMPLEMENTED   (0xE1U)
 Error code, given parameter is not implemented (todo) More...
 
#define RD_SENSOR_ERR_NOT_SUPPORTED   (0xE2U)
 Error code, given parameter is not supported by sensor. More...
 
#define RD_SENSOR_CFG_MIN   (0xF0U)
 Configure smallest supported and implemented value. More...
 
#define RD_SENSOR_CFG_MAX   (0xF1U)
 Configure largest supported and implemented value. More...
 
#define RD_SENSOR_CFG_SLEEP   (0xF2U)
 Sensor should go to sleep immediately. More...
 
#define RD_SENSOR_CFG_SINGLE   (0xF3U)
 Sensor should go to sleep after single measurement. More...
 
#define RD_SENSOR_CFG_CONTINUOUS   (0xF4U)
 Sensor will keep sampling at defined sample rate. More...
 
#define RD_SENSOR_CFG_NO_CHANGE   (0xFFU)
 Do not change configured value. More...
 
#define RD_SENSOR_DSP_LAST   (0U)
 Return last value from sensor. Parameter: No effect. Use default. More...
 
#define RD_SENSOR_DSP_LOW_PASS   (1U<<1U)
 Low pass sensor values Parameter: coefficient. More...
 
#define RD_SENSOR_DSP_HIGH_PASS   (1U<<2U)
 High pass sensor values Parameter: coefficient. More...
 
#define RD_SENSOR_DSP_OS   (1U<<3U)
 Oversample sensor values. Parameter: Number of samples. More...
 
#define RD_GPIO_TO_HANDLE(handle)   ((((handle) >> 3U) & 0xE0U) + ((handle) & 0x1FU))
 convert Ruuvi GPIO into uint8_t More...
 
#define RD_HANDLE_TO_GPIO(handle)   ((((handle) & 0xE0U) << 3U) + ((handle) & 0x1FU))
 convert uint8_t into Ruuvi GPIO More...
 
#define RD_HANDLE_UNUSED   (0xFFU)
 Mark sensor as unused with this handle. More...
 
#define UNUSED_VARIABLE(X)   ((void)(X))
 
#define RD_SENSOR_ACC_X_FIELD   ((rd_sensor_data_fields_t){.datas.acceleration_x_g=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_ACC_Y_FIELD   ((rd_sensor_data_fields_t){.datas.acceleration_y_g=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_ACC_Z_FIELD   ((rd_sensor_data_fields_t){.datas.acceleration_z_g=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_GYR_X_FIELD   ((rd_sensor_data_fields_t){.datas.gyro_x_dps=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_GYR_Y_FIELD   ((rd_sensor_data_fields_t){.datas.gyro_y_dps=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_GYR_Z_FIELD   ((rd_sensor_data_fields_t){.datas.gyro_z_dps=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_HUMI_FIELD   ((rd_sensor_data_fields_t){.datas.humidity_rh=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_PRES_FIELD   ((rd_sensor_data_fields_t){.datas.pressure_pa=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 
#define RD_SENSOR_TEMP_FIELD   ((rd_sensor_data_fields_t){.datas.temperature_c=1})
 Shorthand for calling rd_sensor_data_parse(p_data, FIELD) More...
 

Typedefs

typedef struct rd_sensor_data_t rd_sensor_data_t
 Generic sensor data struct. More...
 
typedef struct rd_sensor_t rd_sensor_t
 Forward declare type definition of sensor structure. More...
 
typedef 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. The sensor wil be initialized to lowest power state possible. More...
 
typedef 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 was written. More...
 
typedef 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, configured value or error code as output. Modifies input parameters to actual values written on the sensor. DSP functions are run on the sensor HW, not in the platform FW. More...
 
typedef 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, calling this function twice in a row returns same data. Configure sensor in a single-shot mode to take a new sample or leave sensor in a continuous mode to get updated data. More...
 
typedef 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 actual values written on the sensor. More...
 
typedef 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 populates pointer data with them. More...
 
typedef 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 FIFO is filled. It is responsibility of application to know the routing of and polarity of GPIO pins and configure the GPIO to register interrupts. More...
 
typedef rd_status_t(* rd_sensor_level_interrupt_use_fp) (const bool enable, float *limit_g)
 Enable level interrupt on sensor. More...
 
typedef uint64_t(* rd_sensor_timestamp_fp) (void)
 Return number of milliseconds since the start of RTC. More...
 

Enumerations

enum  rd_bus_t {
  RD_BUS_NONE = 0U , RD_BUS_SPI = 1U , RD_BUS_I2C = 2U , RD_BUS_UART = 3U ,
  RD_BUS_PDM = 4U , RD_BUS_FAIL = 5U
}
 Type of bus sensor uses. More...
 

Functions

rd_status_t rd_sensor_configuration_set (const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
 Implementation of ref rd_configuration_fp. More...
 
rd_status_t rd_sensor_configuration_get (const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
 Implementation of ref rd_configuration_fp. More...
 
rd_status_t rd_sensor_timestamp_function_set (const rd_sensor_timestamp_fp timestamp_fp)
 Setup timestamping. Set to NULL to disable timestamps. More...
 
uint64_t rd_sensor_timestamp_get (void)
 Calls the timestamp function and returns its value. More...
 
void rd_sensor_initialize (rd_sensor_t *const p_sensor)
 Initialize sensor struct with non-null pointers which return RD_ERROR_NOT_INITIALIZED. More...
 
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 sensor. More...
 
bool rd_sensor_is_init (const rd_sensor_t *const sensor)
 Check if given sensor structure is already initialized. More...
 
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. More...
 
float rd_sensor_data_parse (const rd_sensor_data_t *const provided, const rd_sensor_data_fields_t requested)
 Parse data from provided struct. More...
 
uint8_t rd_sensor_data_fieldcount (const rd_sensor_data_t *const target)
 Count number of floats required for this data structure. More...
 
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. More...
 
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. More...
 
rd_status_t validate_default_input_get (uint8_t *const input)
 Validate and get input when only allowed value is default. More...
 
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. More...
 
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. More...
 

Detailed Description

Ruuvi sensor interface Lifecycle: Beta

Author
Otso Jousimaa otso@.nosp@m.ojou.nosp@m.sima..nosp@m.net
Date
2020-06-01

Common interface to all Ruuvi Sensors Every sensor must implement these functions:

  • init
  • uninit
  • samplerate_set
  • samplerate_get
  • dsp_set
  • dsp_get
  • scale_set
  • scale_set
  • resolution_set
  • resolution_get
  • mode_set
  • mode_get
  • data_get

If function does not make sense for the sensor, it will return error code.

Return name: Return a pointer to a constant 8-byte long string which represensts sensor, e.g. LIS2DH12\0 or BME280\0\0

INIT, UNINT: Init will prepare sensor for use, reset the sensor, run self-test and place it in low-power mode. Additionally function pointers will be set up by init. Uninit will release any resources used by sensor

Samplerate: Applicable on continuous mode, how often sensor takes samples. Hz

DSP: DSP function and parameter, i.e. "OVERSAMPLING, 16". Return error if the device does not support it.

scale: Maximum scale in a meaningful physical unit, such as celcius or pascal.

resolution: Resolution in bits.

mode: Sleep, single, continuous.

  • Sleep mode should enter lowest-power state available
  • Single will return once new data is available with data_get call
  • Continuous: Sensor will sample at given rate. Returns immediately, data will be available after first sample

data get: return latest sample from sensor

Definition in file ruuvi_driver_sensor.h.