Skip to content

Commit 3af0804

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

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/drivers/Bma421.cpp

+30-12
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ 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

4142
auto ret = bma423_init(&bma);
@@ -58,10 +59,6 @@ void Bma421::Init() {
5859
if (ret != BMA4_OK)
5960
return;
6061

61-
ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma);
62-
if (ret != BMA4_OK)
63-
return;
64-
6562
ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma);
6663
if (ret != BMA4_OK)
6764
return;
@@ -74,11 +71,19 @@ void Bma421::Init() {
7471
if (ret != BMA4_OK)
7572
return;
7673

74+
ret = bma4_set_fifo_config(BMA4_FIFO_ACCEL, 1, &bma);
75+
if (ret != BMA4_OK)
76+
return;
77+
fifo_frame.data = (uint8_t*) &fifo;
78+
fifo_frame.length = sizeof(fifo);
79+
fifo_frame.fifo_data_enable = BMA4_FIFO_A_ENABLE;
80+
fifo_frame.fifo_header_enable = 0;
81+
7782
struct bma4_accel_config accel_conf;
78-
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
83+
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_200HZ;
7984
accel_conf.range = BMA4_ACCEL_RANGE_2G;
8085
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
81-
accel_conf.perf_mode = BMA4_CIC_AVG_MODE;
86+
accel_conf.perf_mode = BMA4_CONTINUOUS_MODE;
8287
ret = bma4_set_accel_config(&accel_conf, &bma);
8388
if (ret != BMA4_OK)
8489
return;
@@ -102,21 +107,34 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
102107
Bma421::Values Bma421::Process() {
103108
if (not isOk)
104109
return {};
105-
struct bma4_accel data;
106-
bma4_read_accel_xyz(&data, &bma);
110+
111+
bma4_read_fifo_data(&fifo_frame, &bma);
112+
uint16_t length = 32; // Should be about 20 (200 Hz ODR / 10 Hz main loop)
113+
bma4_extract_accel((bma4_accel*) fifo, &length, &fifo_frame, &bma);
107114

108115
uint32_t steps = 0;
109116
bma423_step_counter_output(&steps, &bma);
110117

111-
int32_t temperature;
118+
int32_t temperature = 0;
112119
bma4_get_temperature(&temperature, BMA4_DEG, &bma);
113120
temperature = temperature / 1000;
114121

115122
uint8_t activity = 0;
116123
bma423_activity_output(&activity, &bma);
117124

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};
125+
int16_t avgs[3] = {0};
126+
for (uint8_t i = 0; i < length; i++) {
127+
// X and Y axis are swapped because of the way the sensor is mounted in the PineTime
128+
int16_t swap = fifo[i][0];
129+
fifo[i][0] = fifo[i][1];
130+
fifo[i][1] = swap;
131+
for (uint8_t j = 0; j < 3; j++)
132+
avgs[j] += fifo[i][j];
133+
}
134+
for (uint8_t j = 0; j < 3; j++)
135+
avgs[j] /= length;
136+
137+
return {steps, avgs[0], avgs[1], avgs[2]};
120138
}
121139

122140
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)