LIS3DH.Config (lis3dh v0.1.0)

Copy Markdown

Encoding and decoding of the LIS3DH's CTRL_REG1 (0x20) and CTRL_REG4 (0x23) bytes, plus the per-mode/per-range sensitivity table used for converting raw ADC samples into physical units.

Bit layouts:

CTRL_REG1 (0x20)
| 7 6 5 4 | 3    | 2   1   0   |
|   ODR   | LPen | Zen Yen Xen |

CTRL_REG4 (0x23)
| 7   | 6   | 5 4 | 3  | 2  1 | 0   |
| BDU | BLE | FS  | HR |  ST  | SIM |

Operating mode is set by the combination of CTRL_REG1.LPen and CTRL_REG4.HR:

LPen  HR  Mode               Data width
 1    0   Low-power           8-bit
 0    0   Normal              10-bit
 0    1  High-resolution     12-bit
 1    1   (Not allowed)

All OUT_* data is signed 16-bit two's complement left-justified — the meaningful bits occupy the MSB end and the lower bits are zero. Use sensitivity/2 to obtain the per-mode mg/LSB conversion factor.

Summary

Types

Axes to enable in CTRL_REG1.

Block-data-update mode for CTRL_REG4.BDU.

High-pass filter cutoff selector. The actual −3 dB cutoff depends on ODR; lower codes give higher cutoffs.

High-pass filter mode for CTRL_REG2.HPM.

Output data rate in Hz, or :power_down to disable the sensor.

Operating mode. Selects the ADC bit width and the LPen / HR bit combination.

Full-scale measurement range in g.

Self-test mode selection for CTRL_REG4.ST.

Functions

Returns the bit width of the auxiliary ADC for the given operating mode.

Decode a CTRL_REG1 byte into a map of its fields.

Decode a CTRL_REG2 byte into a map of its fields.

Decode a CTRL_REG4 byte into a map of its fields.

Encode a CTRL_REG1 byte from keyword options.

Encode a CTRL_REG2 byte (high-pass filter configuration) from keyword options.

Encode a CTRL_REG4 byte from keyword options. Leaves BLE, ST, and SIM at their reset values; callers that need to override them should compose the resulting binary with their own bit twiddling.

Returns the native accelerometer ADC bit width for the given operating mode — equivalent to the right-shift required to recover the N-bit signed value from the 16-bit left-justified OUT_* registers.

Resolve the operating mode from the LPen and HR bits returned by decode_ctrl_reg_1/1 and decode_ctrl_reg_4/1.

Encode a self-test mode atom to its 2-bit CTRL_REG4.ST code.

Returns the per-LSB sensitivity in milli-g at the native bit width for the given operating mode and range.

Types

axis()

@type axis() :: :x | :y | :z

Axes to enable in CTRL_REG1.

bdu()

@type bdu() :: :continuous | :hold

Block-data-update mode for CTRL_REG4.BDU.

hpf_cutoff()

@type hpf_cutoff() :: 0..3

High-pass filter cutoff selector. The actual −3 dB cutoff depends on ODR; lower codes give higher cutoffs.

hpf_mode()

@type hpf_mode() :: :normal_with_reset | :reference | :normal | :autoreset

High-pass filter mode for CTRL_REG2.HPM.

  • :normal_with_reset — continuous HPF; the internal state can be reset by reading the REFERENCE register.
  • :reference — HPF uses the REFERENCE register as the filtered reference signal.
  • :normal — continuous HPF with no reset hook.
  • :autoreset — HPF auto-resets on every interrupt event.

odr()

@type odr() :: :power_down | 1 | 10 | 25 | 50 | 100 | 200 | 400 | 1344 | 1600 | 5376

Output data rate in Hz, or :power_down to disable the sensor.

operating_mode()

@type operating_mode() :: :low_power | :normal | :high_resolution

Operating mode. Selects the ADC bit width and the LPen / HR bit combination.

range()

@type range() :: 2 | 4 | 8 | 16

Full-scale measurement range in g.

self_test_mode()

@type self_test_mode() :: :off | :self_test_0 | :self_test_1

Self-test mode selection for CTRL_REG4.ST.

Functions

aux_adc_width(arg1)

