PebbleOS v4.47.0-1362-gd278268fe
Loading...
Searching...
No Matches
Sensor drivers

Interface for sensor drivers. More...

Data Structures

struct  pbl_sensor_mount
 Mounting of a sensor in the device. More...
 
struct  pbl_sensor_ops
 Driver operations. More...
 
struct  pbl_sensor_state
 Class runtime state. More...
 
struct  pbl_sensor
 A sensor. More...
 
struct  pbl_sensor_rate
 Sampling interval of a sensor, as measured from the time of its frames. More...
 

Macros

#define PBL_SENSOR_MOUNT_MATRIX(xx, xy, xz, yx, yy, yz, zx, zy, zz)
 Initializer for a struct pbl_sensor_mount, from the rows of a mount matrix.
 
#define PBL_SENSOR_MOUNT_IDENTITY   PBL_SENSOR_MOUNT_MATRIX(1, 0, 0, 0, 1, 0, 0, 0, 1)
 Identity mounting, for sensors aligned with the device or without vector types.
 
#define PBL_SENSOR_STATE_DEFINE(sym)
 Define the class state of sensor sym, without batch buffers, for sensors that are only read.
 
#define PBL_SENSOR_STREAM_STATE_DEFINE(sym, _num_bufs, _buf_size)
 Define the class state of streaming sensor sym, with its batch buffers.
 
#define PBL_SENSOR_INIT(sym, _name, _parent, _ops, _chans, _events, ...)
 Initializer for the struct pbl_sensor of sensor sym.
 

Functions

void pbl_sensor_lock (const struct pbl_sensor *sensor)
 Take the device lock.
 
void pbl_sensor_unlock (const struct pbl_sensor *sensor)
 Release the device lock.
 
uint8_t * pbl_sensor_buf_alloc (const struct pbl_sensor *sensor, uint16_t *size)
 Get a free batch buffer.
 
void pbl_sensor_buf_free (const struct pbl_sensor *sensor, const uint8_t *data)
 Give back a buffer from pbl_sensor_buf_alloc() without reporting it.
 
void pbl_sensor_report (const struct pbl_sensor *sensor, const struct pbl_sensor_batch *batch)
 Report a batch to the listener.
 
void pbl_sensor_report_event (const struct pbl_sensor *sensor, const struct pbl_sensor_event *event)
 Report an event to the listener.
 
static uint16_t pbl_sensor_latency_to_frames (int32_t latency_us, uint32_t interval_us, uint16_t max)
 Convert a report latency to the frames a driver queues before reporting them.
 
void pbl_sensor_rate_reset (struct pbl_sensor_rate *rate, uint32_t nominal_us)
 Start measuring a sampling interval over, from the nominal one.
 
uint32_t pbl_sensor_rate_update (struct pbl_sensor_rate *rate, uint64_t timestamp_us, uint32_t frames)
 Take the time of a frame into the measurement of a sampling interval.
 

Detailed Description

Interface for sensor drivers.

A driver embeds a struct pbl_sensor in its instance, sets it up with PBL_SENSOR_STATE_DEFINE() or PBL_SENSOR_STREAM_STATE_DEFINE() and PBL_SENSOR_INIT(), and implements the struct pbl_sensor_ops the class calls.


Data Structure Documentation

◆ pbl_sensor_mount

struct pbl_sensor_mount

Mounting of a sensor in the device.

Device axis i is sensor axis map[i] times dir[i].

Data Fields
int8_t dir[3] Direction of each device axis, 1 or -1.
uint8_t map[3] Sensor axis feeding each device axis.

◆ pbl_sensor_state

struct pbl_sensor_state

Class runtime state.

Data Fields
struct pbl_mutex lock Device lock.

◆ pbl_sensor

struct pbl_sensor

A sensor.

Data Fields
const struct pbl_sensor_chan * chans Channels measured; their scale is the one at the default range.
struct pbl_device dev Device.
uint8_t events Events detected, a bit per enum pbl_sensor_event_type.
struct pbl_sensor_mount mount Mounting in the device.
uint8_t num_chans Number of channels.
const struct pbl_sensor_ops * ops Driver operations.
struct pbl_sensor_state * state Runtime state.

