-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbma4.c
5859 lines (5049 loc) · 175 KB
/
bma4.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 bma4.c
* @date 2023-07-05
* @version V2.29.0
*
*/
/*
* @file bma4.c
* @brief Source file for the BMA4 Sensor API
*/
/***************************************************************************/
/*!
* @defgroup bma4 BMA4
*/
/**\name Header files
****************************************************************************/
#include "bma4.h"
/***************************************************************************/
/**\name Local structures
****************************************************************************/
/*!
* @brief Accel data deviation from ideal value
*/
struct bma4_offset_delta
{
/*! X axis */
int16_t x;
/*! Y axis */
int16_t y;
/*! Z axis */
int16_t z;
};
/*!
* @brief Accel offset xyz structure
*/
struct bma4_accel_offset
{
/*! Accel offset X data */
uint8_t x;
/*! Accel offset Y data */
uint8_t y;
/*! Accel offset Z data */
uint8_t z;
};
/***************************************************************************/
/*! Static Function Declarations
****************************************************************************/
/*!
* @brief This API validates the bandwidth, odr and perf_mode
* combinations set by the user.
*
* param bandwidth[in] : bandwidth value set by the user.
* param perf_mode[in] : perf_mode value set by the user.
* param odr[in] : odr value set by the user.
*/
static int8_t validate_odr_bandwidth_perfmode(uint8_t odr, uint8_t bandwidth, uint8_t perf_mode);
/*!
* @brief This API validates the bandwidth and perfmode
* value set by the user.
*
* param bandwidth[in] : bandwidth value set by the user.
* param perf_mode[in] : perf_mode value set by the user.
*/
static int8_t validate_bandwidth_perfmode(uint8_t bandwidth, uint8_t perf_mode);
/*!
* @brief @brief This API validates the ODR value set by the user.
*
* param odr[in] : odr value set by the user.
*/
static int8_t validate_odr(uint8_t odr);
/*!
* @brief This API validates the bandwidth and odr when perf_mode = BMA4_CIC_AVG_MODE
*
* param bandwidth[in] : bandwidth value set by the user.
* param odr[in] : odr value set by the user.
*/
static int8_t validate_bandwidth_odr(uint8_t bandwidth, uint8_t odr);
/*!
* @brief This API is used to reset the FIFO related configurations
* in the fifo_frame structure.
*
* @param fifo[in,out] : Structure instance of bma4_fifo_frame
*
*/
static void reset_fifo_data_structure(struct bma4_fifo_frame *fifo);
/*!
* @brief This API computes the number of bytes of accel FIFO data
* which is to be parsed in header-less mode
*
* @param[out] start_idx : The start index for parsing data
* @param[out] len : Number of bytes to be parsed
* @param[in] acc_count : Number of accelerometer frames to be read
* @param[in] fifo : Structure instance of bma4_fifo_frame.
*
*/
static void get_accel_len_to_parse(uint16_t *start_idx,
uint16_t *len,
const uint16_t *acc_count,
const struct bma4_fifo_frame *fifo);
/*!
* @brief This API checks the fifo read data as empty frame, if it
* is empty frame then moves the index to last byte.
*
* @param[in,out] data_index : The index of the current data to
* be parsed from fifo data
* @param[in] fifo : Structure instance of bma4_fifo_frame.
*/
static void check_empty_fifo(uint16_t *data_index, const struct bma4_fifo_frame *fifo);
/*!
* @brief This API is used to parse the accelerometer data from the
* FIFO data in header mode.
*
* @param[in,out] accel_data : Structure instance of bma4_accel where
* the accelerometer data in FIFO is stored.
* @param[in,out] accel_length : Number of accelerometer frames
* (x,y,z axes data)
* @param[in,out] fifo : Structure instance of bma4_fifo_frame
* @param[in,out] dev : Structure instance of bma4_dev.
*
*/
static void extract_accel_header_mode(struct bma4_accel *accel_data,
uint16_t *accel_length,
struct bma4_fifo_frame *fifo,
const struct bma4_dev *dev);
/*!
* @brief This API is used to parse the accelerometer data from the
* FIFO data in both header mode and header-less mode.
* It update the idx value which is used to store the index of
* the current data byte which is parsed.
*
* @param[in,out] acc : Structure instance of bma4_accel.
* @param[in,out] idx : Index value of number of bytes parsed
* @param[in,out] acc_idx : Index value of accelerometer data
* (x,y,z axes) frame to be parsed
* @param[in] frm : It consists of either fifo_data_enable
* parameter (Accel and/or mag data enabled in FIFO)
* in header-less mode or frame header data
* in header mode
* @param[in] fifo : Structure instance of bma4_fifo_frame.
* @param[in] dev : Structure instance of bma4_dev.
*
*/
static void unpack_acc_frm(struct bma4_accel *acc,
uint16_t *idx,
uint16_t *acc_idx,
uint8_t frm,
const struct bma4_fifo_frame *fifo,
const struct bma4_dev *dev);
/*!
* @brief This API is used to parse the accelerometer data from the
* FIFO data and store it in the instance of the structure bma4_accel.
*
* @param[out] accel_data : Structure instance of bma4_accel where
* the parsed accel data bytes are stored.
* @param[in] data_start_index : Index value of the accel data bytes
* which is to be parsed from the fifo data.
* @param[in] fifo : Structure instance of bma4_fifo_frame.
* @param[in] dev : Structure instance of bma4_dev.
*
*/
static void unpack_accel_data(struct bma4_accel *accel_data,
uint16_t data_start_index,
const struct bma4_fifo_frame *fifo,
const struct bma4_dev *dev);
/*!
* @brief This API computes the number of bytes of Mag FIFO data which is
* to be parsed in header-less mode
*
* @param[out] start_idx : The start index for parsing data
* @param[out] len : Number of bytes to be parsed
* @param[in] mag_count : Number of magnetometer frames to be read
* @param[in] fifo : Structure instance of bma4_fifo_frame.
*
*/
static void get_mag_len_to_parse(uint16_t *start_idx,
uint16_t *len,
const uint16_t *mag_count,
const struct bma4_fifo_frame *fifo);
/*!
* @brief This API is used to parse the magnetometer data from the
* FIFO data in header mode.
*
* @param[in,out] data : Structure instance of bma4_mag_xyzr where
* the magnetometer data in FIFO is extracted
* and stored.
* @param[in,out] len : Number of magnetometer frames
* (x,y,z,r data)
* @param[in,out] fifo : Structure instance of bma4_fifo_frame.
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t extract_mag_header_mode(const struct bma4_mag *data,
uint16_t *len,
struct bma4_fifo_frame *fifo,
const struct bma4_dev *dev);
/*!
* @brief This API is used to parse the magnetometer data from the
* FIFO data in both header mode and header-less mode and update the
* idx value which is used to store the index of the current
* data byte which is parsed.
*
* @param data : Structure instance of bma4_mag_xyzr.
* @param idx : Index value of number of bytes parsed
* @param mag_idx : Index value magnetometer data frame (x,y,z,r)
* to be parsed
* @param frm : It consists of either the fifo_data_enable parameter
* (Accel and/or mag data enabled in FIFO) in
* header-less mode and frame header data in header mode
* @param fifo : Structure instance of bma4_fifo_frame.
* @param dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t unpack_mag_frm(const struct bma4_mag *data,
uint16_t *idx,
uint16_t *mag_idx,
uint8_t frm,
const struct bma4_fifo_frame *fifo,
const struct bma4_dev *dev);
/*!
* @brief This API is used to parse the auxiliary magnetometer data from
* the FIFO data and store it in the instance of the structure mag_data.
*
* @param mag_data : Structure instance of bma4_mag_xyzr where the
* parsed magnetometer data bytes are stored.
* @param start_idx : Index value of the magnetometer data bytes
* which is to be parsed from the FIFO data
* @param fifo : Structure instance of bma4_fifo_frame.
* @param dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t unpack_mag_data(const struct bma4_mag *mag_data,
uint16_t start_idx,
const struct bma4_fifo_frame *fifo,
const struct bma4_dev *dev);
/*!
* @brief This API is used to parse and store the sensor time from the
* FIFO data in the structure instance dev.
*
* @param[in,out] data_index : Index of the FIFO data which
* has the sensor time.
* @param[in,out] fifo : Structure instance of bma4_fifo_frame.
*
*/
static void unpack_sensortime_frame(uint16_t *data_index, struct bma4_fifo_frame *fifo);
/*!
* @brief This API is used to parse and store the skipped_frame_count from
* the FIFO data in the structure instance dev.
*
* @param[in,out] data_index : Index of the FIFO data which
* has the skipped frame count.
* @param[in,out] fifo : Structure instance of bma4_fifo_frame.
*
*/
static void unpack_skipped_frame(uint16_t *data_index, struct bma4_fifo_frame *fifo);
/*!
* @brief This API is used to parse and store the dropped_frame_count from
* the FIFO data in the structure instance dev.
*
* @param[in,out] data_index : Index of the FIFO data which
* has the dropped frame data.
* @param[in,out] fifo : Structure instance of bma4_fifo_frame.
*
*/
static void unpack_dropped_frame(uint16_t *data_index, struct bma4_fifo_frame *fifo);
/*!
* @brief This API is used to move the data index ahead of the
* current_frame_length parameter when unnecessary FIFO data appears while
* extracting the user specified data.
*
* @param[in,out] data_index : Index of the FIFO data which
* is to be moved ahead of the
* current_frame_length
* @param[in] current_frame_length : Number of bytes in a particular frame
* @param[in] fifo : Structure instance of bma4_fifo_frame.
*
*/
static void move_next_frame(uint16_t *data_index, uint8_t current_frame_length, const struct bma4_fifo_frame *fifo);
/*!
* @brief This API writes the config stream data in memory using burst mode
*
* @param[in] stream_data : Pointer to store data of 32 bytes
* @param[in] index : Represents value in multiple of 32 bytes
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t stream_transfer_write(const uint8_t *stream_data, uint16_t index, struct bma4_dev *dev);
/*!
* @brief This API enables or disables the Accel self-test feature in the
* sensor.
*
* @param[in] accel_self-test_enable : Variable used to enable or disable
* the Accel self-test feature
* Value | Description
* --------|---------------
* 0x00 | BMA4_DISABLE
* 0x01 | BMA4_ENABLE
*
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_accel_selftest_enable(uint8_t accel_selftest_axis, struct bma4_dev *dev);
/*!
* @brief This API selects the sign of Accel self-test excitation
*
* @param[in] accel_selftest_sign: Variable used to select the Accel
* self-test sign
* Value | Description
* --------|--------------------------
* 0x00 | BMA4_DISABLE (negative)
* 0x01 | BMA4_ENABLE (positive)
*
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_accel_selftest_sign(uint8_t accel_selftest_sign, struct bma4_dev *dev);
/*!
* @brief This API sets the Accel self-test amplitude in the sensor.
*
* @param[in] accel_selftest_amp : Variable used to specify the Accel self
* test amplitude
* Value | Description
* --------|------------------------------------
* 0x00 | BMA4_SELFTEST_AMP_LOW
* 0x01 | BMA4_SELFTEST_AMP_HIGH
*
* @param[in] dev : structure instance of bma4_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_accel_selftest_amp(uint8_t accel_selftest_amp, struct bma4_dev *dev);
/*!
* @brief This function enables and configures the Accel which is needed
* for self-test operation.
*
* @param[in] dev : Structure instance of bma4_dev
*
* @return results of self-test
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_accel_selftest_config(struct bma4_dev *dev);
/*!
* @brief This function validates the Accel self-test data and decides the
* result of self-test operation.
*
* @param[in] accel_data_diff : Pointer to structure variable which holds
* the Accel data difference of self-test operation
* @param[in] dev : Structure instance of bma4_dev
*
* @return results of self-test operation
* @retval 0 -> Success
* @retval Any non zero value -> Fail
*
*/
static int8_t validate_selftest(const struct bma4_selftest_delta_limit *accel_data_diff, const struct bma4_dev *dev);
/*!
* @brief This API converts lsb value of axes to mg for self-test
*
* @param[in] accel_data_diff : Pointer variable used to pass accel difference
* values in g
* @param[out] accel_data_diff_mg : Pointer variable used to store accel
* difference values in mg
* @param[out] dev : Structure instance of bma4_dev
*
*/
static void convert_lsb_g(const struct bma4_selftest_delta_limit *accel_data_diff,
struct bma4_selftest_delta_limit *accel_data_diff_mg,
const struct bma4_dev *dev);
/*!
* @brief This API sets the feature config. data start address in the sensor.
*
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t set_feature_config_start_addr(struct bma4_dev *dev);
/*!
* @brief This API increments the feature config. data address according to the user
* provided read/write length in the dev structure.
*
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t increment_feature_config_addr(struct bma4_dev *dev);
/*!
* @brief This API reads the 8-bit data from the given register
* in the sensor.
*
* @param[in] addr : Register address.
* @param[in] data : Read data buffer.
* @param[in] len : No of bytes to read.
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t read_regs(uint8_t addr, uint8_t *data, uint32_t len, struct bma4_dev *dev);
/*!
* @brief This API reads the 8-bit data from the given register
* when length is less than read write length
*
* @param[in] data : Read data buffer.
* @param[in] len : No of bytes to read.
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t read_within_len_regs(uint8_t *data, uint32_t len, struct bma4_dev *dev);
/*!
* @brief This API writes the 8-bit data to the given register
* in the sensor.
*
* @param[in] addr : Register address.
* @param[in] data : Write data buffer
* @param[in] len : No of bytes to write
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t write_regs(uint8_t addr, const uint8_t *data, uint32_t len, struct bma4_dev *dev);
/*!
* @brief This API writes the 8-bit data to the given register
* in the sensor for length less than read write length
*
* @param[in] data : Write data buffer
* @param[in] len : No of bytes to write
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t write_within_len_regs(const uint8_t *data, uint32_t len, struct bma4_dev *dev);
/*!
* @brief This API sets the feature config. data start address in the sensor.
*
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t get_feature_config_start_addr(struct bma4_dev *dev);
/*!
* @brief This API is used to calculate the power of given
* base value.
*
* @param[in] base : value of base
* @param[in] resolution : resolution of the sensor
*
* @return : Return the value of base^resolution
*/
static int32_t power(int16_t base, uint8_t resolution);
/*!
* @brief This API finds the the null error of the device pointer structure
*
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
* @retval BMA4_OK -> Success
* @retval BMA4_E_NULL_PTR -> Null pointer Error
*/
static int8_t null_pointer_check(const struct bma4_dev *dev);
/*!
* @brief This internal API gets the re-mapped x, y and z axes from the sensor.
*
* @param[out] remap_data : Structure instance of bma4_axes_remap
* @param[in] feature_config : Array data containing feature configurations
* @param[in] index : Index to which mapping has to be applied
* @param[in] feature_len : Length of feature configurations
* @param[in, out] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t get_remap_axes(struct bma4_axes_remap *remap_data,
uint8_t *feature_config,
uint8_t index,
uint8_t feature_len,
struct bma4_dev *dev);
/*!
* @brief This internal API sets the re-mapped x, y and z axes in the sensor.
*
* @param[in] remap_data : Structure instance of bma4_axes_remap
* @param[in] feature_config : Array data containing feature configurations
* @param[in] index : Index to which mapping has to be applied
* @param[in] feature_len : Length of feature configurations
* @param[in, out] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t set_remap_axes(const struct bma4_axes_remap *remap_data,
uint8_t *feature_config,
uint8_t index,
uint8_t feature_len,
struct bma4_dev *dev);
/*!
* @brief This internal API is to store re-mapped axis and sign values
* in device structure
*
* @param[in] remap_axis : Value of re-mapped axis
* @param[out] axis : Re-mapped axis value stored in device structure
* @param[out] sign : Re-mapped axis sign stored in device structure
*
* @return None
*
* @retval None
*/
static void assign_remap_axis(uint8_t remap_axis, uint8_t *axis, uint8_t *sign);
/*!
* @brief This internal API is to receive re-mapped axis and sign values
* in device structure
*
* @param[in] remap_axis : Re-mapped axis value
* @param[in] remap_sign : Re-mapped axis sign value
* @param[out] axis : Re-mapped axis stored in local structure
*
* @return None
*
* @retval None
*/
static void receive_remap_axis(uint8_t remap_axis, uint8_t remap_sign, uint8_t *axis);
/*!
* @brief This internal API gets the re-mapped accelerometer/gyroscope data.
*
* @param[out] data : Structure instance of bma4_accel.
* @param[in] dev : Structure instance of bma4_dev.
*
* @return None
*
* @retval None
*/
static void get_remapped_data(struct bma4_accel *data, const struct bma4_dev *dev);
/*!
* @brief This API performs the steps needed for Self test operation
* before reading the Accel Self test data.
*
* @param[in] sign: Variable used to specify the self test sign
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t selftest_config(uint8_t sign, struct bma4_dev *dev);
/*!
* @brief This internal API brings up the secondary interface to access
* auxiliary sensor
*
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t set_aux_interface_config(struct bma4_dev *dev);
/*!
* @brief This internal API reads the data from the auxiliary sensor
* depending on burst length configured
*
* @param[in] dev : Structure instance of bma4_dev.
* @param[out] aux_data : Pointer variable to store data read
* @param[in] aux_reg_addr : Variable to pass address from where
* data is to be read
*
* @return Result of API execution status
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t extract_aux_data(uint8_t aux_reg_addr, uint8_t *aux_data, uint16_t len, struct bma4_dev *dev);
/*!
* @brief This internal API maps the actual burst read length with user length set.
*
* @param[in] dev : Structure instance of bma4_dev.
* @param[out] len : Pointer variable to store mapped length
*
* @return Result of API execution status
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t map_read_len(uint8_t *len, const struct bma4_dev *dev);
/*!
* @brief This internal API validates accel self-test status from positive and negative axes input
*
* @param[in] positive : Positive accel data.
* @param[in] negative : Negative accel data.
* @param[in/out] accel_data_diff_mg : accel data difference data between positive and negative in mg.
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t get_accel_data_difference_and_validate(struct bma4_accel positive,
struct bma4_accel negative,
struct bma4_selftest_delta_limit *accel_data_diff_mg,
const struct bma4_dev *dev);
/*!
* @brief This internal API saves the configurations before performing FOC.
*
* @param[out] acc_cfg : Accelerometer configuration value
* @param[out] aps : Advance power mode value
* @param[out] acc_en : Accelerometer enable value
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval BMA4_OK - Success.
* @retval BMA4_E_COM_FAIL - Error: Communication fail
* @retval BMA4_E_INVALID_SENSOR - Error: Invalid sensor
*/
static int8_t save_accel_foc_config(struct bma4_accel_config *acc_cfg,
uint8_t *aps,
uint8_t *acc_en,
struct bma4_dev *dev);
/*!
* @brief This internal API sets configurations for performing accelerometer FOC.
*
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval BMA4_OK - Success.
* @retval BMA4_E_COM_FAIL - Error: Communication fail
* @retval BMA4_E_INVALID_SENSOR - Error: Invalid sensor
*/
static int8_t set_accel_foc_config(struct bma4_dev *dev);
/*!
* @brief This internal API enables/disables the offset compensation for
* filtered and un-filtered accelerometer data.
*
* @param[in] offset_en : Enables/Disables offset compensation.
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval BMA4_OK - Success.
* @retval BMA4_E_COM_FAIL - Error: Communication fail
*/
static int8_t set_bma4_accel_offset_comp(uint8_t offset_en, struct bma4_dev *dev);
/*!
* @brief This internal API performs Fast Offset Compensation for accelerometer.
*
* @param[in] accel_g_value : This parameter selects the accel FOC
* axis to be performed
*
* Input format is {x, y, z, sign}. '1' to enable. '0' to disable
*
* Eg:- To choose x axis {1, 0, 0, 0}
* Eg:- To choose -x axis {1, 0, 0, 1}
*
* @param[in] acc_cfg : Accelerometer configuration value
* @param[in] dev : Structure instance of bma4_dev.
*
* @return Result of API execution status
*
* @retval BMA4_OK - Success.
* @retval BMA4_E_NULL_PTR - Error: Null pointer error
* @retval BMA4_E_COM_FAIL - Error: Communication fail
*/
static int8_t perform_accel_foc(const struct bma4_accel_foc_g_value *accel_g_value,
const struct bma4_accel_config *acc_cfg,
struct bma4_dev *dev);
/*!
* @brief This internal API converts the range value into accelerometer
* corresponding integer value.
*
* @param[in] range_in : Input range value.
* @param[out] range_out : Stores the integer value of range.
*
* @return None
* @retval None
*/
static void map_accel_range(uint8_t range_in, uint8_t *range_out);
/*!
* @brief This internal API compensate the accelerometer data against gravity.
*
* @param[in] lsb_per_g : LSB value per 1g.
* @param[in] g_val : Gravity reference value of all axes.
* @param[in] data : Accelerometer data
* @param[out] comp_data : Stores the data that is compensated by taking the
* difference in accelerometer data and lsb_per_g
* value.
*
* @return None
* @retval None
*/
static void comp_for_gravity(uint16_t lsb_per_g,
const struct bma4_accel_foc_g_value *g_val,
const struct bma4_accel *data,
struct bma4_offset_delta *comp_data);
/*!
* @brief This internal API scales the compensated accelerometer data according
* to the offset register resolution.
*
* @param[in] range : Gravity range of the accelerometer.
* @param[out] comp_data : Data that is compensated by taking the
* difference in accelerometer data and lsb_per_g
* value.
* @param[out] data : Stores offset data
* @param[in] resolution : Resolution of bma4 sensor
*
* @return None
* @retval None
*/
static void scale_bma4_accel_offset(uint8_t range,
const struct bma4_offset_delta *comp_data,
struct bma4_accel_offset *data,
uint8_t resolution);
/*!
* @brief This internal API inverts the accelerometer offset data.
*
* @param[out] offset_data : Stores the inverted offset data
*
* @return None
* @retval None
*/
static void invert_bma4_accel_offset(struct bma4_accel_offset *offset_data);
/*!
* @brief This internal API writes the offset data in the offset compensation
* register.
*
* @param[in] offset : Offset data
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval BMA4_OK - Success.
* @retval BMA4_E_COM_FAIL - Error: Communication fail
*/
static int8_t write_bma4_accel_offset(const struct bma4_accel_offset *offset, struct bma4_dev *dev);
/*!
* @brief This internal API finds the bit position of 3.9mg according to given
* range and resolution.
*
* @param[in] range : Gravity range of the accelerometer.
* @param[in] resolution : Resolution of sensor
*
* @return Result of API execution status
* @retval Bit position of 3.9mg
*/
static int8_t get_bit_pos_3_9mg(uint8_t range, uint8_t resolution);
/*!
* @brief This internal API restores the configurations saved before performing
* accelerometer FOC.
*
* @param[in] acc_cfg : Accelerometer configuration value
* @param[in] aps : Advance power mode value
* @param[in] acc_en : Accelerometer enable value
* @param[in] dev : Structure instance of bma4_dev
*
* @return Result of API execution status
* @retval BMA4_OK - Success.
* @retval BMA4_E_COM_FAIL - Error: Communication fail
* @retval BMA4_E_INVALID_SENSOR - Error: Invalid sensor
* @retval BMA4_E_SET_APS_FAIL - Error: Set Advance Power Save Fail
*/
static int8_t restore_accel_foc_config(const struct bma4_accel_config *acc_cfg,
uint8_t aps,
uint8_t acc_en,
struct bma4_dev *dev);
/***************************************************************************/
/**\name Extern Declarations
****************************************************************************/
/***************************************************************************/
/**\name Globals
****************************************************************************/
/***************************************************************************/
/**\name Function definitions
****************************************************************************/
/*!
* @brief This API is the entry point.
* Call this API before using all other APIs.
* This API reads the chip-id of the sensor which is the first step to
* verify the sensor and also it configures the read mechanism of SPI and
* I2C interface.
*/
int8_t bma4_init(struct bma4_dev *dev)
{
int8_t rslt;
uint8_t data = 0;
uint8_t dummy_read = 0;
/* NULL pointer check */
rslt = null_pointer_check(dev);
if (rslt == BMA4_OK)
{
if (dev->intf == BMA4_SPI_INTF)
{
dev->dummy_byte = 1;
rslt = bma4_read_regs(BMA4_CHIP_ID_ADDR, &dummy_read, 1, dev);
}
else
{
dev->dummy_byte = 0;
}
if (rslt == BMA4_OK)
{
rslt = bma4_read_regs(BMA4_CHIP_ID_ADDR, &data, 1, dev);
if (rslt == BMA4_OK)
{
/* Assign Chip Id */
dev->chip_id = data;
}
}
}
return rslt;
}
/*!
* @brief This API is used to write the binary configuration in the sensor
*/
int8_t bma4_write_config_file(struct bma4_dev *dev)
{
int8_t rslt;
/* Config loading disable*/
uint8_t config_load = 0;
uint16_t index = 0;
uint8_t config_stream_status = 0;
/* Disable advanced power save */
rslt = bma4_set_advance_power_save(BMA4_DISABLE, dev);
if (rslt == BMA4_OK)
{
/* Disable config loading*/
rslt = bma4_write_regs(BMA4_INIT_CTRL_ADDR, &config_load, 1, dev);
if (rslt == BMA4_OK)
{
/* Write the config stream */
for (index = 0; index < dev->config_size; index += dev->read_write_len)
{
rslt = stream_transfer_write((dev->config_file_ptr + index), index, dev);
}
if (rslt == BMA4_OK)
{
/* Enable config loading and FIFO mode */
config_load = 0x01;
rslt = bma4_write_regs(BMA4_INIT_CTRL_ADDR, &config_load, 1, dev);
if (rslt == BMA4_OK)
{
/* Wait till ASIC is initialized. Refer the data-sheet for