-
Notifications
You must be signed in to change notification settings - Fork 0
/
bma580_features.c
1609 lines (1258 loc) · 59.1 KB
/
bma580_features.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) 2024 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 bma580_features.c
* @date 2024-07-29
* @version v4.2.0
*
*/
/******************************************************************************/
/****************************** Header files **********************************/
/******************************************************************************/
#include "bma580_features.h"
/******************************************************************************/
/*********************** Static function declarations *************************/
/******************************************************************************/
/*!
* @brief Internal API to verify the validity of the primary device handle which
* is passed as argument.
*
* @param[in] dev : Structure instance of bma5_dev.
*
* @return Result of API execution status
* @retval BMA5_OK -> device handle is valid
* @retval BMA5_E_NULL_PTR -> Null pointer error
*/
static int8_t verify_handle(const struct bma5_dev *dev);
/******************************************************************************/
/*********************** User 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 bma580_init(struct bma5_dev *dev)
{
/* Variable to store the function result */
int8_t result;
/* Variable to get chip id */
uint8_t data = 0;
uint8_t sensor_health_status = 0;
/* Null-pointer check */
result = verify_handle(dev);
if (result == BMA5_OK)
{
dev->chip_id = 0;
if (dev->intf == BMA5_SPI_INTF)
{
dev->dummy_byte = 1;
}
else
{
dev->dummy_byte = 0;
}
/* Dummy read results in NACK. Hence result is not checked */
(void) bma5_get_regs(BMA580_REG_CHIP_ID, &data, 1, dev);
result = bma5_get_regs(BMA580_REG_CHIP_ID, &data, 1, dev);
if (result == BMA5_OK)
{
/* Assign Chip Id */
dev->chip_id = data;
}
if (dev->chip_id != BMA580_CHIP_ID)
{
result = BMA5_E_DEV_NOT_FOUND;
}
/* Bring sensor from Suspend Mode to Normal Mode */
if (result == BMA5_OK)
{
result = bma5_set_cmd_suspend(BMA5_SUSPEND_DISABLE, dev);
if (result != BMA5_OK)
{
return result;
}
/* Wait until the sensor wakesup and internal health status is good */
for (;;)
{
result = bma5_get_health_status(&sensor_health_status, dev);
if (result != BMA5_OK)
{
break;
}
if (sensor_health_status == BMA5_SENSOR_HEALTH_STATUS_MSK)
{
break;
}
}
}
}
else
{
result = BMA5_E_NULL_PTR;
}
return result;
}
/*!
* @brief This API sets feature axis configurations
*/
int8_t bma580_set_feature_axis_config(const struct bma580_feat_axis *feat_axis, struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
/* Variable to store base address of feature axis */
uint8_t data = BMA580_BASE_ADDR_GENERAL_SETTINGS;
/* Array to store feature axis data */
uint8_t feat_axis_data[4] = { 0 };
if (feat_axis == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
/* Set the feature axis base address to feature engine transmission address to start DMA transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
if (result == BMA5_OK)
{
/* Get the configuration from the feature engine register */
result = bma5_get_regs(BMA5_REG_FEATURE_DATA_TX, feat_axis_data, 4, dev);
if (result == BMA5_OK)
{
feat_axis_data[2] = BMA5_SET_BITS(feat_axis_data[2], BMA580_FEAT_AXIS_EX, feat_axis->feat_axis_ex);
feat_axis_data[2] = BMA5_SET_BITS(feat_axis_data[2], BMA580_FEAT_X_INV, feat_axis->feat_x_inv);
feat_axis_data[2] = BMA5_SET_BITS(feat_axis_data[2], BMA580_FEAT_Y_INV, feat_axis->feat_y_inv);
feat_axis_data[2] = BMA5_SET_BITS(feat_axis_data[2], BMA580_FEAT_Z_INV, feat_axis->feat_z_inv);
feat_axis_data[0] = feat_axis_data[2];
feat_axis_data[1] = feat_axis_data[3];
/* Set the configuration from the feature engine register */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_TX, feat_axis_data, 2, dev);
}
}
}
return result;
}
/*!
* @brief This API gets feature axis configurations
*/
int8_t bma580_get_feature_axis_config(struct bma580_feat_axis *feat_axis, struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
/* Variable to store base address of feature axis */
uint8_t data = BMA580_BASE_ADDR_GENERAL_SETTINGS;
/* Array to store feature axis data */
uint8_t feat_axis_data[4] = { 0 };
if (feat_axis == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
/* Set the feature axis base address to feature engine transmission address to start DMA transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
if (result == BMA5_OK)
{
/* Get the configuration from the feature engine register */
result = bma5_get_regs(BMA5_REG_FEATURE_DATA_TX, feat_axis_data, 4, dev);
if (result == BMA5_OK)
{
feat_axis->feat_axis_ex = BMA5_GET_BITS(feat_axis_data[2], BMA580_FEAT_AXIS_EX);
feat_axis->feat_x_inv = BMA5_GET_BITS(feat_axis_data[2], BMA580_FEAT_X_INV);
feat_axis->feat_y_inv = BMA5_GET_BITS(feat_axis_data[2], BMA580_FEAT_Y_INV);
feat_axis->feat_z_inv = BMA5_GET_BITS(feat_axis_data[2], BMA580_FEAT_Z_INV);
}
}
}
return result;
}
/*!
* @brief This API sets feature configuration error status
*/
int8_t bma580_set_feat_conf_err(const struct bma580_feat_conf_err *feat_conf_err, struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
/* Variable to store base address of feature config error */
uint8_t data = BMA580_BASE_ADDR_FEAT_CONF_ERR;
/* Array to store feature config error data */
uint8_t feat_conf_err_data[4] = { 0 };
if (feat_conf_err == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
/* Set the feature config error base address to feature engine transmission address to start DMA transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
if (result == BMA5_OK)
{
/* Get the configuration from the feature engine register */
result = bma5_get_regs(BMA5_REG_FEATURE_DATA_TX, feat_conf_err_data, 4, dev);
if (result == BMA5_OK)
{
feat_conf_err_data[2] = BMA5_SET_BITS(feat_conf_err_data[2],
BMA580_GEN_INT1_CONF_ERR,
feat_conf_err->gen_int1_conf_err);
feat_conf_err_data[2] = BMA5_SET_BITS(feat_conf_err_data[2],
BMA580_GEN_INT2_CONF_ERR,
feat_conf_err->gen_int2_conf_err);
feat_conf_err_data[2] = BMA5_SET_BITS(feat_conf_err_data[2],
BMA580_GEN_INT3_CONF_ERR,
feat_conf_err->gen_int3_conf_err);
feat_conf_err_data[2] = BMA5_SET_BITS(feat_conf_err_data[2],
BMA580_ACC_FOC_CONF_ERR,
feat_conf_err->acc_foc_conf_err);
feat_conf_err_data[2] = BMA5_SET_BITS(feat_conf_err_data[2],
BMA580_TAP_CONF_ERR,
feat_conf_err->tap_conf_err);
feat_conf_err_data[2] = BMA5_SET_BITS(feat_conf_err_data[2],
BMA580_VAD_CONF_ERR,
feat_conf_err->vad_conf_err);
feat_conf_err_data[2] = BMA5_SET_BITS(feat_conf_err_data[2],
BMA580_SELF_WAKE_UP_ERR,
feat_conf_err->self_wake_up_err);
feat_conf_err_data[0] = feat_conf_err_data[2];
feat_conf_err_data[1] = feat_conf_err_data[3];
/* Set the configuration from the feature engine register */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_TX, feat_conf_err_data, 2, dev);
}
}
}
return result;
}
/*!
* @brief This API gets feature configuration error status
*/
int8_t bma580_get_feat_conf_err(struct bma580_feat_conf_err *feat_conf_err, struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
/* Variable to store base address of feature config error */
uint8_t data = BMA580_BASE_ADDR_FEAT_CONF_ERR;
/* Array to store feature config error data */
uint8_t feat_conf_err_data[4] = { 0 };
if (feat_conf_err == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
/* Set the feature config error base address to feature engine transmission address to start DMA transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
if (result == BMA5_OK)
{
/* Get the configuration from the feature engine register */
result = bma5_get_regs(BMA5_REG_FEATURE_DATA_TX, feat_conf_err_data, 4, dev);
if (result == BMA5_OK)
{
feat_conf_err->gen_int1_conf_err = BMA5_GET_BITS(feat_conf_err_data[2], BMA580_GEN_INT1_CONF_ERR);
feat_conf_err->gen_int2_conf_err = BMA5_GET_BITS(feat_conf_err_data[2], BMA580_GEN_INT2_CONF_ERR);
feat_conf_err->gen_int3_conf_err = BMA5_GET_BITS(feat_conf_err_data[2], BMA580_GEN_INT3_CONF_ERR);
feat_conf_err->acc_foc_conf_err = BMA5_GET_BITS(feat_conf_err_data[2], BMA580_ACC_FOC_CONF_ERR);
feat_conf_err->tap_conf_err = BMA5_GET_BITS(feat_conf_err_data[2], BMA580_TAP_CONF_ERR);
feat_conf_err->vad_conf_err = BMA5_GET_BITS(feat_conf_err_data[2], BMA580_VAD_CONF_ERR);
feat_conf_err->self_wake_up_err = BMA5_GET_BITS(feat_conf_err_data[2], BMA580_SELF_WAKE_UP_ERR);
}
}
}
return result;
}
/*!
* @brief This API gets default generic interrupt configurations for Generic Interrupt 1 and 2
*/
int8_t bma580_get_default_generic_int_config(struct bma580_generic_interrupt_types *gen_int,
uint8_t n_ints,
struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
uint8_t loop;
/* Variable to store base address of generic interrupt 1 */
uint8_t data;
if (gen_int == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
for (loop = 0; loop < n_ints; loop++)
{
switch (gen_int[loop].generic_interrupt)
{
case BMA580_GEN_INT_1:
data = BMA580_BASE_ADDR_GENERIC_INT1;
break;
case BMA580_GEN_INT_2:
data = BMA580_BASE_ADDR_GENERIC_INT2;
break;
default:
result = BMA5_E_INVALID_GEN_INT;
return result;
}
if (result == BMA5_OK)
{
/* Set the generic interrupt base address to feature engine transmission address to start DMA
* transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
}
if (result == BMA5_OK)
{
gen_int[loop].gen_int.comb_sel = BMA580_GEN_INT_COMB_SEL_LOGICAL_OR;
gen_int[loop].gen_int.axis_sel = BMA580_AXIS_SEL_DEFAULT;
gen_int[loop].gen_int.criterion_sel = BMA580_GEN_INT_CRI_SEL_ACT;
gen_int[loop].gen_int.acc_ref_up = BMA580_GEN_INT_ACC_REF_UP_ON_ALWAYS;
gen_int[loop].gen_int.duration = BMA580_DURATION_DEFAULT;
gen_int[loop].gen_int.wait_time = BMA580_WAIT_TIME_DEFAULT;
gen_int[loop].gen_int.quiet_time = BMA580_QUIET_TIME_DEFAULT;
gen_int[loop].gen_int.ref_acc_x = BMA580_ACC_REF_X_DEFAULT;
gen_int[loop].gen_int.ref_acc_y = BMA580_ACC_REF_Y_DEFAULT;
gen_int[loop].gen_int.ref_acc_z = BMA580_ACC_REF_Z_DEFAULT;
if (dev->context == BMA5_HEARABLE && gen_int[loop].generic_interrupt == BMA580_GEN_INT_1)
{
gen_int[loop].gen_int.slope_thres = BMA580_GENERIC_INTERRUPT1_1_GI1_SLOPE_THRES_H;
gen_int[loop].gen_int.hysteresis = BMA580_GENERIC_INTERRUPT1_2_GI1_HYSTERESIS_H;
}
else if (dev->context == BMA5_WEARABLE && gen_int[loop].generic_interrupt == BMA580_GEN_INT_1)
{
gen_int[loop].gen_int.slope_thres = BMA580_GENERIC_INTERRUPT1_1_GI1_SLOPE_THRES_W;
gen_int[loop].gen_int.hysteresis = BMA580_GENERIC_INTERRUPT1_2_GI1_HYSTERESIS_W;
}
else if (dev->context == BMA5_SMARTPHONE && gen_int[loop].generic_interrupt == BMA580_GEN_INT_1)
{
gen_int[loop].gen_int.slope_thres = BMA580_GENERIC_INTERRUPT1_1_GI1_SLOPE_THRES_S;
gen_int[loop].gen_int.hysteresis = BMA580_GENERIC_INTERRUPT1_2_GI1_HYSTERESIS_S;
}
else if (dev->context == BMA5_HEARABLE && gen_int[loop].generic_interrupt == BMA580_GEN_INT_2)
{
gen_int[loop].gen_int.slope_thres = BMA580_GENERIC_INTERRUPT2_1_GI2_SLOPE_THRES_H;
gen_int[loop].gen_int.hysteresis = BMA580_GENERIC_INTERRUPT2_2_GI2_HYSTERESIS_H;
}
else if (dev->context == BMA5_WEARABLE && gen_int[loop].generic_interrupt == BMA580_GEN_INT_2)
{
gen_int[loop].gen_int.slope_thres = BMA580_GENERIC_INTERRUPT2_1_GI2_SLOPE_THRES_W;
gen_int[loop].gen_int.hysteresis = BMA580_GENERIC_INTERRUPT2_2_GI2_HYSTERESIS_W;
}
else if (dev->context == BMA5_SMARTPHONE && gen_int[loop].generic_interrupt == BMA580_GEN_INT_2)
{
gen_int[loop].gen_int.slope_thres = BMA580_GENERIC_INTERRUPT2_1_GI2_SLOPE_THRES_S;
gen_int[loop].gen_int.hysteresis = BMA580_GENERIC_INTERRUPT2_2_GI2_HYSTERESIS_S;
}
else
{
result = BMA5_E_INVALID_CONTEXT_PARAM;
}
}
}
}
return result;
}
/*!
* @brief This API sets generic interrupt configurations
*/
int8_t bma580_set_generic_int_config(const struct bma580_generic_interrupt_types *gen_int,
uint8_t n_ints,
struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
uint8_t loop;
/* Variable to store base address of generic interrupt */
uint8_t data;
/* Array to store generic interrupt data */
uint8_t int_data[16] = { 0 };
uint16_t slope_thres_1, slope_thres_2, comb_sel, axis_sel, hysteresis_1, hysteresis_2, criterion_sel;
uint16_t acc_ref_up, duration_1, duration_2, wait_time, quiet_time_1, quiet_time_2;
uint16_t ref_acc_x_1, ref_acc_x_2, ref_acc_y_1, ref_acc_y_2, ref_acc_z_1, ref_acc_z_2;
if (gen_int == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
for (loop = 0; loop < n_ints; loop++)
{
switch (gen_int[loop].generic_interrupt)
{
case BMA580_GEN_INT_1:
data = BMA580_BASE_ADDR_GENERIC_INT1;
break;
case BMA580_GEN_INT_2:
data = BMA580_BASE_ADDR_GENERIC_INT2;
break;
case BMA580_GEN_INT_3:
data = BMA580_BASE_ADDR_GENERIC_INT3;
break;
default:
result = BMA5_E_INVALID_GEN_INT;
return result;
}
if (result == BMA5_OK)
{
/* Set the generic interrupt base address to feature engine transmission address to start DMA
* transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
}
if (result == BMA5_OK)
{
/* Get the configuration from the feature engine register */
result = bma5_get_regs(BMA5_REG_FEATURE_DATA_TX, int_data, 16, dev);
if (result == BMA5_OK)
{
/* Settings 1 */
slope_thres_1 =
(BMA5_SET_BITS_POS_0(int_data[2], BMA580_GEN_INT_SLOPE_THRES,
gen_int[loop].gen_int.slope_thres) & BMA580_GEN_INT_SLOPE_THRES_MSK);
slope_thres_2 = (uint16_t)(int_data[3] << 8);
slope_thres_2 =
(BMA5_SET_BITS_POS_0(slope_thres_2, BMA580_GEN_INT_SLOPE_THRES,
gen_int[loop].gen_int.slope_thres) & BMA580_GEN_INT_SLOPE_THRES_MSK);
comb_sel =
(BMA5_SET_BITS(int_data[3], BMA580_GEN_INT_COMB_SEL,
gen_int[loop].gen_int.comb_sel) & BMA580_GEN_INT_COMB_SEL_MSK);
axis_sel =
(BMA5_SET_BITS(int_data[3], BMA580_GEN_INT_AXIS_SEL,
gen_int[loop].gen_int.axis_sel) & BMA580_GEN_INT_AXIS_SEL_MSK);
/* Settings 2 */
hysteresis_1 =
(BMA5_SET_BITS_POS_0(int_data[4], BMA580_GEN_INT_HYST,
gen_int[loop].gen_int.hysteresis) & BMA580_GEN_INT_HYST_MSK);
hysteresis_2 = (uint16_t)(int_data[5] << 8);
hysteresis_2 =
(BMA5_SET_BITS_POS_0(hysteresis_2, BMA580_GEN_INT_HYST,
gen_int[loop].gen_int.hysteresis) & BMA580_GEN_INT_HYST_MSK);
criterion_sel =
(BMA5_SET_BITS(int_data[5], BMA580_GEN_INT_CRIT_SEL,
gen_int[loop].gen_int.criterion_sel) & BMA580_GEN_INT_CRIT_SEL_MSK);
acc_ref_up =
(BMA5_SET_BITS(int_data[5], BMA580_GEN_INT_ACC_REF_UP,
gen_int[loop].gen_int.acc_ref_up) & BMA580_GEN_INT_ACC_REF_UP_MSK);
/* Settings 3 */
duration_1 =
(BMA5_SET_BITS_POS_0(int_data[6], BMA580_GEN_INT_DURATION,
gen_int[loop].gen_int.duration) & BMA580_GEN_INT_DURATION_MSK);
duration_2 = (uint16_t)(int_data[7] << 8);
duration_2 =
(BMA5_SET_BITS_POS_0(duration_2, BMA580_GEN_INT_DURATION,
gen_int[loop].gen_int.duration) & BMA580_GEN_INT_DURATION_MSK);
wait_time =
(BMA5_SET_BITS(int_data[7], BMA580_GEN_INT_WAIT_TIME,
gen_int[loop].gen_int.wait_time) & BMA580_GEN_INT_WAIT_TIME_MSK);
/* Settings 4 */
quiet_time_1 =
(BMA5_SET_BITS_POS_0(int_data[8], BMA580_GEN_INT_QUIET_TIME,
gen_int[loop].gen_int.quiet_time) & BMA580_GEN_INT_QUIET_TIME_MSK);
quiet_time_2 = (uint16_t)(int_data[9] << 8);
quiet_time_2 =
(BMA5_SET_BITS_POS_0(quiet_time_2, BMA580_GEN_INT_QUIET_TIME,
gen_int[loop].gen_int.quiet_time) & BMA580_GEN_INT_QUIET_TIME_MSK);
/* Settings 5 */
ref_acc_x_1 =
BMA5_SET_BITS_POS_0(int_data[10], BMA580_GEN_INT_REF_ACC_X,
(uint16_t)(gen_int[loop].gen_int.ref_acc_x & BMA580_GEN_INT_REF_ACC_X_MSK));
ref_acc_x_2 = (uint16_t)(int_data[11] << 8);
ref_acc_x_2 =
BMA5_SET_BITS_POS_0(ref_acc_x_2, BMA580_GEN_INT_REF_ACC_X,
(uint16_t)(gen_int[loop].gen_int.ref_acc_x & BMA580_GEN_INT_REF_ACC_X_MSK));
/* Settings 6 */
ref_acc_y_1 =
BMA5_SET_BITS_POS_0(int_data[12], BMA580_GEN_INT_REF_ACC_Y,
(uint16_t)(gen_int[loop].gen_int.ref_acc_y & BMA580_GEN_INT_REF_ACC_Y_MSK));
ref_acc_y_2 = (uint16_t)(int_data[13] << 8);
ref_acc_y_2 =
BMA5_SET_BITS_POS_0(ref_acc_y_2, BMA580_GEN_INT_REF_ACC_Y,
(uint16_t)(gen_int[loop].gen_int.ref_acc_y & BMA580_GEN_INT_REF_ACC_Y_MSK));
/* Settings 7 */
ref_acc_z_1 =
BMA5_SET_BITS_POS_0(int_data[14], BMA580_GEN_INT_REF_ACC_Z,
(uint16_t)(gen_int[loop].gen_int.ref_acc_z & BMA580_GEN_INT_REF_ACC_Z_MSK));
ref_acc_z_2 = (uint16_t)(int_data[15] << 8);
ref_acc_z_2 =
BMA5_SET_BITS_POS_0(ref_acc_z_2, BMA580_GEN_INT_REF_ACC_Z,
(uint16_t)(gen_int[loop].gen_int.ref_acc_z & BMA580_GEN_INT_REF_ACC_Z_MSK));
int_data[0] = (uint8_t)slope_thres_1;
int_data[1] = (uint8_t)((slope_thres_2 | comb_sel | axis_sel) >> 8);
int_data[2] = (uint8_t)hysteresis_1;
int_data[3] = (uint8_t)((hysteresis_2 | criterion_sel | acc_ref_up) >> 8);
int_data[4] = (uint8_t)duration_1;
int_data[5] = (uint8_t)((duration_2 | wait_time) >> 8);
int_data[6] = (uint8_t)quiet_time_1;
int_data[7] = (uint8_t)(quiet_time_2 >> 8);
int_data[8] = (uint8_t)ref_acc_x_1;
int_data[9] = (uint8_t)(ref_acc_x_2 >> 8);
int_data[10] = (uint8_t)ref_acc_y_1;
int_data[11] = (uint8_t)(ref_acc_y_2 >> 8);
int_data[12] = (uint8_t)ref_acc_z_1;
int_data[13] = (uint8_t)(ref_acc_z_2 >> 8);
/* Set the configuration from the feature engine register */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_TX, int_data, 14, dev);
}
}
}
}
return result;
}
/*!
* @brief This API gets generic interrupt configurations
*/
int8_t bma580_get_generic_int_config(struct bma580_generic_interrupt_types *gen_int,
uint8_t n_ints,
struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
uint8_t loop;
/* Variable to store base address of generic interrupt */
uint8_t data;
/* Array to store generic interrupt data */
uint8_t int_data[16] = { 0 };
/* Variable to define array offset */
uint8_t idx = 0;
/* Variable to define LSB */
uint16_t lsb;
/* Variable to define MSB */
uint16_t msb;
/* Variable to define a word */
uint16_t lsb_msb;
if (gen_int == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
for (loop = 0; loop < n_ints; loop++)
{
switch (gen_int[loop].generic_interrupt)
{
case BMA580_GEN_INT_1:
data = BMA580_BASE_ADDR_GENERIC_INT1;
break;
case BMA580_GEN_INT_2:
data = BMA580_BASE_ADDR_GENERIC_INT2;
break;
case BMA580_GEN_INT_3:
data = BMA580_BASE_ADDR_GENERIC_INT3;
break;
default:
result = BMA5_E_INVALID_GEN_INT;
return result;
}
if (result == BMA5_OK)
{
/* Set the generic interrupt base address to feature engine transmission address to start DMA
* transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
}
if (result == BMA5_OK)
{
/* Get the configuration from the feature engine register */
result = bma5_get_regs(BMA5_REG_FEATURE_DATA_TX, int_data, 16, dev);
if (result == BMA5_OK)
{
/* First two bytes are dummy bytes */
idx = 2;
/* Settings 1 */
/* Get word to calculate slope threshold, comb_sel and axis select from same word */
lsb = (uint16_t) int_data[idx++];
msb = ((uint16_t) int_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
gen_int[loop].gen_int.slope_thres = lsb_msb & BMA580_GEN_INT_SLOPE_THRES_MSK;
gen_int[loop].gen_int.comb_sel = (lsb_msb & BMA580_GEN_INT_COMB_SEL_MSK) >>
BMA580_GEN_INT_COMB_SEL_POS;
gen_int[loop].gen_int.axis_sel = (lsb_msb & BMA580_GEN_INT_AXIS_SEL_MSK) >>
BMA580_GEN_INT_AXIS_SEL_POS;
/* Settings 2 */
/* Get word to calculate hysteresis, criterion_sel and acc_ref_up from same word */
lsb = (uint16_t) int_data[idx++];
msb = ((uint16_t) int_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
gen_int[loop].gen_int.hysteresis = lsb_msb & BMA580_GEN_INT_HYST_MSK;
gen_int[loop].gen_int.criterion_sel = (lsb_msb & BMA580_GEN_INT_CRIT_SEL_MSK) >>
BMA580_GEN_INT_CRIT_SEL_POS;
gen_int[loop].gen_int.acc_ref_up = (lsb_msb & BMA580_GEN_INT_ACC_REF_UP_MSK) >>
BMA580_GEN_INT_ACC_REF_UP_POS;
/* Settings 3 */
/* Get word to calculate duration and wait time from same word */
lsb = (uint16_t) int_data[idx++];
msb = ((uint16_t) int_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
gen_int[loop].gen_int.duration = lsb_msb & BMA580_GEN_INT_DURATION_MSK;
gen_int[loop].gen_int.wait_time = (lsb_msb & BMA580_GEN_INT_WAIT_TIME_MSK) >>
BMA580_GEN_INT_WAIT_TIME_POS;
/* Settings 4 */
/* Get word to calculate quiet time */
lsb = (uint16_t) int_data[idx++];
msb = ((uint16_t) int_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
gen_int[loop].gen_int.quiet_time = lsb_msb & BMA580_GEN_INT_QUIET_TIME_MSK;
/* Settings 5 */
/* Get word to calculate ref_acc_x */
lsb = (uint16_t) int_data[idx++];
msb = ((uint16_t) int_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
gen_int[loop].gen_int.ref_acc_x = (int16_t)(lsb_msb & BMA580_GEN_INT_REF_ACC_X_MSK);
/* Settings 6 */
/* Get word to calculate ref_acc_y */
lsb = (uint16_t) int_data[idx++];
msb = ((uint16_t) int_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
gen_int[loop].gen_int.ref_acc_y = (int16_t)(lsb_msb & BMA580_GEN_INT_REF_ACC_Y_MSK);
/* Settings 7 */
/* Get word to calculate ref_acc_z */
lsb = (uint16_t) int_data[idx++];
msb = ((uint16_t) int_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
gen_int[loop].gen_int.ref_acc_z = (int16_t)(lsb_msb & BMA580_GEN_INT_REF_ACC_Z_MSK);
}
}
}
}
return result;
}
/*!
* @brief This API sets accel foc configuration
*/
int8_t bma580_set_accel_foc_config(const struct bma580_accel_foc_config *acc_foc, struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
/* Variable to store base address of accel foc */
uint8_t data = BMA580_BASE_ADDR_ACC_FOC;
/* Array to store accel foc config data */
uint8_t acc_foc_data[10] = { 0 };
uint16_t reg_data;
uint16_t foc_off_x, foc_off_y, foc_off_z;
uint8_t foc_apply_corr, foc_filter_coeff, foc_axis_1g;
if (acc_foc == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
/* Set the accel foc base address to feature engine transmission address to start DMA transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
if (result == BMA5_OK)
{
/* Get the configuration from the feature engine register */
result = bma5_get_regs(BMA5_REG_FEATURE_DATA_TX, acc_foc_data, 10, dev);
if (result == BMA5_OK)
{
/* Settings 1 */
reg_data = (uint16_t)(((uint16_t)acc_foc_data[2] | ((uint16_t)acc_foc_data[3] << 8)));
foc_off_x = BMA5_SET_BITS_POS_0(reg_data, BMA580_ACC_FOC_OFF_X, acc_foc->foc_off_x);
/* Settings 2 */
reg_data = (uint16_t)(((uint16_t)acc_foc_data[4] | ((uint16_t)acc_foc_data[5] << 8)));
foc_off_y = BMA5_SET_BITS_POS_0(reg_data, BMA580_ACC_FOC_OFF_Y, acc_foc->foc_off_y);
/* Settings 3 */
reg_data = (uint16_t)(((uint16_t)acc_foc_data[6] | ((uint16_t)acc_foc_data[7] << 8)));
foc_off_z = BMA5_SET_BITS_POS_0(reg_data, BMA580_ACC_FOC_OFF_Z, acc_foc->foc_off_z);
/* Settings 4 */
reg_data = (uint16_t)(((uint16_t)acc_foc_data[8] | ((uint16_t)acc_foc_data[9] << 8)));
foc_apply_corr = (uint8_t)BMA5_SET_BITS_POS_0(reg_data,
BMA580_ACC_FOC_APPLY_CORR,
acc_foc->foc_apply_corr);
foc_filter_coeff = (uint8_t)BMA5_SET_BITS(reg_data,
BMA580_ACC_FOC_FILTER_COEFF,
acc_foc->foc_filter_coeff);
foc_axis_1g = (uint8_t)BMA5_SET_BITS(reg_data, BMA580_ACC_FOC_AXIS_1G, acc_foc->foc_axis_1g);
acc_foc_data[0] = (uint8_t)(foc_off_x & 0x00FF);
acc_foc_data[1] = (uint8_t)((foc_off_x & 0xFF00) >> 8);
acc_foc_data[2] = (uint8_t)(foc_off_y & 0x00FF);
acc_foc_data[3] = (uint8_t)((foc_off_y & 0xFF00) >> 8);
acc_foc_data[4] = (uint8_t)(foc_off_z & 0x00FF);
acc_foc_data[5] = (uint8_t)((foc_off_z & 0xFF00) >> 8);
acc_foc_data[6] = (uint8_t)(foc_apply_corr | foc_filter_coeff | foc_axis_1g);
/* Set the configuration from the feature engine register */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_TX, acc_foc_data, 8, dev);
}
}
}
return result;
}
/*!
* @brief This API gets accel foc configuration
*/
int8_t bma580_get_accel_foc_config(struct bma580_accel_foc_config *acc_foc, struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
/* Variable to store base address of accel foc */
uint8_t data = BMA580_BASE_ADDR_ACC_FOC;
/* Array to store accel foc config data */
uint8_t acc_foc_data[10] = { 0 };
/* Variable to define array offset */
uint8_t idx = 0;
/* Variable to define LSB */
uint16_t lsb;
/* Variable to define MSB */
uint16_t msb;
/* Variable to define a word */
uint16_t lsb_msb;
if (acc_foc == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
/* Set the accel foc base address to feature engine transmission address to start DMA transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
if (result == BMA5_OK)
{
/* Get the configuration from the feature engine register */
result = bma5_get_regs(BMA5_REG_FEATURE_DATA_TX, acc_foc_data, 10, dev);
if (result == BMA5_OK)
{
/* First two bytes are dummy bytes */
idx = 2;
/* Settings 1 */
/* Get word to calculate foc_off_x */
lsb = (uint16_t) acc_foc_data[idx++];
msb = ((uint16_t) acc_foc_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
acc_foc->foc_off_x = lsb_msb & BMA580_ACC_FOC_OFF_X_MSK;
/* Settings 2 */
/* Get word to calculate foc_off_y */
lsb = (uint16_t) acc_foc_data[idx++];
msb = ((uint16_t) acc_foc_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
acc_foc->foc_off_y = lsb_msb & BMA580_ACC_FOC_OFF_Y_MSK;
/* Settings 3 */
/* Get word to calculate foc_off_z */
lsb = (uint16_t) acc_foc_data[idx++];
msb = ((uint16_t) acc_foc_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
acc_foc->foc_off_z = lsb_msb & BMA580_ACC_FOC_OFF_Z_MSK;
/* Settings 4 */
/* Get word to calculate foc_apply_corr, foc_filter_coeff and foc_axis_1g from same word */
lsb = (uint16_t) acc_foc_data[idx++];
msb = ((uint16_t) acc_foc_data[idx++] << 8);
lsb_msb = (uint16_t)(lsb | msb);
acc_foc->foc_apply_corr = lsb_msb & BMA580_ACC_FOC_APPLY_CORR_MSK;
acc_foc->foc_filter_coeff = (lsb_msb & BMA580_ACC_FOC_FILTER_COEFF_MSK) >>
BMA580_ACC_FOC_FILTER_COEFF_POS;
acc_foc->foc_axis_1g = (lsb_msb & BMA580_ACC_FOC_AXIS_1G_MSK) >> BMA580_ACC_FOC_AXIS_1G_POS;
}
}
}
return result;
}
int8_t bma580_set_tap_config(const struct bma580_tap_config *tap_config, struct bma5_dev *dev)
{
/* Variable to define error */
int8_t result = BMA5_OK;
/* Variable to store base address of tap config */
uint8_t data = BMA580_BASE_ADDR_TAP_DETECTOR;
/* Array to store tap config data */
uint8_t tap_conf_data[8] = { 0 };
/* variables to store the data */
uint16_t axis_sel, wait_for_timeout, max_peaks_for_tap, mode, s_tap_en, d_tap_en, t_tap_en;
uint16_t max_gesture_dur, max_dur_between_peaks, tap_shock_settling_dur, min_quite_dur_between_taps;
uint16_t tap_peak_thres_1, tap_peak_thres_2, quite_time_after_gesture;
if (tap_config == NULL)
{
result = BMA5_E_NULL_PTR;
}
else
{
/* Set the tap config base address to feature engine transmission address to start DMA transaction */
result = bma5_set_regs(BMA5_REG_FEATURE_DATA_ADDR, &data, 1, dev);
if (result == BMA5_OK)
{
/* Get the configuration from the feature engine register */
result = bma5_get_regs(BMA5_REG_FEATURE_DATA_TX, tap_conf_data, 8, dev);