pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
  • Pebble Firmware
    • Overview
    • Playground
    • Application SDK
  • Development
    • The pbl CLI
    • Build system
    • Samples
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Drivers
      • Device model
      • Simulators
      • Buses and I/O
      • Clocks, power and timing
      • Memory and storage
      • Display and graphics
      • Sensors and input
      • Audio and haptics
      • Power supply
      • System
    • Subsystems
    • Libraries
    • Third-party
  • Hardware
    • Boards
    • Board Add-ons
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Sensors

A sensor (include/pbl/drivers/sensor.h) is a device measuring one or more channel types: acceleration, angular rate, temperature, etc. A single device covers a part that measures several of them, as an IMU with an accelerometer, a gyroscope and a temperature sensor sharing one FIFO.

Users include include/pbl/drivers/sensor.h, where the sensor is opaque. Sensor drivers include include/pbl/drivers/sensor/driver.h, which adds the sensor structure, its operations, the class state and initializer macros and the calls only drivers make.

A sensor has one consumer: the one that registers its listener and sets its attributes. Several users of a sensor share it through a service that owns it.

Channels and units

Values are in SI units, as integers the caller scales: every read or decode takes a multiplier, a power of ten, so 1000 gives mm/s², m°C or mPa. Values are rounded to the nearest and saturate at the limits of an int32_t: reads and decodes then return -ERANGE, with the values filled all the same.

Type

Modifiers

SI unit

PBL_SENSOR_ACCEL

X, Y, Z

m/s²

PBL_SENSOR_ANGL_VEL

X, Y, Z

rad/s

PBL_SENSOR_MAGN

X, Y, Z

T

PBL_SENSOR_TEMP

none, TEMP_AMBIENT, TEMP_OBJECT

°C

PBL_SENSOR_PRESSURE

none

Pa

A channel is a type, a modifier and an index, a struct pbl_sensor_chan_spec, usually written with PBL_SENSOR_SPEC(). Vector types have a channel per axis. Scalar types have one without modifier, or one per modifier when a sensor measures the type several ways, as an infrared thermometer measuring both the ambient and the object temperature. The index tells apart channels of the same type and modifier, and is usually 0. PBL_SENSOR_MOD_NONE selects the whole vector of a vector type, or the plain channel of a scalar type; any other modifier selects that channel alone.

Drivers do not convert their data. They describe each channel of a frame with a struct pbl_sensor_chan: its channel, its byte offset and encoding, and the scale and bias taking the raw value to the SI unit of the type: (raw * scale_num / scale_den + bias) * 10^scale_exp. The exponent, from -9 to 9, is the resolution the driver writes its scale and bias at, as -6 for µm/s² or -9 for nT, so integers express them exactly. A driver whose conversion is not linear, as a pressure sensor compensated with a polynomial of its calibration coefficients, applies the compensation itself and stores the compensated value with a fixed scale. Descriptors are constant tables; a driver keeps one per configuration that changes the scale, such as the full-scale range.

Every sensor has a channel table, the channels it measures with their scale at the default range, given to PBL_SENSOR_INIT(). pbl_sensor_get_chans() lists it, and pbl_sensor_has_type() tells whether it has a type. Batches carry the descriptors in effect when they were sampled.

Decoding

pbl_sensor_read() and pbl_sensor_decode() give values in SI units, with vectors in the device frame. For processing on raw values first, as filtering or averaging in the integers the hardware produced:

  • pbl_sensor_decode_raw() gives the raw values of a channel, sign-extended and in the sensor’s own axes.

  • pbl_sensor_convert() takes them to SI units later, with the channel descriptors of the batch they came from: keep the batch, or a copy of it.

  • pbl_sensor_mount_apply() moves converted vectors to the device frame.

Conversion is linear for channels without a bias, as every accelerometer and gyroscope: any processing of their raw values converts the same. With a bias, averages convert correctly but differences do not, as the bias is added back: convert both values and subtract the results instead.

Mounting

Vector channels are described in the sensor’s own axes. The sensor’s orientation is given to PBL_SENSOR_INIT() with PBL_SENSOR_MOUNT_MATRIX(), which is filled from the mount-matrix property when the board uses devicetree. Only matrices swapping and flipping axes are supported, with a single 1 or -1 in every row and column; any other fails the build. The class applies it when it decodes vectors and when it reports events, so consumers only see the device frame and drivers never deal with it, and publishes it with pbl_sensor_get_mount_matrix() for those working on raw values. Turning that frame around, as for a device worn on the other wrist, is left to the consumers, which know when the user asks for it.

A sensor has one mount matrix. A part whose types do not share axes, as a magnetometer die turned with respect to the accelerometer one, describes them all in the axes of one of them: the channel modifiers name the axis in that frame, and a negative scale flips it.

Reading

pbl_sensor_read() returns the latest values of a channel. The driver runs a conversion when the sensor is idle, or returns the latest frame when it is sampling. Its read operation fills a single frame, of up to PBL_SENSOR_FRAME_MAX bytes, with its channel descriptors; the class decodes it like any other frame.

Streaming

