-
Notifications
You must be signed in to change notification settings - Fork 5
/
bmm350.c
1786 lines (1469 loc) · 55.6 KB
/
bmm350.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) 2023 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 bmm350.c
* @date 2023-05-26
* @version v1.4.0
*
*/
/*************************** Header files *******************************/
#include "bmm350.h"
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/kernel.h>
#else
#include <stdio.h>
#include <stdlib.h>
#endif
/********************** Static function declarations ************************/
/*!
* @brief This internal API is used to validate the device pointer for
* null conditions.
*
* @param[in] dev : Structure instance of bmm350_dev.
*
* @return Result of API execution status
* @retval = 0 -> Success
* @retval < 0 -> Error
*/
static int8_t null_ptr_check(const struct bmm350_dev *dev);
/*!
* @brief This internal API is used to update magnetometer offset and sensitivity data.
*
* @param[in] dev : Structure instance of bmm350_dev.
*
* @return void
*/
static void update_mag_off_sens(struct bmm350_dev *dev);
/*!
* @brief This internal API converts the raw data from the IC data registers to signed integer
*
* @param[in] inval : Unsigned data from data registers
* @param[in number_of_bits : Width of data register
*
* @return Conversion to signed integer
*/
static int32_t fix_sign(uint32_t inval, int8_t number_of_bits);
/*!
* @brief This internal API is used to read OTP word
*
* @param[in] addr : Stores OTP address
* @param[in, out] lsb_msb : Pointer to store OTP word
* @param[in, out] dev : Structure instance of bmm350_dev.
*
* @return Result of API execution status
* @retval = 0 -> Success
* @retval < 0 -> Error
*/
static int8_t read_otp_word(uint8_t addr, uint16_t *lsb_msb, struct bmm350_dev *dev);
/*!
* @brief This internal API is used to read raw magnetic x,y and z axis data along with temperature.
*
* @param[out] out_data : Pointer variable to store mag and temperature data.
* @param[in, out] dev : Structure instance of bmm350_dev.
*
* @return Result of API execution status
* @retval = 0 -> Success
* @retval < 0 -> Error
*/
static int8_t read_out_raw_data(float *out_data, struct bmm350_dev *dev);
/*!
* @brief This internal API is used to convert raw mag lsb data to uT and raw temperature data to degC.
*
* @param[in,out] lsb_to_ut_degc : Float variable to store converted value of mag lsb in micro tesla(uT) and
* temperature data in degC.
*
* @return void
*/
static void update_default_coefiecents(float *lsb_to_ut_degc);
/*!
* @brief This internal API is used to read OTP data after boot in user mode.
*
* @param[in, out] dev : Structure instance of bmm350_dev.
*
* @return Result of API execution status
* @retval = 0 -> Success
* @retval < 0 -> Error
*/
static int8_t otp_dump_after_boot(struct bmm350_dev *dev);
/*!
* @brief This internal API is used for self-test entry configuration
*
* @param[in, out] dev : Structure instance of bmm350_dev.
*
* @return Result of API execution status
* @retval = 0 -> Success
* @retval < 0 -> Error
*/
static int8_t self_test_entry_config(struct bmm350_dev *dev);
/*!
* @brief This internal API is used to test self-test for X and Y axis
*
* @param[in, out] out_data : Structure instance of bmm350_self_test.
* @param[in, out] dev : Structure instance of bmm350_dev.
*
* @return Result of API execution status
* @retval = 0 -> Success
* @retval < 0 -> Error
*/
static int8_t self_test_xy_axis(struct bmm350_self_test *out_data, struct bmm350_dev *dev);
/*!
* @brief This internal API is used to set self-test configurations.
*
* @param[in] st_cmd : Variable to store self-test command.
* @param[in] pmu_cmd : Variable to store PMU command.
* @param[in, out] out_data : Structure instance of bmm350_self_test.
* @param[in, out] dev : Structure instance of bmm350_dev.
*
* @return Result of API execution status
* @retval = 0 -> Success
* @retval < 0 -> Error
*/
static int8_t self_test_config(uint8_t st_cmd,
uint8_t pmu_cmd,
struct bmm350_self_test *out_data,
struct bmm350_dev *dev);
/*!
* @brief This internal API is used to set powermode.
*
* @param[in] powermode : Variable to set new powermode.
* @param[in, out] dev : Structure instance of bmm350_dev.
*
* @return Result of API execution status
* @retval = 0 -> Success
* @retval < 0 -> Error
*/
static int8_t set_powermode(enum bmm350_power_modes powermode, struct bmm350_dev *dev);
/********************** Global function definitions ************************/
/*!
* @brief This API is the entry point. Call this API before using other APIs.
*/
int8_t bmm350_init(struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
/* Variable to get chip id */
uint8_t chip_id = BMM350_DISABLE;
/* Variable to store the command to power-off the OTP */
uint8_t otp_cmd = BMM350_OTP_CMD_PWR_OFF_OTP;
/* Variable to store soft-reset command */
uint8_t soft_reset;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMM350_OK)
{
dev->chip_id = 0;
/* Assign axis_en with all axis enabled (BMM350_EN_XYZ_MSK) */
dev->axis_en = BMM350_EN_XYZ_MSK;
rslt = bmm350_delay_us(BMM350_START_UP_TIME_FROM_POR, dev);
if (rslt == BMM350_OK)
{
/* Soft-reset */
soft_reset = BMM350_CMD_SOFTRESET;
/* Set the command in the command register */
rslt = bmm350_set_regs(BMM350_REG_CMD, &soft_reset, 1, dev);
if (rslt == BMM350_OK)
{
rslt = bmm350_delay_us(BMM350_SOFT_RESET_DELAY, dev);
}
}
if (rslt == BMM350_OK)
{
/* Chip ID of the sensor is read */
rslt = bmm350_get_regs(BMM350_REG_CHIP_ID, &chip_id, 1, dev);
if (rslt == BMM350_OK)
{
/* Assign chip_id to dev->chip_id */
dev->chip_id = chip_id;
}
}
/* Check for chip id validity */
if ((rslt == BMM350_OK) && (dev->chip_id == BMM350_CHIP_ID))
{
/* Download OTP memory */
rslt = otp_dump_after_boot(dev);
if (rslt == BMM350_OK)
{
/* Power off OTP */
rslt = bmm350_set_regs(BMM350_REG_OTP_CMD_REG, &otp_cmd, 1, dev);
if (rslt == BMM350_OK)
{
rslt = bmm350_magnetic_reset_and_wait(dev);
}
}
}
else
{
rslt = BMM350_E_DEV_NOT_FOUND;
}
}
return rslt;
}
/*!
* @brief This API writes the given data to the register address
* of the sensor.
*/
int8_t bmm350_set_regs(uint8_t reg_addr, const uint8_t *reg_data, uint16_t len, struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMM350_OK) && (reg_data != NULL) && (len != 0))
{
/* Write the data to the reg_addr */
dev->intf_rslt = dev->write(reg_addr, reg_data, len, dev->intf_ptr);
if (dev->intf_rslt != BMM350_INTF_RET_SUCCESS)
{
rslt = BMM350_E_COM_FAIL;
}
}
else
{
rslt = BMM350_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API reads the data from the given register address of sensor.
*/
int8_t bmm350_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
/* Variable to define temporary length */
uint16_t temp_len = len + BMM350_DUMMY_BYTES;
/* Variable to define temporary buffer */
uint8_t temp_buf[BMM350_READ_BUFFER_LENGTH];
/* Variable to define loop */
uint16_t index = 0;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMM350_OK) && (reg_data != NULL))
{
/* Read the data from the reg_addr */
dev->intf_rslt = dev->read(reg_addr, temp_buf, temp_len, dev->intf_ptr);
if (dev->intf_rslt == BMM350_INTF_RET_SUCCESS)
{
/* Copy data after dummy byte indices */
while (index < len)
{
reg_data[index] = temp_buf[index + BMM350_DUMMY_BYTES];
index++;
}
}
else
{
rslt = BMM350_E_COM_FAIL;
}
}
else
{
rslt = BMM350_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This function provides the delay for required time (Microsecond) as per the input provided in some of the
* APIs.
*/
int8_t bmm350_delay_us(uint32_t period_us, const struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
if (rslt == BMM350_OK)
{
dev->delay_us(period_us, dev->intf_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
*/
int8_t bmm350_soft_reset(struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
uint8_t reg_data;
/* Variable to store the command to power-off the OTP */
uint8_t otp_cmd = BMM350_OTP_CMD_PWR_OFF_OTP;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
if (rslt == BMM350_OK)
{
reg_data = BMM350_CMD_SOFTRESET;
/* Set the command in the command register */
rslt = bmm350_set_regs(BMM350_REG_CMD, ®_data, 1, dev);
if (rslt == BMM350_OK)
{
rslt = bmm350_delay_us(BMM350_SOFT_RESET_DELAY, dev);
if (rslt == BMM350_OK)
{
/* Power off OTP */
rslt = bmm350_set_regs(BMM350_REG_OTP_CMD_REG, &otp_cmd, 1, dev);
if (rslt == BMM350_OK)
{
rslt = bmm350_magnetic_reset_and_wait(dev);
}
}
}
}
return rslt;
}
/*!
* @brief This API is used to read the sensor time.
* It converts the sensor time register values to the representative time value.
* Returns the sensor time in ticks.
*/
int8_t bmm350_read_sensortime(uint32_t *seconds, uint32_t *nanoseconds, struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
uint64_t time;
uint8_t reg_data[3];
if ((seconds != NULL) && (nanoseconds != NULL))
{
/* Get sensor time raw data */
rslt = bmm350_get_regs(BMM350_REG_SENSORTIME_XLSB, reg_data, 3, dev);
if (rslt == BMM350_OK)
{
time = (uint32_t)(reg_data[0] + ((uint32_t)reg_data[1] << 8) + ((uint32_t)reg_data[2] << 16));
/* 1 LSB is 39.0625us. Converting to nanoseconds */
time *= UINT64_C(390625);
time /= UINT64_C(10);
*seconds = (uint32_t)(time / UINT64_C(1000000000));
*nanoseconds = (uint32_t)(time - ((*seconds) * UINT64_C(1000000000)));
}
}
else
{
rslt = BMM350_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to get the status flags of all interrupt
* which is used to check for the assertion of interrupts
*/
int8_t bmm350_get_interrupt_status(uint8_t *drdy_status, struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
uint8_t int_status_reg;
if (drdy_status != NULL)
{
/* Get the status of interrupt */
rslt = bmm350_get_regs(BMM350_REG_INT_STATUS, &int_status_reg, 1, dev);
if (rslt == BMM350_OK)
{
/* Read the interrupt status */
(*drdy_status) = BMM350_GET_BITS(int_status_reg, BMM350_DRDY_DATA_REG);
}
}
else
{
rslt = BMM350_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to set the power mode of the sensor
*/
int8_t bmm350_set_powermode(enum bmm350_power_modes powermode, struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
uint8_t last_pwr_mode;
uint8_t reg_data;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
if (rslt == BMM350_OK)
{
rslt = bmm350_get_regs(BMM350_REG_PMU_CMD, &last_pwr_mode, 1, dev);
if (rslt == BMM350_OK)
{
if (last_pwr_mode > BMM350_PMU_CMD_NM_TC)
{
rslt = BMM350_E_INVALID_CONFIG;
}
if ((rslt == BMM350_OK) &&
((last_pwr_mode == BMM350_PMU_CMD_NM) || (last_pwr_mode == BMM350_PMU_CMD_UPD_OAE)))
{
reg_data = BMM350_PMU_CMD_SUS;
/* Set PMU command configuration */
rslt = bmm350_set_regs(BMM350_REG_PMU_CMD, ®_data, 1, dev);
if (rslt == BMM350_OK)
{
rslt = bmm350_delay_us(BMM350_GOTO_SUSPEND_DELAY, dev);
}
}
if (rslt == BMM350_OK)
{
rslt = set_powermode(powermode, dev);
}
}
}
return rslt;
}
/*!
* @brief This API sets the ODR and averaging factor.
*/
int8_t bmm350_set_odr_performance(enum bmm350_data_rates odr,
enum bmm350_performance_parameters performance,
struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
/* Variable to get PMU command */
uint8_t reg_data = 0;
enum bmm350_performance_parameters performance_fix = performance;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
if (rslt == BMM350_OK)
{
/* Reduce the performance setting when too high for the chosen ODR */
if ((odr == BMM350_DATA_RATE_400HZ) && (performance >= BMM350_AVERAGING_2))
{
performance_fix = BMM350_NO_AVERAGING;
}
else if ((odr == BMM350_DATA_RATE_200HZ) && (performance >= BMM350_AVERAGING_4))
{
performance_fix = BMM350_AVERAGING_2;
}
else if ((odr == BMM350_DATA_RATE_100HZ) && (performance >= BMM350_AVERAGING_8))
{
performance_fix = BMM350_AVERAGING_4;
}
/* ODR is an enum taking the generated constants from the register map */
reg_data = ((uint8_t)odr & BMM350_ODR_MSK);
/* AVG / performance is an enum taking the generated constants from the register map */
reg_data = BMM350_SET_BITS(reg_data, BMM350_AVG, (uint8_t)performance_fix);
/* Set PMU command configurations for ODR and performance */
rslt = bmm350_set_regs(BMM350_REG_PMU_CMD_AGGR_SET, ®_data, 1, dev);
if (rslt == BMM350_OK)
{
/* Set PMU command configurations to update odr and average */
reg_data = BMM350_PMU_CMD_UPD_OAE;
/* Set PMU command configuration */
rslt = bmm350_set_regs(BMM350_REG_PMU_CMD, ®_data, 1, dev);
if (rslt == BMM350_OK)
{
rslt = bmm350_delay_us(BMM350_UPD_OAE_DELAY, dev);
}
}
}
return rslt;
}
/*!
* @brief This API is used to enable or disable the magnetic
* measurement of x,y,z axes
*/
int8_t bmm350_enable_axes(enum bmm350_x_axis_en_dis en_x,
enum bmm350_y_axis_en_dis en_y,
enum bmm350_z_axis_en_dis en_z,
struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
/* Variable to store axis data */
uint8_t data;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
if (rslt == BMM350_OK)
{
if ((en_x == BMM350_X_DIS) && (en_y == BMM350_Y_DIS) && (en_z == BMM350_Z_DIS))
{
rslt = BMM350_E_ALL_AXIS_DISABLED;
/* Assign axis_en with all axis disabled status */
dev->axis_en = BMM350_DISABLE;
}
else
{
data = (en_x & BMM350_EN_X_MSK);
data = BMM350_SET_BITS(data, BMM350_EN_Y, en_y);
data = BMM350_SET_BITS(data, BMM350_EN_Z, en_z);
rslt = bmm350_set_regs(BMM350_REG_PMU_CMD_AXIS_EN, &data, 1, dev);
if (rslt == BMM350_OK)
{
/* Assign axis_en with the axis selection done */
dev->axis_en = data;
}
}
}
return rslt;
}
/*!
* @brief This API is used to enable or disable the data ready interrupt
*/
int8_t bmm350_enable_interrupt(enum bmm350_interrupt_enable_disable enable_disable, struct bmm350_dev *dev)
{
/* Variable to get interrupt control configuration */
uint8_t reg_data = 0;
/* Variable to store the function result */
int8_t rslt;
/* Get interrupt control configuration */
rslt = bmm350_get_regs(BMM350_REG_INT_CTRL, ®_data, 1, dev);
if (rslt == BMM350_OK)
{
reg_data = BMM350_SET_BITS(reg_data, BMM350_DRDY_DATA_REG_EN, (uint8_t)enable_disable);
/* Finally transfer the interrupt configurations */
rslt = bmm350_set_regs(BMM350_REG_INT_CTRL, ®_data, 1, dev);
}
return rslt;
}
/*!
* @brief This API is used to configure the interrupt control settings
*/
int8_t bmm350_configure_interrupt(enum bmm350_intr_latch latching,
enum bmm350_intr_polarity polarity,
enum bmm350_intr_drive drivertype,
enum bmm350_intr_map map_nomap,
struct bmm350_dev *dev)
{
/* Variable to get interrupt control configuration */
uint8_t reg_data = 0;
/* Variable to store the function result */
int8_t rslt;
/* Get interrupt control configuration */
rslt = bmm350_get_regs(BMM350_REG_INT_CTRL, ®_data, 1, dev);
if (rslt == BMM350_OK)
{
reg_data = BMM350_SET_BITS_POS_0(reg_data, BMM350_INT_MODE, latching);
reg_data = BMM350_SET_BITS(reg_data, BMM350_INT_POL, polarity);
reg_data = BMM350_SET_BITS(reg_data, BMM350_INT_OD, drivertype);
reg_data = BMM350_SET_BITS(reg_data, BMM350_INT_OUTPUT_EN, map_nomap);
/* Finally transfer the interrupt configurations */
rslt = bmm350_set_regs(BMM350_REG_INT_CTRL, ®_data, 1, dev);
}
return rslt;
}
/*!
* @brief This API is used to read uncompensated mag and temperature data.
*/
int8_t bmm350_read_uncomp_mag_temp_data(struct bmm350_raw_mag_data *raw_data, struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
uint8_t mag_data[12] = { 0 };
uint32_t raw_mag_x, raw_mag_y, raw_mag_z, raw_temp;
if (raw_data != NULL)
{
/* Get uncompensated mag data */
rslt = bmm350_get_regs(BMM350_REG_MAG_X_XLSB, mag_data, BMM350_MAG_TEMP_DATA_LEN, dev);
if (rslt == BMM350_OK)
{
raw_mag_x = mag_data[0] + ((uint32_t)mag_data[1] << 8) + ((uint32_t)mag_data[2] << 16);
raw_mag_y = mag_data[3] + ((uint32_t)mag_data[4] << 8) + ((uint32_t)mag_data[5] << 16);
raw_mag_z = mag_data[6] + ((uint32_t)mag_data[7] << 8) + ((uint32_t)mag_data[8] << 16);
raw_temp = mag_data[9] + ((uint32_t)mag_data[10] << 8) + ((uint32_t)mag_data[11] << 16);
if ((dev->axis_en & BMM350_EN_X_MSK) == BMM350_DISABLE)
{
raw_data->raw_xdata = BMM350_DISABLE;
}
else
{
raw_data->raw_xdata = fix_sign(raw_mag_x, BMM350_SIGNED_24_BIT);
}
if ((dev->axis_en & BMM350_EN_Y_MSK) == BMM350_DISABLE)
{
raw_data->raw_ydata = BMM350_DISABLE;
}
else
{
raw_data->raw_ydata = fix_sign(raw_mag_y, BMM350_SIGNED_24_BIT);
}
if ((dev->axis_en & BMM350_EN_Z_MSK) == BMM350_DISABLE)
{
raw_data->raw_zdata = BMM350_DISABLE;
}
else
{
raw_data->raw_zdata = fix_sign(raw_mag_z, BMM350_SIGNED_24_BIT);
}
raw_data->raw_data_t = fix_sign(raw_temp, BMM350_SIGNED_24_BIT);
}
}
else
{
rslt = BMM350_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API sets the interrupt control IBI configurations to the sensor.
*/
int8_t bmm350_set_int_ctrl_ibi(enum bmm350_drdy_int_map_to_ibi en_dis,
enum bmm350_clear_drdy_int_status_upon_ibi clear_on_ibi,
struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
/* Variable to get interrupt control configuration */
uint8_t reg_data = 0;
/* Get interrupt control configuration */
rslt = bmm350_get_regs(BMM350_REG_INT_CTRL_IBI, ®_data, 1, dev);
if (rslt == BMM350_OK)
{
reg_data = BMM350_SET_BITS_POS_0(reg_data, BMM350_DRDY_INT_MAP_TO_IBI, en_dis);
reg_data = BMM350_SET_BITS(reg_data, BMM350_CLEAR_DRDY_INT_STATUS_UPON_IBI, clear_on_ibi);
/* Set the IBI control configuration */
rslt = bmm350_set_regs(BMM350_REG_INT_CTRL_IBI, ®_data, 1, dev);
if (en_dis == BMM350_IBI_ENABLE)
{
/* Enable data ready interrupt if IBI is enabled */
rslt = bmm350_enable_interrupt(BMM350_ENABLE_INTERRUPT, dev);
}
}
return rslt;
}
/*!
* @brief This API is used to set the pad drive strength
*/
int8_t bmm350_set_pad_drive(uint8_t drive, struct bmm350_dev *dev)
{
uint8_t reg_data;
/* Variable to store the function result */
int8_t rslt = BMM350_E_BAD_PAD_DRIVE;
if (drive <= BMM350_PAD_DRIVE_STRONGEST)
{
reg_data = drive & BMM350_DRV_MSK;
/* Set drive */
rslt = bmm350_set_regs(BMM350_REG_PAD_CTRL, ®_data, 1, dev);
}
return rslt;
}
/*!
* @brief This API is used to perform the magnetic reset of the sensor
* which is necessary after a field shock (400mT field applied to sensor).
* It sends flux guide or bit reset to the device in suspend mode.
*/
int8_t bmm350_magnetic_reset_and_wait(struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
uint8_t pmu_cmd = 0;
struct bmm350_pmu_cmd_status_0 pmu_cmd_stat_0 = { 0 };
uint8_t restore_normal = BMM350_DISABLE;
rslt = null_ptr_check(dev);
if ((rslt == BMM350_OK) && (dev->mraw_override) && (dev->var_id >= BMM350_MIN_VAR))
{
rslt = dev->mraw_override(dev);
}
else
{
/* Read PMU CMD status */
rslt = bmm350_get_pmu_cmd_status_0(&pmu_cmd_stat_0, dev);
/* Check the powermode is normal before performing magnetic reset */
if ((rslt == BMM350_OK) && (pmu_cmd_stat_0.pwr_mode_is_normal == BMM350_ENABLE))
{
restore_normal = BMM350_ENABLE;
/* Reset can only be triggered in suspend */
rslt = bmm350_set_powermode(BMM350_SUSPEND_MODE, dev);
}
if (rslt == BMM350_OK)
{
/* Set BR to PMU_CMD register */
pmu_cmd = BMM350_PMU_CMD_BR;
rslt = bmm350_set_regs(BMM350_REG_PMU_CMD, &pmu_cmd, 1, dev);
if (rslt == BMM350_OK)
{
rslt = bmm350_delay_us(BMM350_BR_DELAY, dev);
}
}
if (rslt == BMM350_OK)
{
/* Verify if PMU_CMD_STATUS_0 register has BR set */
rslt = bmm350_get_pmu_cmd_status_0(&pmu_cmd_stat_0, dev);
if ((rslt == BMM350_OK) && (pmu_cmd_stat_0.pmu_cmd_value != BMM350_PMU_CMD_STATUS_0_BR))
{
rslt = BMM350_E_PMU_CMD_VALUE;
}
}
if (rslt == BMM350_OK)
{
/* Set FGR to PMU_CMD register */
pmu_cmd = BMM350_PMU_CMD_FGR;
rslt = bmm350_set_regs(BMM350_REG_PMU_CMD, &pmu_cmd, 1, dev);
if (rslt == BMM350_OK)
{
rslt = bmm350_delay_us(BMM350_FGR_DELAY, dev);
}
}
if (rslt == BMM350_OK)
{
/* Verify if PMU_CMD_STATUS_0 register has FGR set */
rslt = bmm350_get_pmu_cmd_status_0(&pmu_cmd_stat_0, dev);
if ((rslt == BMM350_OK) && (pmu_cmd_stat_0.pmu_cmd_value != BMM350_PMU_CMD_STATUS_0_FGR))
{
rslt = BMM350_E_PMU_CMD_VALUE;
}
}
if ((rslt == BMM350_OK) && (restore_normal == BMM350_ENABLE))
{
rslt = bmm350_set_powermode(BMM350_NORMAL_MODE, dev);
}
}
return rslt;
}
/*!
* @brief This API is used to perform compensation for raw magnetometer and temperature data.
*/
int8_t bmm350_get_compensated_mag_xyz_temp_data(struct bmm350_mag_temp_data *mag_temp_data, struct bmm350_dev *dev)
{
/* Variable to store the function result */
int8_t rslt;
uint8_t indx;
float out_data[4] = { 0.0f };
float dut_offset_coef[3], dut_sensit_coef[3], dut_tco[3], dut_tcs[3];
float cr_ax_comp_x, cr_ax_comp_y, cr_ax_comp_z;
if (mag_temp_data != NULL)
{
/* Reads raw magnetic x,y and z axis along with temperature */
rslt = read_out_raw_data(out_data, dev);
if (rslt == BMM350_OK)
{
/* Apply compensation to temperature reading */
out_data[3] = (1 + dev->mag_comp.dut_sensit_coef.t_sens) * out_data[3] +
dev->mag_comp.dut_offset_coef.t_offs;
/* Store magnetic compensation structure to an array */
dut_offset_coef[0] = dev->mag_comp.dut_offset_coef.offset_x;
dut_offset_coef[1] = dev->mag_comp.dut_offset_coef.offset_y;
dut_offset_coef[2] = dev->mag_comp.dut_offset_coef.offset_z;
dut_sensit_coef[0] = dev->mag_comp.dut_sensit_coef.sens_x;
dut_sensit_coef[1] = dev->mag_comp.dut_sensit_coef.sens_y;
dut_sensit_coef[2] = dev->mag_comp.dut_sensit_coef.sens_z;
dut_tco[0] = dev->mag_comp.dut_tco.tco_x;
dut_tco[1] = dev->mag_comp.dut_tco.tco_y;
dut_tco[2] = dev->mag_comp.dut_tco.tco_z;
dut_tcs[0] = dev->mag_comp.dut_tcs.tcs_x;
dut_tcs[1] = dev->mag_comp.dut_tcs.tcs_y;
dut_tcs[2] = dev->mag_comp.dut_tcs.tcs_z;
/* Compensate raw magnetic data */
for (indx = 0; indx < 3; indx++)
{
out_data[indx] *= 1 + dut_sensit_coef[indx];
out_data[indx] += dut_offset_coef[indx];
out_data[indx] += dut_tco[indx] * (out_data[3] - dev->mag_comp.dut_t0);
out_data[indx] /= 1 + dut_tcs[indx] * (out_data[3] - dev->mag_comp.dut_t0);
}
cr_ax_comp_x = (out_data[0] - dev->mag_comp.cross_axis.cross_x_y * out_data[1]) /
(1 - dev->mag_comp.cross_axis.cross_y_x * dev->mag_comp.cross_axis.cross_x_y);
cr_ax_comp_y = (out_data[1] - dev->mag_comp.cross_axis.cross_y_x * out_data[0]) /
(1 - dev->mag_comp.cross_axis.cross_y_x * dev->mag_comp.cross_axis.cross_x_y);
cr_ax_comp_z =
(out_data[2] +
(out_data[0] *
(dev->mag_comp.cross_axis.cross_y_x * dev->mag_comp.cross_axis.cross_z_y -
dev->mag_comp.cross_axis.cross_z_x) - out_data[1] *
(dev->mag_comp.cross_axis.cross_z_y - dev->mag_comp.cross_axis.cross_x_y *
dev->mag_comp.cross_axis.cross_z_x)) /
(1 - dev->mag_comp.cross_axis.cross_y_x * dev->mag_comp.cross_axis.cross_x_y));
out_data[0] = cr_ax_comp_x;
out_data[1] = cr_ax_comp_y;
out_data[2] = cr_ax_comp_z;
}
if (rslt == BMM350_OK)
{
if ((dev->axis_en & BMM350_EN_X_MSK) == BMM350_DISABLE)
{
mag_temp_data->x = BMM350_DISABLE;
}
else
{
mag_temp_data->x = out_data[0];
}
if ((dev->axis_en & BMM350_EN_Y_MSK) == BMM350_DISABLE)
{
mag_temp_data->y = BMM350_DISABLE;
}
else
{
mag_temp_data->y = out_data[1];
}
if ((dev->axis_en & BMM350_EN_Z_MSK) == BMM350_DISABLE)
{
mag_temp_data->z = BMM350_DISABLE;
}
else
{
mag_temp_data->z = out_data[2];
}
mag_temp_data->temperature = out_data[3];
}
}
else
{
rslt = BMM350_E_NULL_PTR;
}