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
@type axis() :: :x | :y | :z
Axes to enable in CTRL_REG1.
@type bdu() :: :continuous | :hold
Block-data-update mode for CTRL_REG4.BDU.
@type hpf_cutoff() :: 0..3
High-pass filter cutoff selector. The actual −3 dB cutoff depends on ODR; lower codes give higher cutoffs.
@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 theREFERENCEregister.:reference— HPF uses theREFERENCEregister as the filtered reference signal.:normal— continuous HPF with no reset hook.:autoreset— HPF auto-resets on every interrupt event.
@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.
@type operating_mode() :: :low_power | :normal | :high_resolution
Operating mode. Selects the ADC bit width and the LPen / HR bit combination.
@type range() :: 2 | 4 | 8 | 16
Full-scale measurement range in g.
@type self_test_mode() :: :off | :self_test_0 | :self_test_1
Self-test mode selection for CTRL_REG4.ST.
Functions
@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 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.
@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 a CTRL_REG4 byte into a map of its fields.
@spec encode_ctrl_reg_1(keyword()) :: <<_::8>>
Encode a CTRL_REG1 byte from keyword options.
Options
:mode—operating_mode/0(required). Sets theLPenbit.:odr—odr/0(required).:axes— list ofaxis/0to enable (default[:x, :y, :z]).
@spec encode_ctrl_reg_2(keyword()) :: <<_::8>>
Encode a CTRL_REG2 byte (high-pass filter configuration) from keyword
options.
Options
:mode—hpf_mode/0(default:normal_with_reset).:cutoff—hpf_cutoff/0(default0). The actual −3 dB cutoff depends on ODR per the datasheet figures.:filtered_data_output(defaultfalse) — whentrue, the HPF output replaces the unfiltered data inOUT_*and the FIFO. Whenfalse(FDS=0) the HPF only affects the click / interrupt detectors.:enable_for_click(defaultfalse) — apply HPF to the click detector.:enable_for_interrupt_1(defaultfalse) — apply HPF to inertial interrupt 1 (AOI 1).:enable_for_interrupt_2(defaultfalse) — apply HPF to inertial interrupt 2 (AOI 2).
@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
:mode—operating_mode/0(required). Sets theHRbit.:range—range/0(default2).:block_data_update—bdu/0(default:hold, recommended to avoid reading LSB/MSB pairs from different samples).
@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.
@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.
@spec self_test_code(self_test_mode()) :: 0..2
Encode a self-test mode atom to its 2-bit CTRL_REG4.ST code.
@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.