-
Notifications
You must be signed in to change notification settings - Fork 51
/
bmi08a.c
2205 lines (1873 loc) · 70.4 KB
/
bmi08a.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 bmi08a.c
* @date 2024-07-29
* @version v1.9.0
*
*/
/****************************************************************************/
/**\name Header files
****************************************************************************/
#include "bmi08x.h"
/*********************** Function prototypes ************************/
/*!
* @brief This API is used to validate the device structure pointer for
* null conditions.
*
* @param[in] dev : Structure instance of bmi08_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t dev_null_ptr_check(const struct bmi08_dev *dev);
/*!
* @brief This API is used to validate the variable for null conditions.
*
* @param[in] data_parm : void pointer to hold address of variables.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t generic_null_ptr_check(void *data_parm);
/*!
* @brief This API writes or reads the data to/from the given register address.
*
* @param[in] reg_addr : Register address from where the data to be read or write.
* @param[out] reg_data : Pointer to data buffer.
* @param[in] len : No. of bytes of data to be read or write.
* @param[in] dev : Structure instance of bmi08_dev.
* @param[in] select : Flag to select functionality between getting and setting
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bmi08_dev *dev, uint8_t select);
/*!
* @brief This API configures the pins which fire the
* interrupt signal when any interrupt occurs.
*
* @param[in] int_config : Structure instance of bmi08_accel_int_channel_cfg.
* @param[in] dev : Structure instance of bmi08_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_int_pin_config(const struct bmi08_accel_int_channel_cfg *int_config, struct bmi08_dev *dev);
/*!
* @brief This API sets the data ready interrupt for accel sensor
*
* @param[in] int_config : Structure instance of bmi08_accel_int_channel_cfg.
* @param[in] dev : Structure instance of bmi08_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_accel_data_ready_int(const struct bmi08_accel_int_channel_cfg *int_config, struct bmi08_dev *dev);
/*!
* @brief This API sets the synchronized data ready interrupt for accel sensor
*
* @param[in] int_config : Structure instance of bmi08_accel_int_channel_cfg.
* @param[in] dev : Structure instance of bmi08_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_accel_sync_data_ready_int(const struct bmi08_accel_int_channel_cfg *int_config,
struct bmi08_dev *dev);
/*!
* @brief This API configures the given interrupt channel as input for accel sensor
*
* @param[in] int_config : Structure instance of bmi08_accel_int_channel_cfg.
* @param[in] dev : Structure instance of bmi08_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_accel_sync_input(const struct bmi08_accel_int_channel_cfg *int_config, struct bmi08_dev *dev);
/*!
* @brief This API writes the config stream data in memory using burst mode
*
* @param[in] stream_data : Pointer to store data of 32 bytes
* @param[in] index : Represents value in multiple of 32 bytes
* @param[in] dev : Structure instance of bmi08_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t stream_transfer_write(const uint8_t *stream_data, uint16_t index, struct bmi08_dev *dev);
/*!
* @brief This internal API is used to parse accelerometer data from the FIFO
* data.
*
* @param[out] acc : Structure instance of bmi08_sensor_data
* where the parsed data bytes are stored.
* @param[in] data_start_index : Index value of the accelerometer data bytes
* which is to be parsed from the FIFO data.
* @param[in] fifo : Structure instance of bmi08_fifo_frame.
*
* @return None
* @retval None
*
*/
static void unpack_accel_data(struct bmi08_sensor_data *acc,
uint16_t data_start_index,
const struct bmi08_fifo_frame *fifo);
/*!
* @brief This internal API is used to parse the accelerometer data from the
* FIFO data in both header and header-less mode. It updates the current data
* byte to be parsed.
*
* @param[in,out] acc : Structure instance of bmi08_sensor_data where
* where the parsed data bytes are stored.
* @param[in,out] idx : Index value of number of bytes parsed.
* @param[in,out] acc_idx : Index value of accelerometer data (x,y,z axes)
* frame to be parsed.
* @param[in] frame : Either data is enabled by user in header-less
* mode or header frame value in header mode.
* @param[in] fifo : Structure instance of bmi08_fifo_frame.
*
* @return Result of API execution status
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
* @retval > 0 -> Warning
*
*/
static int8_t unpack_accel_frame(struct bmi08_sensor_data *acc,
uint16_t *idx,
uint16_t *acc_idx,
uint16_t frame,
const struct bmi08_fifo_frame *fifo);
/*!
* @brief This internal API is used to parse and store the skipped frame count
* from the FIFO data.
*
* @param[in,out] data_index : Index of the FIFO data which contains skipped
* frame count.
* @param[in] fifo : Structure instance of bmi08_fifo_frame.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
* @retval > 0 -> Warning
*
*/
static int8_t unpack_skipped_frame(uint16_t *data_index, struct bmi08_fifo_frame *fifo);
/*!
* @brief This internal API is used to move the data index ahead of the
* current frame length parameter when unnecessary FIFO data appears while
* extracting the user specified data.
*
* @param[in,out] data_index : Index of the FIFO data which is to be
* moved ahead of the current frame length
* @param[in] current_frame_length : Number of bytes in the current frame.
* @param[in] fifo : Structure instance of bmi08_fifo_frame.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
* @retval > 0 -> Warning
*
*/
static int8_t move_next_frame(uint16_t *data_index, uint8_t current_frame_length, const struct bmi08_fifo_frame *fifo);
/*!
* @brief This internal API is used to parse and store the sensor time from the
* FIFO data.
*
* @param[in,out] data_index : Index of the FIFO data which has the sensor time.
* @param[in] fifo : Structure instance of bmi08_fifo_frame.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
* @retval > 0 -> Warning
*
*/
static int8_t unpack_sensortime_frame(uint16_t *data_index, struct bmi08_fifo_frame *fifo);
/*!
* @brief This internal API is used to reset the FIFO related configurations
* in the FIFO frame structure for the next FIFO read.
*
* @param[in, out] fifo : Structure instance of bmi08_fifo_frame.
* @param[in] dev : Structure instance of bmi08_dev.
*
* @return None
* @retval None
*
*/
static void reset_fifo_frame_structure(struct bmi08_fifo_frame *fifo);
/*!
* @brief This internal API is used to parse accelerometer data from the FIFO
* data in header mode.
*
* @param[out] acc : Structure instance of bmi08x_sens_data where
* the parsed accelerometer data bytes are stored.
* @param[in] accel_length : Number of accelerometer frames (x,y,z data).
* @param[in] fifo : Structure instance of bmi08_fifo_frame.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
* @retval > 0 -> Warning
*
*/
static int8_t extract_acc_header_mode(struct bmi08_sensor_data *acc,
uint16_t *accel_length,
struct bmi08_fifo_frame *fifo);
/*!
* @brief This API sets the FIFO full or FIFO water mark interrupt for accelerometer sensor
*
* @param[in] int_config : Structure instance of bmi08_accel_int_channel_cfg.
* @param[in] dev : Structure instance of bmi08_dev.
* @param[in] select_fifo_trigger : Flag to select FIFO full or FIFO water-mark interrupt
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*
*/
static int8_t set_fifo_full_wm_int(const struct bmi08_accel_int_channel_cfg *int_config,
struct bmi08_dev *dev,
uint8_t select_fifo_trigger);
/*!
* @brief This API configures and maps the interrupt pin
* @param[in] rslt : Return value to be validated
* @param[in] data : Data to be configured
* @param[in] int_config : Structure instance of bmi08_accel_int_channel_cfg.
* @param[in] dev : Structure instance of bmi08_dev.
*
* @return None
* @retval None
*/
static void config_map_int_pin(int8_t *rslt,
uint8_t *data,
const struct bmi08_accel_int_channel_cfg *int_config,
struct bmi08_dev *dev);
/*!
* @brief This API configures the interrupt pins based the selected channel
* @param[in] int_config : Structure instance of bmi08_accel_int_channel_cfg.
* @param[in] reg_addr : To store the register address
* @param[in] ch1 : Interrupt Channel 1
* @param[in] ch2 : Interrupt Channel 2
* @param[out] rslt : Stores the error values
* @param[out] val : Error values
*
* @return None
* @retval None
*/
static void int_pin_channel_config(const struct bmi08_accel_int_channel_cfg *int_config,
uint8_t *reg_addr,
uint8_t ch1,
uint8_t ch2,
int8_t *rslt,
int8_t val);
/**\name Function definitions
****************************************************************************/
/*!
* @brief This API is the entry point for accel sensor.
* It performs the selection of I2C/SPI read mechanism according to the
* selected interface and reads the chip-id of accel sensor.
*/
int8_t bmi08a_init(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t chip_id = 0;
/* Check for null pointer in the device structure */
rslt = dev_null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
dev->accel_chip_id = 0;
if (dev->intf == BMI08_SPI_INTF)
{
/* Set dummy byte in case of SPI interface */
dev->dummy_byte = BMI08_ENABLE;
/* Dummy read of Chip-ID in SPI mode */
rslt = set_get_regs(BMI08_REG_ACCEL_CHIP_ID, &chip_id, BMI08_REG_ACCEL_CHIP_ID_LENGTH, dev, GET_FUNC);
}
else
{
/* Make dummy byte 0 in case of I2C interface */
dev->dummy_byte = BMI08_DISABLE;
}
if (rslt == BMI08_OK)
{
rslt = set_get_regs(BMI08_REG_ACCEL_CHIP_ID, &chip_id, BMI08_REG_ACCEL_CHIP_ID_LENGTH, dev, GET_FUNC);
if (rslt == BMI08_OK)
{
/* Store the chip ID in dev structure */
dev->accel_chip_id = chip_id;
}
}
}
return rslt;
}
/*!
* @brief This API uploads the bmi08 config file onto the device.
*/
int8_t bmi08a_load_config_file(struct bmi08_dev *dev)
{
int8_t rslt;
/* Config loading disable */
uint8_t config_load = BMI08_DISABLE;
/* APS disable */
uint8_t aps_disable = BMI08_DISABLE;
uint16_t index = 0;
uint8_t reg_data = 0;
/* Check for null pointer in the device structure */
rslt = dev_null_ptr_check(dev); /* Type-casted to void to address compilation warning */
rslt |= generic_null_ptr_check((void *) dev->config_file_ptr);
/* Check if config file pointer is not null */
if (rslt == BMI08_OK)
{
/* Check whether the read/write length is valid */
if (dev->read_write_len > 0)
{
/* Disable advanced power save mode */
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_PWR_CONF,
&aps_disable,
BMI08_REG_ACCEL_PWR_CONF_LENGTH,
dev,
SET_FUNC);
if (rslt == BMI08_OK)
{
/* Wait until APS disable is set. Refer the data-sheet for more information */
dev->delay_us(450, dev->intf_ptr_accel);
/* Disable config loading */
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_INIT_CTRL,
&config_load,
BMI08_REG_ACCEL_PWR_CONF_LENGTH,
dev,
SET_FUNC);
}
if (rslt == BMI08_OK)
{
for (index = 0; index < BMI08_CONFIG_STREAM_SIZE;
index += dev->read_write_len)
{
/* Write the config stream */
rslt = stream_transfer_write((dev->config_file_ptr + index), index, dev);
}
if (rslt == BMI08_OK)
{
/* Enable config loading and FIFO mode */
config_load = BMI08_ENABLE;
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_INIT_CTRL,
&config_load,
BMI08_REG_ACCEL_INIT_CTRL_LENGHT,
dev,
SET_FUNC);
if (rslt == BMI08_OK)
{
/* Wait till ASIC is initialized. Refer the data-sheet for more information */
dev->delay_us(BMI08_MS_TO_US(BMI08_ASIC_INIT_TIME_MS), dev->intf_ptr_accel);
/* Check for config initialization status (1 = OK) */
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_INTERNAL_STAT,
®_data,
BMI08_REG_ACCEL_INTERNAL_STAT_LENGTH,
dev,
GET_FUNC);
}
}
/* Check for initialization status */
if (rslt == BMI08_OK && reg_data != BMI08_INIT_OK)
{
rslt = BMI08_E_CONFIG_STREAM_ERROR;
}
}
}
else
{
rslt = BMI08_E_RD_WR_LENGTH_INVALID;
}
}
return rslt;
}
/*!
* @brief This API writes the feature configuration to the accel sensor.
*/
int8_t bmi08a_write_feature_config(uint8_t reg_addr, const uint16_t *reg_data, uint8_t len, struct bmi08_dev *dev)
{
int8_t rslt;
int8_t index = 0;
uint16_t read_length = (reg_addr * 2) + (len * 2);
uint8_t feature_data[read_length];
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev); /* Type-casted to void to address compilation warning */
rslt |= generic_null_ptr_check(((void *)reg_data));
if (rslt == BMI08_OK)
{
/* Read feature space up to the given feature position */
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_FEATURE_CFG, &feature_data[0], read_length, dev, GET_FUNC);
if (rslt == BMI08_OK)
{
/* Apply the given feature config. */
for (index = 0; index < len; ++index)
{
/* The feature config space is 16bit aligned. */
feature_data[(reg_addr * 2) + (index * 2)] = reg_data[index] & 0xFF;
feature_data[(reg_addr * 2) + (index * 2) + 1] = reg_data[index] >> 8;
}
/* Write back updated feature space */
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_FEATURE_CFG, &feature_data[0], read_length, dev, SET_FUNC);
}
}
return rslt;
}
/*!
* @brief This API reads the data from the given register address of accel sensor.
*/
int8_t bmi08a_get_set_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bmi08_dev *dev, uint8_t select)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
rslt |= generic_null_ptr_check((void *) reg_data);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
if (len > 0)
{
if (select == GET_FUNC)
{
/* Reading from the register */
rslt = set_get_regs(reg_addr, reg_data, len, dev, GET_FUNC);
}
else
{
/* Writing to the register */
rslt = set_get_regs(reg_addr, reg_data, len, dev, SET_FUNC);
/* Delay for suspended mode of the sensor is 450 us */
if (dev->accel_cfg.power == BMI08_ACCEL_PM_SUSPEND)
{
dev->delay_us(450, dev->intf_ptr_accel);
}
/* Delay for Normal mode of the sensor is 2 us */
else if (dev->accel_cfg.power == BMI08_ACCEL_PM_ACTIVE)
{
dev->delay_us(2, dev->intf_ptr_accel);
}
else
{
/* Invalid power input */
rslt = BMI08_E_INVALID_INPUT;
}
}
}
else
{
rslt = BMI08_E_RD_WR_LENGTH_INVALID;
}
}
return rslt;
}
/*!
* @brief This API reads the error status from the accel sensor.
*/
int8_t bmi08a_get_error_status(struct bmi08_err_reg *err_reg, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data = 0;
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
rslt |= generic_null_ptr_check((void *) err_reg);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
/* Read the error codes */
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_ERR, &data, BMI08_REG_ACCEL_ERR_LENGTH, dev, GET_FUNC);
if (rslt == BMI08_OK)
{
/* Fatal error */
err_reg->fatal_err = BMI08_GET_BITS_POS_0(data, BMI08_FATAL_ERR);
/* User error */
err_reg->err_code = BMI08_GET_BITS(data, BMI08_ERR_CODE);
}
}
return rslt;
}
/*!
* @brief This API reads the status of the accel sensor.
*/
int8_t bmi08a_get_status(uint8_t *status, struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data = 0;
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
rslt |= generic_null_ptr_check((void *)status);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
/* Read the status */
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_STATUS, &data, BMI08_REG_ACCEL_STATUS_LENGTH, dev, GET_FUNC);
if (rslt == BMI08_OK)
{
/* Updating the status */
*status = BMI08_GET_BITS(data, BMI08_ACCEL_STATUS);
}
}
return rslt;
}
/*!
* @brief This API resets the accel sensor.
*/
int8_t bmi08a_soft_reset(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data;
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
data = BMI08_SOFT_RESET_CMD;
/* Reset accel device */
rslt = set_get_regs(BMI08_REG_ACCEL_SOFTRESET, &data, BMI08_REG_ACCEL_SOFTRESET_LENGTH, dev, SET_FUNC);
if (rslt == BMI08_OK)
{
/* Delay 1 ms after reset value is written to its register */
dev->delay_us(BMI08_MS_TO_US(BMI08_ACCEL_SOFTRESET_DELAY_MS), dev->intf_ptr_accel);
/* After soft reset SPI mode in the initialization phase, need to perform a dummy SPI read
* operation, The soft-reset performs a fundamental reset to the device,
* which is largely equivalent to a power cycle. */
if (dev->intf == BMI08_SPI_INTF)
{
/* Dummy SPI read operation of Chip-ID */
rslt =
bmi08a_get_set_regs(BMI08_REG_ACCEL_CHIP_ID, &data, BMI08_REG_ACCEL_CHIP_ID_LENGTH, dev, GET_FUNC);
}
}
}
return rslt;
}
/*!
* @brief This API reads the accel config value i.e. odr, band width and range from the sensor,
* store it in the bmi08_dev structure instance passed by the user.
*
*/
int8_t bmi08a_get_meas_conf(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data[2];
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_CONF, data, BMI08_REG_ACCEL_CONF_LENGTH, dev, GET_FUNC);
if (rslt == BMI08_OK)
{
dev->accel_cfg.odr = data[0] & BMI08_ACCEL_ODR_MASK;
dev->accel_cfg.bw = (data[0] & BMI08_ACCEL_BW_MASK) >> 4;
dev->accel_cfg.range = data[1] & BMI08_ACCEL_RANGE_MASK;
}
}
return rslt;
}
/*!
* @brief This API sets the output data rate and bandwidth
* of accel sensor.
*/
int8_t bmi08a_set_meas_conf(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t bw, odr;
uint8_t data = { 0 };
uint8_t is_odr_invalid = FALSE, is_bw_invalid = FALSE;
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
odr = dev->accel_cfg.odr;
bw = dev->accel_cfg.bw;
/* Check for valid ODR */
if ((odr < BMI08_ACCEL_ODR_12_5_HZ) || (odr > BMI08_ACCEL_ODR_1600_HZ))
{
/* Updating the status */
is_odr_invalid = TRUE;
}
/* Check for valid bandwidth */
if (bw > BMI08_ACCEL_BW_NORMAL)
{
/* Updating the status */
is_bw_invalid = TRUE;
}
/* Invalid configuration present in ODR and BW */
if ((!is_odr_invalid) && (!is_bw_invalid))
{
/* Read accel config. register */
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_CONF, &data, (BMI08_REG_ACCEL_CONF_LENGTH - 1), dev, GET_FUNC);
if (rslt == BMI08_OK)
{
/* Update data with new odr and bw values */
data = BMI08_SET_BITS_POS_0(data, BMI08_ACCEL_ODR, odr);
data = BMI08_SET_BITS(data, BMI08_ACCEL_BW, bw);
/* Write accel range to register */
rslt =
bmi08a_get_set_regs(BMI08_REG_ACCEL_CONF, &data, (BMI08_REG_ACCEL_CONF_LENGTH - 1), dev, SET_FUNC);
if (rslt == BMI08_OK)
{
/* Delay required to set accel configurations */
dev->delay_us(BMI08_SET_ACCEL_CONF_DELAY * 1000, dev->intf_ptr_accel);
}
}
else
{
/* Invalid configuration present in ODR and BW */
rslt = BMI08_E_INVALID_CONFIG;
}
}
}
return rslt;
}
/*!
* @brief This API reads the accel power mode from the sensor, store it in the bmi08_dev structure
* instance passed by the user.
*/
int8_t bmi08a_get_power_mode(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t data;
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_PWR_CONF, &data, BMI08_REG_ACCEL_PWR_CONF_LENGTH, dev, GET_FUNC);
if (rslt == BMI08_OK)
{
/* Updating the current power mode */
dev->accel_cfg.power = data;
}
}
return rslt;
}
/*!
* @brief This API sets the power mode of the accel sensor.
*/
int8_t bmi08a_set_power_mode(struct bmi08_dev *dev)
{
int8_t rslt;
uint8_t power_mode;
uint8_t data[2] = { 0 };
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
power_mode = dev->accel_cfg.power;
/* Configure data array to write to accel power configuration register */
if (power_mode == BMI08_ACCEL_PM_ACTIVE)
{
data[0] = BMI08_ACCEL_PM_ACTIVE;
data[1] = BMI08_ACCEL_POWER_ENABLE;
}
else if (power_mode == BMI08_ACCEL_PM_SUSPEND)
{
data[0] = BMI08_ACCEL_PM_SUSPEND;
data[1] = BMI08_ACCEL_POWER_DISABLE;
}
else
{
/* Invalid power input */
rslt = BMI08_E_INVALID_INPUT;
}
if (rslt == BMI08_OK)
{
/*enable accel sensor*/
rslt = set_get_regs(BMI08_REG_ACCEL_PWR_CONF, &data[0], BMI08_REG_ACCEL_PWR_CONF_LENGTH, dev, SET_FUNC);
if (rslt == BMI08_OK)
{
/*delay between power ctrl and power config*/
dev->delay_us(BMI08_MS_TO_US(BMI08_POWER_CONFIG_DELAY), dev->intf_ptr_accel);
/* write to accel power configuration register */
rslt = set_get_regs(BMI08_REG_ACCEL_PWR_CTRL, &data[1], BMI08_REG_ACCEL_PWR_CTRL_LENGTH, dev, SET_FUNC);
if (rslt == BMI08_OK)
{
/*delay required to switch power modes*/
dev->delay_us(BMI08_MS_TO_US(BMI08_POWER_CONFIG_DELAY), dev->intf_ptr_accel);
}
}
}
}
return rslt;
}
/*!
* @brief This API reads the accel data from the sensor,
* store it in the bmi08_sensor_data structure instance
* passed by the user.
*/
int8_t bmi08a_get_data(struct bmi08_sensor_data *accel, 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 = dev_null_ptr_check(dev);
rslt |= generic_null_ptr_check((void *) accel);
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
/* Read accel sensor data */
rslt = bmi08a_get_set_regs(BMI08_REG_ACCEL_X_LSB, data, BMI08_REG_ACCEL_X_LSB_LENGHT, dev, GET_FUNC);
if (rslt == BMI08_OK)
{
lsb = data[0];
msb = data[1];
msblsb = (msb << 8) | lsb;
accel->x = ((int16_t) msblsb); /* Data in X axis */
lsb = data[2];
msb = data[3];
msblsb = (msb << 8) | lsb;
accel->y = ((int16_t) msblsb); /* Data in Y axis */
lsb = data[4];
msb = data[5];
msblsb = (msb << 8) | lsb;
accel->z = ((int16_t) msblsb); /* Data in Z axis */
}
}
return rslt;
}
/*!
* @brief This API configures the necessary accel interrupt
* based on the user settings in the bmi08x_int_cfg
* structure instance.
*/
int8_t bmi08a_set_int_config(const struct bmi08_accel_int_channel_cfg *int_config, struct bmi08_dev *dev)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
rslt |= generic_null_ptr_check(((void *)int_config));
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
switch (int_config->int_type)
{
case BMI08_ACCEL_INT_DATA_RDY:
/* Data ready interrupt */
rslt = set_accel_data_ready_int(int_config, dev);
break;
case BMI08_ACCEL_INT_SYNC_DATA_RDY:
/* synchronized data ready interrupt */
rslt = set_accel_sync_data_ready_int(int_config, dev);
break;
case BMI08_ACCEL_SYNC_INPUT:
/* input for synchronization on accel */
rslt = set_accel_sync_input(int_config, dev);
break;
case BMI08_ACCEL_INT_FIFO_WM:
/* FIFO watermark interrupt */
rslt = set_fifo_full_wm_int(int_config, dev, FIFO_WM_INT);
break;
case BMI08_ACCEL_INT_FIFO_FULL:
/* FIFO full interrupt */
rslt = set_fifo_full_wm_int(int_config, dev, FIFO_FULL_INT);
break;
default:
rslt = BMI08_E_INVALID_CONFIG;
break;
}
}
return rslt;
}
/*!
* @brief This API reads the temperature of the sensor in degree Celcius.
*/
int8_t bmi08a_get_sensor_temperature(struct bmi08_dev *dev, int32_t *sensor_temp)
{
int8_t rslt;
uint8_t data[2] = { 0 };
uint16_t msb, lsb;
uint16_t msblsb;
int16_t temp;
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
rslt |= generic_null_ptr_check(((void *)sensor_temp));
/* Proceed if null check is fine */
if (rslt == BMI08_OK)
{
/* Read sensor temperature */
rslt = bmi08a_get_set_regs(BMI08_REG_TEMP_MSB, data, BMI08_REG_TEMP_MSB_LENGTH, dev, GET_FUNC);
if (rslt == BMI08_OK)
{
msb = (data[0] << 3); /* MSB data */
lsb = (data[1] >> 5); /* LSB data */
msblsb = (uint16_t) (msb + lsb);
if (msblsb > 1023)
{
/* Updating the msblsb */
temp = (int16_t) (msblsb - 2048);
}
else
{
temp = (int16_t) msblsb;
}
/* sensor temperature */
*sensor_temp = (temp * 125) + 23000;
}
}
return rslt;
}
/*!
* @brief This API reads the sensor time of the accel sensor.
*/
int8_t bmi08a_get_sensor_time(struct bmi08_dev *dev, uint32_t *sensor_time)
{
int8_t rslt;
uint8_t data[3] = { 0 };
uint32_t byte2, byte1, byte0;
/* Check for null pointer in the device structure*/
rslt = dev_null_ptr_check(dev);
rslt |= generic_null_ptr_check(((void *)sensor_time));