forked from boschsensortec/BMA400_SensorAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bma400.c
3552 lines (3015 loc) · 122 KB
/
bma400.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 bma400.c
* @date 2020-06-05
* @version v1.5.8
*
*/
#include "bma400.h"
/*
* @brief Accel self test diff xyz data structure
*/
struct bma400_selftest_delta_limit
{
/* Accel X data */
int32_t x;
/* Accel Y data */
int32_t y;
/* Accel Z data */
int32_t z;
};
/************************************************************************************/
/*********************** Static function declarations *******************************/
/************************************************************************************/
/*
* @brief This internal API is used to validate the device pointer for
* null conditions.
*
* @param[in] dev : Structure instance of bma400_dev.
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t null_ptr_check(const struct bma400_dev *dev);
/*
* @brief This internal API is used to set sensor configurations
*
* @param[in] data : Data to be mapped with interrupt
* @param[in] conf : Sensor configurations to be set
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_sensor_conf(uint8_t *data, const struct bma400_sensor_conf *conf, struct bma400_dev *dev);
/*
* @brief This internal API is used to get sensor configurations
*
* @param[in] data : Data to be mapped with interrupt
* @param[in] conf : Sensor configurations to be set
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_sensor_conf(const uint8_t *data, struct bma400_sensor_conf *conf, struct bma400_dev *dev);
/*
* @brief This internal API is used to set the accel configurations in sensor
*
* @param[in] accel_conf : Structure instance with accel configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_accel_conf(const struct bma400_acc_conf *accel_conf, struct bma400_dev *dev);
/*
* @brief This API reads accel data along with sensor time
*
* @param[in] data_sel : Variable to select the data to be read
* @param[in,out] accel : Structure instance to store the accel data
* @param[in] dev : Structure instance of bma400_dev
*
* Assignable values for data_sel:
* - BMA400_DATA_ONLY
* - BMA400_DATA_SENSOR_TIME
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_accel_data(uint8_t data_sel, struct bma400_sensor_data *accel, struct bma400_dev *dev);
/*
* @brief This API enables the auto-wakeup feature
* of the sensor using a timeout value
*
* @param[in] wakeup_conf : Structure instance of wakeup configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_autowakeup_timeout(const struct bma400_auto_wakeup_conf *wakeup_conf, struct bma400_dev *dev);
/*
* @brief This API enables the auto-wakeup feature of the sensor
*
* @param[in] conf : Configuration value to enable/disable
* auto-wakeup interrupt
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_auto_wakeup(uint8_t conf, struct bma400_dev *dev);
/*
* @brief This API sets the parameters for auto-wakeup feature
* of the sensor
*
* @param[in] wakeup_conf : Structure instance of wakeup configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_autowakeup_interrupt(const struct bma400_wakeup_conf *wakeup_conf, struct bma400_dev *dev);
/*
* @brief This API sets the sensor to enter low power mode
* automatically based on the configurations
*
* @param[in] auto_lp_conf : Structure instance of auto-low power settings
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_auto_low_power(const struct bma400_auto_lp_conf *auto_lp_conf, struct bma400_dev *dev);
/*
* @brief This API sets the tap setting parameters
*
* @param[in] tap_set : Structure instance of tap configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_tap_conf(const struct bma400_tap_conf *tap_set, struct bma400_dev *dev);
/*
* @brief This API sets the parameters for activity change detection
*
* @param[in] act_ch_set : Structure instance of activity change
* configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_activity_change_conf(const struct bma400_act_ch_conf *act_ch_set, struct bma400_dev *dev);
/*
* @brief This API sets the parameters for generic interrupt1 configuration
*
* @param[in] gen_int_set : Structure instance of generic interrupt
* configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_gen1_int(const struct bma400_gen_int_conf *gen_int_set, struct bma400_dev *dev);
/*
* @brief This API sets the parameters for generic interrupt2 configuration
*
* @param[in] gen_int_set : Structure instance of generic interrupt
* configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_gen2_int(const struct bma400_gen_int_conf *gen_int_set, struct bma400_dev *dev);
/*
* @brief This API sets the parameters for orientation interrupt
*
* @param[in] orient_conf : Structure instance of orient interrupt
* configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_orient_int(const struct bma400_orient_int_conf *orient_conf, struct bma400_dev *dev);
/*
* @brief This internal API is used to get the accel configurations in sensor
*
* @param[in,out] accel_conf : Structure instance of basic
* accelerometer configuration
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_accel_conf(struct bma400_acc_conf *accel_conf, struct bma400_dev *dev);
/*
* @brief This API gets the set sensor settings for auto-wakeup timeout feature
*
* @param[in,out] wakeup_conf : Structure instance of wake-up configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_autowakeup_timeout(struct bma400_auto_wakeup_conf *wakeup_conf, struct bma400_dev *dev);
/*
* @brief This API gets the set sensor settings for
* auto-wakeup interrupt feature
*
* @param[in,out] wakeup_conf : Structure instance of wake-up configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_autowakeup_interrupt(struct bma400_wakeup_conf *wakeup_conf, struct bma400_dev *dev);
/*
* @brief This API gets the sensor to get the auto-low
* power mode configuration settings
*
* @param[in,out] auto_lp_conf : Structure instance of low power
* configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_auto_low_power(struct bma400_auto_lp_conf *auto_lp_conf, struct bma400_dev *dev);
/*
* @brief This API sets the tap setting parameters
*
* @param[in,out] tap_set : Structure instance of tap configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_tap_conf(struct bma400_tap_conf *tap_set, struct bma400_dev *dev);
/*
* @brief This API gets the parameters for activity change detection
*
* @param[in,out] act_ch_set : Structure instance of activity
* change configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_activity_change_conf(struct bma400_act_ch_conf *act_ch_set, struct bma400_dev *dev);
/*
* @brief This API gets the generic interrupt1 configuration
*
* @param[in,out] gen_int_set : Structure instance of generic
* interrupt configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_gen1_int(struct bma400_gen_int_conf *gen_int_set, struct bma400_dev *dev);
/*
* @brief This API gets the generic interrupt2 configuration
*
* @param[in,out] gen_int_set : Structure instance of generic
* interrupt configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_gen2_int(struct bma400_gen_int_conf *gen_int_set, struct bma400_dev *dev);
/*
* @brief This API gets the parameters for orientation interrupt
*
* @param[in,out] orient_conf : Structure instance of orient
* interrupt configurations
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_orient_int(struct bma400_orient_int_conf *orient_conf, struct bma400_dev *dev);
/*
* @brief This API sets the selected interrupt to be mapped to
* the hardware interrupt pin of the sensor
*
* @param[in,out] data_array : Data array of interrupt pin configurations
* @param[in] int_enable : Interrupt selected for pin mapping
* @param[in] int_map : Interrupt channel to be mapped
*
* @return Nothing
*/
static void map_int_pin(uint8_t *data_array, uint8_t int_enable, enum bma400_int_chan int_map);
/*
* @brief This API checks whether the interrupt is mapped to the INT pin1
* or INT pin2 of the sensor
*
* @param[in] int_1_map : Variable to denote whether the interrupt is
* mapped to INT1 pin or not
* @param[in] int_2_map : Variable to denote whether the interrupt is
* mapped to INT2 pin or not
* @param[in,out] int_map : Interrupt channel which is mapped
* INT1/INT2/NONE/BOTH
*
* @return Nothing
*/
static void check_mapped_interrupts(uint8_t int_1_map, uint8_t int_2_map, enum bma400_int_chan *int_map);
/*
* @brief This API gets the selected interrupt and its mapping to
* the hardware interrupt pin of the sensor
*
* @param[in,out] data_array : Data array of interrupt pin configurations
* @param[in] int_enable : Interrupt selected for pin mapping
* @param[out] int_map : Interrupt channel which is mapped
*
* @return Nothing
*/
static void get_int_pin_map(const uint8_t *data_array, uint8_t int_enable, enum bma400_int_chan *int_map);
/*
* @brief This API is used to set the interrupt pin configurations
*
* @param[in] int_conf : Interrupt pin configuration
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_int_pin_conf(struct bma400_int_pin_conf int_conf, struct bma400_dev *dev);
/*
* @brief This API is used to set the interrupt pin configurations
*
* @param[in,out] int_conf : Interrupt pin configuration
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_int_pin_conf(struct bma400_int_pin_conf *int_conf, struct bma400_dev *dev);
/*
* @brief This API is used to set the FIFO configurations
*
* @param[in,out] fifo_conf : Structure instance containing the FIFO
* configuration set in the sensor
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t set_fifo_conf(const struct bma400_fifo_conf *fifo_conf, struct bma400_dev *dev);
/*
* @brief This API is used to get the FIFO configurations
*
* @param[in,out] fifo_conf : Structure instance containing the FIFO
* configuration set in the sensor
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
static int8_t get_fifo_conf(struct bma400_fifo_conf *fifo_conf, struct bma400_dev *dev);
/*
* @brief This API is used to get the number of bytes filled in FIFO
*
* @param[in,out] fifo_byte_cnt : Number of bytes in the FIFO buffer
* actually filled by the sensor
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t get_fifo_length(uint16_t *fifo_byte_cnt, struct bma400_dev *dev);
/*
* @brief This API is used to read the FIFO of BMA400
*
* @param[in,out] fifo : Pointer to the fifo structure.
*
* @param[in] dev : Structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t read_fifo(struct bma400_fifo_data *fifo, struct bma400_dev *dev);
/*
* @brief This API is used to unpack the accelerometer frames from the FIFO
*
* @param[in,out] fifo : Pointer to the fifo structure.
* @param[in,out] accel_data : Structure instance to store the accel data
* @param[in,out] frame_count : Number of frames requested by user as input
* Number of frames actually parsed as output
* @param[in] dev : Structure instance of bma400_dev
*
* @return Nothing
*/
static void unpack_accel_frame(struct bma400_fifo_data *fifo,
struct bma400_sensor_data *accel_data,
uint16_t *frame_count,
const struct bma400_dev *dev);
/*
* @brief This API is used to check for a frame availability in FIFO
*
* @param[in,out] fifo : Pointer to the fifo structure.
* @param[in,out] frame_available : Variable to denote availability of a frame
* @param[in] accel_width : Variable to denote 12/8 bit accel data
* @param[in] data_en : Data enabled in FIFO
* @param[in,out] data_index : Index of the currently parsed FIFO data
*
* @return Nothing
*/
static void check_frame_available(const struct bma400_fifo_data *fifo,
uint8_t *frame_available,
uint8_t accel_width,
uint8_t data_en,
uint16_t *data_index);
/*
* @brief This API is used to unpack the accelerometer xyz data from the FIFO
* and store it in the user defined buffer
*
* @param[in,out] fifo : Pointer to the fifo structure.
* @param[in,out] accel_data : Structure instance to store the accel data
* @param[in,out] data_index : Index of the currently parsed FIFO data
* @param[in] accel_width : Variable to denote 12/8 bit accel data
* @param[in] frame_header : Variable to get the data enabled
*
* @return Nothing
*/
static void unpack_accel(const struct bma400_fifo_data *fifo,
struct bma400_sensor_data *accel_data,
uint16_t *data_index,
uint8_t accel_width,
uint8_t frame_header);
/*
* @brief This API is used to parse and store the sensor time from the
* FIFO data in the structure instance dev
*
* @param[in,out] fifo : Pointer to the fifo structure.
* @param[in,out] data_index : Index of the FIFO data which has sensor time
*
* @return Nothing
*/
static void unpack_sensortime_frame(struct bma400_fifo_data *fifo, uint16_t *data_index);
/*
* @brief This API validates the self test results
*
* @param[in] accel_pos : Structure pointer to store accel data
* for positive excitation
* @param[in] accel_neg : Structure pointer to store accel data
* for negative excitation
*
*@param[in] dev : structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval +ve value -> Warning
* @retval -ve value -> Error
*/
static int8_t validate_accel_self_test(const struct bma400_sensor_data *accel_pos,
const struct bma400_sensor_data *accel_neg);
/*
* @brief This API performs self test with positive excitation
*
* @param[in] accel_pos : Structure pointer to store accel data
* for positive excitation
* @param[in] dev : structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval -ve value -> Error
*/
static int8_t positive_excited_accel(struct bma400_sensor_data *accel_pos, struct bma400_dev *dev);
/*
* @brief This API performs self test with negative excitation
*
* @param[in] accel_neg : Structure pointer to store accel data
* for negative excitation
* @param[in] dev : structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval -ve value -> Error
*/
static int8_t negative_excited_accel(struct bma400_sensor_data *accel_neg, struct bma400_dev *dev);
/*
* @brief This API performs the pre-requisites needed to perform the self test
*
* @param[in] dev : structure instance of bma400_dev
*
* @return Result of API execution status
* @retval zero -> Success
* @retval -ve value -> Error
*/
static int8_t enable_self_test(struct bma400_dev *dev);
/************************************************************************************/
/*********************** User function definitions **********************************/
/************************************************************************************/
int8_t bma400_init(struct bma400_dev *dev)
{
int8_t rslt;
uint8_t chip_id = 0;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMA400_OK)
{
/* Initial power-up time */
dev->delay_us(5000, dev->intf_ptr);
/* Assigning dummy byte value */
if (dev->intf == BMA400_SPI_INTF)
{
/* Dummy Byte availability */
dev->dummy_byte = 1;
/* Dummy read of Chip-ID in SPI mode */
rslt = bma400_get_regs(BMA400_REG_CHIP_ID, &chip_id, 1, dev);
}
else
{
dev->dummy_byte = 0;
}
if (rslt == BMA400_OK)
{
/* Chip ID of the sensor is read */
rslt = bma400_get_regs(BMA400_REG_CHIP_ID, &chip_id, 1, dev);
/* Proceed if everything is fine until now */
if (rslt == BMA400_OK)
{
/* Check for chip id validity */
if (chip_id == BMA400_CHIP_ID)
{
/* Store the chip ID in dev structure */
dev->chip_id = chip_id;
}
else
{
rslt = BMA400_E_DEV_NOT_FOUND;
}
}
}
}
return rslt;
}
int8_t bma400_set_regs(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, struct bma400_dev *dev)
{
int8_t rslt;
uint8_t count;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMA400_OK) && (reg_data != NULL))
{
/* 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
*/
if (len == 1)
{
dev->intf_rslt = dev->write(reg_addr, reg_data, len, dev->intf_ptr);
if (dev->intf_rslt != BMA400_INTF_RET_SUCCESS)
{
/* Failure case */
rslt = BMA400_E_COM_FAIL;
}
}
/* Burst write is not allowed thus we split burst case write
* into single byte writes Thus user can write multiple bytes
* with ease
*/
if (len > 1)
{
for (count = 0; (count < len) && (rslt == BMA400_OK); count++)
{
dev->intf_rslt = dev->write(reg_addr, ®_data[count], 1, dev->intf_ptr);
reg_addr++;
if (dev->intf_rslt != BMA400_INTF_RET_SUCCESS)
{
/* Failure case */
rslt = BMA400_E_COM_FAIL;
}
}
}
}
else
{
rslt = BMA400_E_NULL_PTR;
}
return rslt;
}
int8_t bma400_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bma400_dev *dev)
{
int8_t rslt;
uint16_t index;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMA400_OK) && (reg_data != NULL))
{
uint32_t temp_len = len + dev->dummy_byte;
uint8_t temp_buff[temp_len];
if (dev->intf != BMA400_I2C_INTF)
{
/* If interface selected is SPI */
reg_addr = reg_addr | BMA400_SPI_RD_MASK;
}
/* Read the data from the reg_addr */
dev->intf_rslt = dev->read(reg_addr, temp_buff, temp_len, dev->intf_ptr);
if (dev->intf_rslt == BMA400_INTF_RET_SUCCESS)
{
for (index = 0; index < len; index++)
{
/* Parse the data read and store in "reg_data"
* buffer so that the dummy byte is removed
* and user will get only valid data
*/
reg_data[index] = temp_buff[index + dev->dummy_byte];
}
}
else
{
/* Failure case */
rslt = BMA400_E_COM_FAIL;
}
}
else
{
rslt = BMA400_E_NULL_PTR;
}
return rslt;
}
int8_t bma400_soft_reset(struct bma400_dev *dev)
{
int8_t rslt;
uint8_t data = BMA400_SOFT_RESET_CMD;
/* Null-pointer check */
rslt = null_ptr_check(dev);
if (rslt == BMA400_OK)
{
/* Reset the device */
rslt = bma400_set_regs(BMA400_REG_COMMAND, &data, 1, dev);
dev->delay_us(BMA400_DELAY_US_SOFT_RESET, dev->intf_ptr);
if ((rslt == BMA400_OK) && (dev->intf == BMA400_SPI_INTF))
{
/* Dummy read of 0x7F register to enable SPI Interface
* if SPI is used
*/
rslt = bma400_get_regs(0x7F, &data, 1, dev);
}
}
return rslt;
}
int8_t bma400_set_power_mode(uint8_t power_mode, struct bma400_dev *dev)
{
int8_t rslt;
uint8_t reg_data = 0;
rslt = null_ptr_check(dev);
if (rslt == BMA400_OK)
{
rslt = bma400_get_regs(BMA400_REG_ACCEL_CONFIG_0, ®_data, 1, dev);
}
if (rslt == BMA400_OK)
{
reg_data = BMA400_SET_BITS_POS_0(reg_data, BMA400_POWER_MODE, power_mode);
/* Set the power mode of sensor */
rslt = bma400_set_regs(BMA400_REG_ACCEL_CONFIG_0, ®_data, 1, dev);
if (power_mode == BMA400_MODE_LOW_POWER)
{
/* A delay of 1/ODR is required to switch power modes
* Low power mode has 25Hz frequency and hence it needs
* 40ms delay to enter low power mode
*/
dev->delay_us(40000, dev->intf_ptr);
}
else
{
dev->delay_us(10000, dev->intf_ptr); /* TBC */
}
}
return rslt;
}
int8_t bma400_get_power_mode(uint8_t *power_mode, struct bma400_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 == BMA400_OK) && (power_mode != NULL))
{
rslt = bma400_get_regs(BMA400_REG_STATUS, ®_data, 1, dev);
*power_mode = BMA400_GET_BITS(reg_data, BMA400_POWER_MODE_STATUS);
}
else
{
rslt = BMA400_E_NULL_PTR;
}
return rslt;
}
int8_t bma400_get_accel_data(uint8_t data_sel, struct bma400_sensor_data *accel, struct bma400_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 == BMA400_OK) && (accel != NULL))
{
/* Read and store the accel data */
rslt = get_accel_data(data_sel, accel, dev);
}
else
{
rslt = BMA400_E_NULL_PTR;
}
return rslt;
}
int8_t bma400_set_sensor_conf(const struct bma400_sensor_conf *conf, uint16_t n_sett, struct bma400_dev *dev)
{
int8_t rslt;
uint16_t idx = 0;
uint8_t data_array[3] = { 0 };
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMA400_OK) && (conf != NULL))
{
/* Read the interrupt pin mapping configurations */
rslt = bma400_get_regs(BMA400_REG_INT_MAP, data_array, 3, dev);
if (rslt == BMA400_OK)
{
for (idx = 0; (idx < n_sett) && (rslt == BMA400_OK); idx++)
{
rslt = set_sensor_conf(data_array, conf + idx, dev);
}
if (rslt == BMA400_OK)
{
/* Set the interrupt pin mapping configurations */
rslt = bma400_set_regs(BMA400_REG_INT_MAP, data_array, 3, dev);
}
}
}
else
{
rslt = BMA400_E_NULL_PTR;
}
return rslt;
}
int8_t bma400_get_sensor_conf(struct bma400_sensor_conf *conf, uint16_t n_sett, struct bma400_dev *dev)
{
int8_t rslt;
uint16_t idx;
uint8_t data_array[3] = { 0 };
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
if ((rslt == BMA400_OK) && (conf != NULL))
{
/* Read the interrupt pin mapping configurations */
rslt = bma400_get_regs(BMA400_REG_INT_MAP, data_array, 3, dev);
for (idx = 0; (idx < n_sett) && (rslt == BMA400_OK); idx++)
{
rslt = get_sensor_conf(data_array, conf + idx, dev);
}
}
else
{
rslt = BMA400_E_NULL_PTR;
}
return rslt;
}
int8_t bma400_set_device_conf(const struct bma400_device_conf *conf, uint8_t n_sett, struct bma400_dev *dev)
{
int8_t rslt;
uint16_t idx;
uint8_t data_array[3] = { 0 };
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
if ((rslt == BMA400_OK) && (conf != NULL))
{
/* Read the interrupt pin mapping configurations */
rslt = bma400_get_regs(BMA400_REG_INT_MAP, data_array, 3, dev);
for (idx = 0; (idx < n_sett) && (rslt == BMA400_OK); idx++)
{
switch (conf[idx].type)
{
case BMA400_AUTOWAKEUP_TIMEOUT:
rslt = set_autowakeup_timeout(&conf[idx].param.auto_wakeup, dev);
break;
case BMA400_AUTOWAKEUP_INT:
rslt = set_autowakeup_interrupt(&conf[idx].param.wakeup, dev);
if (rslt == BMA400_OK)
{
/* Interrupt pin mapping */
map_int_pin(data_array, BMA400_WAKEUP_INT_MAP, conf[idx].param.wakeup.int_chan);
}
break;
case BMA400_AUTO_LOW_POWER:
rslt = set_auto_low_power(&conf[idx].param.auto_lp, dev);
break;
case BMA400_INT_PIN_CONF:
rslt = set_int_pin_conf(conf[idx].param.int_conf, dev);
break;
case BMA400_INT_OVERRUN_CONF:
/* Interrupt pin mapping */
map_int_pin(data_array, BMA400_INT_OVERRUN_MAP, conf[idx].param.overrun_int.int_chan);
break;
case BMA400_FIFO_CONF:
rslt = set_fifo_conf(&conf[idx].param.fifo_conf, dev);
if (rslt == BMA400_OK)
{
/* Interrupt pin mapping */
map_int_pin(data_array, BMA400_FIFO_WM_INT_MAP, conf[idx].param.fifo_conf.fifo_wm_channel);
map_int_pin(data_array, BMA400_FIFO_FULL_INT_MAP, conf[idx].param.fifo_conf.fifo_full_channel);
}
break;
default:
rslt = BMA400_E_INVALID_CONFIG;
}
}
if (rslt == BMA400_OK)
{
/* Set the interrupt pin mapping configurations */
rslt = bma400_set_regs(BMA400_REG_INT_MAP, data_array, 3, dev);
}
}