33#include "../../../SensorBuildOpt.h"
34#if !SENSORLIB_EXCLUDE_BMA4XX_COMMON
36#include "../../../platform/comm/ComplexStaticDeviceWithHal.hpp"
37#include "../../../sensor/AccelerometerBase.hpp"
38#include "../../../bosch/bma4xx/bma4.h"
43static constexpr uint8_t BMA4XX_I2C_ADDR_SDO_LOW = (0x18);
47static constexpr uint8_t BMA4XX_I2C_ADDR_SDO_HIGH = (0x19);
122 return static_cast<Capability>(
static_cast<uint32_t
>(a) |
static_cast<uint32_t
>(b));
127 return (
static_cast<uint32_t
>(a) &
static_cast<uint32_t
>(b)) != 0;
230 struct bma4_accel sens_data = { 0, 0, 0 };
231 if (bma4_read_accel_xyz(&sens_data,
dev.get()) != 0) {
259 uint16_t int_status = 0;
260 bma4_read_int_status(&int_status,
dev.get());
261 return (int_status & BMA4_ACCEL_DATA_RDY_INT) != 0;
275 return bma4_soft_reset(
dev.get()) == 0;
289 int8_t selftest_rslt = 0;
290 if (bma4_perform_accel_selftest(&selftest_rslt,
dev.get()) != 0) {
294 return selftest_rslt == 0;
306 uint8_t regValue = 0;
309 regValue = BMA4_ACCEL_RANGE_2G;
312 regValue = BMA4_ACCEL_RANGE_4G;
315 regValue = BMA4_ACCEL_RANGE_8G;
318 regValue = BMA4_ACCEL_RANGE_16G;
343 int rangeInt =
static_cast<int>(data_rate_hz * 100 + 0.5);
345 uint8_t regValue = 0;
348 regValue = BMA4_OUTPUT_DATA_RATE_0_78HZ;
351 regValue = BMA4_OUTPUT_DATA_RATE_1_56HZ;
354 regValue = BMA4_OUTPUT_DATA_RATE_3_12HZ;
357 regValue = BMA4_OUTPUT_DATA_RATE_6_25HZ;
360 regValue = BMA4_OUTPUT_DATA_RATE_12_5HZ;
363 regValue = BMA4_OUTPUT_DATA_RATE_25HZ;
366 regValue = BMA4_OUTPUT_DATA_RATE_50HZ;
369 regValue = BMA4_OUTPUT_DATA_RATE_100HZ;
372 regValue = BMA4_OUTPUT_DATA_RATE_200HZ;
375 regValue = BMA4_OUTPUT_DATA_RATE_400HZ;
378 regValue = BMA4_OUTPUT_DATA_RATE_800HZ;
381 regValue = BMA4_OUTPUT_DATA_RATE_1600HZ;
403 uint8_t regValue = 0;
406 regValue = BMA4_ACCEL_OSR4_AVG1;
409 regValue = BMA4_ACCEL_OSR2_AVG2;
412 regValue = BMA4_ACCEL_NORMAL_AVG4;
415 regValue = BMA4_ACCEL_CIC_AVG8;
418 regValue = BMA4_ACCEL_RES_AVG16;
421 regValue = BMA4_ACCEL_RES_AVG32;
424 regValue = BMA4_ACCEL_RES_AVG64;
427 regValue = BMA4_ACCEL_RES_AVG128;
430 SENSORLIB_LOG_E(
"Error: Invalid bandwidth %d\n",
static_cast<int>(bandwidth));
450 uint8_t regValue = 0;
453 regValue = BMA4_CIC_AVG_MODE;
456 regValue = BMA4_CONTINUOUS_MODE;
459 SENSORLIB_LOG_E(
"Error: Invalid performance mode %d\n",
static_cast<int>(mode));
486 SENSORLIB_LOG_E(
"Error: Invalid operation mode %d\n",
static_cast<int>(mode));
489 if (bma4_set_accel_enable(enable,
dev.get()) == 0) {
547 int32_t raw_temperature = 0;
548 if (bma4_get_temperature(&raw_temperature, BMA4_DEG,
dev.get()) == 0) {
549 if (((raw_temperature - 23) / BMA4_SCALE_TEMP) != 0x80) {
550 temperature =
static_cast<float>(raw_temperature) / BMA4_SCALE_TEMP;
568 uint16_t int_status = 0;
569 if (bma4_read_int_status(&int_status,
dev.get()) != 0) {
586 return (int_status & interrupt_mask) != 0;
606 bool edge_trigger =
false,
607 bool active_low =
true,
608 bool output_en =
true,
609 bool input_en =
false)
611 struct bma4_int_pin_config config ;
612 config.edge_ctrl = edge_trigger ? BMA4_EDGE_TRIGGER : BMA4_LEVEL_TRIGGER;
613 config.lvl = active_low ? BMA4_ACTIVE_LOW : BMA4_ACTIVE_HIGH;
614 config.od = active_low ? BMA4_OPEN_DRAIN : BMA4_PUSH_PULL;
615 config.output_en = output_en ? BMA4_OUTPUT_ENABLE : BMA4_OUTPUT_DISABLE;
616 config.input_en = input_en ? BMA4_INPUT_ENABLE : BMA4_INPUT_DISABLE;
618 return bma4_set_int_pin_config(&config, int_line,
dev.get()) == 0;
636 uint8_t int_line = 0;
649 int8_t result = bma4_map_interrupt(int_line, interrupt_sources, enable,
dev.get());
652 enable ?
"enable" :
"disable",
674 struct bma4_remap remap_data;
677 remap_data.x = BMA4_NEG_Y;
678 remap_data.y = BMA4_X;
679 remap_data.z = BMA4_Z;
682 remap_data.x = BMA4_X;
683 remap_data.y = BMA4_Y;
684 remap_data.z = BMA4_Z;
687 remap_data.x = BMA4_Y;
688 remap_data.y = BMA4_NEG_Y;
689 remap_data.z = BMA4_Z;
692 remap_data.x = BMA4_NEG_X;
693 remap_data.y = BMA4_NEG_Y;
694 remap_data.z = BMA4_Z;
697 remap_data.x = BMA4_NEG_X;
698 remap_data.y = BMA4_Y;
699 remap_data.z = BMA4_NEG_Z;
702 remap_data.x = BMA4_Y;
703 remap_data.y = BMA4_X;
704 remap_data.z = BMA4_NEG_Z;
707 remap_data.x = BMA4_X;
708 remap_data.y = BMA4_NEG_Y;
709 remap_data.z = BMA4_NEG_Z;
712 remap_data.x = BMA4_NEG_Y;
713 remap_data.y = BMA4_NEG_X;
714 remap_data.z = BMA4_NEG_Z;
721 auto feature_config = std::unique_ptr<uint8_t[]>(
new uint8_t[
dev->feature_len]());
722 return bma4_set_remap_axes(&remap_data, feature_config.get(),
737 switch (
dev->chip_id) {
745 return "BMA456_AN_SEC";
772 uint32_t sensor_time_ticks = 0;
773 bma4_get_sensor_time(&sensor_time_ticks,
dev.get());
775 float time_ms = sensor_time_ticks * 0.0390625f;
776 return static_cast<uint32_t
>(time_ms);
800 bool interrupt_enable =
false,
805 (void)interrupt_enable;
814 bool interrupt_enable =
false,
819 (void)interrupt_enable;
848 virtual bool configMotion(
bool any_motion, uint16_t threshold, uint16_t duration)
883 (void)interrupt_enable;
892 bool reset_counter =
false)
895 (void)step_counter_wm;
907 (void)interrupt_enable;
919 (void)interrupt_enable;
931 (void)interrupt_enable;
950 virtual bool boschInitImpl() = 0;
959 bool initImpl(uint8_t param)
override
961 dev = std::make_unique<struct bma4_dev>();
968 dev->intf = BMA4_I2C_INTF;
971 dev->intf = BMA4_SPI_INTF;
980 dev->read_write_len = 32;
981 dev->perf_mode_status = BMA4_DISABLE;
983 if (bma4_soft_reset(
dev.get()) != 0) {
990 if (!boschInitImpl()) {
1003 struct bma4_axes_remap axes_remap;
1004 axes_remap.x_axis = BMA4_MAP_X_AXIS;
1005 axes_remap.x_axis_sign = BMA4_MAP_POSITIVE;
1006 axes_remap.y_axis = BMA4_MAP_Y_AXIS;
1007 axes_remap.y_axis_sign = BMA4_MAP_POSITIVE;
1008 axes_remap.z_axis = BMA4_MAP_Z_AXIS;
1009 axes_remap.z_axis_sign = BMA4_MAP_POSITIVE;
1010 dev->remap = axes_remap;
1046 std::unique_ptr<struct bma4_dev>
dev;
@ ACCELEROMETER
Accelerometer sensor.
OperationMode
Enumeration of sensor operation modes.
SensorRemap
Enumeration representing different remapping options for the sensor's orientation.
@ BOTTOM_LAYER_BOTTOM_RIGHT_CORNER
@ BOTTOM_LAYER_TOP_RIGHT_CORNER
@ TOP_LAYER_BOTTOM_LEFT_CORNER
@ BOTTOM_LAYER_TOP_LEFT_CORNER
@ BOTTOM_LAYER_BOTTOM_LEFT_CORNER
@ TOP_LAYER_BOTTOM_RIGHT_CORNER
AccelFullScaleRange
Enumeration of accelerometer full-scale range settings.
InterruptPinMap
Enumeration of interrupt pin mapping options.
#define SENSORLIB_LOG_I(...)
#define SENSORLIB_LOG_E(...)
#define SENSORLIB_LOG_D(...)
SensorTemperature temperature(bhy)
Abstract base class for accelerometer implementations.
std::unique_ptr< SensorCommStatic > staticComm
std::unique_ptr< SensorHal > hal
bool setBandwidth(AccelBandwidth bandwidth)
Set the bandwidth of the accelerometer.
bool setOperationMode(OperationMode mode) override
Set the operation mode of the accelerometer.
virtual bool enableNoMotionDetection(const MotionAxesConfig &cfg, bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable no-motion detection (default: not supported)
bma4_dev bma4_dev_t
typedef for BMA4 device structure
uint8_t _remap_reg_offset
bool isDataReady() override
Check if new accelerometer data is available.
virtual bool enableStepCounter(bool enable, uint16_t step_counter_wm=1, bool reset_counter=false)
Enable or disable step counter (default: not supported)
struct bma4_accel_config _accel_conf
virtual bool configMotion(bool any_motion, uint16_t threshold, uint16_t duration)
Configure motion detection (default: not supported)
virtual void update()=0
Update the sensor data, this method is implemented by a subclass.
bool enableInterrupt(InterruptPinMap pin_map, uint16_t interrupt_sources, bool enable)
Enable or disable specific interrupt sources on a pin.
virtual bool configAnyMotionInPhysicalUnits(float threshold_mg, float duration_ms)
Configure any-motion using physical units (default: not supported)
virtual bool configNoMotion(uint16_t threshold, uint16_t duration)
Configure no-motion threshold and duration (default: not supported)
virtual bool setRemapAxes(SensorRemap remap)
Set the remapping of axes for the BMA4XX sensor.
uint32_t getTimeSampleMs() const
Get the current sensor time in milliseconds.
~SensorBMA4XX()=default
Destructor for the SensorBMA4XX class.
uint16_t getInterruptStatus()
Get the current interrupt status flags.
bool setOutputDataRate(float data_rate_hz) override
Set the output data rate of the accelerometer.
virtual bool enableStepDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable step detector (default: not supported)
bool setPerformanceMode(AccelPerfMode mode)
Set the performance mode of the accelerometer.
bool setInterruptPinConfig(InterruptPinMap pin_map, bool edge_trigger=false, bool active_low=true, bool output_en=true, bool input_en=false)
Configure the electrical properties of an interrupt pin.
SensorBMA4XX()
Constructor for the SensorBMA4XX class.
bma4_dev_t * getDev()
Get the device structure for the BMA4XX sensor.
virtual bool enableAnyMotionDetection(const MotionAxesConfig &cfg, bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable any-motion detection (default: not supported)
virtual bool enableTapDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable tap detection (default: not supported)
virtual bool enableTiltDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable tilt detection (default: not supported)
virtual bool enableActivityRecognition(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable or disable activity recognition (default: not supported)
const char * getModelName() const
Get the model name of the BMA4XX sensor.
bool selfTest() override
Perform a self-test on the sensor.
float getTemperature() const
Get the temperature reading from the sensor.
bool reset() override
Reset the sensor.
bool isInterruptActive(uint16_t interrupt_mask)
Check if a specific interrupt is active.
virtual bool configAnyMotion(uint16_t threshold, uint16_t duration)
Configure any-motion threshold and duration (default: not supported)
virtual BMA4XXCapability::Capability getCapabilities() const =0
Get supported capabilities.
virtual bool enableDataReady(bool enable=true, InterruptPinMap pin_map=InterruptPinMap::PIN1)
Enable data ready interrupt (default: not supported)
bool readData(AccelerometerData &data) override
Get the accelerometer data.
bool configAccelerometer(OperationMode mode, AccelFullScaleRange range, float data_rate_hz, AccelBandwidth bandwidth, AccelPerfMode perf_mode)
Configure the accelerometer.
BMA4XXModel
Enumeration of BMA4XX models.
virtual bool configNoMotionInPhysicalUnits(float threshold_mg, float duration_ms)
Configure no-motion using physical units (default: not supported)
std::unique_ptr< struct bma4_dev > dev
bool setFullScaleRange(AccelFullScaleRange range) override
Set the full-scale range of the accelerometer.
SensorConfig _config
Current configuration.
SensorInfo _info
Sensor information.
static int8_t sensor_static_read_data(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr)
static int8_t sensor_static_write_data(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, void *intf_ptr)
static void sensor_static_delay_us(uint32_t us, void *private_data)
float rangeToG(AccelFullScaleRange range)
Convert full-scale range enumeration to g-force value.
float gToMps2(float g_value)
Convert acceleration from g-force to m/s²
constexpr bool operator&(Capability a, Capability b)
constexpr Capability operator|(Capability a, Capability b)
AccelBandwidth
BMA4 Accelerometer averaging filter configuration These values configure the internal averaging filte...
@ OSR2_AVG2
OSR2 mode with 2 samples averaged.
@ NORMAL_AVG4
Normal mode with 4 samples averaged.
@ RES_AVG32
Resolution mode with 32 samples averaged.
@ OSR4_AVG1
OSR4 mode with 1 sample averaging (lowest power, highest noise)
@ RES_AVG64
Resolution mode with 64 samples averaged.
@ CIC_AVG8
CIC filter with 8 samples averaged.
@ RES_AVG128
Resolution mode with 128 samples averaged (best noise reduction, highest power)
@ RES_AVG16
Resolution mode with 16 samples averaged.
ActivityType
Activity detection type.
TapType
Tap detection type.
AccelPerfMode
BMA4 Accelerometer performance mode configuration Configures the internal filter behavior for sample ...
@ CONTINUOUS_MODE
Continuous mode: no averaging, raw samples with lower latency.
@ CIC_AVG_MODE
Averaging filter mode: samples are averaged based on configured bandwidth and ODR.
Structure representing accelerometer data.
SensorVector mps2
Acceleration in meters per second squared (m/s²)
float temperature
Temperature in degrees Celsius (if available)
RawVector raw
Raw sensor values in LSB (least significant bits)
int16_t x
X-axis raw value.
int16_t y
Y-axis raw value.
int16_t z
Z-axis raw value.
Motion axes configuration.
uint8_t z_axis
Enable motion detection on Z-axis (1=enabled, 0=disabled)
uint8_t y_axis
Enable motion detection on Y-axis (1=enabled, 0=disabled)
MotionAxesConfig(uint8_t x=1, uint8_t y=1, uint8_t z=1)
Constructor for MotionAxesConfig.
uint8_t x_axis
Enable motion detection on X-axis (1=enabled, 0=disabled)
uint32_t latency
Reporting latency in milliseconds.
float sample_rate
Sample rate in Hz.
float range
Measurement range.
SensorType type
Type of the sensor.
OperationMode mode
Operation mode.
uint8_t version
Hardware/firmware version.
uint8_t i2c_address
Default I2C address.
uint32_t uid
Unique identifier.
SensorType type
Sensor type.
const char * manufacturer
Manufacturer name.
const char * model
Model name/identifier.