Skip to content

Commit bce82e6

Browse files
committed
BMA421: Use internal FIFO for smoothing
1 parent ecf2f56 commit bce82e6

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/drivers/Bma421.cpp

+42-12
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}
3232
bma.intf_ptr = this;
3333
bma.delay_us = user_delay;
3434
bma.read_write_len = 16;
35+
bma.resolution = 12;
3536
}
3637

3738
void Bma421::Init() {
38-
if (not isResetOk)
39+
if (!isResetOk)
3940
return; // Call SoftReset (and reset TWI device) first!
4041

42+
// Initialize interface
4143
auto ret = bma423_init(&bma);
4244
if (ret != BMA4_OK)
4345
return;
4446

47+
// Identify chip by ID. The driver code has been modified to handle BMA421 as BMA423
4548
switch (bma.chip_id) {
4649
case BMA423_CHIP_ID:
4750
deviceType = DeviceTypes::BMA421;
@@ -54,14 +57,12 @@ void Bma421::Init() {
5457
break;
5558
}
5659

60+
// Load proprietary firmware blob required for step counting engine
5761
ret = bma423_write_config_file(&bma);
5862
if (ret != BMA4_OK)
5963
return;
6064

61-
ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma);
62-
if (ret != BMA4_OK)
63-
return;
64-
65+
// Enable step counter and accelerometer, disable step detector
6566
ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma);
6667
if (ret != BMA4_OK)
6768
return;
@@ -74,11 +75,21 @@ void Bma421::Init() {
7475
if (ret != BMA4_OK)
7576
return;
7677

78+
// Configure FIFO
79+
ret = bma4_set_fifo_config(BMA4_FIFO_ACCEL, 1, &bma);
80+
if (ret != BMA4_OK)
81+
return;
82+
fifo_frame.data = (uint8_t*) &fifo;
83+
fifo_frame.length = sizeof(fifo);
84+
fifo_frame.fifo_data_enable = BMA4_FIFO_A_ENABLE;
85+
fifo_frame.fifo_header_enable = 0;
86+
87+
// Configure accelerometer
7788
struct bma4_accel_config accel_conf;
78-
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
89+
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_200HZ;
7990
accel_conf.range = BMA4_ACCEL_RANGE_2G;
8091
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
81-
accel_conf.perf_mode = BMA4_CIC_AVG_MODE;
92+
accel_conf.perf_mode = BMA4_CONTINUOUS_MODE;
8293
ret = bma4_set_accel_config(&accel_conf, &bma);
8394
if (ret != BMA4_OK)
8495
return;
@@ -102,21 +113,40 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
102113
Bma421::Values Bma421::Process() {
103114
if (not isOk)
104115
return {};
105-
struct bma4_accel data;
106-
bma4_read_accel_xyz(&data, &bma);
107116

117+
// Dump entire FIFO into buffer
118+
bma4_read_fifo_data(&fifo_frame, &bma);
119+
// Decode FIFO frames in-place sequentially
120+
uint16_t length = 32; // Should be about 20 (200 Hz ODR / 10 Hz main loop)
121+
bma4_extract_accel((bma4_accel*) fifo, &length, &fifo_frame, &bma);
122+
123+
// Read step counter engine
108124
uint32_t steps = 0;
109125
bma423_step_counter_output(&steps, &bma);
110126

111-
int32_t temperature;
127+
// Read temperature sensor
128+
int32_t temperature = 0;
112129
bma4_get_temperature(&temperature, BMA4_DEG, &bma);
113130
temperature = temperature / 1000;
114131

132+
// Read sport activity mode
115133
uint8_t activity = 0;
116134
bma423_activity_output(&activity, &bma);
117135

118-
// X and Y axis are swapped because of the way the sensor is mounted in the PineTime
119-
return {steps, data.y, data.x, data.z};
136+
// Compute averages of FIFO
137+
int16_t avgs[3] = {0};
138+
for (uint8_t i = 0; i < length; i++) {
139+
// X and Y axis are swapped because of the way the sensor is mounted in the PineTime
140+
int16_t swap = fifo[i][0];
141+
fifo[i][0] = fifo[i][1];
142+
fifo[i][1] = swap;
143+
for (uint8_t j = 0; j < 3; j++)
144+
avgs[j] += fifo[i][j];
145+
}
146+
for (uint8_t j = 0; j < 3; j++)
147+
avgs[j] /= length;
148+
149+
return {steps, avgs[0], avgs[1], avgs[2]};
120150
}
121151

122152
bool Bma421::IsOk() const {

src/drivers/Bma421.h

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ namespace Pinetime {
4141
TwiMaster& twiMaster;
4242
uint8_t deviceAddress = 0x18;
4343
struct bma4_dev bma;
44+
struct bma4_fifo_frame fifo_frame;
45+
int16_t fifo[32][3] = {0};
4446
bool isOk = false;
4547
bool isResetOk = false;
4648
DeviceTypes deviceType = DeviceTypes::Unknown;

0 commit comments

Comments
 (0)