@spec aux_adc_width(operating_mode()) :: 8 | 10

Returns the bit width of the auxiliary ADC for the given operating mode.

Per datasheet §3.7, the auxiliary ADC is 8-bit when LPen=1 (low-power) and 10-bit otherwise — including in high-resolution mode, even though the accelerometer itself is 12-bit there.

decode_ctrl_reg_1(arg)

@spec decode_ctrl_reg_1(<<_::8>>) :: %{lpen: boolean(), odr: odr(), axes: [axis()]}

Decode a CTRL_REG1 byte into a map of its fields.

Note that the LPen bit alone doesn't fully determine the operating mode — the HR bit in CTRL_REG4 is also needed. This function reports :lpen as a boolean; combine it with decode_ctrl_reg_4/1 to recover the full operating_mode/0.

decode_ctrl_reg_2(arg)

@spec decode_ctrl_reg_2(<<_::8>>) :: %{
  mode: hpf_mode(),
  cutoff: hpf_cutoff(),
  filtered_data_output: boolean(),
  enable_for_click: boolean(),
  enable_for_interrupt_1: boolean(),
  enable_for_interrupt_2: boolean()
}

Decode a CTRL_REG2 byte into a map of its fields.

decode_ctrl_reg_4(arg)

@spec decode_ctrl_reg_4(<<_::8>>) :: %{
  hr: boolean(),
  range: range(),
  block_data_update: bdu()
}

Decode a CTRL_REG4 byte into a map of its fields.

encode_ctrl_reg_1(opts)

@spec encode_ctrl_reg_1(keyword()) :: <<_::8>>

Encode a CTRL_REG1 byte from keyword options.

Options

  • :modeoperating_mode/0 (required). Sets the LPen bit.
  • :odrodr/0 (required).
  • :axes — list of axis/0 to enable (default [:x, :y, :z]).

encode_ctrl_reg_2(opts \\ [])

@spec encode_ctrl_reg_2(keyword()) :: <<_::8>>

Encode a CTRL_REG2 byte (high-pass filter configuration) from keyword options.

Options

  • :modehpf_mode/0 (default :normal_with_reset).
  • :cutoffhpf_cutoff/0 (default 0). The actual −3 dB cutoff depends on ODR per the datasheet figures.
  • :filtered_data_output (default false) — when true, the HPF output replaces the unfiltered data in OUT_* and the FIFO. When false (FDS=0) the HPF only affects the click / interrupt detectors.
  • :enable_for_click (default false) — apply HPF to the click detector.
  • :enable_for_interrupt_1 (default false) — apply HPF to inertial interrupt 1 (AOI 1).
  • :enable_for_interrupt_2 (default false) — apply HPF to inertial interrupt 2 (AOI 2).

encode_ctrl_reg_4(opts)

@spec encode_ctrl_reg_4(keyword()) :: <<_::8>>

Encode a CTRL_REG4 byte from keyword options. Leaves BLE, ST, and SIM at their reset values; callers that need to override them should compose the resulting binary with their own bit twiddling.

Options

  • :modeoperating_mode/0 (required). Sets the HR bit.
  • :rangerange/0 (default 2).
  • :block_data_updatebdu/0 (default :hold, recommended to avoid reading LSB/MSB pairs from different samples).

native_width(mode)

@spec native_width(operating_mode()) :: 8 | 10 | 12

Returns the native accelerometer ADC bit width for the given operating mode — equivalent to the right-shift required to recover the N-bit signed value from the 16-bit left-justified OUT_* registers.

operating_mode(bool1, bool2)

@spec operating_mode(boolean(), boolean()) :: operating_mode()

Resolve the operating mode from the LPen and HR bits returned by decode_ctrl_reg_1/1 and decode_ctrl_reg_4/1.

Raises ArgumentError for the disallowed combination LPen=1, HR=1.

self_test_code(mode)

@spec self_test_code(self_test_mode()) :: 0..2

Encode a self-test mode atom to its 2-bit CTRL_REG4.ST code.

sensitivity(mode, range)

@spec sensitivity(operating_mode(), range()) :: pos_integer()

Returns the per-LSB sensitivity in milli-g at the native bit width for the given operating mode and range.