forked from geekpm-emouse/BMA2x2_driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bma2x2.c
8896 lines (8659 loc) · 252 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) 2015 - 2016 Bosch Sensortec GmbH
*
* bma2x2.c
* Date: 2016/11/14
* Revision: 2.0.7 $
*
* Usage: Sensor Driver for BMA2x2 sensor
*
****************************************************************************
* Disclaimer
*
* Common:
* Bosch Sensortec products are developed for the consumer goods industry.
* They may only be used within the parameters of the respective valid
* product data sheet. Bosch Sensortec products are provided with the
* express understanding that there is no warranty of fitness for a
* particular purpose.They are not fit for use in life-sustaining,
* safety or security sensitive systems or any system or device
* that may lead to bodily harm or property damage if the system
* or device malfunctions. In addition,Bosch Sensortec products are
* not fit for use in products which interact with motor vehicle systems.
* The resale and or use of products are at the purchasers own risk and
* his own responsibility. The examination of fitness for the intended use
* is the sole responsibility of the Purchaser.
*
* The purchaser shall indemnify Bosch Sensortec from all third party
* claims, including any claims for incidental, or consequential damages,
* arising from any product use not covered by the parameters of
* the respective valid product data sheet or not approved by
* Bosch Sensortec and reimburse Bosch Sensortec for all costs in
* connection with such claims.
*
* The purchaser must monitor the market for the purchased products,
* particularly with regard to product safety and inform Bosch Sensortec
* without delay of all security relevant incidents.
*
* Engineering Samples are marked with an asterisk (*) or (e).
* Samples may vary from the valid technical specifications of the product
* series. They are therefore not intended or fit for resale to third
* parties or for use in end products. Their sole purpose is internal
* client testing. The testing of an engineering sample may in no way
* replace the testing of a product series. Bosch Sensortec assumes
* no liability for the use of engineering samples.
* By accepting the engineering samples, the Purchaser agrees to indemnify
* Bosch Sensortec from all claims arising from the use of engineering
* samples.
*
* Special:
* This software module (hereinafter called "Software") and any information
* on application-sheets (hereinafter called "Information") is provided
* free of charge for the sole purpose to support your application work.
* The Software and Information is subject to the following
* terms and conditions:
*
* The Software is specifically designed for the exclusive use for
* Bosch Sensortec products by personnel who have special experience
* and training. Do not use this Software if you do not have the
* proper experience or training.
*
* This Software package is provided `` as is `` and without any expressed
* or implied warranties,including without limitation, the implied warranties
* of merchantability and fitness for a particular purpose.
*
* Bosch Sensortec and their representatives and agents deny any liability
* for the functional impairment
* of this Software in terms of fitness, performance and safety.
* Bosch Sensortec and their representatives and agents shall not be liable
* for any direct or indirect damages or injury, except as
* otherwise stipulated in mandatory applicable law.
*
* The Information provided is believed to be accurate and reliable.
* Bosch Sensortec 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 Bosch. Specifications mentioned in the Information are
* subject to change without notice.
**************************************************************************/
/*! 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 value_u8 should be modified */
u8 V_BMA2x2RESOLUTION_U8 = BMA2x2_14_RESOLUTION;
/****************************************************************************/
/*! Static Function Declarations
*****************************************************************************/
/*!
* @brief This API computes the number of bytes of accel FIFO data
* which is to be parsed.
*
* @param[out] data_index : The start index for parsing data
* @param[out] data_read_length : No of bytes to be parsed from FIFO buffer
* @param[in] accel_frame_count : Number of accelerometer frames to be read
* @param[in] fifo_data_select : Denoting enabled axes data stored in FIFO
* @param[in] fifo_conf : FIFO configuration structure
*
*/
static void get_accel_len_to_parse(u8 *data_index, u8 *data_read_length,
u8 accel_frame_count, u8 fifo_data_select,
struct fifo_configuration *fifo_conf);
/*!
* @brief This API is used to parse the accelerometer frame from the
* user defined FIFO data buffer mapped to the structure fifo_conf and store
* it in the union fifo_frame
*
* @note It update the data_index value which is used to store the index of
* the current data byte which is parsed.
*
* @note The parsed accel frames stored in the union fifo_conf contains data
* in accordance with the enabled data axes to be stored in FIFO
* (XYZ axes or individual axis)
*
* @param[in,out] accel_frame : Instance of union fifo_frame
* @param[in,out] data_index : Index value of noumber of bytes parsed
* @param[in,out] accel_index : Index value of accelerometer frame parsed
* @param[in] fifo_data_select : Denoting enabled axes data stored in FIFO
* @param[in] fifo_conf : FIFO configuration structure
*
*/
static void unpack_accel_frame(union fifo_frame *accel_frame, u8 *data_index,
u8 *accel_index, u8 fifo_data_select,
struct fifo_configuration *fifo_conf);
/*!
* @brief This API is used to parse the accelerometer data and
* store it in the union fifo_frame
* It also updates the data_index value which stores the index of
* the current data byte which is parsed
*
* @param[in,out] accel_frame : Instance of union fifo_frame
* @param[in,out] data_index : Index value of noumber of bytes parsed
* @param[in] fifo_conf : FIFO configuration structure
*
*/
static void unpack_accel_xyz(union fifo_frame *accel_frame, u8 *data_index,
struct fifo_configuration *fifo_conf);
/*!
* @brief
* This API reads the data from
* the given register continuously
*
*
* @param addr_u8 -> Address of the register
* @param data_u8 -> The data from the register
* @param 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 addr_u8,
u8 *data_u8, u32 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, addr_u8, data_u8, 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 data_u8 = BMA2x2_INIT_VALUE;
u8 config_data_u8 = BMA2x2_INIT_VALUE;
/* assign bma2x2 ptr */
p_bma2x2 = bma2x2;
if (p_bma2x2 == BMA2x2_NULL) {
/* Check the struct p_bma2x2 is empty */
com_rslt = E_BMA2x2_NULL_PTR;
} else {
/* read Chip Id */
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_CHIP_ID_REG, &data_u8, BMA2x2_GEN_READ_WRITE_LENGTH);
p_bma2x2->chip_id = 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,
&config_data_u8, BMA2x2_GEN_READ_WRITE_LENGTH);
p_bma2x2->fifo_config = 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 adr_u8 -> Address of the register
* @param data_u8 -> The data to the register
* @param 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 adr_u8,
u8 *data_u8, u8 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, adr_u8, data_u8, len_u8);
if (p_bma2x2->power_mode_u8 != BMA2x2_MODE_NORMAL) {
/*A minimum interface idle time delay
of atleast 450us is required as per the data sheet.*/
p_bma2x2->delay_msec(BMA2x2_INTERFACE_IDLE_TIME_DELAY);
}
}
return com_rslt;
}
/*!
* @brief This API reads the data from
* the given register address
*
*
* @param adr_u8 -> Address of the register
* @param data_u8 -> The data from the register
* @param 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 adr_u8,
u8 *data_u8, u8 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, adr_u8, data_u8, len_u8);
}
return com_rslt;
}
/*!
* @brief
* This API reads acceleration data X values
* from location 02h and 03h
*
*
* @param 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 *accel_x_s16)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the accel x value
data_u8[0] - x->LSB
data_u8[1] - x->MSB
*/
u8 data_u8[BMA2x2_ACCEL_DATA_SIZE] = {
BMA2x2_INIT_VALUE, BMA2x2_INIT_VALUE};
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, data_u8,
BMA2x2_LSB_MSB_READ_LENGTH);
*accel_x_s16 = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_ACCEL_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_ACCEL_LSB] &
BMA2x2_RESOLUTION_12_MASK));
*accel_x_s16 = *accel_x_s16 >>
BMA2x2_SHIFT_FOUR_BITS;
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, data_u8,
BMA2x2_LSB_MSB_READ_LENGTH);
*accel_x_s16 = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_ACCEL_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_ACCEL_LSB] &
BMA2x2_RESOLUTION_10_MASK));
*accel_x_s16 = *accel_x_s16 >>
BMA2x2_SHIFT_SIX_BITS;
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, data_u8,
BMA2x2_LSB_MSB_READ_LENGTH);
*accel_x_s16 = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_ACCEL_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_ACCEL_LSB] &
BMA2x2_RESOLUTION_14_MASK));
*accel_x_s16 = *accel_x_s16 >>
BMA2x2_SHIFT_TWO_BITS;
break;
default:
break;
}
}
return com_rslt;
}
/*!
* @brief
* This API reads acceleration data X values
* from location 02h and 03h bit resolution support 8bit
*
*
* @param 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_eight_resolution_x(
s8 *accel_x_s8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 data = BMA2x2_INIT_VALUE;
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_ADDR, &data,
BMA2x2_GEN_READ_WRITE_LENGTH);
*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 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 *accel_y_s16)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the accel y value
data_u8[0] - y->LSB
data_u8[1] - y->MSB
*/
u8 data_u8[BMA2x2_ACCEL_DATA_SIZE] = {BMA2x2_INIT_VALUE,
BMA2x2_INIT_VALUE};
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, data_u8,
BMA2x2_LSB_MSB_READ_LENGTH);
*accel_y_s16 = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_ACCEL_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_ACCEL_LSB] &
BMA2x2_12_BIT_SHIFT));
*accel_y_s16 = *accel_y_s16 >>
BMA2x2_SHIFT_FOUR_BITS;
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, data_u8,
BMA2x2_LSB_MSB_READ_LENGTH);
*accel_y_s16 = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_ACCEL_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_ACCEL_LSB] &
BMA2x2_10_BIT_SHIFT));
*accel_y_s16 = *accel_y_s16 >>
BMA2x2_SHIFT_SIX_BITS;
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, data_u8,
BMA2x2_LSB_MSB_READ_LENGTH);
*accel_y_s16 = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_ACCEL_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_ACCEL_LSB] &
BMA2x2_14_BIT_SHIFT));
*accel_y_s16 = *accel_y_s16 >>
BMA2x2_SHIFT_TWO_BITS;
break;
default:
break;
}
}
return com_rslt;
}
/*!
* @brief This API reads acceleration data Y values of
* 8bit resolution from location 05h
*
*
*
*
* @param 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_eight_resolution_y(
s8 *accel_y_s8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 data = BMA2x2_INIT_VALUE;
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_ADDR, &data,
BMA2x2_GEN_READ_WRITE_LENGTH);
*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 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 *accel_z_s16)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the accel z value
data_u8[0] - z->LSB
data_u8[1] - z->MSB
*/
u8 data_u8[BMA2x2_ACCEL_DATA_SIZE] = {BMA2x2_INIT_VALUE,
BMA2x2_INIT_VALUE};
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, data_u8,
BMA2x2_LSB_MSB_READ_LENGTH);
*accel_z_s16 = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_ACCEL_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_ACCEL_LSB]
& BMA2x2_12_BIT_SHIFT));
*accel_z_s16 = *accel_z_s16 >>
BMA2x2_SHIFT_FOUR_BITS;
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, data_u8,
BMA2x2_LSB_MSB_READ_LENGTH);
*accel_z_s16 = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_ACCEL_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_ACCEL_LSB]
& BMA2x2_10_BIT_SHIFT));
*accel_z_s16 = *accel_z_s16 >>
BMA2x2_SHIFT_SIX_BITS;
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, data_u8,
BMA2x2_LSB_MSB_READ_LENGTH);
*accel_z_s16 = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_ACCEL_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_ACCEL_LSB]
& BMA2x2_14_BIT_SHIFT));
*accel_z_s16 = *accel_z_s16 >>
BMA2x2_SHIFT_TWO_BITS;
break;
default:
break;
}
}
return com_rslt;
}
/*!
* @brief
* This API reads acceleration data Z values of
* 8bit resolution from location 07h
*
*
*
*
* \@aram 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_eight_resolution_z(
s8 *accel_z_s8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 data = BMA2x2_INIT_VALUE;
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_ADDR, &data,
BMA2x2_GEN_READ_WRITE_LENGTH);
*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
data_u8[0] - x->LSB
data_u8[1] - x->MSB
data_u8[2] - y->MSB
data_u8[3] - y->MSB
data_u8[4] - z->MSB
data_u8[5] - z->MSB
*/
u8 data_u8[BMA2x2_ACCEL_XYZ_DATA_SIZE] = {
BMA2x2_INIT_VALUE, BMA2x2_INIT_VALUE,
BMA2x2_INIT_VALUE, BMA2x2_INIT_VALUE,
BMA2x2_INIT_VALUE, BMA2x2_INIT_VALUE};
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,
data_u8, BMA2x2_SHIFT_SIX_BITS);
/* read the x data_u8*/
accel->x = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_XYZ_X_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_XYZ_X_LSB] &
BMA2x2_12_BIT_SHIFT));
accel->x = accel->x >> BMA2x2_SHIFT_FOUR_BITS;
/* read the y data_u8*/
accel->y = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_XYZ_Y_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_XYZ_Y_LSB] &
BMA2x2_12_BIT_SHIFT));
accel->y = accel->y >> BMA2x2_SHIFT_FOUR_BITS;
/* read the z data_u8*/
accel->z = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_XYZ_Z_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_XYZ_Z_LSB] &
BMA2x2_12_BIT_SHIFT));
accel->z = accel->z >> BMA2x2_SHIFT_FOUR_BITS;
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,
data_u8, BMA2x2_SHIFT_SIX_BITS);
/* read the x data_u8*/
accel->x = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_XYZ_X_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_XYZ_X_LSB] &
BMA2x2_10_BIT_SHIFT));
accel->x = accel->x >> BMA2x2_SHIFT_SIX_BITS;
/* read the y data_u8*/
accel->y = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_XYZ_Y_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_XYZ_Y_LSB] &
BMA2x2_10_BIT_SHIFT));
accel->y = accel->y >> BMA2x2_SHIFT_SIX_BITS;
/* read the z data_u8*/
accel->z = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_XYZ_Z_MSB]))
<< BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_XYZ_Z_LSB]
& BMA2x2_10_BIT_SHIFT));
accel->z = accel->z >> BMA2x2_SHIFT_SIX_BITS;
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,
data_u8, BMA2x2_SHIFT_SIX_BITS);
/* read the x data_u8*/
accel->x = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_XYZ_X_MSB]))<<
BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_XYZ_X_LSB]
& BMA2x2_14_BIT_SHIFT));
accel->x = accel->x >> BMA2x2_SHIFT_TWO_BITS;
/* read the y data_u8*/
accel->y = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_XYZ_Y_MSB]))<<
BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_XYZ_Y_LSB]
& BMA2x2_14_BIT_SHIFT));
accel->y = accel->y >> BMA2x2_SHIFT_TWO_BITS;
/* read the z data_u8*/
accel->z = (s16)((((s32)((s8)
data_u8[BMA2x2_SENSOR_DATA_XYZ_Z_MSB]))<<
BMA2x2_SHIFT_EIGHT_BITS) |
(data_u8[BMA2x2_SENSOR_DATA_XYZ_Z_LSB]
& BMA2x2_14_BIT_SHIFT));
accel->z = accel->z >> BMA2x2_SHIFT_TWO_BITS;
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 data_u8 = BMA2x2_INIT_VALUE;
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_ADDR, &data_u8,
BMA2x2_GEN_READ_WRITE_LENGTH);
accel->x = BMA2x2_GET_BITSLICE(data_u8,
BMA2x2_ACCEL_X_MSB);
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_Y_AXIS_MSB_ADDR, &data_u8,
BMA2x2_GEN_READ_WRITE_LENGTH);
accel->y = BMA2x2_GET_BITSLICE(data_u8,
BMA2x2_ACCEL_Y_MSB);
com_rslt = p_bma2x2->BMA2x2_BUS_READ_FUNC
(p_bma2x2->dev_addr,
BMA2x2_Z_AXIS_MSB_ADDR, &data_u8,
BMA2x2_GEN_READ_WRITE_LENGTH);
accel->z = BMA2x2_GET_BITSLICE(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 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 *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_ADDR,
stat_tap_u8, BMA2x2_GEN_READ_WRITE_LENGTH);
}
return com_rslt;
}
/*!
* @brief This API read orient, high-sign and high-first-xyz
* status register byte from location 0Ch
*
*
* @param 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 *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_ADDR,
stat_orient_u8, BMA2x2_GEN_READ_WRITE_LENGTH);
}
return com_rslt;
}
/*!
* @brief This API reads fifo overrun and fifo frame counter
* status register byte from location 0Eh
*
* @param 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 *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_ADDR,
stat_fifo_u8, BMA2x2_GEN_READ_WRITE_LENGTH);
}
return com_rslt;
}
/*!
* @brief This API read fifo frame count
* from location 0Eh bit position 0 to 6
*
*
* @param 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 *frame_count_u8)
{
/* Variable used to return value of
communication routine*/
BMA2x2_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 data_u8 = BMA2x2_INIT_VALUE;
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,
&data_u8, BMA2x2_GEN_READ_WRITE_LENGTH);
*frame_count_u8 = BMA2x2_GET_BITSLICE(data_u8,
BMA2x2_FIFO_FRAME_COUNT_STAT);
}
return com_rslt;
}
/*!
* @brief This API read fifo overrun
* from location 0Eh bit position 7
*
*
* @param fifo_overrun_u8 : The status of fifo overrun
*
*
*