Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BMA421: Use internal FIFO for smoothing #1145

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 39 additions & 21 deletions src/drivers/Bma421.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}
bma.intf_ptr = this;
bma.delay_us = user_delay;
bma.read_write_len = 16;
bma.resolution = 12;
}

void Bma421::Init() {
if (not isResetOk)
if (!isResetOk)
return; // Call SoftReset (and reset TWI device) first!

auto ret = bma423_init(&bma);
Expand All @@ -68,10 +69,6 @@ void Bma421::Init() {
if (ret != BMA4_OK)
return;

ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma);
if (ret != BMA4_OK)
return;

ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma);
if (ret != BMA4_OK)
return;
Expand All @@ -84,10 +81,19 @@ void Bma421::Init() {
if (ret != BMA4_OK)
return;

accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
ret = bma4_set_fifo_config(BMA4_FIFO_ACCEL, 1, &bma);
if (ret != BMA4_OK)
return;
fifo_frame.data = (uint8_t*) &fifo;
fifo_frame.length = sizeof(fifo);
fifo_frame.fifo_data_enable = BMA4_FIFO_A_ENABLE;
fifo_frame.fifo_header_enable = 0;

struct bma4_accel_config accel_conf;
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_200HZ;
accel_conf.range = BMA4_ACCEL_RANGE_2G;
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
accel_conf.perf_mode = BMA4_CIC_AVG_MODE;
accel_conf.perf_mode = BMA4_CONTINUOUS_MODE;
ret = bma4_set_accel_config(&accel_conf, &bma);
if (ret != BMA4_OK)
return;
Expand All @@ -111,30 +117,42 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
Bma421::Values Bma421::Process() {
if (not isOk)
return {};
struct bma4_accel rawData;
struct bma4_accel data;
bma4_read_accel_xyz(&rawData, &bma);

// Scale the measured ADC counts to units of 'binary milli-g'
// where 1g = 1024 'binary milli-g' units.
// See https://github.com/InfiniTimeOrg/InfiniTime/pull/1950 for
// discussion of why we opted for scaling to 1024 rather than 1000.
data.x = 1024 * rawData.x / accelScaleFactors[accel_conf.range];
data.y = 1024 * rawData.y / accelScaleFactors[accel_conf.range];
data.z = 1024 * rawData.z / accelScaleFactors[accel_conf.range];

bma4_read_fifo_data(&fifo_frame, &bma);
uint16_t length = 32; // Should be about 20 (200 Hz ODR / 10 Hz main loop)
bma4_extract_accel((bma4_accel*) fifo, &length, &fifo_frame, &bma);

uint32_t steps = 0;
bma423_step_counter_output(&steps, &bma);

int32_t temperature;
int32_t temperature = 0;
bma4_get_temperature(&temperature, BMA4_DEG, &bma);
temperature = temperature / 1000;

uint8_t activity = 0;
bma423_activity_output(&activity, &bma);

// 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};
int16_t avgs[3] = {0};
for (uint8_t i = 0; i < length; i++) {
// X and Y axis are swapped because of the way the sensor is mounted in the PineTime
int16_t swap = fifo[i][0];
fifo[i][0] = fifo[i][1];
fifo[i][1] = swap;
for (uint8_t j = 0; j < 3; j++)
avgs[j] += fifo[i][j];
}

for (uint8_t j = 0; j < 3; j++)
{
avgs[j] /= length;
// Scale the measured ADC counts to units of 'binary milli-g'
// where 1g = 1024 'binary milli-g' units.
// See https://github.com/InfiniTimeOrg/InfiniTime/pull/1950 for
// discussion of why we opted for scaling to 1024 rather than 1000.
avgs[j] = 1024 * avgs[j] / accelScaleFactors[accel_conf.range];
}

return {steps, avgs[0], avgs[1], avgs[2]};
}

bool Bma421::IsOk() const {
Expand Down
2 changes: 2 additions & 0 deletions src/drivers/Bma421.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace Pinetime {
uint8_t deviceAddress = 0x18;
struct bma4_dev bma;
struct bma4_accel_config accel_conf; // Store the device configuration for later reference.
struct bma4_fifo_frame fifo_frame;
int16_t fifo[32][3] = {0};
bool isOk = false;
bool isResetOk = false;
DeviceTypes deviceType = DeviceTypes::Unknown;
Expand Down
Loading