Microchip® Advanced Software Framework

sensor.h File Reference

Atmel Software Framework Common Sensor API.

The sensor device service provides high-level interfaces and binary driver libraries for sensor devices on systems built around UC3, XMEGA, and SAM microcontrollers.

Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.

#include <asf.h>
#include "physics/physics.h"
#include "sensor_bus.h"
#include "sensor_nvram.h"

Data Structures

struct  sensor_axis_vec_t
 Sensor Axis Descriptor. More...
 
struct  sensor_caps_t
 Sensor Capabilities. More...
 
struct  sensor_data_t
 Sensor Data Descriptor. More...
 
struct  sensor_desc
 Generic Sensor API Descriptor. More...
 
struct  sensor_device_t
 Sensor Device Driver Specific Descriptor. More...
 
struct  sensor_event_callback_t
 allocate one per event type (v. More...
 
struct  sensor_event_data_t
 Sensor Event Data Descriptor. More...
 
struct  sensor_funcs_t
 Sensor Driver Entry Point Table. More...
 
struct  sensor_hal
 Sensor Platform Hardware Abstraction Descriptor. More...
 
struct  sensor_map_t
 Sensor Operational Capabilities Descriptors. More...
 
struct  sensor_orient_t
 Sensor Orientation Descriptor. More...
 
struct  sensor_tap_params_t
 Sensor Tap Event Parameters. More...
 
struct  sensor_threshold_desc_t
 Sensor Threshold Descriptor. More...
 

Macros

#define SENSOR_CHANNEL_ALL   (-1)
 Sensor Channel Constants - used to select channel within device. More...
 
Sensor Bus I/O Wrapper Routines
#define sensor_bus_init   bus_init
 
#define sensor_bus_read(hal,...)   bus_read( & (hal)->bus, __VA_ARGS__)
 
#define sensor_bus_write(hal,...)   bus_write( & (hal)->bus, __VA_ARGS__)
 
#define sensor_bus_probe(hal,...)   bus_probe( & (hal)->bus, __VA_ARGS__)
 
#define sensor_bus_get(hal,...)   bus_get( & (hal)->bus, __VA_ARGS__)
 
#define sensor_bus_put(hal,...)   bus_put( & (hal)->bus, __VA_ARGS__)
 
#define sensor_reg_bitclear(hal,...)
 
#define sensor_reg_bitset(hal,...)   bus_reg_bitset( & (hal)->bus, __VA_ARGS__)
 
#define sensor_reg_fieldget(hal,...)
 
#define sensor_reg_fieldset(hal,...)
 

Typedefs

typedef struct sensor_hal sensor_hal_t
 Sensor Hardware Abstraction Descriptor. More...
 
typedef struct sensor_desc sensor_t
 Generic Sensor API Descriptor. More...
 

Enumerations

enum  sensor_calibration_t {
  AUTO_CALIBRATE,
  MANUAL_CALIBRATE,
  HARD_IRON_CALIBRATE,
  SOFT_IRON_CALIBRATE,
  FIELD_CALIBRATE,
  PROXIMITY_CALIBRATE
}
 Sensor Calibration Type Constants. More...
 
enum  sensor_command_t {
  SENSOR_INFO_QUERY,
  SENSOR_STATUS_RUNNING,
  SENSOR_STATUS_SLEEPING,
  SENSOR_STATUS_SUSPENDED,
  SENSOR_EXEC_SLEEP,
  SENSOR_EXEC_WAKE,
  SENSOR_EXEC_SUSPEND,
  SENSOR_EXEC_RESET,
  SENSOR_EXEC_SELFTEST,
  SENSOR_GET_RANGE,
  SENSOR_GET_BANDWIDTH,
  SENSOR_GET_POWER,
  SENSOR_GET_RESOLUTION,
  SENSOR_GET_SAMPLE_RATE,
  SENSOR_GET_THRESHOLD,
  SENSOR_GET_SLEEP_TIME,
  SENSOR_SET_STATE,
  SENSOR_SET_RANGE,
  SENSOR_SET_BANDWIDTH,
  SENSOR_SET_POWER,
  SENSOR_SET_CURRENT,
  SENSOR_SET_RESOLUTION,
  SENSOR_SET_SAMPLE_RATE,
  SENSOR_SET_THRESHOLD,
  SENSOR_SET_SLEEP_TIME,
  SENSOR_SET_TAP,
  SENSOR_ENABLE_EVENT,
  SENSOR_DISABLE_EVENT,
  SENSOR_READ_SCALAR,
  SENSOR_READ_VECTOR
}
 Sensor Command Constants - used for sensor ioctl operations. More...
 
enum  sensor_error_t {
  SENSOR_ERR_NONE = 0x0000,
  SENSOR_ERR_CALDATA = 0x0001,
  SENSOR_ERR_RAWDATA = 0x0002,
  SENSOR_ERR_COMPDATA = 0x0004,
  SENSOR_ERR_HARDWARE = 0x0008,
  SENSOR_ERR_SOFTWARE = 0x0010,
  SENSOR_ERR_PARAMS = 0x0020,
  SENSOR_ERR_CONFIG = 0x0040,
  SENSOR_ERR_SELFTEST = 0x0080,
  SENSOR_ERR_IO = 0x0100,
  SENSOR_ERR_DRIVER = 0x0200,
  SENSOR_ERR_UNSUPPORTED = 0x8000,
  SENSOR_ERR_FUNCTION = 0X8200,
  SENSOR_ERR_IOCTL = 0x8210
}
 Sensor Driver and API Error Constants. More...
 
enum  sensor_feature_t {
  SENSOR_CAPS_UNKNOWN = 0x0000,
  SENSOR_CAPS_1_AXIS = 0x0001,
  SENSOR_CAPS_2_AXIS = 0x0002,
  SENSOR_CAPS_3_AXIS = 0x0004,
  SENSOR_CAPS_SELFTEST = 0x0008,
  SENSOR_CAPS_AUX_TEMP = 0x0010,
  SENSOR_CAPS_HI_G_EVENT = 0x0020,
  SENSOR_CAPS_LO_G_EVENT = 0x0040,
  SENSOR_CAPS_TAP_EVENT = 0x0080,
  SENSOR_CAPS_S_TAP_EVENT = 0x0080,
  SENSOR_CAPS_D_TAP_EVENT = 0x0100,
  SENSOR_CAPS_TILT_EVENT = 0x0200,
  SENSOR_CAPS_DROP_EVENT = 0x0400,
  SENSOR_CAPS_AUTO_CAL = 0x0800,
  SENSOR_CAPS_AUX_ACCEL = 0x1000
}
 Sensor Feature Constants. More...
 
enum  sensor_proximity_t {
  PROXIMITY_NONE,
  PROXIMITY_FAR,
  PROXIMITY_MEDIUM,
  PROXIMITY_NEAR
}
 Sensor Proximity Indicator Constants. More...
 
enum  sensor_read_t {
  SENSOR_READ_ACCELERATION,
  SENSOR_READ_FIELD,
  SENSOR_READ_HEADING,
  SENSOR_READ_ID,
  SENSOR_READ_INTENSITY,
  SENSOR_READ_LIGHT,
  SENSOR_READ_PRESSURE,
  SENSOR_READ_PROXIMITY,
  SENSOR_READ_ROTATION,
  SENSOR_READ_TEMPERATURE
}
 Sensor Data Read Operations. More...
 
enum  sensor_state_t {
  SENSOR_STATE_UNKNOWN,
  SENSOR_STATE_NORMAL,
  SENSOR_STATE_SLEEP,
  SENSOR_STATE_SUSPEND,
  SENSOR_STATE_LOWEST_POWER,
  SENSOR_STATE_LOW_POWER,
  SENSOR_STATE_HIGH_POWER,
  SENSOR_STATE_HIGHEST_POWER,
  SENSOR_STATE_X_AXIS_STANDBY,
  SENSOR_STATE_Y_AXIS_STANDBY,
  SENSOR_STATE_Z_AXIS_STANDBY,
  SENSOR_STATE_RESET,
  SENSOR_STATE_POWER_DOWN,
  SENSOR_STATE_SINGLE,
  SENSOR_STATE_CONTINUOUS
}
 Sensor Operational State Type Constants. More...
 
enum  sensor_test_t {
  SENSOR_TEST_DEFAULT,
  SENSOR_TEST_BIAS_POS,
  SENSOR_TEST_BIAS_NEG,
  SENSOR_TEST_DEFLECTION,
  SENSOR_TEST_INTERRUPT
}
 Sensor Self Test Types. More...
 
enum  sensor_testerr_t {
  SENSOR_TEST_ERR_NONE = 0x0000,
  SENSOR_TEST_ERR_FUNCTION = 0x0001,
  SENSOR_TEST_ERR_READ = 0x0002,
  SENSOR_TEST_ERR_WRITE = 0x0003,
  SENSOR_TEST_ERR_RANGE = 0x0004
}
 Sensor Self-Test Error Constants. More...
 
enum  sensor_threshold_t {
  SENSOR_THRESHOLD_MOTION,
  SENSOR_THRESHOLD_LOW_G,
  SENSOR_THRESHOLD_HIGH_G,
  SENSOR_THRESHOLD_TAP,
  SENSOR_THRESHOLD_S_TAP,
  SENSOR_THRESHOLD_D_TAP,
  SENSOR_THRESHOLD_TILT,
  SENSOR_THRESHOLD_DURATION,
  SENSOR_THRESHOLD_LOW_LIGHT,
  SENSOR_THRESHOLD_HIGH_LIGHT,
  SENSOR_THRESHOLD_NEAR_PROXIMITY,
  SENSOR_THRESHOLD_MED_PROXIMITY,
  SENSOR_THRESHOLD_FAR_PROXIMITY
}
 Sensor Threshold Type Constants. More...
 
enum  sensor_type_t {
  SENSOR_TYPE_UNKNOWN = 0x0000,
  SENSOR_TYPE_ACCELEROMETER = 0x0001,
  SENSOR_TYPE_BAROMETER = 0x0002,
  SENSOR_TYPE_COMPASS = 0x0004,
  SENSOR_TYPE_GYROSCOPE = 0x0008,
  SENSOR_TYPE_HUMIDITY = 0x0010,
  SENSOR_TYPE_LIGHT = 0x0020,
  SENSOR_TYPE_MAGNETIC = 0x0040,
  SENSOR_TYPE_PRESSURE = 0x0080,
  SENSOR_TYPE_PROXIMITY = 0x0100,
  SENSOR_TYPE_TEMPERATURE = 0x0200,
  SENSOR_TYPE_VOLTAGE = 0x0400
}
 Sensor Type Constants. More...
 
enum  sensor_vendor_t {
  SENSOR_VENDOR_UNKNOWN,
  SENSOR_VENDOR_ADI,
  SENSOR_VENDOR_AKM,
  SENSOR_VENDOR_AVAGO,
  SENSOR_VENDOR_BOSCH,
  SENSOR_VENDOR_HONEYWELL,
  SENSOR_VENDOR_INTERSIL,
  SENSOR_VENDOR_INVENSENSE,
  SENSOR_VENDOR_KIONIX,
  SENSOR_VENDOR_OSRAM,
  SENSOR_VENDOR_STM,
  SENSOR_VENDOR_TAOS,
  SENSOR_VENDOR_FREESCALE
}
 Sensor Vendor Constants. More...
 

Functions

bool sensor_add_event (sensor_t *sensor, sensor_event_t sensor_event, sensor_event_callback handler, volatile void *arg, bool enable)
 Install a sensor event handler. More...
 
bool sensor_attach (sensor_t *sensor, sensor_type_t type, int num, void *aux)
 Attach a specified sensor descriptor to a sensor device. More...
 
bool sensor_calibrate (sensor_t *sensor, sensor_calibration_t caltype, int code, void *info)
 Calibrate a sensor device. More...
 
bool sensor_device_enum (sensor_type_t type, sensor_enum_callback func, void *arg)
 Enumerate sensor devices. More...
 
bool sensor_device_id (sensor_t *sensor, uint32_t *id, uint8_t *ver)
 Get sensor hardware device ID. More...
 
static bool sensor_disable_event (sensor_t *sensor, sensor_event_t sensor_event)
 Disable a sensor event handler. More...
 
static bool sensor_enable_event (sensor_t *sensor, sensor_event_t sensor_event)
 Enable a sensor event handler. More...
 
bool sensor_enum_bandwidth (sensor_t *sensor, const sensor_band_t **table, size_t *count)
 Enumerate sensor bandwidth settings. More...
 
bool sensor_enum_range (sensor_t *sensor, const sensor_range_t **table, size_t *count)
 Enumerate sensor range settings. More...
 
static bool sensor_get_acceleration (sensor_t *sensor, sensor_data_t *data)
 Get acceleration sensor data. More...
 
static bool sensor_get_angle (sensor_t *sensor, sensor_data_t *data, int angle)
 Read angular rate sensor data. More...
 
static bool sensor_get_bandwidth (sensor_t *sensor, int16_t *bandwidth_Hz)
 Get the sensor operational bandwidth. More...
 
static bool sensor_get_channel (sensor_t *sensor, uint16_t *channel)
 Get sensor channel. More...
 
static bool sensor_get_field (sensor_t *sensor, sensor_data_t *data)
 Get sensor field strength data. More...
 
static bool sensor_get_heading (sensor_t *sensor, sensor_data_t *data)
 Get sensor heading data. More...
 
static bool sensor_get_intensity (sensor_t *sensor, sensor_data_t *data)
 Get sensor intensity data. More...
 
static bool sensor_get_light (sensor_t *sensor, sensor_data_t *data)
 Get sensor light level data. More...
 
static bool sensor_get_pressure (sensor_t *sensor, sensor_data_t *data)
 Get pressure sensor data. More...
 
static bool sensor_get_proximity (sensor_t *sensor, sensor_data_t *data)
 Get sensor proximity data. More...
 
static bool sensor_get_range (sensor_t *sensor, int16_t *range)
 Get the sensor operational range. More...
 
static bool sensor_get_rotation (sensor_t *sensor, sensor_data_t *data)
 Get rotation sensor data. More...
 
static bool sensor_get_sample_interval (sensor_t *sensor, int16_t *interval_ms)
 Get the sensor operational sample interval. More...
 
static bool sensor_get_sample_rate (sensor_t *sensor, int16_t *sample_Hz)
 Get the sensor operational sample rate. More...
 
bool sensor_get_state (sensor_t *sensor, sensor_state_t *mode)
 Get a sensor mode. More...
 
static bool sensor_get_temperature (sensor_t *sensor, sensor_data_t *data)
 Get temperature sensor data. More...
 
bool sensor_get_threshold (sensor_t *sensor, sensor_threshold_t threshold, int16_t *value)
 Get a sensor operational threshold. More...
 
static bool sensor_get_vector (sensor_t *sensor, vector3_t *vector)
 Get vector sensor data. More...
 
bool sensor_ioctl (sensor_t *sensor, sensor_command_t cmd, void *arg)
 Execute a sensor device control function. More...
 
bool sensor_read (sensor_t *sensor, sensor_read_t type, sensor_data_t *data)
 Read sensor data. More...
 
bool sensor_reset (sensor_t *sensor, int arg)
 Initiate a sensor device software reset. More...
 
bool sensor_selftest (sensor_t *sensor, int *test_code, void *arg)
 Activate a sensor self-test function. More...
 
static bool sensor_set_bandwidth (sensor_t *sensor, int16_t bandwidth_Hz)
 Set the sensor operational bandwidth. More...
 
static bool sensor_set_channel (sensor_t *sensor, int16_t channel)
 Set sensor channel. More...
 
static bool sensor_set_current (sensor_t *sensor, int16_t current_mA)
 Set the sensor operational current. More...
 
static bool sensor_set_range (sensor_t *sensor, int16_t range)
 Set the sensor operational range. More...
 
static bool sensor_set_sample_interval (sensor_t *sensor, int16_t interval_ms)
 Set the sensor operational sample interval. More...
 
static bool sensor_set_sample_rate (sensor_t *sensor, int16_t sample_Hz)
 Set the sensor operational sample rate. More...
 
bool sensor_set_state (sensor_t *sensor, sensor_state_t mode)
 Set a sensor mode. More...
 
bool sensor_set_tap (sensor_t *sensor, sensor_tap_params_t *params)
 Set tap event parameters. More...
 
bool sensor_set_threshold (sensor_t *sensor, sensor_threshold_t threshold, int16_t value)
 Set a sensor operational threshold. More...
 
bool sensor_sleep (sensor_t *sensor, int arg)
 Set a sensor device to low-power or standby mode. More...
 
Platform Interfaces
const char * sensor_api_version (void)
 Atmel Software Framework Common Sensor API version. More...
 
sensor_hal_tsensor_find (sensor_type_t type)
 Find a sensor hardware descriptor. More...
 
bool sensor_irq_connect (uint32_t intr_pin, void(*handler)(volatile void *), void *arg)
 Install a sensor device interrupt handler. More...
 
sensor_hal_tsensor_list (size_t *dev_count)
 Fetch the sensor hardware descriptor list. More...
 
bool sensor_platform_init (void)
 Initialize the Sensor Platform Hardware. More...
 
uint32_t sensor_timestamp (void)
 This function returns the current timestamp counter value. More...
 

Sensor Physical Axis Definitions

#define AXIS_X_POS   {SENSOR_AXIS_X, SENSOR_SIGN_POS}
 
#define AXIS_X_NEG   {SENSOR_AXIS_X, SENSOR_SIGN_NEG}
 
#define AXIS_Y_POS   {SENSOR_AXIS_Y, SENSOR_SIGN_POS}
 
#define AXIS_Y_NEG   {SENSOR_AXIS_Y, SENSOR_SIGN_NEG}
 
#define AXIS_Z_POS   {SENSOR_AXIS_Z, SENSOR_SIGN_POS}
 
#define AXIS_Z_NEG   {SENSOR_AXIS_Z, SENSOR_SIGN_NEG}
 
#define AXIS_NONE   {SENSOR_AXIS_NONE, SENSOR_SIGN_NONE}
 
enum  sensor_axis_t {
  SENSOR_AXIS_X = 0,
  SENSOR_AXIS_Y = 1,
  SENSOR_AXIS_Z = 2,
  SENSOR_AXIS_NONE = -1
}
 Sensor Axis Name Constants. More...
 
enum  sensor_sign_t {
  SENSOR_SIGN_POS = 1,
  SENSOR_SIGN_NEG = -1,
  SENSOR_SIGN_NONE = 0
}
 Sensor Axis Sign Constants. More...
 

Sensor Event Interface

enum  sensor_event_t {
  SENSOR_EVENT_UNKNOWN = 0x0000,
  SENSOR_EVENT_NEW_DATA = 0x0001,
  SENSOR_EVENT_MOTION = 0x0002,
  SENSOR_EVENT_LOW_G = 0x0004,
  SENSOR_EVENT_HIGH_G = 0x0008,
  SENSOR_EVENT_TILT = 0x0010,
  SENSOR_EVENT_TAP = 0x0020,
  SENSOR_EVENT_S_TAP = 0x0020,
  SENSOR_EVENT_D_TAP = 0x0040,
  SENSOR_EVENT_DROP = 0x0080,
  SENSOR_EVENT_NEAR_PROXIMITY = 0x0100,
  SENSOR_EVENT_MED_PROXIMITY = 0x0200,
  SENSOR_EVENT_FAR_PROXIMITY = 0x0400,
  SENSOR_EVENT_LOW_LIGHT = 0x0800,
  SENSOR_EVENT_HIGH_LIGHT = 0x1000
}
 Sensor Event Types. More...
 
enum  sensor_tap_axis_t {
  SENSOR_TAP_AXIS_UNKNOWN = 0x0000,
  SENSOR_TAP_AXIS_X = 0x0001,
  SENSOR_TAP_AXIS_Y = 0x0002,
  SENSOR_TAP_AXIS_Z = 0x0004
}
 Sensor Tap Event. More...
 
enum  sensor_tap_direction_t {
  SENSOR_TAP_DIRECTION_UNKNOWN = 0x0000,
  SENSOR_TAP_DIRECTION_POS = 0x0001,
  SENSOR_TAP_DIRECTION_NEG = 0x0002
}
 Sensor Tap Event (event direction) More...
 
typedef void(* sensor_event_callback )(volatile sensor_event_data_t *, volatile void *)
 
typedef bool(* sensor_enum_callback )(const sensor_hal_t *, void *)
 

Sensor Sample Commands and Data Formats

!

enum  sample_format_t {
  SAMPLE_FORMAT_COUNTS,
  SAMPLE_FORMAT_SCALED
}
 Sensor Sample Data Format. More...
 
enum  sensor_units_t {
  SENSOR_UNITS_NONE,
  SENSOR_UNITS_g0,
  SENSOR_UNITS_gauss,
  SENSOR_UNITS_lux,
  SENSOR_UNITS_lumen,
  SENSOR_UNITS_pascal,
  SENSOR_UNITS_deg_per_sec,
  SENSOR_UNITS_deg_Celcius,
  SENSOR_UNITS_tesla,
  SENSOR_UNITS_volt_DC,
  SENSOR_UNITS_volt_AC
}
 Sensor Sample Units. More...
 
enum  sensor_scale_t {
  SENSOR_SCALE_micro = -6,
  SENSOR_SCALE_milli = -3,
  SENSOR_SCALE_centi = -2,
  SENSOR_SCALE_deci = -1,
  SENSOR_SCALE_one = 0,
  SENSOR_SCALE_deca = 1,
  SENSOR_SCALE_hecto = 2,
  SENSOR_SCALE_kilo = 3
}
 Sensor Sample Unit Scale. More...
 
typedef sensor_map_t sensor_range_t
 
typedef sensor_map_t sensor_band_t
 

Sensor API Convenience Macros and Functions

!

#define ARRAYSIZE(a)   (sizeof(a) / sizeof(a[0]))
 
#define sensor_get_x(sensor, data)   (sensor_get_axis(sensor, data, 0))
 
#define sensor_get_y(sensor, data)   (sensor_get_axis(sensor, data, 1))
 
#define sensor_get_z(sensor, data)   (sensor_get_axis(sensor, data, 2))
 
#define sensor_get_pitch(sensor, data)   (sensor_get_angle(sensor, data, 0))
 
#define sensor_get_roll(sensor, data)   (sensor_get_angle(sensor, data, 1))
 
#define sensor_get_yaw(sensor, data)   (sensor_get_angle(sensor, data, 2))
 
static int32_t raw_to_scaled (const sensor_hal_t *hal, int32_t counts)
 Convert raw sensor data to scaled engineering units. More...
 
static int32_t scaled_to_raw (const sensor_hal_t *hal, int32_t value)
 Convert scaled sensor data to raw counts. More...
 
void default_event_handler (volatile sensor_event_data_t *data, volatile void *arg)
 Default sensor event callback handler. More...