A sensor streams while it has a sampling frequency and a report latency. Its consumer registers a listener with pbl_sensor_set_listener(), which returns -EBUSY while the sensor has another, and sets both attributes with pbl_sensor_attr_set():

  • PBL_SENSOR_ATTR_SAMPLING_FREQUENCY is the sampling frequency in millihertz; the driver rounds it up to what the hardware supports and writes it back. 0 stops sampling.

  • PBL_SENSOR_ATTR_REPORT_LATENCY is the longest the driver may hold frames before reporting them as a batch, in microseconds, which lets it sleep while a hardware FIFO fills. PBL_SENSOR_LATENCY_NONE, the default, reports no batches, and 0 reports every frame as soon as it is acquired. The driver keeps to it when the sampling frequency changes, and writes back the latency it applies: 0 when it does not queue frames.

  • PBL_SENSOR_ATTR_BATCH_MAX, read-only, is the most frames a batch holds, the depth of that FIFO.

  • PBL_SENSOR_ATTR_RANGE is the largest magnitude measured, in the SI unit of the type times 1000; the driver selects the smallest range it supports that holds it.

pbl_sensor_attr_get_avail() tells the values an attribute takes, where the driver knows them: each of them, or the smallest, the step and the largest. pbl_sensor_flush() asks for the frames queued in the hardware right away, without waiting for the latency; the listener gets them like any batch.

A batch is a run of frames, oldest first, with the time of its last frame in microseconds of uptime and the interval between frames. Frames usually share one layout, a stride apart. Hardware that interleaves channels sampled at different rates, such as tagged FIFO entries, is reported as it came: the driver gives a next_frame function with the batch, which finds the frames of a type and their time, and the class still does the conversion. pbl_sensor_num_frames() and pbl_sensor_frame_time() give the frames of a type and the time of each, whichever way they are laid out.

Drivers take the time of a batch in the interrupt of the part, where it is closest to the sample. The part’s clock is not the system’s, so a driver measures the interval between frames from those times with struct pbl_sensor_rate, and reports that one instead of the nominal one.

Batches live in buffers owned by the class. The driver states how many it needs and how large they are with PBL_SENSOR_STREAM_STATE_DEFINE(); the buffers are aligned and padded for DMA, so a driver can read its FIFO straight into one with pbl_sensor_buf_alloc() and hand it over without a copy. pbl_sensor_report() is callable from any context, ISRs included: a driver can chain the FIFO read from its interrupt with pbl_i2c_transfer_async() and report the batch from the transfer callback, without waking a thread until there is data.

The class calls the listener from the high priority system work queue, in the order batches were reported and without the device lock: the listener may take locks of its own and call back into the sensor. The buffer returns to the driver once the listener returns, unless the listener keeps it with pbl_sensor_hold() to process it later and gives it back with pbl_sensor_release(). While all buffers are taken the driver has nowhere to read into; it leaves the data in its FIFO until one is free.

Events

Events such as shakes and double taps are enabled with pbl_sensor_set_event(). Each event has attributes of its own, set with pbl_sensor_event_attr_set(): PBL_SENSOR_EVENT_ATTR_THRESHOLD is the level it triggers above, in the SI unit of the measured type times 1000, as mm/s² for a shake. The driver reports events from any context with pbl_sensor_report_event(), with the time it detected them and the axis modifier in its own frame; the class remaps them to the device frame and calls the listener from the same work queue as for batches.

Roles

The board’s sensors are reached through roles. An application enables the Kconfig symbol of each role it uses, which fails the build on boards without that role and enables the sensor class. CONFIG_SENSOR builds the drivers of every sensor the board’s devicetree enables, in a menu and a directory under drivers/sensor/ per type, whether a role uses them or not:

Role

Symbol

Kconfig

Drivers

accel

ACCEL

CONFIG_SENSOR_ACCEL

accel/, imu/

mag

MAG

CONFIG_SENSOR_MAG

magnetometer/

die_temp

DIE_TEMP

CONFIG_SENSOR_TEMP

temperature/

pressure

PRESSURE

CONFIG_SENSOR_PRESSURE

pressure/

Shell

The sensor shell command, with CONFIG_SENSOR_SHELL, lists every sensor with its channels, each a type, a modifier and an index, reads it, gets or sets its attributes and those of its events, tells the values an attribute takes and flushes it:

> sensor list
0: tsen
  thermometer - 0
1: mmc5603nj
  magnetometer x 0
  magnetometer y 0
  magnetometer z 0
> sensor read 0
thermometer: 31.250 C
> sensor attr 1 magnetometer sampling-frequency 10000
10000
> sensor avail 1 magnetometer sampling-frequency
1000 to 255000, step 1000

Attributes are sampling-frequency, report-latency, batch-max and range, in the units of the API. sensor flush <sensor> reports what the sensor has queued, and a read marks values that saturated. sensor event <sensor> <event> on|off detects an event, and sensor stream <sensor> on|off makes the shell the listener of a sensor that has none, printing its batches and events as they come. The sensor sample enables the command on any board.

Generated from PebbleOS d278268fe. This page is maintained in the pebbleos repository: docs/drivers/sensor/index.md.

Overview

  • Channels and units
  • Decoding
  • Mounting
  • Reading
  • Streaming
  • Events
  • Roles
  • Shell