-
Notifications
You must be signed in to change notification settings - Fork 19
/
bmm150.c
2187 lines (1894 loc) · 73.1 KB
/
bmm150.c
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
*
* BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bmm150.c
* @date 2020-06-03
* @version v2.0.0
*
*/
/*! @file bmm150.c
* @brief Sensor driver for BMM150 sensor */
#include "bmm150.h"
/************************** Internal macros *******************************/
/* Sensor ODR, Repetition and axes enable/disable settings */
#define MODE_SETTING_SEL UINT16_C(0x000F)
/* Interrupt pin settings like polarity,latch and int_pin enable */
#define INTERRUPT_PIN_SETTING_SEL UINT16_C(0x01F0)
/* Settings to enable/disable interrupts */
#define INTERRUPT_CONFIG_SEL UINT16_C(0x1E00)
/* Interrupt settings for configuring threshold values */
#define INTERRUPT_THRESHOLD_CONFIG_SEL UINT16_C(0x6000)
/********************** Static function declarations ************************/
/*!
* @brief This internal API is used to validate the device pointer for
* null conditions.
*
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t null_ptr_check(const struct bmm150_dev *dev);
/*!
* @brief This internal API sets/resets the power control bit of 0x4B register.
*
* @param[in] pwrcntrl_bit : Variable used to select/deselect the suspend mode.
* @param[in,out] dev : Structure instance of bmm150_dev
*
* pwrcntrl_bit | power mode
* -----------------|-------------------------
* 0x00 | Suspend mode
* 0x01 | Sleep/Active modes
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_power_control_bit(uint8_t pwrcntrl_bit, struct bmm150_dev *dev);
/*!
* @brief This internal API reads the trim registers of the sensor and stores
* the trim values in the "trim_data" of device structure.
*
* @param[in,out] dev : Structure instance of bmm150_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t read_trim_registers(struct bmm150_dev *dev);
/*!
* @brief This internal API writes the op_mode value in the Opmode bits
* (bits 1 and 2) of 0x4C register.
*
* op_mode | Power mode
* ------------|-----------------------
* 0x00 | BMM150_POWERMODE_NORMAL
* 0x01 | BMM150_POWERMODE_FORCED
* 0x03 | BMM150_POWERMODE_SLEEP
*
* @param[in,out] dev : Structure instance of bmm150_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t write_op_mode(uint8_t op_mode, struct bmm150_dev *dev);
/*!
* @brief This internal API sets the device from suspend to sleep mode
* by setting the power control bit to '1' of 0x4B register
*
* @param[in,out] dev : Structure instance of bmm150_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t suspend_to_sleep_mode(struct bmm150_dev *dev);
/*!
* @brief This internal API sets the xy repetition value in the 0x51 register.
*
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in,out] dev : Structure instance of bmm150_dev
*
* settings->xy_rep | nXY(XY Repetitions)
* -------------------------|-----------------------
* 0x00 | 1
* 0x01 | 3
* 0x02 | 5
* . | .
* . | .
* 0xFF | 511
*
* @note number of XY Repetitions nXY = 1+2(settings->xy_rep)
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_xy_rep(const struct bmm150_settings *settings, struct bmm150_dev *dev);
/*!
* @brief This internal API sets the z repetition value in the 0x52 register.
*
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in,out] dev : Structure instance of bmm150_dev
*
* settings->z_rep | nZ(Z Repetitions)
* -------------------------|-----------------------
* 0x00 | 1
* 0x01 | 2
* 0x02 | 3
* . | .
* . | .
* 0xFF | 256
*
* @note number of Z Repetitions nZ = 1+(settings->z_rep)
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_z_rep(const struct bmm150_settings *settings, struct bmm150_dev *dev);
/*!
* @brief This internal API is used to set the output data rate of the sensor
*
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
* settings->data_rate | Data rate (ODR)
* -------------------------|-----------------------
* 0x00 | BMM150_DATA_RATE_10HZ
* 0x01 | BMM150_DATA_RATE_02HZ
* 0x02 | BMM150_DATA_RATE_06HZ
* 0x03 | BMM150_DATA_RATE_08HZ
* 0x04 | BMM150_DATA_RATE_15HZ
* 0x05 | BMM150_DATA_RATE_20HZ
* 0x06 | BMM150_DATA_RATE_25HZ
* 0x07 | BMM150_DATA_RATE_30HZ
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_odr(const struct bmm150_settings *settings, struct bmm150_dev *dev);
/*!
* @brief This internal API sets the preset mode ODR and repetition settings.
* @param[in] settings : Structure instance of bmm150_settings
* @param[in] dev : Structure instance of bmm150_dev
*
* API settings | Representation
* ---------------------|------------------------------
* settings->data_rate | Output Data Rate (ODR)
* settings->xy_rep | XY repetition value
* settings->z_rep | Z-repetition value
*
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_odr_xyz_rep(const struct bmm150_settings *settings, struct bmm150_dev *dev);
/*!
* @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.
*
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
* settings->xyz_axes_control | Measurement axes/channel
* ---------------------------|--------------------------
* Bit 0 | X - Channel
* Bit 1 | Y - Channel
* Bit 2 | Z - Channel
*
* @note Setting 1 - Disables Channel measurement
* @note Setting 0 - Enables Channel measurement
*
* settings->xyz_axes_control | Measurement axes Enabled/disabled
* -------------------------------|------------------------------------
* 0x01 | Disables X axis (Y,Z axes enabled)
* 0x02 | Disables Y axis (X,Z axes enabled)
* 0x04 | Disables Z axis (X,Y axes enabled)
* 0x07 | Disables all X,Y,Z axes measurement
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_control_measurement_xyz(const struct bmm150_settings *settings, struct bmm150_dev *dev);
/*!
* @brief This internal API is used to identify the settings which the user
* wants to modify in the sensor.
*
* @param[in] sub_settings : Contains the settings subset to identify particular
* group of settings which the user is interested to change.
* @param[in] settings : Contains the user specified settings.
*
* @return Indicates whether user is interested to modify the settings which
* are related to sub_settings.
* @retval True -> User wants to modify this group of settings
* @retval False -> User does not want to modify this group of settings
*/
static uint8_t are_settings_changed(uint16_t sub_settings, uint16_t settings);
/*!
* @brief This API sets the ODR , measurement axes control ,
* repetition values of xy,z.
*
* @param[in] desired_settings : Contains the settings which user wants to
* change.
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t mode_settings(uint16_t desired_settings, const struct bmm150_settings *settings, struct bmm150_dev *dev);
/*!
* @brief This internal API is used to parse and store the sensor
* settings in the device structure
*
* @param[in] reg_data : Pointer of an array consisting all sensor
* setting data from 0x4B to 0x52 registers.
* @param[in] settings : Structure instance of bmm150_settings.
*
*/
static void parse_setting(const uint8_t *reg_data, struct bmm150_settings *settings);
/*!
* @brief This API is used to enable the interrupts and map them to the
* corresponding interrupt pins and specify the pin characteristics like the
* polarity , latch settings for the interrupt pins.
*
* @note The other interrupts can be latched or non-latched but,
* Data ready interrupt is always cleared after reading out the data
*
* @param[in] desired_settings : Contains the settings which user wants to
* change.
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t interrupt_pin_settings(uint16_t desired_settings,
const struct bmm150_settings *settings,
struct bmm150_dev *dev);
/*!
* @brief This API is used to enable data overrun , overflow interrupts and
* enable/disable high/low threshold interrupts for x,y,z axis based on the
* threshold values set by the user in the High threshold (0x50) and
* Low threshold (0x4F) registers.
*
* @param[in] desired_settings : Contains the settings which user wants to
* change.
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t interrupt_config(uint16_t desired_settings, const struct bmm150_settings *settings,
struct bmm150_dev *dev);
/*!
* @brief This API is used to write the user specified High/Low threshold value
* as a reference to generate the high/low threshold interrupt.
*
* @param[in] desired_settings : Contains the settings which user wants to
* change.
* @param[in] settings : Structure instance of bmm150_settings.
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t interrupt_threshold_settings(uint16_t desired_settings,
const struct bmm150_settings *settings,
struct bmm150_dev *dev);
#ifdef BMM150_USE_FLOATING_POINT
/*!
* @brief This internal API is used to obtain the compensated
* magnetometer X axis data in float.
*
* @param[in] mag_data_x : The value of raw X data
* @param[in] data_rhall : The value of raw RHALL data
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of compensated X data value in float
*/
static float compensate_x(int16_t mag_data_x, uint16_t data_rhall, const struct bmm150_dev *dev);
/*!
* @brief This internal API is used to obtain the compensated
* magnetometer Y axis data in float.
*
* @param[in] mag_data_y : The value of raw Y data
* @param[in] data_rhall : The value of raw RHALL data
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of compensated Y data value in float
*/
static float compensate_y(int16_t mag_data_y, uint16_t data_rhall, const struct bmm150_dev *dev);
/*!
* @brief This internal API is used to obtain the compensated
* magnetometer Z axis data in float.
*
* @param[in] mag_data_z : The value of raw Z data
* @param[in] data_rhall : The value of raw RHALL data
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of compensated Z data value in float
*/
static float compensate_z(int16_t mag_data_z, uint16_t data_rhall, const struct bmm150_dev *dev);
#else
/*!
* @brief This internal API is used to obtain the compensated
* magnetometer X axis data in int16_t.
*
* @param[in] mag_data_x : The value of raw X data
* @param[in] data_rhall : The value of raw RHALL data
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of compensated X data value in int16_t format
*/
static int16_t compensate_x(int16_t mag_data_x, uint16_t data_rhall, const struct bmm150_dev *dev);
/*!
* @brief This internal API is used to obtain the compensated
* magnetometer Y axis data in int16_t.
*
* @param[in] mag_data_y : The value of raw Y data
* @param[in] data_rhall : The value of raw RHALL data
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of compensated Y data value in int16_t format
*/
static int16_t compensate_y(int16_t mag_data_y, uint16_t data_rhall, const struct bmm150_dev *dev);
/*!
* @brief This internal API is used to obtain the compensated
* magnetometer Z axis data in int16_t.
*
* @param[in] mag_data_z : The value of raw Z data
* @param[in] data_rhall : The value of raw RHALL data
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of compensated Z data value in int16_t format
*/
static int16_t compensate_z(int16_t mag_data_z, uint16_t data_rhall, const struct bmm150_dev *dev);
#endif
/*!
* @brief This internal API is used to perform the normal self test
* of the sensor and return the self test result as return value
*
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t perform_normal_self_test(struct bmm150_dev *dev);
/*!
* @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
*
* @param[out] self_test_enable : The value of self test bit0 in 0x4C register
* @param[in] dev : Structure instance of bmm150_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t enable_normal_self_test(uint8_t *self_test_enable, struct bmm150_dev *dev);
/*!
* @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.
*
* @param[in] dev : Structure instance of bmm150_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t validate_normal_self_test(struct bmm150_dev *dev);
/*!
* @brief This internal API is used to perform advanced self test for Z axis
*
* @param[in] dev : Structure instance of bmm150_dev
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* Return value | Status of self-test
*----------------------|---------------------------
* 0 | BMM150_OK
* 8 | BMM150_W_ADV_SELF_TEST_FAIL
*/
static int8_t perform_adv_self_test(struct bmm150_dev *dev);
/*!
* @brief This internal API is used to set the desired power mode ,
* axes control and repetition settings for advanced self test
*
* @param[in] dev : Structure instance of bmm150_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t adv_self_test_settings(struct bmm150_dev *dev);
/*!
* @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
*
* @param[in] self_test_current : Self test current either positive/negative
* @param[out] data_z : Z-axis Magnetometer data
* @param[in] dev : Structure instance of bmm150_dev
*
* self_test_current | Self-test current Direction
*-------------------------|------------------------------
* 0x03 | BMM150_ENABLE_POSITIVE_CURRENT
* 0x02 | BMM150_ENABLE_NEGATIVE_CURRENT
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t adv_self_test_measurement(uint8_t self_test_current, int16_t *data_z, struct bmm150_dev *dev);
/*!
* @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.
*
* @param[in] positive_data_z : Z-axis Mag data by positive self-test current
* @param[in] negative_data_z : Z-axis Mag data by negative self-test current
*
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* Return value | Status of self-test
*----------------------|---------------------------
* 0 | BMM150_OK
* 8 | BMM150_W_ADV_SELF_TEST_FAIL
*/
static int8_t validate_adv_self_test(int16_t positive_data_z, int16_t negative_data_z);
/*!
* @brief This internal API is used to set the self test current value in
* the Adv. ST bits (bit6 and bit7) of 0x4C register
*
* @param[in] self_test_current : Self test current value (+ve/-ve)
* @param[in] dev : Structure instance of bmm150_dev
*
* self_test_current | Self-test current Direction
*-------------------------|------------------------------
* 0x00 | BMM150_DISABLE_SELF_TEST_CURRENT
* 0x02 | BMM150_ENABLE_NEGATIVE_CURRENT
* 0x03 | BMM150_ENABLE_POSITIVE_CURRENT
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_adv_self_test_current(uint8_t self_test_current, struct bmm150_dev *dev);
/********************** Global function definitions ************************/
/*!
* @brief This API is the entry point, Call this API before using other APIs.
* This API reads the chip-id of the sensor which is the first step to
* verify the sensor and updates the trim parameters of the sensor.
*/
int8_t bmm150_init(struct bmm150_dev *dev)
{
int8_t rslt;
uint8_t chip_id = 0;
/* Power up the sensor from suspend to sleep mode */
rslt = set_power_control_bit(BMM150_POWER_CNTRL_ENABLE, dev);
if (rslt == BMM150_OK)
{
/* Start-up time delay of 3ms */
dev->delay_us(BMM150_START_UP_TIME, dev->intf_ptr);
/* Chip ID of the sensor is read */
rslt = bmm150_get_regs(BMM150_REG_CHIP_ID, &chip_id, 1, dev);
/* Proceed if everything is fine until now */
if (rslt == BMM150_OK)
{
/* Check for chip id validity */
if (chip_id == BMM150_CHIP_ID)
{
dev->chip_id = chip_id;
/* Function to update trim values */
rslt = read_trim_registers(dev);
}
}
}
return rslt;
}
/*!
* @brief This API writes the given data to the register address
* of the sensor.
*/
int8_t bmm150_set_regs(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, struct bmm150_dev *dev)
{
int8_t rslt;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMM150_OK) && (reg_data != NULL) && (len != 0))
{
/* Write the data to the reg_addr */
/* SPI write requires to set The MSB of reg_addr as 0
* but in default the MSB is always 0
*/
dev->intf_rslt = dev->write(reg_addr, reg_data, len, dev->intf_ptr);
}
else
{
rslt = BMM150_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API reads the data from the given register address of the sensor.
*/
int8_t bmm150_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bmm150_dev *dev)
{
int8_t rslt;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMM150_OK) && (reg_data != NULL))
{
if (dev->intf != BMM150_I2C_INTF)
{
/* If interface selected is SPI */
reg_addr = reg_addr | 0x80;
}
/* Read the data from the reg_addr */
dev->intf_rslt = dev->read(reg_addr, reg_data, len, dev->intf_ptr);
}
else
{
rslt = BMM150_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to perform soft-reset of the sensor
* where all the registers are reset to their default values except 0x4B.
*/
int8_t bmm150_soft_reset(struct bmm150_dev *dev)
{
int8_t rslt;
uint8_t reg_data;
rslt = bmm150_get_regs(BMM150_REG_POWER_CONTROL, ®_data, 1, dev);
if (rslt == BMM150_OK)
{
reg_data = reg_data | BMM150_SET_SOFT_RESET;
rslt = bmm150_set_regs(BMM150_REG_POWER_CONTROL, ®_data, 1, dev);
dev->delay_us(BMM150_DELAY_SOFT_RESET, dev->intf_ptr);
}
return rslt;
}
/*!
* @brief This API is used to set the power mode of the sensor.
*/
int8_t bmm150_set_op_mode(const struct bmm150_settings *settings, struct bmm150_dev *dev)
{
int8_t rslt;
uint8_t pwr_mode;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMM150_OK)
{
pwr_mode = settings->pwr_mode;
/* Select the power mode to set */
switch (pwr_mode)
{
case BMM150_POWERMODE_NORMAL:
/* If the sensor is in suspend mode
* put the device to sleep mode
*/
rslt = suspend_to_sleep_mode(dev);
if (rslt == BMM150_OK)
{
/* write the op mode */
rslt = write_op_mode(pwr_mode, dev);
}
break;
case BMM150_POWERMODE_FORCED:
/* If the sensor is in suspend mode
* put the device to sleep mode
*/
rslt = suspend_to_sleep_mode(dev);
if (rslt == BMM150_OK)
{
/* write the op mode */
rslt = write_op_mode(pwr_mode, dev);
}
break;
case BMM150_POWERMODE_SLEEP:
/* If the sensor is in suspend mode
* put the device to sleep mode
*/
rslt = suspend_to_sleep_mode(dev);
if (rslt == BMM150_OK)
{
/* write the op mode */
rslt = write_op_mode(pwr_mode, dev);
}
break;
case BMM150_POWERMODE_SUSPEND:
/* Set the power control bit to zero */
rslt = set_power_control_bit(BMM150_POWER_CNTRL_DISABLE, dev);
break;
default:
rslt = BMM150_E_INVALID_CONFIG;
break;
}
}
return rslt;
}
/*!
* @brief This API is used to get the power mode of the sensor.
*/
int8_t bmm150_get_op_mode(uint8_t *op_mode, struct bmm150_dev *dev)
{
int8_t rslt;
uint8_t reg_data;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMM150_OK)
{
if (dev->pwr_cntrl_bit == BMM150_POWER_CNTRL_DISABLE)
{
/* Power mode set is suspend mode */
*op_mode = BMM150_POWERMODE_SUSPEND;
}
else
{
/* Power mode set is stored in the op_mode */
rslt = bmm150_get_regs(BMM150_REG_OP_MODE, ®_data, 1, dev);
*op_mode = BMM150_GET_BITS(reg_data, BMM150_OP_MODE);
}
}
return rslt;
}
/*!
* @brief This API is used to set the preset mode of the sensor.
*/
int8_t bmm150_set_presetmode(struct bmm150_settings *settings, struct bmm150_dev *dev)
{
int8_t rslt;
uint8_t preset_mode;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMM150_OK)
{
preset_mode = settings->preset_mode;
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_REPXY_LOWPOWER;
settings->z_rep = BMM150_REPZ_LOWPOWER;
rslt = set_odr_xyz_rep(settings, dev);
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_REPXY_REGULAR;
settings->z_rep = BMM150_REPZ_REGULAR;
rslt = set_odr_xyz_rep(settings, dev);
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_REPXY_HIGHACCURACY;
settings->z_rep = BMM150_REPZ_HIGHACCURACY;
rslt = set_odr_xyz_rep(settings, dev);
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_REPXY_ENHANCED;
settings->z_rep = BMM150_REPZ_ENHANCED;
rslt = set_odr_xyz_rep(settings, dev);
break;
default:
rslt = BMM150_E_INVALID_CONFIG;
break;
}
}
return rslt;
}
/*!
* @brief This API sets the sensor settings based on the desired_settings
* and the dev structure configuration
*/
int8_t bmm150_set_sensor_settings(uint16_t desired_settings,
const struct bmm150_settings *settings,
struct bmm150_dev *dev)
{
int8_t rslt;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMM150_OK)
{
if (are_settings_changed(MODE_SETTING_SEL, desired_settings))
{
/* ODR, Control measurement, XY,Z repetition values */
rslt = mode_settings(desired_settings, settings, dev);
}
if ((!rslt) && are_settings_changed(INTERRUPT_PIN_SETTING_SEL, desired_settings))
{
/* Interrupt pin settings */
rslt = interrupt_pin_settings(desired_settings, settings, dev);
}
if ((!rslt) && are_settings_changed(INTERRUPT_CONFIG_SEL, desired_settings))
{
/* Interrupt configuration settings */
rslt = interrupt_config(desired_settings, settings, dev);
}
if ((!rslt) && are_settings_changed(INTERRUPT_THRESHOLD_CONFIG_SEL, desired_settings))
{
/* Interrupt threshold settings */
rslt = interrupt_threshold_settings(desired_settings, settings, dev);
}
}
return rslt;
}
/*!
* @brief This API gets the sensor settings and updates the dev structure
*/
int8_t bmm150_get_sensor_settings(struct bmm150_settings *settings, struct bmm150_dev *dev)
{
int8_t rslt;
uint8_t setting[BMM150_LEN_SETTING_DATA] = { 0 };
/* Read the entire sensor settings */
rslt = bmm150_get_regs(BMM150_REG_POWER_CONTROL, setting, BMM150_LEN_SETTING_DATA, dev);
if (rslt == BMM150_OK)
{
/* Parse and store the settings */
parse_setting(setting, settings);
}
return rslt;
}
/*!
* @brief This API is used to read the magnetometer data from registers
* 0x42 to 0x49 and update the dev structure with the
* compensated mag data in micro-tesla.
*/
int8_t bmm150_read_mag_data(struct bmm150_mag_data *mag_data, struct bmm150_dev *dev)
{
int8_t rslt;
int16_t msb_data;
uint8_t reg_data[BMM150_LEN_XYZR_DATA] = { 0 };
struct bmm150_raw_mag_data raw_mag_data;
/* Read the mag data registers */
rslt = bmm150_get_regs(BMM150_REG_DATA_X_LSB, reg_data, BMM150_LEN_XYZR_DATA, dev);
if (rslt == BMM150_OK)
{
/* Mag X axis data */
reg_data[0] = BMM150_GET_BITS(reg_data[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)reg_data[1])) * 32;
/* Raw mag X axis data */
raw_mag_data.raw_datax = (int16_t)(msb_data | reg_data[0]);
/* Mag Y axis data */
reg_data[2] = BMM150_GET_BITS(reg_data[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)reg_data[3])) * 32;
/* Raw mag Y axis data */
raw_mag_data.raw_datay = (int16_t)(msb_data | reg_data[2]);
/* Mag Z axis data */
reg_data[4] = BMM150_GET_BITS(reg_data[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)reg_data[5])) * 128;
/* Raw mag Z axis data */
raw_mag_data.raw_dataz = (int16_t)(msb_data | reg_data[4]);
/* Mag R-HALL data */
reg_data[6] = BMM150_GET_BITS(reg_data[6], BMM150_DATA_RHALL);
raw_mag_data.raw_data_r = (uint16_t)(((uint16_t)reg_data[7] << 6) | reg_data[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, dev);
/* Compensated Mag Y data in int16_t format */
mag_data->y = compensate_y(raw_mag_data.raw_datay, raw_mag_data.raw_data_r, dev);
/* Compensated Mag Z data in int16_t format */
mag_data->z = compensate_z(raw_mag_data.raw_dataz, raw_mag_data.raw_data_r, dev);
}
return rslt;
}
/*!
* @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, struct bmm150_dev *dev)
{
int8_t rslt;
int8_t self_test_rslt = 0;
struct bmm150_settings settings;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMM150_OK)
{
switch (self_test_mode)
{
case BMM150_SELF_TEST_NORMAL:
/* Set the sensor in sleep mode */