M5Unit-IMU 0.0.2 git rev:e5b00aa
Loading...
Searching...
No Matches
unit_bmi270_data.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_IMU_UNIT_UNIT_BMI270_DATA_HPP
11#define M5_UNIT_IMU_UNIT_UNIT_BMI270_DATA_HPP
12
13#include <array>
14#include <cstdint>
15#include "unit_bmm150_data.hpp"
16
17namespace m5 {
18namespace unit {
19namespace bmi270 {
20
25enum class AccelRange : uint8_t {
26 G2,
27 G4,
28 G8,
29 G16,
30};
31
38enum class GyroRange : uint8_t {
39 DPS2000,
40 DPS1000,
41 DPS500,
42 DPS250,
43 DPS125,
44};
45
50enum class AccelODR : uint8_t {
51 Rate12_5Hz = 0x05,
52 Rate25Hz,
53 Rate50Hz,
54 Rate100Hz,
55 Rate200Hz,
56 Rate400Hz,
57 Rate800Hz,
59};
60
65enum class GyroODR : uint8_t {
66 Rate25Hz = 0x06,
67 Rate50Hz,
68 Rate100Hz,
69 Rate200Hz,
70 Rate400Hz,
71 Rate800Hz,
74};
75
81enum class FilterPerf : uint8_t {
84};
85
93enum class AccelBwp : uint8_t {
94 Osr4Avg1,
95 Osr2Avg2,
96 NormAvg4,
97 CicAvg8,
98 Avg16,
99 Avg32,
100 Avg64,
101 Avg128,
102};
103
110enum class GyroBwp : uint8_t {
111 Osr4,
112 Osr2,
113 Norm,
114};
115
118constexpr bool bwp_fits(const FilterPerf perf, const AccelBwp bwp)
119{
120 return (perf == FilterPerf::PowerOptimized) ||
121 (static_cast<uint8_t>(bwp) <= static_cast<uint8_t>(AccelBwp::CicAvg8));
122}
123
126constexpr uint8_t acc_conf_value(const FilterPerf perf, const AccelBwp bwp, const AccelODR odr)
127{
128 return static_cast<uint8_t>((static_cast<uint8_t>(perf) << 7) | (static_cast<uint8_t>(bwp) << 4) |
129 static_cast<uint8_t>(odr));
130}
131
135constexpr uint8_t gyr_conf_value(const FilterPerf perf, const GyroBwp bwp, const GyroODR odr)
136{
137 return static_cast<uint8_t>((static_cast<uint8_t>(perf) << 7) | (static_cast<uint8_t>(bwp) << 4) |
138 static_cast<uint8_t>(odr));
139}
140
142constexpr float accel_res(const AccelRange r)
143{
144 return (2.0f * (1 << static_cast<uint8_t>(r))) / 32768;
145}
147constexpr float gyro_res(const GyroRange r)
148{
149 return (2000.0f / (1 << static_cast<uint8_t>(r))) / 32768;
150}
151
152// Sampling interval (ms) per GyroODR raw register value, ceil(1000/Hz), indexed 0x00..0x0D
153// (only 0x06..0x0D are valid GyroODR values; others are unused placeholders).
154// constexpr namespace-scope array: internal linkage per translation unit (no ODR issue).
155constexpr uint32_t GYRO_ODR_INTERVAL_MS[14] = {
156 0, 0, 0, 0, 0, 0, // 0x00..0x05 (unused)
157 40, // 0x06 = 25Hz
158 20, // 0x07 = 50Hz
159 10, // 0x08 = 100Hz
160 5, // 0x09 = 200Hz
161 3, // 0x0A = 400Hz
162 2, // 0x0B = 800Hz
163 1, // 0x0C = 1600Hz
164 1, // 0x0D = 3200Hz
165};
166
168constexpr uint32_t odr_interval_ms(const GyroODR odr)
169{
170 return GYRO_ODR_INTERVAL_MS[static_cast<uint8_t>(odr)];
171}
172
179struct Data {
180 std::array<uint8_t, 12> raw{};
185 bool aux_mag{false};
186
190 inline int16_t accel_x() const
191 {
192 return static_cast<int16_t>((raw[1] << 8) | raw[0]);
193 }
195 inline int16_t accel_y() const
196 {
197 return static_cast<int16_t>((raw[3] << 8) | raw[2]);
198 }
200 inline int16_t accel_z() const
201 {
202 return static_cast<int16_t>((raw[5] << 8) | raw[4]);
203 }
205 inline int16_t gyro_x() const
206 {
207 return static_cast<int16_t>((raw[7] << 8) | raw[6]);
208 }
210 inline int16_t gyro_y() const
211 {
212 return static_cast<int16_t>((raw[9] << 8) | raw[8]);
213 }
215 inline int16_t gyro_z() const
216 {
217 return static_cast<int16_t>((raw[11] << 8) | raw[10]);
218 }
220};
221
222} // namespace bmi270
223} // namespace unit
224} // namespace m5
225#endif
Top level namespace of M5Stack.
Definition ahrs_controller.cpp:26
Unit-related namespace.
One measurement. Accel/gyro are always 12 bytes, little-endian (register 0x0C when the AUX/BMM150 bri...
Definition unit_bmi270_data.hpp:179
int16_t gyro_x() const
Raw gyroscope X.
Definition unit_bmi270_data.hpp:205
int16_t accel_y() const
Raw accelerometer Y.
Definition unit_bmi270_data.hpp:195
int16_t accel_z() const
Raw accelerometer Z.
Definition unit_bmi270_data.hpp:200
int16_t gyro_y() const
Raw gyroscope Y.
Definition unit_bmi270_data.hpp:210
bool aux_mag
True if mag holds a sample from the AUX (BMM150) bridge.
Definition unit_bmi270_data.hpp:185
std::array< uint8_t, 12 > raw
[0..5] accel XYZ, [6..11] gyro XYZ
Definition unit_bmi270_data.hpp:180
bmm150::Data mag
AUX (on-chip BMM150) magnetometer sample, valid only when aux_mag is true. Reuses bmm150::Data's bit-...
Definition unit_bmi270_data.hpp:184
int16_t accel_x() const
Raw accelerometer X.
Definition unit_bmi270_data.hpp:190
int16_t gyro_z() const
Raw gyroscope Z.
Definition unit_bmi270_data.hpp:215
One measurement (8 bytes burst from REG_DATA_X_LSB, register 0x42)
Definition unit_bmm150_data.hpp:143
GyroBwp
Gyroscope bandwidth parameter (REG_GYR_CONF register 0x42, bits[5:4])
Definition unit_bmi270_data.hpp:110
@ Norm
Normal filter (power-on default)
AccelBwp
Accelerometer bandwidth parameter (REG_ACC_CONF register 0x40, bits[6:4])
Definition unit_bmi270_data.hpp:93
@ Avg32
Average 32 samples (PowerOptimized only)
@ Osr2Avg2
OSR2 filter / average 2 samples.
@ Avg16
Average 16 samples (PowerOptimized only; reserved in Performance mode)
@ NormAvg4
Normal filter (power-on default) / average 4 samples.
@ Avg64
Average 64 samples (PowerOptimized only)
@ Osr4Avg1
OSR4 filter / no averaging.
@ Avg128
Average 128 samples (PowerOptimized only)
@ CicAvg8
CIC filter / average 8 samples.
constexpr uint8_t gyr_conf_value(const FilterPerf perf, const GyroBwp bwp, const GyroODR odr)
REG_GYR_CONF value for the given settings.
Definition unit_bmi270_data.hpp:135
FilterPerf
Filter performance mode (REG_ACC_CONF bit7 / REG_GYR_CONF bit7)
Definition unit_bmi270_data.hpp:81
@ PowerOptimized
Power optimized (ulp): the accelerometer's Bwp averages instead of filtering.
@ Performance
Performance optimized (hp, power-on default)
GyroODR
Gyroscope output data rate (REG_GYR_CONF register 0x42, bits[3:0])
Definition unit_bmi270_data.hpp:65
constexpr uint8_t acc_conf_value(const FilterPerf perf, const AccelBwp bwp, const AccelODR odr)
REG_ACC_CONF value for the given settings.
Definition unit_bmi270_data.hpp:126
constexpr float gyro_res(const GyroRange r)
dps per LSB for each GyroRange (note: value 0 is the largest range, see GyroRange)
Definition unit_bmi270_data.hpp:147
GyroRange
Gyroscope full scale range (REG_GYR_RANGE register 0x43)
Definition unit_bmi270_data.hpp:38
@ DPS2000
+-2000 dps (power-on default)
AccelRange
Accelerometer full scale range (REG_ACC_RANGE register 0x41)
Definition unit_bmi270_data.hpp:25
@ G16
+-16G (2048 LSB/G)
@ G2
+-2G (16384 LSB/G)
@ G8
+-8G (4096 LSB/G, power-on default)
constexpr bool bwp_fits(const FilterPerf perf, const AccelBwp bwp)
Is this bandwidth parameter usable in the given filter performance mode?
Definition unit_bmi270_data.hpp:118
constexpr float accel_res(const AccelRange r)
G per LSB for each AccelRange.
Definition unit_bmi270_data.hpp:142
constexpr uint32_t odr_interval_ms(const GyroODR odr)
Sampling interval in milliseconds for the given GyroODR.
Definition unit_bmi270_data.hpp:168
AccelODR
Accelerometer output data rate (REG_ACC_CONF register 0x40, bits[3:0])
Definition unit_bmi270_data.hpp:50
@ Rate100Hz
100Hz (power-on default)
BMM150 setting enums and measurement data (framework independent)