@@ -32,16 +32,19 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}
32
32
bma.intf_ptr = this ;
33
33
bma.delay_us = user_delay;
34
34
bma.read_write_len = 16 ;
35
+ bma.resolution = 12 ;
35
36
}
36
37
37
38
void Bma421::Init () {
38
- if (not isResetOk)
39
+ if (! isResetOk)
39
40
return ; // Call SoftReset (and reset TWI device) first!
40
41
42
+ // Initialize interface
41
43
auto ret = bma423_init (&bma);
42
44
if (ret != BMA4_OK)
43
45
return ;
44
46
47
+ // Identify chip by ID. The driver code has been modified to handle BMA421 as BMA423
45
48
switch (bma.chip_id ) {
46
49
case BMA423_CHIP_ID:
47
50
deviceType = DeviceTypes::BMA421;
@@ -54,14 +57,12 @@ void Bma421::Init() {
54
57
break ;
55
58
}
56
59
60
+ // Load proprietary firmware blob required for step counting engine
57
61
ret = bma423_write_config_file (&bma);
58
62
if (ret != BMA4_OK)
59
63
return ;
60
64
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
65
66
ret = bma423_feature_enable (BMA423_STEP_CNTR, 1 , &bma);
66
67
if (ret != BMA4_OK)
67
68
return ;
@@ -74,11 +75,21 @@ void Bma421::Init() {
74
75
if (ret != BMA4_OK)
75
76
return ;
76
77
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
77
88
struct bma4_accel_config accel_conf;
78
- accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ ;
89
+ accel_conf.odr = BMA4_OUTPUT_DATA_RATE_200HZ ;
79
90
accel_conf.range = BMA4_ACCEL_RANGE_2G;
80
91
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
81
- accel_conf.perf_mode = BMA4_CIC_AVG_MODE ;
92
+ accel_conf.perf_mode = BMA4_CONTINUOUS_MODE ;
82
93
ret = bma4_set_accel_config (&accel_conf, &bma);
83
94
if (ret != BMA4_OK)
84
95
return ;
@@ -102,22 +113,42 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
102
113
Bma421::Values Bma421::Process () {
103
114
if (not isOk)
104
115
return {};
105
- struct bma4_accel data;
106
- bma4_read_accel_xyz (&data, &bma);
107
116
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
108
124
uint32_t steps = 0 ;
109
125
bma423_step_counter_output (&steps, &bma);
110
126
111
- int32_t temperature;
127
+ // Read temperature sensor
128
+ int32_t temperature = 0 ;
112
129
bma4_get_temperature (&temperature, BMA4_DEG, &bma);
113
130
temperature = temperature / 1000 ;
114
131
132
+ // Read sport activity mode
115
133
uint8_t activity = 0 ;
116
134
bma423_activity_output (&activity, &bma);
117
135
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 ]};
120
150
}
151
+
121
152
bool Bma421::IsOk () const {
122
153
return isOk;
123
154
}
0 commit comments