@@ -42,10 +42,11 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}
42
42
bma.intf_ptr = this ;
43
43
bma.delay_us = user_delay;
44
44
bma.read_write_len = 16 ;
45
+ bma.resolution = 12 ;
45
46
}
46
47
47
48
void Bma421::Init () {
48
- if (not isResetOk)
49
+ if (! isResetOk)
49
50
return ; // Call SoftReset (and reset TWI device) first!
50
51
51
52
auto ret = bma423_init (&bma);
@@ -68,10 +69,6 @@ void Bma421::Init() {
68
69
if (ret != BMA4_OK)
69
70
return ;
70
71
71
- ret = bma4_set_interrupt_mode (BMA4_LATCH_MODE, &bma);
72
- if (ret != BMA4_OK)
73
- return ;
74
-
75
72
ret = bma423_feature_enable (BMA423_STEP_CNTR, 1 , &bma);
76
73
if (ret != BMA4_OK)
77
74
return ;
@@ -84,10 +81,19 @@ void Bma421::Init() {
84
81
if (ret != BMA4_OK)
85
82
return ;
86
83
87
- accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
84
+ ret = bma4_set_fifo_config (BMA4_FIFO_ACCEL, 1 , &bma);
85
+ if (ret != BMA4_OK)
86
+ return ;
87
+ fifo_frame.data = (uint8_t *) &fifo;
88
+ fifo_frame.length = sizeof (fifo);
89
+ fifo_frame.fifo_data_enable = BMA4_FIFO_A_ENABLE;
90
+ fifo_frame.fifo_header_enable = 0 ;
91
+
92
+ struct bma4_accel_config accel_conf;
93
+ accel_conf.odr = BMA4_OUTPUT_DATA_RATE_200HZ;
88
94
accel_conf.range = BMA4_ACCEL_RANGE_2G;
89
95
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
90
- accel_conf.perf_mode = BMA4_CIC_AVG_MODE ;
96
+ accel_conf.perf_mode = BMA4_CONTINUOUS_MODE ;
91
97
ret = bma4_set_accel_config (&accel_conf, &bma);
92
98
if (ret != BMA4_OK)
93
99
return ;
@@ -111,30 +117,42 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
111
117
Bma421::Values Bma421::Process () {
112
118
if (not isOk)
113
119
return {};
114
- struct bma4_accel rawData;
115
- struct bma4_accel data;
116
- bma4_read_accel_xyz (&rawData, &bma);
117
-
118
- // Scale the measured ADC counts to units of 'binary milli-g'
119
- // where 1g = 1024 'binary milli-g' units.
120
- // See https://github.com/InfiniTimeOrg/InfiniTime/pull/1950 for
121
- // discussion of why we opted for scaling to 1024 rather than 1000.
122
- data.x = 1024 * rawData.x / accelScaleFactors[accel_conf.range ];
123
- data.y = 1024 * rawData.y / accelScaleFactors[accel_conf.range ];
124
- data.z = 1024 * rawData.z / accelScaleFactors[accel_conf.range ];
120
+
121
+ bma4_read_fifo_data (&fifo_frame, &bma);
122
+ uint16_t length = 32 ; // Should be about 20 (200 Hz ODR / 10 Hz main loop)
123
+ bma4_extract_accel ((bma4_accel*) fifo, &length, &fifo_frame, &bma);
125
124
126
125
uint32_t steps = 0 ;
127
126
bma423_step_counter_output (&steps, &bma);
128
127
129
- int32_t temperature;
128
+ int32_t temperature = 0 ;
130
129
bma4_get_temperature (&temperature, BMA4_DEG, &bma);
131
130
temperature = temperature / 1000 ;
132
131
133
132
uint8_t activity = 0 ;
134
133
bma423_activity_output (&activity, &bma);
135
134
136
- // X and Y axis are swapped because of the way the sensor is mounted in the PineTime
137
- return {steps, data.y , data.x , data.z };
135
+ int16_t avgs[3 ] = {0 };
136
+ for (uint8_t i = 0 ; i < length; i++) {
137
+ // X and Y axis are swapped because of the way the sensor is mounted in the PineTime
138
+ int16_t swap = fifo[i][0 ];
139
+ fifo[i][0 ] = fifo[i][1 ];
140
+ fifo[i][1 ] = swap;
141
+ for (uint8_t j = 0 ; j < 3 ; j++)
142
+ avgs[j] += fifo[i][j];
143
+ }
144
+
145
+ for (uint8_t j = 0 ; j < 3 ; j++)
146
+ {
147
+ avgs[j] /= length;
148
+ // Scale the measured ADC counts to units of 'binary milli-g'
149
+ // where 1g = 1024 'binary milli-g' units.
150
+ // See https://github.com/InfiniTimeOrg/InfiniTime/pull/1950 for
151
+ // discussion of why we opted for scaling to 1024 rather than 1000.
152
+ avgs[j] = 1024 * avgs[j] / accelScaleFactors[accel_conf.range ];
153
+ }
154
+
155
+ return {steps, avgs[0 ], avgs[1 ], avgs[2 ]};
138
156
}
139
157
140
158
bool Bma421::IsOk () const {
0 commit comments