2021-11-05 18:55:34 -04:00
|
|
|
#include "drivers/Bma421.h"
|
2021-03-31 13:47:27 -04:00
|
|
|
#include <libraries/delay/nrf_delay.h>
|
|
|
|
#include <libraries/log/nrf_log.h>
|
2021-10-13 16:08:35 -04:00
|
|
|
#include "drivers/TwiMaster.h"
|
2021-03-31 13:47:27 -04:00
|
|
|
#include <drivers/Bma421_C/bma423.h>
|
|
|
|
|
|
|
|
using namespace Pinetime::Drivers;
|
|
|
|
|
2021-04-01 15:18:59 -04:00
|
|
|
namespace {
|
|
|
|
int8_t user_i2c_read(uint8_t reg_addr, uint8_t* reg_data, uint32_t length, void* intf_ptr) {
|
|
|
|
auto bma421 = static_cast<Bma421*>(intf_ptr);
|
|
|
|
bma421->Read(reg_addr, reg_data, length);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t user_i2c_write(uint8_t reg_addr, const uint8_t* reg_data, uint32_t length, void* intf_ptr) {
|
|
|
|
auto bma421 = static_cast<Bma421*>(intf_ptr);
|
|
|
|
bma421->Write(reg_addr, reg_data, length);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void user_delay(uint32_t period_us, void* intf_ptr) {
|
|
|
|
nrf_delay_us(period_us);
|
|
|
|
}
|
2021-03-31 13:47:27 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, deviceAddress {twiAddress} {
|
2021-03-31 13:47:27 -04:00
|
|
|
bma.intf = BMA4_I2C_INTF;
|
|
|
|
bma.bus_read = user_i2c_read;
|
|
|
|
bma.bus_write = user_i2c_write;
|
|
|
|
bma.variant = BMA42X_VARIANT;
|
|
|
|
bma.intf_ptr = this;
|
|
|
|
bma.delay_us = user_delay;
|
2021-04-10 14:09:33 -04:00
|
|
|
bma.read_write_len = 16;
|
2021-03-31 13:47:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bma421::Init() {
|
2021-04-18 13:28:14 -04:00
|
|
|
if (not isResetOk)
|
|
|
|
return; // Call SoftReset (and reset TWI device) first!
|
2021-03-31 13:47:27 -04:00
|
|
|
|
2021-04-08 14:07:24 -04:00
|
|
|
auto ret = bma423_init(&bma);
|
2021-04-18 13:28:14 -04:00
|
|
|
if (ret != BMA4_OK)
|
|
|
|
return;
|
2021-03-31 13:47:27 -04:00
|
|
|
|
2022-05-09 11:16:08 -04:00
|
|
|
switch (bma.chip_id) {
|
|
|
|
case BMA423_CHIP_ID:
|
|
|
|
deviceType = DeviceTypes::BMA421;
|
|
|
|
break;
|
|
|
|
case BMA425_CHIP_ID:
|
|
|
|
deviceType = DeviceTypes::BMA425;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
deviceType = DeviceTypes::Unknown;
|
|
|
|
break;
|
2021-06-19 14:27:59 -04:00
|
|
|
}
|
|
|
|
|
2021-03-31 13:47:27 -04:00
|
|
|
ret = bma423_write_config_file(&bma);
|
2021-04-18 13:28:14 -04:00
|
|
|
if (ret != BMA4_OK)
|
|
|
|
return;
|
2021-03-31 13:47:27 -04:00
|
|
|
|
2021-04-01 15:18:59 -04:00
|
|
|
ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma);
|
2021-04-18 13:28:14 -04:00
|
|
|
if (ret != BMA4_OK)
|
|
|
|
return;
|
2021-03-31 13:47:27 -04:00
|
|
|
|
2021-04-01 15:18:59 -04:00
|
|
|
ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma);
|
2021-04-18 13:28:14 -04:00
|
|
|
if (ret != BMA4_OK)
|
|
|
|
return;
|
2021-03-31 13:47:27 -04:00
|
|
|
|
2021-04-01 15:18:59 -04:00
|
|
|
ret = bma423_step_detector_enable(0, &bma);
|
2021-04-18 13:28:14 -04:00
|
|
|
if (ret != BMA4_OK)
|
|
|
|
return;
|
2021-03-31 13:47:27 -04:00
|
|
|
|
|
|
|
ret = bma4_set_accel_enable(1, &bma);
|
2021-04-18 13:28:14 -04:00
|
|
|
if (ret != BMA4_OK)
|
|
|
|
return;
|
2021-03-31 13:47:27 -04:00
|
|
|
|
2021-04-01 15:18:59 -04:00
|
|
|
struct bma4_accel_config accel_conf;
|
|
|
|
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
|
|
|
|
accel_conf.range = BMA4_ACCEL_RANGE_2G;
|
|
|
|
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
|
|
|
|
accel_conf.perf_mode = BMA4_CIC_AVG_MODE;
|
2021-03-31 13:47:27 -04:00
|
|
|
ret = bma4_set_accel_config(&accel_conf, &bma);
|
2021-04-18 13:28:14 -04:00
|
|
|
if (ret != BMA4_OK)
|
|
|
|
return;
|
2021-04-02 10:56:14 -04:00
|
|
|
|
|
|
|
isOk = true;
|
2021-03-31 13:47:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bma421::Reset() {
|
|
|
|
uint8_t data = 0xb6;
|
|
|
|
twiMaster.Write(deviceAddress, 0x7E, &data, 1);
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void Bma421::Read(uint8_t registerAddress, uint8_t* buffer, size_t size) {
|
2021-03-31 13:47:27 -04:00
|
|
|
twiMaster.Read(deviceAddress, registerAddress, buffer, size);
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
|
2021-03-31 13:47:27 -04:00
|
|
|
twiMaster.Write(deviceAddress, registerAddress, data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bma421::Values Bma421::Process() {
|
2021-04-18 13:28:14 -04:00
|
|
|
if (not isOk)
|
|
|
|
return {};
|
2021-03-31 13:47:27 -04:00
|
|
|
struct bma4_accel data;
|
|
|
|
bma4_read_accel_xyz(&data, &bma);
|
|
|
|
|
|
|
|
uint32_t steps = 0;
|
|
|
|
bma423_step_counter_output(&steps, &bma);
|
|
|
|
|
|
|
|
int32_t temperature;
|
|
|
|
bma4_get_temperature(&temperature, BMA4_DEG, &bma);
|
|
|
|
temperature = temperature / 1000;
|
|
|
|
|
|
|
|
uint8_t activity = 0;
|
|
|
|
bma423_activity_output(&activity, &bma);
|
|
|
|
|
2021-04-01 15:18:59 -04:00
|
|
|
// X and Y axis are swapped because of the way the sensor is mounted in the PineTime
|
|
|
|
return {steps, data.y, data.x, data.z};
|
2021-03-31 13:47:27 -04:00
|
|
|
}
|
2023-01-03 09:05:30 -05:00
|
|
|
|
2021-04-02 10:56:14 -04:00
|
|
|
bool Bma421::IsOk() const {
|
|
|
|
return isOk;
|
|
|
|
}
|
2021-04-02 11:33:49 -04:00
|
|
|
|
|
|
|
void Bma421::ResetStepCounter() {
|
|
|
|
bma423_reset_step_counter(&bma);
|
|
|
|
}
|
2021-04-08 14:07:24 -04:00
|
|
|
|
|
|
|
void Bma421::SoftReset() {
|
|
|
|
auto ret = bma4_soft_reset(&bma);
|
2021-04-18 13:28:14 -04:00
|
|
|
if (ret == BMA4_OK) {
|
2021-04-08 14:07:24 -04:00
|
|
|
isResetOk = true;
|
|
|
|
nrf_delay_ms(1);
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 09:05:30 -05:00
|
|
|
|
2021-06-19 14:27:59 -04:00
|
|
|
Bma421::DeviceTypes Bma421::DeviceType() const {
|
|
|
|
return deviceType;
|
|
|
|
}
|