-
Notifications
You must be signed in to change notification settings - Fork 51
/
bmi08g.c
1385 lines (1167 loc) · 41.3 KB
/
bmi08g.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 bmi08g.c
* @date 2024-07-29
* @version v1.9.0
*
*/
/*! \file bmi08g.c
* \brief Sensor Driver for BMI08 family of sensors */
/****************************************************************************/
/**\name Header files
****************************************************************************/
#include "bmi08.h"
/****************************************************************************/
/**\name Local structures
****************************************************************************/
/****************************************************************************/
/*! Static Function Declarations
****************************************************************************/
/*!
* @brief This API is used to validate the device structure pointer for
* null conditions.
*
* @param[in] dev : Structure instance of bmi08x_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t null_ptr_check(const struct bmi08_dev *dev);
/*!
* @brief This API reads the data from the given register address of gyro sensor.
*
* @param[in] reg_addr : Register address from where the data to be read
* @param[out]reg_data : Pointer to data buffer to store the read data.
* @param[in] len : No. of bytes of data to be read.
* @param[in] dev : Structure instance of bmi08x_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t get_regs(uint8_t reg_addr, uint8_t *data, uint32_t len, struct bmi08_dev *dev);
/*!
* @brief This API writes the given data to the register address
* of gyro sensor.
*
* @param[in] reg_addr : Register address to where the data to be written.
* @param[in] reg_data : Pointer to data buffer which is to be written
* in the sensor.
* @param[in] len : No. of bytes of data to write.
* @param[in] dev : Structure instance of bmi08x_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_regs(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, struct bmi08_dev *dev);
/*!
* @brief This API sets the data ready interrupt for gyro sensor.
*
* @param[in] int_config : Structure instance of bmi08x_gyro_int_channel_cfg.
* @param[in] dev : Structure instance of bmi08x_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_gyro_data_ready_int(const struct bmi08_gyro_int_channel_cfg *int_config, struct bmi08_dev *dev);
/*!
* @brief This API sets the FIFO full, FIFO watermark interrupts for gyro sensor
*
* @param[in] int_config : Structure instance of bmi08x_gyro_int_channel_cfg.
* @param[in] dev : Structure instance of bmi08x_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_fifo_int(const struct bmi08_gyro_int_channel_cfg *int_config, struct bmi08_dev *dev);
/*!
* @brief This API configures the pins which fire the
* interrupt signal when any interrupt occurs.
*
* @param[in] int_config : Structure instance of bmi08x_gyro_int_channel_cfg.
* @param[in] dev : Structure instance of bmi08x_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_int_pin_config(const struct bmi08_gyro_int_channel_cfg *int_config, struct bmi08_dev *dev);
/*!
* @brief This API enables or disables the Gyro Self test feature in the
* sensor.
*
* @param[in] selftest : Variable used to enable or disable
* the Gyro self test feature
* Value | Description
* --------|---------------
* 0x00 | BMI08_DISABLE
* 0x01 | BMI08_ENABLE
*
* @param[in] dev : Structure instance of bmi08x_dev
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_gyro_selftest(uint8_t selftest, struct bmi08_dev *dev);
/*!
* @brief This internal API is used to get fifo data byte count
*
*
* @param[in] fifo : Structure instance of bmi08x_gyr_fifo_config.
* @param[out] frame_size : Size of the frame with respect to axis selected
* @param[out] fifo_data_byte : Stores the number of bytes to be read
*
*/
static void get_fifo_data_length(const struct bmi08_gyr_fifo_config *fifo, int8_t frame_size, uint16_t *fifo_data_byte);
/*!
* @brief This internal API computes the number of bytes of gyroscope FIFO data
* which is to be parsed.
*
* @param[out] len : Number of bytes to be parsed.
* @param[in] gyr_count : Number of gyroscope frames to be read.
* @param[in] fifo_conf : Structure instance of bmi08x_gyr_fifo_config.
* @param[in] fifo : Structure instance of bmi08x_fifo_frame.
*
*/
static void parse_fifo_gyro_len(uint16_t *len,
const uint16_t *gyr_count,
const struct bmi08_gyr_fifo_config *fifo_conf,
const struct bmi08_fifo_frame *fifo);
/*!
* @brief This internal API computes the number of bytes of gyroscope FIFO data
* which is to be parsed in header-less mode.
*
* @param[out] gyro : Structure instance of bmi08x_sensor_data.
* @param[in,out] data_index : Index value of number of bytes
* @param[in] fifo_conf : Structure instance of bmi08x_gyr_fifo_config.
* @param[in] fifo : Structure instance of bmi08x_fifo_frame.
*/
static void unpack_gyro_data(struct bmi08_sensor_data *gyro,
uint16_t *data_index,
const struct bmi08_gyr_fifo_config *fifo_conf,
const struct bmi08_fifo_frame *fifo);
/****************************************************************************/
/**\name Extern Declarations
****************************************************************************/
/****************************************************************************/
/**\name Globals
****************************************************************************/
/****************************************************************************/
/**\name Function definitions
****************************************************************************/
/*!
* @brief This API is the entry point for gyro sensor.
* It performs the selection of I2C/SPI read mechanism according to the
* selected interface and reads the chip-id of gyro sensor.
*/
int8_t bmi08g_init(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t chip_id = 0;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
dev->gyro_chip_id = 0;
/* Read gyro chip id */
rslt = get_regs(BMI08_REG_GYRO_CHIP_ID, &chip_id, BMI08_REG_GYRO_CHIP_ID_LENGTH, dev);
if (rslt == BMI08_OK)
{
if (chip_id == BMI08_GYRO_CHIP_ID)
{
/* Store the chip ID in dev structure */
dev->gyro_chip_id = chip_id;
}
else
{
rslt = BMI08_E_DEV_NOT_FOUND;
}
}
}
return rslt;
}
/*!
* @brief This API reads the data from the given register address
* of gyro sensor.
*/
int8_t bmi08g_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bmi08_dev *dev)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMI08_OK) && (reg_data != NULL))
{
if (len > 0)
{
/* Reading from the register */
rslt = get_regs(reg_addr, reg_data, len, dev);
}
else
{
rslt = BMI08_E_RD_WR_LENGTH_INVALID;
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API writes the given data to the register address
* of gyro sensor.
*/
int8_t bmi08g_set_regs(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, struct bmi08_dev *dev)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMI08_OK) && (reg_data != NULL))
{
if (len > 0)
{
/* Writing to the register */
rslt = set_regs(reg_addr, reg_data, len, dev);
/* Delay for suspended mode of the sensor is 450 us */
if (dev->gyro_cfg.power == BMI08_GYRO_PM_SUSPEND || dev->gyro_cfg.power == BMI08_GYRO_PM_DEEP_SUSPEND)
{
dev->delay_us(450, dev->intf_ptr_gyro);
}
/* Delay for Normal mode of the sensor is 2 us */
else if (dev->gyro_cfg.power == BMI08_GYRO_PM_NORMAL)
{
dev->delay_us(2, dev->intf_ptr_gyro);
}
else
{
/* Invalid power input */
rslt = BMI08_E_INVALID_INPUT;
}
}
else
{
rslt = BMI08_E_RD_WR_LENGTH_INVALID;
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API resets the gyro sensor.
*/
int8_t bmi08g_soft_reset(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
/* Reset gyro device */
data = BMI08_SOFT_RESET_CMD;
rslt = bmi08g_set_regs(BMI08_REG_GYRO_SOFTRESET, &data, BMI08_REG_GYRO_SOFTRESET_LENGTH, dev);
if (rslt == BMI08_OK)
{
/* delay 30 ms after writing reset value to its register */
dev->delay_us(BMI08_MS_TO_US(BMI08_GYRO_SOFTRESET_DELAY), dev->intf_ptr_gyro);
}
}
return rslt;
}
/*!
* @brief This API reads the gyro odr and range from the sensor, store it in the bmi08x_dev
* structure instance passed by the user.
*/
int8_t bmi08g_get_meas_conf(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data[2];
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_RANGE, data, (BMI08_REG_GYRO_RANGE_LENGTH - 1), dev);
if (rslt == BMI08_OK)
{
dev->gyro_cfg.range = data[0];
dev->gyro_cfg.odr = (data[1] & BMI08_GYRO_BW_MASK);
dev->gyro_cfg.bw = dev->gyro_cfg.odr;
}
}
return rslt;
}
/*!
* @brief This API sets the output data rate, range and bandwidth
* of gyro sensor.
*/
int8_t bmi08g_set_meas_conf(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data;
uint8_t odr, range;
uint8_t is_range_invalid = FALSE, is_odr_invalid = FALSE;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
odr = dev->gyro_cfg.odr;
range = dev->gyro_cfg.range;
if (odr > BMI08_GYRO_BW_32_ODR_100_HZ)
{
/* Updating the status */
is_odr_invalid = TRUE;
}
if (range > BMI08_GYRO_RANGE_125_DPS)
{
/* Updating the status */
is_range_invalid = TRUE;
}
/* If ODR and Range is valid, write it to gyro config. registers */
if ((!is_odr_invalid) && (!is_range_invalid))
{
/* Read range value from the range register */
rslt = bmi08g_get_regs(BMI08_REG_GYRO_BANDWIDTH, &data, BMI08_REG_GYRO_BANDWIDTH_LENGTH, dev);
if (rslt == BMI08_OK)
{
data = BMI08_SET_BITS_POS_0(data, BMI08_GYRO_BW, odr);
/* Write odr value to odr register */
rslt = bmi08g_set_regs(BMI08_REG_GYRO_BANDWIDTH, &data, BMI08_REG_GYRO_BANDWIDTH_LENGTH, dev);
if (rslt == BMI08_OK)
{
/* Read range value from the range register */
rslt = bmi08g_get_regs(BMI08_REG_GYRO_RANGE, &data, (BMI08_REG_GYRO_RANGE_LENGTH - 2), dev);
}
if (rslt == BMI08_OK)
{
data = BMI08_SET_BITS_POS_0(data, BMI08_GYRO_RANGE, range);
/* Write range value to range register */
rslt = bmi08g_set_regs(BMI08_REG_GYRO_RANGE, &data, (BMI08_REG_GYRO_RANGE_LENGTH - 2), dev);
}
if (rslt == BMI08_OK)
{
/* Delay required to set configurations */
dev->delay_us(BMI08_GYRO_SET_CONFIG_DELAY * 1000, dev->intf_ptr_gyro);
}
}
}
else
{
/* Invalid configuration present in ODR, Range */
rslt = BMI08_E_INVALID_CONFIG;
}
}
return rslt;
}
/*!
* @brief This API reads the gyro power mode from the sensor,
* store it in the bmi08x_dev structure instance
* passed by the user.
*
*/
int8_t bmi08g_get_power_mode(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_LPM1, &data, BMI08_REG_GYRO_LPM_LENGTH, dev);
if (rslt == BMI08_OK)
{
/* Updating the power mode in the dev structure */
dev->gyro_cfg.power = data;
}
}
return rslt;
}
/*!
* @brief This API sets the power mode of the gyro sensor.
*/
int8_t bmi08g_set_power_mode(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t power_mode, data;
uint8_t is_power_switching_mode_valid = TRUE;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
/*read the previous power state*/
rslt = bmi08g_get_regs(BMI08_REG_GYRO_LPM1, &data, BMI08_REG_GYRO_LPM_LENGTH, dev);
if (rslt == BMI08_OK)
{
power_mode = dev->gyro_cfg.power;
/* Switching between normal mode and the suspend modes is allowed, it is not possible to switch
* between suspend and deep suspend and vice versa. Check for invalid power switching,
* (i.e) deep suspend to suspend */
if ((power_mode == BMI08_GYRO_PM_SUSPEND) && (data == BMI08_GYRO_PM_DEEP_SUSPEND))
{
/* Updating the status */
is_power_switching_mode_valid = FALSE;
}
/* Check for invalid power switching (i.e) from suspend to deep suspend */
if ((power_mode == BMI08_GYRO_PM_DEEP_SUSPEND) && (data == BMI08_GYRO_PM_SUSPEND))
{
/* Updating the status */
is_power_switching_mode_valid = FALSE;
}
/* Check if power switching mode is valid*/
if (is_power_switching_mode_valid)
{
/* Write power to power register */
rslt = bmi08g_set_regs(BMI08_REG_GYRO_LPM1, &power_mode, BMI08_REG_GYRO_LPM_LENGTH, dev);
if (rslt == BMI08_OK)
{
/* Time required to switch the power mode */
dev->delay_us(BMI08_MS_TO_US(BMI08_GYRO_POWER_MODE_CONFIG_DELAY), dev->intf_ptr_gyro);
}
}
else
{
/* Updating the error */
rslt = BMI08_E_INVALID_INPUT;
}
}
}
return rslt;
}
/*!
* @brief This API reads the gyro data from the sensor,
* store it in the bmi08x_sensor_data structure instance
* passed by the user.
*/
int8_t bmi08g_get_data(struct bmi08_sensor_data *gyro, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data[6];
uint8_t lsb, msb;
uint16_t msblsb;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMI08_OK) && (gyro != NULL))
{
/* read gyro sensor data */
rslt = bmi08g_get_regs(BMI08_REG_GYRO_X_LSB, data, BMI08_REG_GYRO_X_LSB_LENGTH, dev);
if (rslt == BMI08_OK)
{
lsb = data[0];
msb = data[1];
msblsb = (msb << 8) | lsb;
gyro->x = (int16_t)msblsb; /* Data in X axis */
lsb = data[2];
msb = data[3];
msblsb = (msb << 8) | lsb;
gyro->y = (int16_t)msblsb; /* Data in Y axis */
lsb = data[4];
msb = data[5];
msblsb = (msb << 8) | lsb;
gyro->z = (int16_t)msblsb; /* Data in Z axis */
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API configures the necessary gyro interrupt
* based on the user settings in the bmi08x_int_cfg
* structure instance.
*/
int8_t bmi08g_set_int_config(const struct bmi08_gyro_int_channel_cfg *int_config, struct bmi08_dev *dev)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMI08_OK) && (int_config != NULL))
{
switch (int_config->int_type)
{
case BMI08_GYRO_INT_DATA_RDY:
/* Data ready interrupt */
rslt = set_gyro_data_ready_int(int_config, dev);
break;
case BMI08_GYRO_INT_FIFO_WM:
case BMI08_GYRO_INT_FIFO_FULL:
/* FIFO interrupt */
rslt = set_fifo_int(int_config, dev);
break;
default:
rslt = BMI08_E_INVALID_CONFIG;
break;
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API checks whether the self test functionality of the
* gyro sensor is working or not.
*/
int8_t bmi08g_perform_selftest(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data = 0, loop_break = 1;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
/* Enable the gyro self-test */
rslt = set_gyro_selftest(BMI08_ENABLE, dev);
if (rslt == BMI08_OK)
{
/* Loop till self-test ready bit is set */
while (loop_break)
{
/* Read self-test register to check if self-test ready bit is set */
rslt = bmi08g_get_regs(BMI08_REG_GYRO_SELF_TEST, &data, BMI08_REG_GYRO_SELF_TEST_LENGTH, dev);
if (rslt == BMI08_OK)
{
data = BMI08_GET_BITS(data, BMI08_GYRO_SELF_TEST_RDY);
if (data)
{
/* If self-test ready bit is set, exit the loop */
loop_break = 0;
}
}
else
{
/* Exit the loop in case of communication failure */
loop_break = 0;
}
}
if (rslt == BMI08_OK)
{
/* Read self-test register to check for self-test Ok bit */
rslt = bmi08g_get_regs(BMI08_REG_GYRO_SELF_TEST, &data, BMI08_REG_GYRO_SELF_TEST_LENGTH, dev);
if (rslt == BMI08_OK)
{
data = BMI08_GET_BITS(data, BMI08_GYRO_SELF_TEST_RESULT);
rslt = bmi08g_soft_reset(dev);
if (rslt == BMI08_OK)
{
/* Updating the self test result */
rslt = (int8_t) data;
}
}
}
}
}
return rslt;
}
/*!
* @brief This internal API gets gyro data ready interrupt status
*/
int8_t bmi08g_get_data_int_status(uint8_t *int_status, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t status = 0;
if (int_status != NULL)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_INT_STAT_1, &status, BMI08_REG_GYRO_INT_STAT_LENGTH, dev);
if (rslt == BMI08_OK)
{
(*int_status) = status;
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to get fifo overrun.
*/
int8_t bmi08g_get_fifo_overrun(uint8_t *fifo_overrun, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t reg_data = 0;
if (fifo_overrun != NULL)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_FIFO_STATUS, ®_data, BMI08_FIFO_STATUS_LENGTH, dev);
if (rslt == BMI08_OK)
{
*fifo_overrun = BMI08_GET_BITS(reg_data, BMI08_GYRO_FIFO_OVERRUN);
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to get fifo configuration of the sensor.
*/
int8_t bmi08g_get_fifo_config(struct bmi08_gyr_fifo_config *fifo_conf, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t fifo_config[2] = { 0 };
uint8_t reg_data = 0;
if (fifo_conf != NULL)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_FIFO_CONFIG0, fifo_config, BMI08_FIFO_CONFIG_LENGTH, dev);
if (rslt == BMI08_OK)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_FIFO_STATUS, ®_data, BMI08_FIFO_STATUS_LENGTH, dev);
if (rslt == BMI08_OK)
{
fifo_conf->tag = BMI08_GET_BITS(fifo_config[0], BMI08_GYRO_FIFO_TAG);
fifo_conf->wm_level = BMI08_GET_BITS_POS_0(fifo_config[0], BMI08_GYRO_FIFO_WM_LEVEL);
fifo_conf->mode = BMI08_GET_BITS(fifo_config[1], BMI08_GYRO_FIFO_MODE);
fifo_conf->data_select = BMI08_GET_BITS_POS_0(fifo_config[1], BMI08_GYRO_FIFO_DATA_SELECT);
fifo_conf->frame_count = BMI08_GET_BITS_POS_0(reg_data, BMI08_GYRO_FIFO_FRAME_COUNT);
}
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to get external fifo synchronization of the sensor.
*/
int8_t bmi08g_get_fifo_ext_int_sync(struct bmi08_gyro_fifo_ext_int *fifo_config, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t fifo_reg_data = 0;
if (fifo_config != NULL)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_FIFO_EXT_INT_S, &fifo_reg_data, BMI08_FIFO_LENGTH_MSB_BYTE, dev);
if (rslt == BMI08_OK)
{
fifo_config->ext_fifo_sync_en = BMI08_GET_BITS(fifo_reg_data, BMI08_GYRO_FIFO_EXT_INT_EN);
fifo_config->ext_fifo_ext_int_sync_src = BMI08_GET_BITS(fifo_reg_data, BMI08_GYRO_FIFO_EXT_INT_SYNC);
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to set external fifo synchronization of the sensor.
*/
int8_t bmi08g_set_fifo_ext_int_sync(const struct bmi08_gyro_fifo_ext_int *fifo_config, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t fifo_reg_data = 0;
if (fifo_config != NULL)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_FIFO_EXT_INT_S, &fifo_reg_data, BMI08_FIFO_LENGTH_MSB_BYTE, dev);
if (rslt == BMI08_OK)
{
fifo_reg_data = BMI08_SET_BITS(fifo_reg_data, BMI08_GYRO_FIFO_EXT_INT_EN, fifo_config->ext_fifo_sync_en);
fifo_reg_data = BMI08_SET_BITS(fifo_reg_data,
BMI08_GYRO_FIFO_EXT_INT_SYNC,
fifo_config->ext_fifo_ext_int_sync_src);
rslt = bmi08g_set_regs(BMI08_REG_GYRO_FIFO_EXT_INT_S, &fifo_reg_data, 1, dev);
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to get fifo configuration of the sensor.
*/
int8_t bmi08g_set_fifo_config(const struct bmi08_gyr_fifo_config *fifo_conf, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t fifo_config[2] = { 0 };
if (fifo_conf != NULL)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_FIFO_CONFIG0, fifo_config, BMI08_FIFO_CONFIG_LENGTH, dev);
if (rslt == BMI08_OK)
{
fifo_config[0] = BMI08_SET_BITS(fifo_config[0], BMI08_GYRO_FIFO_TAG, fifo_conf->tag);
fifo_config[0] = BMI08_SET_BITS_POS_0(fifo_config[0], BMI08_GYRO_FIFO_WM_LEVEL, fifo_conf->wm_level);
fifo_config[1] = BMI08_SET_BITS_POS_0(fifo_config[1], BMI08_GYRO_FIFO_DATA_SELECT, fifo_conf->data_select);
fifo_config[1] = BMI08_SET_BITS(fifo_config[1], BMI08_GYRO_FIFO_MODE, fifo_conf->mode);
rslt = bmi08g_set_regs(BMI08_REG_GYRO_FIFO_CONFIG0, fifo_config, BMI08_FIFO_CONFIG_LENGTH, dev);
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API gets the length of FIFO data available in the sensor in
* bytes.
*/
int8_t bmi08g_get_fifo_length(const struct bmi08_gyr_fifo_config *fifo_config, struct bmi08_fifo_frame *fifo)
{
int8_t rslt = BMI08_OK;
uint16_t fifo_data_byte_count = 0;
if ((fifo != NULL) && (fifo_config != NULL))
{
if (fifo_config->data_select == BMI08_GYRO_FIFO_XYZ_AXIS_ENABLED)
{
get_fifo_data_length(fifo_config, BMI08_GYRO_FIFO_XYZ_AXIS_FRAME_SIZE, &fifo_data_byte_count);
}
else
{
get_fifo_data_length(fifo_config, BMI08_GYRO_FIFO_SINGLE_AXIS_FRAME_SIZE, &fifo_data_byte_count);
}
if (fifo->length > fifo_data_byte_count)
{
fifo->length = fifo_data_byte_count;
}
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to read the fifo data from the sensor.
*/
int8_t bmi08g_read_fifo_data(const struct bmi08_fifo_frame *fifo, struct bmi08_dev *dev)
{
int8_t rslt = BMI08_OK;
if (fifo != NULL)
{
rslt = bmi08g_get_regs(BMI08_REG_GYRO_FIFO_DATA, fifo->data, fifo->length, dev);
}
else
{
rslt = BMI08_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to extract gyroscope data from fifo.
*/
void bmi08g_extract_gyro(struct bmi08_sensor_data *gyro_data,
const uint16_t *gyro_length,
const struct bmi08_gyr_fifo_config *fifo_conf,
const struct bmi08_fifo_frame *fifo)
{
uint16_t data_index = 0;
uint16_t gyro_index = 0;
uint16_t data_read_length = 0;
/* Get the number of gyro bytes to be read */
parse_fifo_gyro_len(&data_read_length, gyro_length, fifo_conf, fifo);
for (; data_index < data_read_length;)
{
unpack_gyro_data(&gyro_data[gyro_index], &data_index, fifo_conf, fifo);
gyro_index++;
}
}
int8_t bmi08g_enable_watermark(uint8_t enable, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t reg_data;
if (enable)
{
reg_data = BMI08_GYRO_FIFO_WM_ENABLE_VAL;
rslt = bmi08g_set_regs(BMI08_REG_GYRO_FIFO_WM_ENABLE, ®_data, (BMI08_FIFO_WTM_LENGTH - 1), dev);
}
else
{
reg_data = BMI08_GYRO_FIFO_WM_DISABLE_VAL;
rslt = bmi08g_set_regs(BMI08_REG_GYRO_FIFO_WM_ENABLE, ®_data, (BMI08_FIFO_WTM_LENGTH - 1), dev);
}
return rslt;
}
/*****************************************************************************/
/* Static function definition */
/*! @cond DOXYGEN_SUPRESS */
/* Suppressing doxygen warnings triggered for same static function names present across various sensor variant
* directories */
/*!
* @brief This API is used to validate the device structure pointer for
* null conditions.
*/
static int8_t null_ptr_check(const struct bmi08_dev *dev)
{
int8_t rslt;