◆ pbl_sensor_rate

struct pbl_sensor_rate

Sampling interval of a sensor, as measured from the time of its frames.

Data Fields
uint32_t interval_us Interval in microseconds.

Macro Definition Documentation

◆ PBL_SENSOR_INIT

#define PBL_SENSOR_INIT (   sym,
  _name,
  _parent,
  _ops,
  _chans,
  _events,
  ... 
)
Value:
{ \
.dev = PBL_DEVICE_INIT_CLASS(sym, _name, pbl_sensor_dev_init, _parent, &pbl_sensor_class), \
.ops = (_ops), \
.state = &sym##_sensor_state, \
.chans = (_chans), \
.num_chans = PBL_ARRAY_SIZE(_chans), \
.events = (_events), \
.mount = __VA_ARGS__, \
}
#define PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, _cls)
Initializer for the struct pbl_device of device instance sym, in class _cls.
Definition device.h:153
#define PBL_ARRAY_SIZE(array)
Get the number of elements of an array.
Definition size.h:40

Initializer for the struct pbl_sensor of sensor sym.

Parameters
symSymbol of the sensor instance, with its state defined by PBL_SENSOR_STATE_DEFINE() or PBL_SENSOR_STREAM_STATE_DEFINE().
_nameName.
_parentParent device, such as the I2C bus, or NULL.
_opsDriver operations.
_chansChannels measured, an array of struct pbl_sensor_chan.
_eventsEvents detected, PBL_BIT() of each enum pbl_sensor_event_type.
...Mounting, a struct pbl_sensor_mount initializer such as PBL_SENSOR_MOUNT_MATRIX().

◆ PBL_SENSOR_MOUNT_IDENTITY

#define PBL_SENSOR_MOUNT_IDENTITY   PBL_SENSOR_MOUNT_MATRIX(1, 0, 0, 0, 1, 0, 0, 0, 1)

Identity mounting, for sensors aligned with the device or without vector types.

◆ PBL_SENSOR_MOUNT_MATRIX

#define PBL_SENSOR_MOUNT_MATRIX (   xx,
  xy,
  xz,
  yx,
  yy,
  yz,
  zx,
  zy,
  zz 
)
Value:
{ \
.map = \
{PBL_SENSOR_MOUNT_COL_(xx, xy, xz) + \
PBL_SENSOR_MOUNT_CHECK_(xx, xy, xz, yx, yy, yz, zx, zy, zz), \
PBL_SENSOR_MOUNT_COL_(yx, yy, yz), PBL_SENSOR_MOUNT_COL_(zx, zy, zz)}, \
.dir = {(xx) + (xy) + (xz), (yx) + (yy) + (yz), (zx) + (zy) + (zz)}, \
}

Initializer for a struct pbl_sensor_mount, from the rows of a mount matrix.

With devicetree, the rows come from the node's mount-matrix property. Only matrices swapping and flipping axes are supported, with a single 1 or -1 in every row and column: anything else fails the build.

◆ PBL_SENSOR_STATE_DEFINE

#define PBL_SENSOR_STATE_DEFINE (   sym)
Value:
static struct pbl_sensor_state sym##_sensor_state = {.lock = PBL_MUTEX_INITIALIZER}
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132
struct pbl_mutex lock
Device lock.
Definition driver.h:123
Class runtime state.
Definition driver.h:121
#define PBL_MUTEX_INITIALIZER
Static initializer for an unlocked mutex.
Definition mutex.h:50

Define the class state of sensor sym, without batch buffers, for sensors that are only read.

Parameters
symSymbol of the sensor instance.

◆ PBL_SENSOR_STREAM_STATE_DEFINE

#define PBL_SENSOR_STREAM_STATE_DEFINE (   sym,
  _num_bufs,
  _buf_size 
)
Value:
static uint8_t sym##_sensor_data[(_num_bufs) * PBL_DCACHE_ROUND_UP(_buf_size)] PBL_ALIGNED( \
static struct pbl_sensor_buf sym##_sensor_bufs[(_num_bufs)]; \
static struct pbl_sensor_state sym##_sensor_state = { \
.bufs = sym##_sensor_bufs, \
.data = sym##_sensor_data, \
.num_bufs = (_num_bufs), \
.buf_size = PBL_DCACHE_ROUND_UP(_buf_size), \
}
#define PBL_DCACHE_LINE_SIZE
Definition cache.h:46
#define PBL_DCACHE_ROUND_UP(size)
size rounded up to whole data cache lines.
Definition cache.h:57
#define PBL_ALIGNED(bytes)
Align the symbol or type.
Definition compiler.h:87

