-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmm150.cpp
686 lines (593 loc) · 24.2 KB
/
bmm150.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#include "bmm150.h"
#include "stm32f4xx_hal.h"
BMM150::BMM150() {
}
int8_t BMM150::initialize(I2C_HandleTypeDef* hi2c1) {
/* Power up the sensor from suspend to sleep mode */
set_op_mode(BMM150_SLEEP_MODE, hi2c1);
HAL_Delay(BMM150_START_UP_TIME);
/* Check chip ID */
uint8_t id = i2c_read(BMM150_CHIP_ID_ADDR, hi2c1);
if (id != BMM150_CHIP_ID) {
return BMM150_E_ID_NOT_CONFORM;
}
/* Function to update trim values */
read_trim_registers(hi2c1);
/* Setting the power mode as normal */
set_op_mode(BMM150_NORMAL_MODE, hi2c1);
/* Setting the preset mode as Low power mode
i.e. data rate = 10Hz XY-rep = 1 Z-rep = 2*/
//set_presetmode(BMM150_PRESETMODE_LOWPOWER, hi2c1);
return BMM150_OK;
}
void BMM150::read_mag_data(I2C_HandleTypeDef* hi2c1) {
int8_t all_reg[8] = {0};
int16_t msb_data;
i2c_read(BMM150_DATA_X_LSB, all_reg, 1, 8, hi2c1);
/* Mag X axis data */
all_reg[0] = BMM150_GET_BITS(all_reg[0], BMM150_DATA_X);
/* Shift the MSB data to left by 5 bits */
/* Multiply by 32 to get the shift left by 5 value */
msb_data = ((int16_t)((int8_t)all_reg[1])) * 32;
/* Raw mag X axis data */
raw_mag_data.raw_datax = (int16_t)(msb_data | all_reg[0]);
/* Mag Y axis data */
all_reg[2] = BMM150_GET_BITS(all_reg[2], BMM150_DATA_Y);
/* Shift the MSB data to left by 5 bits */
/* Multiply by 32 to get the shift left by 5 value */
msb_data = ((int16_t)((int8_t)all_reg[3])) * 32;
/* Raw mag Y axis data */
raw_mag_data.raw_datay = (int16_t)(msb_data | all_reg[2]);
/* Mag Z axis data */
all_reg[4] = BMM150_GET_BITS(all_reg[4], BMM150_DATA_Z);
/* Shift the MSB data to left by 7 bits */
/* Multiply by 128 to get the shift left by 7 value */
msb_data = ((int16_t)((int8_t)all_reg[5])) * 128;
/* Raw mag Z axis data */
raw_mag_data.raw_dataz = (int16_t)(msb_data | all_reg[4]);
/* Mag R-HALL data */
all_reg[6] = BMM150_GET_BITS(all_reg[6], BMM150_DATA_RHALL);
raw_mag_data.raw_data_r = (uint16_t)(((uint16_t)all_reg[7] << 6) | all_reg[6]);
/* Compensated Mag X data in int16_t format */
mag_data.x = compensate_x(raw_mag_data.raw_datax, raw_mag_data.raw_data_r);
/* Compensated Mag Y data in int16_t format */
mag_data.y = compensate_y(raw_mag_data.raw_datay, raw_mag_data.raw_data_r);
/* Compensated Mag Z data in int16_t format */
mag_data.z = compensate_z(raw_mag_data.raw_dataz, raw_mag_data.raw_data_r);
}
/*
@brief This internal API is used to obtain the compensated
magnetometer X axis data(micro-tesla) in int16_t.
*/
int16_t BMM150::compensate_x(int16_t mag_data_x, uint16_t data_rhall) {
int16_t retval;
uint16_t process_comp_x0 = 0;
int32_t process_comp_x1;
uint16_t process_comp_x2;
int32_t process_comp_x3;
int32_t process_comp_x4;
int32_t process_comp_x5;
int32_t process_comp_x6;
int32_t process_comp_x7;
int32_t process_comp_x8;
int32_t process_comp_x9;
int32_t process_comp_x10;
/* Overflow condition check */
if (mag_data_x != BMM150_XYAXES_FLIP_OVERFLOW_ADCVAL) {
if (data_rhall != 0) {
/* Availability of valid data*/
process_comp_x0 = data_rhall;
} else if (trim_data.dig_xyz1 != 0) {
process_comp_x0 = trim_data.dig_xyz1;
} else {
process_comp_x0 = 0;
}
if (process_comp_x0 != 0) {
/* Processing compensation equations*/
process_comp_x1 = ((int32_t)trim_data.dig_xyz1) * 16384;
process_comp_x2 = ((uint16_t)(process_comp_x1 / process_comp_x0)) - ((uint16_t)0x4000);
retval = ((int16_t)process_comp_x2);
process_comp_x3 = (((int32_t)retval) * ((int32_t)retval));
process_comp_x4 = (((int32_t)trim_data.dig_xy2) * (process_comp_x3 / 128));
process_comp_x5 = (int32_t)(((int16_t)trim_data.dig_xy1) * 128);
process_comp_x6 = ((int32_t)retval) * process_comp_x5;
process_comp_x7 = (((process_comp_x4 + process_comp_x6) / 512) + ((int32_t)0x100000));
process_comp_x8 = ((int32_t)(((int16_t)trim_data.dig_x2) + ((int16_t)0xA0)));
process_comp_x9 = ((process_comp_x7 * process_comp_x8) / 4096);
process_comp_x10 = ((int32_t)mag_data_x) * process_comp_x9;
retval = ((int16_t)(process_comp_x10 / 8192));
retval = (retval + (((int16_t)trim_data.dig_x1) * 8)) / 16;
} else {
retval = BMM150_OVERFLOW_OUTPUT;
}
} else {
/* Overflow condition */
retval = BMM150_OVERFLOW_OUTPUT;
}
return retval;
}
/*
@brief This internal API is used to obtain the compensated
magnetometer Y axis data(micro-tesla) in int16_t.
*/
int16_t BMM150::compensate_y(int16_t mag_data_y, uint16_t data_rhall) {
int16_t retval;
uint16_t process_comp_y0 = 0;
int32_t process_comp_y1;
uint16_t process_comp_y2;
int32_t process_comp_y3;
int32_t process_comp_y4;
int32_t process_comp_y5;
int32_t process_comp_y6;
int32_t process_comp_y7;
int32_t process_comp_y8;
int32_t process_comp_y9;
/* Overflow condition check */
if (mag_data_y != BMM150_XYAXES_FLIP_OVERFLOW_ADCVAL) {
if (data_rhall != 0) {
/* Availability of valid data*/
process_comp_y0 = data_rhall;
} else if (trim_data.dig_xyz1 != 0) {
process_comp_y0 = trim_data.dig_xyz1;
} else {
process_comp_y0 = 0;
}
if (process_comp_y0 != 0) {
/*Processing compensation equations*/
process_comp_y1 = (((int32_t)trim_data.dig_xyz1) * 16384) / process_comp_y0;
process_comp_y2 = ((uint16_t)process_comp_y1) - ((uint16_t)0x4000);
retval = ((int16_t)process_comp_y2);
process_comp_y3 = ((int32_t) retval) * ((int32_t)retval);
process_comp_y4 = ((int32_t)trim_data.dig_xy2) * (process_comp_y3 / 128);
process_comp_y5 = ((int32_t)(((int16_t)trim_data.dig_xy1) * 128));
process_comp_y6 = ((process_comp_y4 + (((int32_t)retval) * process_comp_y5)) / 512);
process_comp_y7 = ((int32_t)(((int16_t)trim_data.dig_y2) + ((int16_t)0xA0)));
process_comp_y8 = (((process_comp_y6 + ((int32_t)0x100000)) * process_comp_y7) / 4096);
process_comp_y9 = (((int32_t)mag_data_y) * process_comp_y8);
retval = (int16_t)(process_comp_y9 / 8192);
retval = (retval + (((int16_t)trim_data.dig_y1) * 8)) / 16;
} else {
retval = BMM150_OVERFLOW_OUTPUT;
}
} else {
/* Overflow condition*/
retval = BMM150_OVERFLOW_OUTPUT;
}
return retval;
}
/*
@brief This internal API is used to obtain the compensated
magnetometer Z axis data(micro-tesla) in int16_t.
*/
int16_t BMM150::compensate_z(int16_t mag_data_z, uint16_t data_rhall) {
int32_t retval;
int16_t process_comp_z0;
int32_t process_comp_z1;
int32_t process_comp_z2;
int32_t process_comp_z3;
int16_t process_comp_z4;
if (mag_data_z != BMM150_ZAXIS_HALL_OVERFLOW_ADCVAL) {
if ((trim_data.dig_z2 != 0) && (trim_data.dig_z1 != 0)
&& (data_rhall != 0) && (trim_data.dig_xyz1 != 0)) {
/*Processing compensation equations*/
process_comp_z0 = ((int16_t)data_rhall) - ((int16_t) trim_data.dig_xyz1);
process_comp_z1 = (((int32_t)trim_data.dig_z3) * ((int32_t)(process_comp_z0))) / 4;
process_comp_z2 = (((int32_t)(mag_data_z - trim_data.dig_z4)) * 32768);
process_comp_z3 = ((int32_t)trim_data.dig_z1) * (((int16_t)data_rhall) * 2);
process_comp_z4 = (int16_t)((process_comp_z3 + (32768)) / 65536);
retval = ((process_comp_z2 - process_comp_z1) / (trim_data.dig_z2 + process_comp_z4));
/* saturate result to +/- 2 micro-tesla */
if (retval > BMM150_POSITIVE_SATURATION_Z) {
retval = BMM150_POSITIVE_SATURATION_Z;
} else {
if (retval < BMM150_NEGATIVE_SATURATION_Z) {
retval = BMM150_NEGATIVE_SATURATION_Z;
}
}
/* Conversion of LSB to micro-tesla*/
retval = retval / 16;
} else {
retval = BMM150_OVERFLOW_OUTPUT;
}
} else {
/* Overflow condition*/
retval = BMM150_OVERFLOW_OUTPUT;
}
return (int16_t)retval;
}
void BMM150::set_presetmode(uint8_t preset_mode, I2C_HandleTypeDef* hi2c1) {
switch (preset_mode) {
case BMM150_PRESETMODE_LOWPOWER:
/* Set the data rate x,y,z repetition
for Low Power mode */
settings.data_rate = BMM150_DATA_RATE_10HZ;
settings.xy_rep = BMM150_LOWPOWER_REPXY;
settings.z_rep = BMM150_LOWPOWER_REPZ;
set_odr_xyz_rep(settings, hi2c1);
break;
case BMM150_PRESETMODE_REGULAR:
/* Set the data rate x,y,z repetition
for Regular mode */
settings.data_rate = BMM150_DATA_RATE_10HZ;
settings.xy_rep = BMM150_REGULAR_REPXY;
settings.z_rep = BMM150_REGULAR_REPZ;
set_odr_xyz_rep(settings, hi2c1);
break;
case BMM150_PRESETMODE_HIGHACCURACY:
/* Set the data rate x,y,z repetition
for High Accuracy mode */
settings.data_rate = BMM150_DATA_RATE_20HZ;
settings.xy_rep = BMM150_HIGHACCURACY_REPXY;
settings.z_rep = BMM150_HIGHACCURACY_REPZ;
set_odr_xyz_rep(settings, hi2c1);
break;
case BMM150_PRESETMODE_ENHANCED:
/* Set the data rate x,y,z repetition
for Enhanced Accuracy mode */
settings.data_rate = BMM150_DATA_RATE_10HZ;
settings.xy_rep = BMM150_ENHANCED_REPXY;
settings.z_rep = BMM150_ENHANCED_REPZ;
set_odr_xyz_rep(settings, hi2c1);
break;
default:
break;
}
}
void BMM150::set_odr_xyz_rep(struct bmm150_settings settings, I2C_HandleTypeDef* hi2c1) {
/* Set the ODR */
set_odr(settings, hi2c1);
/* Set the XY-repetitions number */
set_xy_rep(settings, hi2c1);
/* Set the Z-repetitions number */
set_z_rep(settings, hi2c1);
}
void BMM150::set_xy_rep(struct bmm150_settings settings, I2C_HandleTypeDef* hi2c1) {
uint8_t rep_xy;
rep_xy = settings.xy_rep;
i2c_write(BMM150_REP_XY_ADDR, rep_xy, hi2c1);
}
void BMM150::set_z_rep(struct bmm150_settings settings, I2C_HandleTypeDef* hi2c1) {
uint8_t rep_z;
rep_z = settings.z_rep;
i2c_write(BMM150_REP_Z_ADDR, rep_z, hi2c1);
}
void BMM150::soft_reset(I2C_HandleTypeDef* hi2c1) {
uint8_t reg_data;
reg_data = i2c_read(BMM150_POWER_CONTROL_ADDR, hi2c1);
reg_data = reg_data | BMM150_SET_SOFT_RESET;
i2c_write(BMM150_POWER_CONTROL_ADDR, reg_data, hi2c1);
HAL_Delay(BMM150_SOFT_RESET_DELAY);
}
void BMM150::set_odr(struct bmm150_settings settings, I2C_HandleTypeDef* hi2c1) {
uint8_t reg_data;
reg_data = i2c_read(BMM150_OP_MODE_ADDR, hi2c1);
/*Set the ODR value */
reg_data = BMM150_SET_BITS(reg_data, BMM150_ODR, settings.data_rate);
i2c_write(BMM150_OP_MODE_ADDR, reg_data, hi2c1);
}
void BMM150::i2c_write(short mem_address, short data, I2C_HandleTypeDef* hi2c1) {
HAL_I2C_Mem_Write(hi2c1, (uint16_t)BMM150_I2C_Address<<1, mem_address, (uint16_t) 1, (uint8_t *)&data, (uint16_t)1, HAL_MAX_DELAY);
}
void BMM150::i2c_read(short mem_address, uint8_t* buffer, short length, I2C_HandleTypeDef* hi2c1) {
HAL_I2C_Mem_Read(hi2c1, (uint16_t)BMM150_I2C_Address<<1, mem_address, (uint16_t) length,(uint8_t*) buffer, (uint16_t)length, HAL_MAX_DELAY);
}
void BMM150::i2c_read(short mem_address, int8_t* buffer, short length, I2C_HandleTypeDef* hi2c1) {
HAL_I2C_Mem_Read(hi2c1, (uint16_t)BMM150_I2C_Address<<1, mem_address, (uint16_t) length, (uint8_t*) buffer, (uint16_t)length, HAL_MAX_DELAY);
}
void BMM150::i2c_read(short mem_address, int8_t* buffer, short length, short bytes_to_grab, I2C_HandleTypeDef* hi2c1) {
HAL_I2C_Mem_Read(hi2c1, (uint16_t)BMM150_I2C_Address<<1, mem_address, (uint16_t) length, (uint8_t*) buffer, (uint16_t)bytes_to_grab, HAL_MAX_DELAY);
}
uint8_t BMM150::i2c_read(uint16_t mem_address, I2C_HandleTypeDef* hi2c1) {
uint8_t byte;
HAL_I2C_Mem_Read(hi2c1, (uint16_t)BMM150_I2C_Address<<1, mem_address, (uint16_t) 1, (uint8_t *)&byte, (uint16_t)1, HAL_MAX_DELAY);
return byte;
}
// char* BMM150::getErrorText(short errorCode);
// {
// if(ERRORCODE_1_NUM == 1)
// return ERRORCODE_1;
// return "Error not defined.";
// }
void BMM150::set_op_mode(uint8_t pwr_mode, I2C_HandleTypeDef* hi2c1) {
/* Select the power mode to set */
switch (pwr_mode) {
case BMM150_NORMAL_MODE:
/* If the sensor is in suspend mode
put the device to sleep mode */
suspend_to_sleep_mode(hi2c1);
/* write the op mode */
write_op_mode(pwr_mode,hi2c1);
break;
case BMM150_FORCED_MODE:
/* If the sensor is in suspend mode
put the device to sleep mode */
suspend_to_sleep_mode(hi2c1);
/* write the op mode */
write_op_mode(pwr_mode,hi2c1);
break;
case BMM150_SLEEP_MODE:
/* If the sensor is in suspend mode
put the device to sleep mode */
suspend_to_sleep_mode(hi2c1);
/* write the op mode */
write_op_mode(pwr_mode,hi2c1);
break;
case BMM150_SUSPEND_MODE:
/* Set the power control bit to zero */
set_power_control_bit(BMM150_POWER_CNTRL_DISABLE,hi2c1);
break;
default:
break;
}
}
void BMM150::suspend_to_sleep_mode(I2C_HandleTypeDef* hi2c1) {
set_power_control_bit(BMM150_POWER_CNTRL_ENABLE, hi2c1);
/* Start-up time delay of 3ms*/
HAL_Delay(3);
}
void BMM150::read_trim_registers(I2C_HandleTypeDef* hi2c1) {
uint8_t trim_x1y1[2] = {0};
uint8_t trim_xyz_data[4] = {0};
uint8_t trim_xy1xy2[10] = {0};
uint16_t temp_msb = 0;
/* Trim register value is read */
i2c_read(BMM150_DIG_X1, trim_x1y1, 2, hi2c1);
i2c_read(BMM150_DIG_Z4_LSB, trim_xyz_data, 4, hi2c1);
i2c_read(BMM150_DIG_Z2_LSB, trim_xy1xy2, 10, hi2c1);
/* Trim data which is read is updated
in the device structure */
trim_data.dig_x1 = (int8_t)trim_x1y1[0];
trim_data.dig_y1 = (int8_t)trim_x1y1[1];
trim_data.dig_x2 = (int8_t)trim_xyz_data[2];
trim_data.dig_y2 = (int8_t)trim_xyz_data[3];
temp_msb = ((uint16_t)trim_xy1xy2[3]) << 8;
trim_data.dig_z1 = (uint16_t)(temp_msb | trim_xy1xy2[2]);
temp_msb = ((uint16_t)trim_xy1xy2[1]) << 8;
trim_data.dig_z2 = (int16_t)(temp_msb | trim_xy1xy2[0]);
temp_msb = ((uint16_t)trim_xy1xy2[7]) << 8;
trim_data.dig_z3 = (int16_t)(temp_msb | trim_xy1xy2[6]);
temp_msb = ((uint16_t)trim_xyz_data[1]) << 8;
trim_data.dig_z4 = (int16_t)(temp_msb | trim_xyz_data[0]);
trim_data.dig_xy1 = trim_xy1xy2[9];
trim_data.dig_xy2 = (int8_t)trim_xy1xy2[8];
temp_msb = ((uint16_t)(trim_xy1xy2[5] & 0x7F)) << 8;
trim_data.dig_xyz1 = (uint16_t)(temp_msb | trim_xy1xy2[4]);
}
void BMM150::write_op_mode(uint8_t op_mode, I2C_HandleTypeDef* hi2c1) {
uint8_t reg_data = 0;
reg_data = i2c_read(BMM150_OP_MODE_ADDR, hi2c1);
/* Set the op_mode value in Opmode bits of 0x4C */
reg_data = BMM150_SET_BITS(reg_data, BMM150_OP_MODE, op_mode);
i2c_write(BMM150_OP_MODE_ADDR, reg_data, hi2c1);
//reg_data = i2c_read(BMM150_OP_MODE_ADDR, hi2c1);
}
void BMM150::set_power_control_bit(uint8_t pwrcntrl_bit, I2C_HandleTypeDef* hi2c1) {
uint8_t reg_data = 0;
/* Power control register 0x4B is read */
reg_data = i2c_read(BMM150_POWER_CONTROL_ADDR, hi2c1);
/* Sets the value of power control bit */
// reg_data = BMM150_SET_BITS_POS_0(reg_data, BMM150_PWR_CNTRL, pwrcntrl_bit);
reg_data = 1<<0;
i2c_write(BMM150_POWER_CONTROL_ADDR, reg_data, hi2c1);
}
//DO NOT MESS WITH BELOW. As IS :)
// /*!
// * @brief This API is used to perform the complete self test
// * (both normal and advanced) for the BMM150 sensor
// */
// int8_t BMM150::perform_self_test(uint8_t self_test_mode)
// {
// int8_t rslt;
// int8_t self_test_rslt = 0;
// switch (self_test_mode) {
// case BMM150_NORMAL_SELF_TEST:
// /* Set the sensor in sleep mode */
// settings.pwr_mode = BMM150_SLEEP_MODE;
// set_op_mode(BMM150_SLEEP_MODE);
// /* Perform the normal self test */
// rslt = perform_normal_self_test();
// break;
// case BMM150_ADVANCED_SELF_TEST:
// /* Perform the advanced self test */
// rslt = perform_adv_self_test();
// /* Check to ensure bus error does not occur */
// if (rslt >= BMM150_OK) {
// /* Store the status of self test result */
// self_test_rslt = rslt;
// /* Perform soft reset */
// soft_reset();
// }
// rslt = self_test_rslt;
// break;
// default:
// rslt = BMM150_E_INVALID_CONFIG;
// break;
// }
// return rslt;
// }
// /*
// * @brief This internal API is used to perform the normal self test
// * of the sensor and return the self test result as return value
// */
// int8_t BMM150::perform_normal_self_test()
// {
// int8_t rslt;
// uint8_t self_test_bit;
// /* Triggers the start of normal self test */
// enable_normal_self_test(&self_test_bit);
// /* Check for self test completion status */
// if (self_test_bit == 0) {
// /* Validates the self test results for all 3 axes */
// rslt = validate_normal_self_test();
// }
// return rslt;
// }
// /*!
// * @brief This internal API is used to enable the normal self test by setting
// * the Self Test bit (bit0) of the 0x4C register,
// * which triggers the start of self test
// */
// void BMM150::enable_normal_self_test(uint8_t *self_test_enable)
// {
// uint8_t reg_data;
// uint8_t self_test_val;
// /* Read the data from register 0x4C */
// reg_data = i2c_read(BMM150_OP_MODE_ADDR);
// /* Set the Self Test bit(bit0) of the 0x4C register */
// self_test_val = 1;
// reg_data = BMM150_SET_BITS_POS_0(reg_data, BMM150_SELF_TEST, self_test_val);
// /* Write the data to 0x4C register to trigger self test */
// i2c_write(BMM150_OP_MODE_ADDR, reg_data);
// HAL_Delay(BMM150_NORMAL_SELF_TEST_DELAY);
// /* Read the data from register 0x4C */
// reg_data = i2c_read(BMM150_OP_MODE_ADDR);
// /* Self Test bit(bit0) is stored in self_test_enable,
// It will be reset to zero after the self test is over */
// *self_test_enable = BMM150_GET_BITS_POS_0(reg_data, BMM150_SELF_TEST);
// }
// /*!
// * @brief This internal API is used to validate the results of normal self test
// * by using the self test status available in the bit0 of registers 0x42,0x44
// * and 0x46.
// */
// int8_t BMM150::validate_normal_self_test()
// {
// int8_t rslt;
// uint8_t status;
// uint8_t self_test_rslt[5];
// /* Read the data from register 0x42 to 0x46 */
// i2c_read(BMM150_DATA_X_LSB, self_test_rslt, BMM150_SELF_TEST_LEN);
// /* Parse and get the self test status bits */
// /* X-Self-Test (bit0) of 0x42 register is stored*/
// self_test_rslt[0] = BMM150_GET_BITS_POS_0(self_test_rslt[0], BMM150_SELF_TEST);
// /* Y-Self-Test (bit0) of 0x44 register is stored */
// self_test_rslt[2] = BMM150_GET_BITS_POS_0(self_test_rslt[2], BMM150_SELF_TEST);
// /* Z-Self-Test (bit0) of 0x46 register is stored */
// self_test_rslt[4] = BMM150_GET_BITS_POS_0(self_test_rslt[4], BMM150_SELF_TEST);
// /* Combine the self test status and store it in the first
// 3 bits of the status variable for processing*/
// status = (uint8_t)((self_test_rslt[4] << 2) | (self_test_rslt[2] << 1) | self_test_rslt[0]);
// /* Validate status and store Self test result in "rslt" */
// if (status == BMM150_SELF_TEST_STATUS_SUCCESS) {
// /* Self test is success when all status bits are set */
// rslt = BMM150_OK;
// } else {
// if (status == BMM150_SELF_TEST_STATUS_XYZ_FAIL) {
// /* Self test - all axis fail condition */
// rslt = BMM150_W_NORMAL_SELF_TEST_XYZ_FAIL;
// } else {
// /* Self test - some axis fail condition */
// rslt = (int8_t)status;
// }
// }
// return rslt;
// }
// /*!
// * @brief This internal API is used to perform advanced self test for Z axis
// */
// int8_t BMM150::perform_adv_self_test()
// {
// uint8_t self_test_current;
// int16_t positive_data_z;
// int16_t negative_data_z;
// int8_t rslt;
// /* Set the desired power mode ,axes control and repetition settings */
// adv_self_test_settings();
// /* Measure the Z axes data with positive self-test current */
// self_test_current = BMM150_ENABLE_POSITIVE_CURRENT;
// adv_self_test_measurement(self_test_current, &positive_data_z);
// /* Measure the Z axes data with
// negative self-test current */
// self_test_current = BMM150_ENABLE_NEGATIVE_CURRENT;
// adv_self_test_measurement(self_test_current, &negative_data_z);
// /* Disable self-test current */
// self_test_current = BMM150_DISABLE_SELF_TEST_CURRENT;
// set_adv_self_test_current(self_test_current);
// /* Validate the advanced self test */
// rslt = validate_adv_self_test(positive_data_z, negative_data_z);
// return rslt;
// }
// /*!
// * @brief This internal API is used to set the desired power mode ,
// * axes control and repetition settings for advanced self test
// */
// void BMM150::adv_self_test_settings()
// {
// /* Set the power mode as sleep mode */
// settings.pwr_mode = BMM150_SLEEP_MODE;
// set_op_mode(BMM150_SLEEP_MODE);
// /* Disable XY-axis measurement */
// settings.xyz_axes_control = BMM150_DISABLE_XY_AXIS;
// set_control_measurement_xyz(settings);
// /* Repetition value is set as 0x04 */
// settings.z_rep = BMM150_SELF_TEST_REP_Z;
// set_z_rep(settings);
// }
// /*!
// * @brief This internal API is used to set the positive or negative value of
// * self-test current and obtain the corresponding magnetometer z axis data
// */
// void BMM150::adv_self_test_measurement(uint8_t self_test_current, int16_t *data_z)
// {
// /* Set the advanced self test current as positive or
// negative based on the value of parameter "self_test_current" */
// set_adv_self_test_current(self_test_current);
// /* Set the device in forced mode*/
// settings.pwr_mode = BMM150_FORCED_MODE;
// set_op_mode(BMM150_FORCED_MODE);
// /* HAL_Delay to ensure measurement is complete */
// HAL_Delay(4);
// /* Read Mag data and store the value of Z axis data */
// read_mag_data();
// /* Mag Z axis data is stored */
// *data_z = mag_data.z;
// }
// /*!
// * @brief This internal API is used to get the difference between the
// * Z axis mag data obtained by positive and negative self-test current
// * and validate whether the advanced self test is done successfully or not.
// */
// int8_t BMM150::validate_adv_self_test(int16_t positive_data_z, int16_t negative_data_z)
// {
// int32_t adv_self_test_rslt;
// int8_t rslt;
// /* Advanced self test difference between the Z axis mag data
// obtained by the positive and negative self-test current */
// adv_self_test_rslt = positive_data_z - negative_data_z;
// /* Advanced self test validation */
// /*Value of adv_self_test_rslt should be in between 180-240 micro-tesla*/
// if ((adv_self_test_rslt > 180) && (adv_self_test_rslt < 240)) {
// /* Advanced self test success */
// rslt = BMM150_OK;
// } else {
// /* Advanced self test fail */
// rslt = BMM150_W_ADV_SELF_TEST_FAIL;
// }
// return rslt;
// }
// /*
// * @brief This internal API is used to set the self test current value in
// * the Adv. ST bits (bit6 and bit7) of 0x4C register
// */
// void BMM150::set_adv_self_test_current(uint8_t self_test_current)
// {
// uint8_t reg_data;
// /* Read the 0x4C register */
// reg_data = i2c_read(BMM150_OP_MODE_ADDR);
// /* Set the self test current value in the Adv. ST bits
// (bit6 and bit7) of 0x4c register */
// reg_data = BMM150_SET_BITS(reg_data, BMM150_ADV_SELF_TEST, self_test_current);
// i2c_write(BMM150_OP_MODE_ADDR, reg_data);
// }
// /*
// * @brief This internal API is used to enable or disable the magnetic
// * measurement of x,y,z axes based on the value of xyz_axes_control.
// */
// void BMM150::set_control_measurement_xyz(struct bmm150_settings settings)
// {
// uint8_t reg_data;
// reg_data = i2c_read(BMM150_AXES_ENABLE_ADDR);
// /* Set the axes to be enabled/disabled*/
// reg_data = BMM150_SET_BITS(reg_data, BMM150_CONTROL_MEASURE, settings.xyz_axes_control);
// i2c_write(BMM150_AXES_ENABLE_ADDR, reg_data);
// }