@@ -185,14 +185,12 @@ bool hmc5883lDetect(mag_t* mag, const hmc5883Config_t *hmc5883ConfigToUse)
185
185
return true;
186
186
}
187
187
188
- #define INIT_MAX_FAILURES 5
188
+ #define INITIALISATION_MAX_READ_FAILURES 5
189
189
bool hmc5883lInit (void )
190
190
{
191
191
int16_t magADC [3 ];
192
192
int32_t xyz_total [3 ] = { 0 , 0 , 0 }; // 32 bit totals so they won't overflow.
193
193
bool bret = true; // Error indicator
194
- bool error_read = false;
195
- bool error_saturation = false;
196
194
197
195
delay (50 );
198
196
i2cWrite (MAG_I2C_INSTANCE , MAG_ADDRESS , HMC58X3_R_CONFA , 0x010 + HMC_POS_BIAS ); // Reg A DOR = 0x010 + MS1, MS0 set to pos bias
@@ -202,68 +200,64 @@ bool hmc5883lInit(void)
202
200
delay (100 );
203
201
hmc5883lRead (magADC );
204
202
205
- int validSamples = 0 ;
206
- int failedSamples = 0 ;
207
- while (validSamples < 10 && failedSamples < INIT_MAX_FAILURES ) { // Collect 10 samples
203
+ int validSamples1 = 0 ;
204
+ int failedSamples1 = 0 ;
205
+ int saturatedSamples1 = 0 ;
206
+ while (validSamples1 < 10 && failedSamples1 < INITIALISATION_MAX_READ_FAILURES ) { // Collect 10 samples
208
207
i2cWrite (MAG_I2C_INSTANCE , MAG_ADDRESS , HMC58X3_R_MODE , 1 );
209
- delay (50 );
208
+ delay (70 );
210
209
if (hmc5883lRead (magADC )) { // Get the raw values in case the scales have already been changed.
211
- ++ validSamples ;
212
- // Since the measurements are noisy, they should be averaged rather than taking the max.
213
- xyz_total [X ] += magADC [X ];
214
- xyz_total [Y ] += magADC [Y ];
215
- xyz_total [Z ] += magADC [Z ];
216
210
// Detect saturation.
217
211
if (-4096 >= MIN (magADC [X ], MIN (magADC [Y ], magADC [Z ]))) {
218
- error_saturation = true;
219
- bret = false;
220
- break ; // Breaks out of the for loop. No sense in continuing if we saturated.
212
+ ++ saturatedSamples1 ;
213
+ ++ failedSamples1 ;
214
+ } else {
215
+ ++ validSamples1 ;
216
+ // Since the measurements are noisy, they should be averaged rather than taking the max.
217
+ xyz_total [X ] += magADC [X ];
218
+ xyz_total [Y ] += magADC [Y ];
219
+ xyz_total [Z ] += magADC [Z ];
220
+
221
221
}
222
222
} else {
223
- ++ failedSamples ;
223
+ ++ failedSamples1 ;
224
224
}
225
225
LED1_TOGGLE ;
226
226
}
227
227
228
- if (failedSamples >= INIT_MAX_FAILURES ) {
229
- bret = false;
230
- }
231
-
232
- if (failedSamples > 0 ) {
233
- error_read = true;
234
- }
235
-
236
228
// Apply the negative bias. (Same gain)
237
229
i2cWrite (MAG_I2C_INSTANCE , MAG_ADDRESS , HMC58X3_R_CONFA , 0x010 + HMC_NEG_BIAS ); // Reg A DOR = 0x010 + MS1, MS0 set to negative bias.
238
- validSamples = 0 ;
239
- failedSamples = 0 ;
240
- while (validSamples < 10 && failedSamples < INIT_MAX_FAILURES ) { // Collect 10 samples
230
+ int validSamples2 = 0 ;
231
+ int failedSamples2 = 0 ;
232
+ int saturatedSamples2 = 0 ;
233
+ while (validSamples2 < 10 && failedSamples2 < INITIALISATION_MAX_READ_FAILURES ) { // Collect 10 samples
241
234
i2cWrite (MAG_I2C_INSTANCE , MAG_ADDRESS , HMC58X3_R_MODE , 1 );
242
- delay (50 );
235
+ delay (70 );
243
236
if (hmc5883lRead (magADC )) { // Get the raw values in case the scales have already been changed.
244
- ++ validSamples ;
245
- // Since the measurements are noisy, they should be averaged.
246
- xyz_total [X ] -= magADC [X ];
247
- xyz_total [Y ] -= magADC [Y ];
248
- xyz_total [Z ] -= magADC [Z ];
249
237
// Detect saturation.
250
238
if (-4096 >= MIN (magADC [X ], MIN (magADC [Y ], magADC [Z ]))) {
251
- error_saturation = true;
252
- bret = false;
253
- break ; // Breaks out of the for loop. No sense in continuing if we saturated.
239
+ ++ saturatedSamples2 ;
240
+ ++ failedSamples2 ;
241
+ } else {
242
+ ++ validSamples2 ;
243
+ // Since the measurements are noisy, they should be averaged.
244
+ xyz_total [X ] -= magADC [X ];
245
+ xyz_total [Y ] -= magADC [Y ];
246
+ xyz_total [Z ] -= magADC [Z ];
254
247
}
255
248
} else {
256
- ++ failedSamples ;
249
+ ++ failedSamples2 ;
257
250
}
258
251
LED1_TOGGLE ;
259
252
}
260
253
261
- if (failedSamples >= INIT_MAX_FAILURES ) {
254
+ if (failedSamples1 >= INITIALISATION_MAX_READ_FAILURES || failedSamples2 >= INITIALISATION_MAX_READ_FAILURES ) {
255
+ addBootlogEvent4 (BOOT_EVENT_HMC5883L_READ_OK_COUNT , BOOT_EVENT_FLAGS_NONE , validSamples1 , validSamples2 );
256
+ addBootlogEvent4 (BOOT_EVENT_HMC5883L_READ_FAILED , BOOT_EVENT_FLAGS_WARNING , failedSamples1 , failedSamples2 );
262
257
bret = false;
263
258
}
264
-
265
- if (failedSamples > 0 ) {
266
- error_read = true;
259
+ if (saturatedSamples1 > 0 || saturatedSamples2 > 0 ) {
260
+ addBootlogEvent4 (BOOT_EVENT_HMC5883L_SATURATION , BOOT_EVENT_FLAGS_WARNING , saturatedSamples1 , saturatedSamples2 );
267
261
}
268
262
269
263
if (bret ) {
@@ -285,14 +279,6 @@ bool hmc5883lInit(void)
285
279
286
280
hmc5883lConfigureDataReadyInterruptHandling ();
287
281
288
- if (error_read ) {
289
- addBootlogEvent2 (BOOT_EVENT_HMC5883L_READ_FAILED , BOOT_EVENT_FLAGS_WARNING );
290
- }
291
-
292
- if (error_saturation ) {
293
- addBootlogEvent2 (BOOT_EVENT_HMC5883L_SATURATION , BOOT_EVENT_FLAGS_ERROR );
294
- }
295
-
296
282
return bret ;
297
283
}
298
284
0 commit comments