-
Notifications
You must be signed in to change notification settings - Fork 39
/
bma2x2.c
8324 lines (8253 loc) · 237 KB
/
bma2x2.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) 2011 - 2014 Bosch Sensortec GmbH
*
* bma2x2.c
* Date: 2014/12/12
* Revision: 2.0.3 $
*
* Usage: Sensor Driver for BMA2x2 sensor
*
****************************************************************************
* License:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 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.
*
* Neither the name of the copyright holder nor the names of the
* 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 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
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
**************************************************************************/
/*! file <BMA2x2 >
brief <Sensor driver for BMA2x2> */
#include "bma2x2.h"
/*! user defined code to be added here ... */
static struct bma2x2_t *p_bma2x2;
/*! Based on Bit resolution v_value_u8 should be modified */
u8 V_BMA2x2RESOLUTION_U8 = BMA2x2_14_RESOLUTION;
/*!
* @brief
* This API reads the data from
* the given register continuously
*
*
* @param v_addr_u8 -> Address of the register
* @param v_data_u8 -> The data from the register
* @param v_len_u32 -> no of bytes to read
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_burst_read(u8 v_addr_u8,
u8 *v_data_u8, u32 v_len_u32)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Read the data from the register*/
com_rslt = p_bma2x2->BMA2x2_BURST_READ_FUNC
(p_bma2x2->dev_addr, v_addr_u8, v_data_u8, v_len_u32);
}
return com_rslt;
}
/*!
* @brief
* This function is used for initialize
* bus read and bus write functions
* assign the chip id and device address
* chip id is read in the register 0x00 bit from 0 to 7
*
* @param bma2x2 : structure pointer
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
* @note
* While changing the parameter of the bma2x2_t
* consider the following point:
* Changing the reference value of the parameter
* will changes the local copy or local reference
* make sure your changes will not
* affect the reference value of the parameter
* (Better case don't change the reference value of the parameter)
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_init(struct bma2x2_t *bma2x2)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = C_BMA2x2_ZERO_U8X;
u8 v_config_data_u8 = C_BMA2x2_ZERO_U8X;
/* assign bma2x2 ptr */
p_bma2x2 = bma2x2;
/* read Chip Id */
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_CHIP_ID__REG, &v_data_u8, C_BMA2x2_ONE_U8X);
p_bma2x2->chip_id = v_data_u8; /* get bit slice */
/* read the fifo config register and update
the value to the fifo_config*/
com_rslt += bma2x2_read_reg(BMA2x2_FIFO_MODE_REG,
&v_config_data_u8, C_BMA2x2_ONE_U8X);
p_bma2x2->fifo_config = v_config_data_u8;
return com_rslt;
}
/*!
* @brief
* This API gives data to the given register and
* the data is written in the corresponding register address
*
*
* @param v_adr_u8 -> Address of the register
* @param v_data_u8 -> The data to the register
* @param v_len_u8 -> no of bytes to read
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_write_reg(u8 v_adr_u8,
u8 *v_data_u8, u8 v_len_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Write the data to the register*/
com_rslt = p_bma2x2->BMA2x2_BUS_WRITE_FUNC
(p_bma2x2->dev_addr, v_adr_u8, v_data_u8, v_len_u8);
}
return com_rslt;
}
/*!
* @brief This API reads the data from
* the given register address
*
*
* @param v_adr_u8 -> Address of the register
* @param v_data_u8 -> The data from the register
* @param v_len_u8 -> no of bytes to read
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_read_reg(u8 v_adr_u8,
u8 *v_data_u8, u8 v_len_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/*Read the data from the register*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr, v_adr_u8, v_data_u8, v_len_u8);
}
return com_rslt;
}
/*!
* @brief
* This API reads acceleration data X values
* from location 02h and 03h
*
*
* @param v_accel_x_s16 : pointer holding the data of accel X
* value | resolution
* ----------------- | --------------
* 0 | BMA2x2_12_RESOLUTION
* 1 | BMA2x2_10_RESOLUTION
* 2 | BMA2x2_14_RESOLUTION
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_read_accel_x(s16 *v_accel_x_s16)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the accel x value
v_data_u8[0] - x->LSB
v_data_u8[MSB_ONE] - x->MSB
*/
u8 v_data_u8[ARRAY_SIZE_TWO] = {
C_BMA2x2_ZERO_U8X, C_BMA2x2_ZERO_U8X};
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
switch (V_BMA2x2RESOLUTION_U8) {
/* This case used for the resolution bit 12*/
case BMA2x2_12_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_ACCEL_X12_LSB__REG, v_data_u8, C_BMA2x2_TWO_U8X);
*v_accel_x_s16 = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & RESOLUTION_12_MASK));
*v_accel_x_s16 = *v_accel_x_s16 >> C_BMA2x2_FOUR_U8X;
break;
/* This case used for the resolution bit 10*/
case BMA2x2_10_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_ACCEL_X10_LSB__REG, v_data_u8, C_BMA2x2_TWO_U8X);
*v_accel_x_s16 = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & RESOLUTION_10_MASK));
*v_accel_x_s16 = *v_accel_x_s16 >> C_BMA2x2_SIX_U8X;
break;
/* This case used for the resolution bit 14*/
case BMA2x2_14_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_ACCEL_X14_LSB__REG, v_data_u8, C_BMA2x2_TWO_U8X);
*v_accel_x_s16 = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & RESOLUTION_14_MASK));
*v_accel_x_s16 = *v_accel_x_s16 >> C_BMA2x2_TWO_U8X;
break;
default:
break;
}
}
return com_rslt;
}
/*!
* @brief
* This API reads acceleration data X values
* from location 02h and 03h bit resolution support 8bit
*
*
* @param v_accel_x_s8 : pointer holding the data of accel X
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_read_accel_elight_resolution_x(
s8 *v_accel_x_s8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 data = C_BMA2x2_ZERO_U8X;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Read the sensor X data*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_X_AXIS_MSB_REG, &data, C_BMA2x2_ONE_U8X);
*v_accel_x_s8 = BMA2x2_GET_BITSLICE(data,
BMA2x2_ACCEL_X_MSB);
}
return com_rslt;
}
/*!
* @brief
* This API reads acceleration data Y values
* from location 04h and 05h
*
* @param v_accel_y_s16 : pointer holding the data of accel Y
* value | resolution
* ----------------- | --------------
* 0 | BMA2x2_12_RESOLUTION
* 1 | BMA2x2_10_RESOLUTION
* 2 | BMA2x2_14_RESOLUTION
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_read_accel_y(s16 *v_accel_y_s16)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the accel y value
v_data_u8[LSB_ZERO] - y->LSB
v_data_u8[MSB_ONE] - y->MSB
*/
u8 v_data_u8[ARRAY_SIZE_TWO] = {C_BMA2x2_ZERO_U8X, C_BMA2x2_ZERO_U8X};
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
switch (V_BMA2x2RESOLUTION_U8) {
/* This case used for the resolution bit 12*/
case BMA2x2_12_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_ACCEL_Y12_LSB__REG, v_data_u8, C_BMA2x2_TWO_U8X);
*v_accel_y_s16 = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & BMA2x2_12_BIT_SHIFT));
*v_accel_y_s16 = *v_accel_y_s16 >> C_BMA2x2_FOUR_U8X;
break;
/* This case used for the resolution bit 10*/
case BMA2x2_10_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_ACCEL_Y10_LSB__REG, v_data_u8, C_BMA2x2_TWO_U8X);
*v_accel_y_s16 = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & BMA2x2_10_BIT_SHIFT));
*v_accel_y_s16 = *v_accel_y_s16 >> C_BMA2x2_SIX_U8X;
break;
/* This case used for the resolution bit 14*/
case BMA2x2_14_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_ACCEL_Y14_LSB__REG, v_data_u8, C_BMA2x2_TWO_U8X);
*v_accel_y_s16 = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & BMA2x2_14_BIT_SHIFT));
*v_accel_y_s16 = *v_accel_y_s16 >> C_BMA2x2_TWO_U8X;
break;
default:
break;
}
}
return com_rslt;
}
/*!
* @brief This API reads acceleration data Y values of
* 8bit resolution from location 05h
*
*
*
*
* @param v_accel_y_s8 The data of y
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_read_accel_elight_resolution_y(
s8 *v_accel_y_s8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 data = C_BMA2x2_ZERO_U8X;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_Y_AXIS_MSB_REG, &data, C_BMA2x2_ONE_U8X);
*v_accel_y_s8 = BMA2x2_GET_BITSLICE(data,
BMA2x2_ACCEL_Y_MSB);
}
return com_rslt;
}
/*!
* @brief This API reads acceleration data Z values
* from location 06h and 07h
*
*
* @param v_accel_z_s16 : pointer holding the data of accel Z
* value | resolution
* ----------------- | --------------
* 0 | BMA2x2_12_RESOLUTION
* 1 | BMA2x2_10_RESOLUTION
* 2 | BMA2x2_14_RESOLUTION
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_read_accel_z(s16 *v_accel_z_s16)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the accel z value
v_data_u8[LSB_ZERO] - z->LSB
v_data_u8[MSB_ONE] - z->MSB
*/
u8 v_data_u8[ARRAY_SIZE_TWO] = {C_BMA2x2_ZERO_U8X, C_BMA2x2_ZERO_U8X};
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
switch (V_BMA2x2RESOLUTION_U8) {
case BMA2x2_12_RESOLUTION:
/* This case used for the resolution bit 12*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_ACCEL_Z12_LSB__REG, v_data_u8, C_BMA2x2_TWO_U8X);
*v_accel_z_s16 = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & BMA2x2_12_BIT_SHIFT));
*v_accel_z_s16 = *v_accel_z_s16 >> C_BMA2x2_FOUR_U8X;
break;
/* This case used for the resolution bit 10*/
case BMA2x2_10_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_ACCEL_Z10_LSB__REG, v_data_u8, C_BMA2x2_TWO_U8X);
*v_accel_z_s16 = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & BMA2x2_10_BIT_SHIFT));
*v_accel_z_s16 = *v_accel_z_s16 >> C_BMA2x2_SIX_U8X;
break;
/* This case used for the resolution bit 14*/
case BMA2x2_14_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_ACCEL_Z14_LSB__REG, v_data_u8, C_BMA2x2_TWO_U8X);
*v_accel_z_s16 = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & BMA2x2_14_BIT_SHIFT));
*v_accel_z_s16 = *v_accel_z_s16 >> C_BMA2x2_TWO_U8X;
break;
default:
break;
}
}
return com_rslt;
}
/*!
* @brief
* This API reads acceleration data Z values of
* 8bit resolution from location 07h
*
*
*
*
* \@aram v_accel_z_s8 : the data of z
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_read_accel_elight_resolution_z(
s8 *v_accel_z_s8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 data = C_BMA2x2_ZERO_U8X;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_Z_AXIS_MSB_REG, &data, C_BMA2x2_ONE_U8X);
*v_accel_z_s8 = BMA2x2_GET_BITSLICE(data,
BMA2x2_ACCEL_Z_MSB);
}
return com_rslt;
}
/*!
* @brief This API reads acceleration data X,Y,Z values
* from location 02h to 07h
*
* @param accel : pointer holding the data of accel
* value | resolution
* ----------------- | --------------
* 0 | BMA2x2_12_RESOLUTION
* 1 | BMA2x2_10_RESOLUTION
* 2 | BMA2x2_14_RESOLUTION
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_read_accel_xyz(
struct bma2x2_accel_data *accel)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the accel xyz value
v_data_u8[LSB_ZERO] - x->LSB
v_data_u8[MSB_ONE] - x->MSB
v_data_u8[2] - y->MSB
v_data_u8[3] - y->MSB
v_data_u8[4] - z->MSB
v_data_u8[5] - z->MSB
*/
u8 v_data_u8[ARRAY_SIZE_SIX] = {
C_BMA2x2_ZERO_U8X, C_BMA2x2_ZERO_U8X,
C_BMA2x2_ZERO_U8X, C_BMA2x2_ZERO_U8X,
C_BMA2x2_ZERO_U8X, C_BMA2x2_ZERO_U8X};
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
switch (V_BMA2x2RESOLUTION_U8) {
/* This case used for the resolution bit 12*/
case BMA2x2_12_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr, BMA2x2_ACCEL_X12_LSB__REG,
v_data_u8, C_BMA2x2_SIX_U8X);
/* read the x v_data_u8*/
accel->x = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & BMA2x2_12_BIT_SHIFT));
accel->x = accel->x >> C_BMA2x2_FOUR_U8X;
/* read the y v_data_u8*/
accel->y = (s16)((((s32)((s8)v_data_u8[MSB_THREE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_TWO] & BMA2x2_12_BIT_SHIFT));
accel->y = accel->y >> C_BMA2x2_FOUR_U8X;
/* read the z v_data_u8*/
accel->z = (s16)((((s32)((s8)v_data_u8[MSB_FIVE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_FOUR] & BMA2x2_12_BIT_SHIFT));
accel->z = accel->z >> C_BMA2x2_FOUR_U8X;
break;
case BMA2x2_10_RESOLUTION:
/* This case used for the resolution bit 10*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr, BMA2x2_ACCEL_X10_LSB__REG,
v_data_u8, C_BMA2x2_SIX_U8X);
/* read the x v_data_u8*/
accel->x = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & BMA2x2_10_BIT_SHIFT));
accel->x = accel->x >> C_BMA2x2_SIX_U8X;
/* read the y v_data_u8*/
accel->y = (s16)((((s32)((s8)v_data_u8[MSB_THREE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_TWO] & BMA2x2_10_BIT_SHIFT));
accel->y = accel->y >> C_BMA2x2_SIX_U8X;
/* read the z v_data_u8*/
accel->z = (s16)((((s32)((s8)v_data_u8[MSB_FIVE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_FOUR] & BMA2x2_10_BIT_SHIFT));
accel->z = accel->z >> C_BMA2x2_SIX_U8X;
break;
/* This case used for the resolution bit 14*/
case BMA2x2_14_RESOLUTION:
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr, BMA2x2_ACCEL_X14_LSB__REG,
v_data_u8, C_BMA2x2_SIX_U8X);
/* read the x v_data_u8*/
accel->x = (s16)((((s32)((s8)v_data_u8[MSB_ONE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_ZERO] & BMA2x2_14_BIT_SHIFT));
accel->x = accel->x >> C_BMA2x2_TWO_U8X;
/* read the y v_data_u8*/
accel->y = (s16)((((s32)((s8)v_data_u8[MSB_THREE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_TWO] & BMA2x2_14_BIT_SHIFT));
accel->y = accel->y >> C_BMA2x2_TWO_U8X;
/* read the z v_data_u8*/
accel->z = (s16)((((s32)((s8)v_data_u8[MSB_FIVE]))
<< C_BMA2x2_EIGHT_U8X) |
(v_data_u8[LSB_FOUR] & BMA2x2_14_BIT_SHIFT));
accel->z = accel->z >> C_BMA2x2_TWO_U8X;
break;
default:
break;
}
}
return com_rslt;
}
/*!
* @brief This API reads acceleration of 8 bit resolution
* data of X,Y,Z values
* from location 03h , 05h and 07h
*
*
*
*
* @param accel : pointer holding the data of accel
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_read_accel_eight_resolution_xyz(
struct bma2x2_accel_eight_resolution *accel)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = C_BMA2x2_ZERO_U8X;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_X_AXIS_MSB_REG, &v_data_u8, C_BMA2x2_ONE_U8X);
accel->x = BMA2x2_GET_BITSLICE(v_data_u8,
BMA2x2_ACCEL_X_MSB);
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_Y_AXIS_MSB_REG, &v_data_u8, C_BMA2x2_ONE_U8X);
accel->y = BMA2x2_GET_BITSLICE(v_data_u8,
BMA2x2_ACCEL_Y_MSB);
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_Z_AXIS_MSB_REG, &v_data_u8, C_BMA2x2_ONE_U8X);
accel->z = BMA2x2_GET_BITSLICE(v_data_u8,
BMA2x2_ACCEL_Z_MSB);
}
return com_rslt;
}
/*!
* @brief This API read tap-sign, tap-first-xyz
* slope-sign, slope-first-xyz status register byte
* from location 0Bh
*
* @param v_stat_tap_u8 : The status of tap and slope
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_get_intr_tap_stat(
u8 *v_stat_tap_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Read the interrupt status register 0x0B*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr, BMA2x2_STAT_TAP_SLOPE_REG,
v_stat_tap_u8, C_BMA2x2_ONE_U8X);
}
return com_rslt;
}
/*!
* @brief This API read orient, high-sign and high-first-xyz
* status register byte from location 0Ch
*
*
* @param v_stat_orient_u8 : The status of orient and high
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_get_intr_orient_stat(
u8 *v_stat_orient_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Read the interrupt status register 0x0C*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr, BMA2x2_STAT_ORIENT_HIGH_REG,
v_stat_orient_u8, C_BMA2x2_ONE_U8X);
}
return com_rslt;
}
/*!
* @brief This API reads fifo overrun and fifo frame counter
* status register byte from location 0Eh
*
* @param v_stat_fifo_u8 : The status of fifo overrun and frame counter
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_get_fifo_stat(
u8 *v_stat_fifo_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Read the interrupt status register 0x0E*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC(
p_bma2x2->dev_addr,
BMA2x2_STAT_FIFO_REG,
v_stat_fifo_u8, C_BMA2x2_ONE_U8X);
}
return com_rslt;
}
/*!
* @brief This API read fifo frame count
* from location 0Eh bit position 0 to 6
*
*
* @param v_frame_count_u8 : The status of fifo frame count
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_get_fifo_frame_count(
u8 *v_frame_count_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = C_BMA2x2_ZERO_U8X;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Read the FIFO frame count*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC(
p_bma2x2->dev_addr,
BMA2x2_FIFO_FRAME_COUNT_STAT__REG,
&v_data_u8, C_BMA2x2_ONE_U8X);
*v_frame_count_u8 = BMA2x2_GET_BITSLICE(v_data_u8,
BMA2x2_FIFO_FRAME_COUNT_STAT);
}
return com_rslt;
}
/*!
* @brief This API read fifo overrun
* from location 0Eh bit position 7
*
*
* @param v_fifo_overrun_u8 : The status of fifo overrun
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_get_fifo_overrun(
u8 *v_fifo_overrun_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = C_BMA2x2_ZERO_U8X;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Read the status of fifo over run*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC(
p_bma2x2->dev_addr,
BMA2x2_FIFO_OVERRUN_STAT__REG,
&v_data_u8, C_BMA2x2_ONE_U8X);
*v_fifo_overrun_u8 = BMA2x2_GET_BITSLICE(v_data_u8,
BMA2x2_FIFO_OVERRUN_STAT);
}
return com_rslt;
}
/*!
* @brief This API read interrupt status of flat, orient, single tap,
* double tap, slow no motion, slope, highg and lowg from location 09h
*
*
*
* @param v_intr_stat_u8 : The value of interrupt status
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_get_intr_stat(
u8 *v_intr_stat_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Read the interrupt status register 0x09*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC(
p_bma2x2->dev_addr,
BMA2x2_STAT1_REG, v_intr_stat_u8, C_BMA2x2_FOUR_U8X);
}
return com_rslt;
}
/*!
* @brief This API is used to get the ranges(g values) of the sensor
* in the register 0x0F bit from 0 to 3
*
*
* @param v_range_u8 : The value of range
* v_range_u8 | result
* ----------------- | --------------
* 0x00 | BMA2x2_RANGE_2G
* 0x01 | BMA2x2_RANGE_4G
* 0x02 | BMA2x2_RANGE_8G
* 0x03 | BMA2x2_RANGE_16G
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_get_range(u8 *v_range_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = C_BMA2x2_ZERO_U8X;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
/* Read the range register 0x0F*/
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC(p_bma2x2->dev_addr,
BMA2x2_RANGE_SELECT__REG, &v_data_u8, C_BMA2x2_ONE_U8X);
v_data_u8 = BMA2x2_GET_BITSLICE(v_data_u8, BMA2x2_RANGE_SELECT);
*v_range_u8 = v_data_u8;
}
return com_rslt;
}
/*!
* @brief This API is used to set the ranges(g values) of the sensor
* in the register 0x0F bit from 0 to 3
*
*
* @param v_range_u8 : The value of range
* v_range_u8 | result
* ----------------- | --------------
* 0x00 | BMA2x2_RANGE_2G
* 0x01 | BMA2x2_RANGE_4G
* 0x02 | BMA2x2_RANGE_8G
* 0x03 | BMA2x2_RANGE_16G
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BMA2x2_RETURN_FUNCTION_TYPE bma2x2_set_range(u8 v_range_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = C_BMA2x2_ZERO_U8X;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
return E_BMA2x2_NULL_PTR;
} else {
if ((v_range_u8 == C_BMA2x2_THREE_U8X) ||
(v_range_u8 == C_BMA2x2_FIVE_U8X) ||
(v_range_u8 == C_BMA2x2_EIGHT_U8X) ||
(v_range_u8 == C_BMA2x2_TWELVE_U8X)) {
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_RANGE_SELECT_REG, &v_data_u8, C_BMA2x2_ONE_U8X);
switch (v_range_u8) {
case BMA2x2_RANGE_2G:
v_data_u8 = BMA2x2_SET_BITSLICE(v_data_u8,
BMA2x2_RANGE_SELECT, C_BMA2x2_THREE_U8X);
break;
case BMA2x2_RANGE_4G:
v_data_u8 = BMA2x2_SET_BITSLICE(v_data_u8,
BMA2x2_RANGE_SELECT, C_BMA2x2_FIVE_U8X);
break;
case BMA2x2_RANGE_8G:
v_data_u8 = BMA2x2_SET_BITSLICE(v_data_u8,
BMA2x2_RANGE_SELECT, C_BMA2x2_EIGHT_U8X);
break;
case BMA2x2_RANGE_16G:
v_data_u8 = BMA2x2_SET_BITSLICE(v_data_u8,
BMA2x2_RANGE_SELECT, C_BMA2x2_TWELVE_U8X);
break;
default:
break;
}
/* Write the range register 0x0F*/
com_rslt += p_bma2x2->BMA2x2_BUS_WRITE_FUNC
(p_bma2x2->dev_addr,
BMA2x2_RANGE_SELECT_REG, &v_data_u8, C_BMA2x2_ONE_U8X);
} else {
com_rslt = E_OUT_OF_RANGE;
}
}
return com_rslt;
}
/*!
* @brief This API is used to get the bandwidth of the sensor in the register
* 0x10 bit from 0 to 4