Define the class state of streaming sensor sym, with its batch buffers.

Parameters
symSymbol of the sensor instance.
_num_bufsNumber of buffers, at least 1, up to 32.
_buf_sizeSize of each buffer in bytes.

Function Documentation

◆ pbl_sensor_buf_alloc()

uint8_t * pbl_sensor_buf_alloc ( const struct pbl_sensor *  sensor,
uint16_t *  size 
)

Get a free batch buffer.

Callable from any context.

Parameters
sensorSensor.
[out]sizeSize of the buffer in bytes, or NULL.
Returns
Buffer data, DMA-safe, or NULL if every buffer is in use: the buf_ready operation then tells when one is free.

◆ pbl_sensor_buf_free()

void pbl_sensor_buf_free ( const struct pbl_sensor *  sensor,
const uint8_t *  data 
)

Give back a buffer from pbl_sensor_buf_alloc() without reporting it.

Callable from any context.

Parameters
sensorSensor.
dataBuffer data, or a pointer into it.

◆ pbl_sensor_latency_to_frames()

static uint16_t pbl_sensor_latency_to_frames ( int32_t  latency_us,
uint32_t  interval_us,
uint16_t  max 
)
inlinestatic

Convert a report latency to the frames a driver queues before reporting them.

Parameters
latency_usReport latency in microseconds, or PBL_SENSOR_LATENCY_NONE.
interval_usSampling interval in microseconds.
maxMost frames the driver queues.
Returns
0 for no batches, otherwise the closest from 1 to max.

References PBL_CLAMP.

◆ pbl_sensor_lock()

void pbl_sensor_lock ( const struct pbl_sensor *  sensor)

Take the device lock.

Recursive. Taken around what a driver does outside of its operations.

Parameters
sensorSensor.

◆ pbl_sensor_rate_reset()

void pbl_sensor_rate_reset ( struct pbl_sensor_rate *  rate,
uint32_t  nominal_us 
)

Start measuring a sampling interval over, from the nominal one.

Called when the sampling frequency changes or frames are lost.

Parameters
[out]rateMeasurement.
nominal_usInterval the part is set to, in microseconds.

◆ pbl_sensor_rate_update()

uint32_t pbl_sensor_rate_update ( struct pbl_sensor_rate *  rate,
uint64_t  timestamp_us,
uint32_t  frames 
)

Take the time of a frame into the measurement of a sampling interval.

Called with times taken in the interrupt of the part. Intervals far from the nominal one are left out.

Parameters
rateMeasurement.
timestamp_usTime of the frame, in microseconds of uptime.
framesFrames sampled since the previous call, this one included.
Returns
Interval in microseconds.

◆ pbl_sensor_report()

void pbl_sensor_report ( const struct pbl_sensor *  sensor,
const struct pbl_sensor_batch *  batch 
)

Report a batch to the listener.

Callable from any context, ISRs included. The batch data is in a buffer from pbl_sensor_buf_alloc(), which the class takes back. Batches reach the listener in the order they are reported, from the high priority system work queue.

Parameters
sensorSensor.
batchBatch, copied.

◆ pbl_sensor_report_event()

void pbl_sensor_report_event ( const struct pbl_sensor *  sensor,
const struct pbl_sensor_event *  event 
)

Report an event to the listener.

Callable from any context, ISRs included. Dropped unless detection of that event type is enabled. The axis modifier is in the sensor frame. Events reach the listener from the high priority system work queue; those it has not caught up with are dropped past a few.

Parameters
sensorSensor.
eventEvent, copied.

◆ pbl_sensor_unlock()

void pbl_sensor_unlock ( const struct pbl_sensor *  sensor)

Release the device lock.

Parameters
sensorSensor.