-
Notifications
You must be signed in to change notification settings - Fork 23
/
bmp3.c
2960 lines (2575 loc) · 100 KB
/
bmp3.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) 2022 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 bmp3.c
* @date 2022-04-01
* @version v2.0.6
*
*/
/*! @file bmp3.c
* @brief Sensor driver for BMP3 sensor */
#include "bmp3.h"
/***************** Static function declarations ******************************/
/*!
* @brief This internal API reads the calibration data from the sensor, parse
* it then compensates it and store in the device structure.
*
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t get_calib_data(struct bmp3_dev *dev);
/*!
* @brief This internal API is used to parse the calibration data, compensates
* it and store it in device structure.
*
* @param[in] dev : Structure instance of bmp3_dev.
* @param[out] reg_data : Contains calibration data to be parsed.
*
*/
static void parse_calib_data(const uint8_t *reg_data, struct bmp3_dev *dev);
/*!
* @brief This internal API gets the over sampling, ODR and filter settings
* from the sensor.
*
* @param[in] settings : Structure instance of bmp3_settings.
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t get_odr_filter_settings(struct bmp3_settings *settings, struct bmp3_dev *dev);
/*!
* @brief This internal API is used to parse the pressure and temperature data
* and store it in the bmp3_uncomp_data structure instance.
*
* @param[in] reg_data : Contains the register data which needs to be parsed.
* @param[out] uncomp_data : Contains the uncompensated press and temp data.
*
*/
static void parse_sensor_data(const uint8_t *reg_data, struct bmp3_uncomp_data *uncomp_data);
/*!
* @brief This internal API is used to compensate the pressure or temperature
* or both the data according to the component selected by the user.
*
* @param[in] sensor_comp : Used to select pressure or temperature.
* @param[in] uncomp_data : Contains the uncompensated pressure and
* temperature data.
* @param[out] comp_data : Contains the compensated pressure and
* temperature data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t compensate_data(uint8_t sensor_comp,
const struct bmp3_uncomp_data *uncomp_data,
struct bmp3_data *comp_data,
struct bmp3_calib_data *calib_data);
#ifdef BMP3_FLOAT_COMPENSATION
/*!
* @brief This internal API is used to compensate the raw temperature data and
* return the compensated temperature data.
*
* @param[out] temperature : Compensated temperature data in double.
* @param[in] uncomp_data : Contains the uncompensated temperature data.
* @param[in] calib_data : Pointer to calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*
*/
static int8_t compensate_temperature(double *temperature,
const struct bmp3_uncomp_data *uncomp_data,
struct bmp3_calib_data *calib_data);
/*!
* @brief This internal API is used to compensate the pressure data and return
* the compensated pressure data.
*
* @param[out] comp_pressure : Compensated pressure data in double.
* @param[in] uncomp_data : Contains the uncompensated pressure data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t compensate_pressure(double *pressure,
const struct bmp3_uncomp_data *uncomp_data,
const struct bmp3_calib_data *calib_data);
/*!
* @brief This internal API is used to calculate the power functionality for
* double precision floating point values.
*
* @param[in] base : Contains the base value.
* @param[in] power : Contains the power value.
*
* @return Output of power function.
* @retval Calculated power function output in float.
*/
static float pow_bmp3(double base, uint8_t power);
#else
/*!
* @brief This internal API is used to compensate the raw temperature data and
* return the compensated temperature data in integer data type.
*
* @param[out] temperature : Compensated temperature data in integer.
* @param[in] uncomp_data : Contains the uncompensated temperature data.
* @param[in] calib_data : Pointer to calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t compensate_temperature(int64_t *temperature,
const struct bmp3_uncomp_data *uncomp_data,
struct bmp3_calib_data *calib_data);
/*!
* @brief This internal API is used to compensate the pressure data and return
* the compensated pressure data in integer data type.
*
* @param[out] comp_pressure : Compensated pressure data in integer.
* @param[in] uncomp_data : Contains the uncompensated pressure data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t compensate_pressure(uint64_t *pressure,
const struct bmp3_uncomp_data *uncomp_data,
const struct bmp3_calib_data *calib_data);
/*!
* @brief This internal API is used to calculate the power functionality.
*
* @param[in] base : Contains the base value.
* @param[in] power : Contains the power value.
*
* @return Output of power function.
* @retval Calculated power function output in integer.
*/
static uint32_t pow_bmp3(uint8_t base, uint8_t power);
#endif /* BMP3_FLOAT_COMPENSATION */
/*!
* @brief This internal API is used to identify the settings which the user
* wants to modify in the sensor.
*
* @param[in] sub_settings : Contains the settings subset to identify particular
* group of settings which the user is interested to change.
* @param[in] settings : Contains the user specified settings.
*
* @return Indicates whether user is interested to modify the settings which
* are related to sub_settings.
* @retval True -> User wants to modify this group of settings
* @retval False -> User does not want to modify this group of settings
*/
static uint8_t are_settings_changed(uint32_t sub_settings, uint32_t settings);
/*!
* @brief This internal API interleaves the register address between the
* register data buffer for burst write operation.
*
* @param[in] reg_addr : Contains the register address array.
* @param[out] temp_buff : Contains the temporary buffer to store the
* register data and register address.
* @param[in] reg_data : Contains the register data to be written in the
* temporary buffer.
* @param[in] len : No of bytes of data to be written for burst write.
*
*/
static void interleave_reg_addr(const uint8_t *reg_addr, uint8_t *temp_buff, const uint8_t *reg_data, uint32_t len);
/*!
* @brief This internal API sets the pressure enable and
* temperature enable settings of the sensor.
*
* @param[in] desired_settings : Contains the settings which user wants to
* change.
* @param[in] settings : Structure instance of bmp3_settings
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_pwr_ctrl_settings(uint32_t desired_settings,
const struct bmp3_settings *settings,
struct bmp3_dev *dev);
/*!
* @brief This internal API sets the over sampling, ODR and filter settings of
* the sensor based on the settings selected by the user.
*
* @param[in] desired_settings : Variable used to select the settings which
* are to be set.
* @param[in] settings : Structure instance of bmp3_settings
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_odr_filter_settings(uint32_t desired_settings, struct bmp3_settings *settings, struct bmp3_dev *dev);
/*!
* @brief This internal API sets the interrupt control (output mode, level,
* latch and data ready) settings of the sensor based on the settings
* selected by the user.
*
* @param[in] desired_settings : Variable used to select the settings which
* are to be set.
* @param[in] settings : Structure instance of bmp3_settings
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_int_ctrl_settings(uint32_t desired_settings,
const struct bmp3_settings *settings,
struct bmp3_dev *dev);
/*!
* @brief This internal API sets the advance (i2c_wdt_en, i2c_wdt_sel)
* settings of the sensor based on the settings selected by the user.
*
* @param[in] desired_settings : Variable used to select the settings which
* are to be set.
* @param[in] settings : Structure instance of bmp3_settings
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_advance_settings(uint32_t desired_settings, const struct bmp3_settings *settings,
struct bmp3_dev *dev);
/*!
* @brief This internal API fills the register address and register data of the
* the over sampling settings for burst write operation.
*
* @param[in] desired_settings : Variable which specifies the settings which
* are to be set in the sensor.
* @param[out] addr : To store the address to fill in register buffer.
* @param[out] reg_data : To store the osr register data.
* @param[out] len : To store the len for burst write.
* @param[in] settings : Structure instance of bmp3_settings
*
*/
static void fill_osr_data(uint32_t desired_settings,
uint8_t *addr,
uint8_t *reg_data,
uint8_t *len,
const struct bmp3_settings *settings);
/*!
* @brief This internal API fills the register address and register data of the
* the ODR settings for burst write operation.
*
* @param[out] addr : To store the address to fill in register buffer.
* @param[out] reg_data : To store the register data to set the odr data.
* @param[out] len : To store the len for burst write.
* @param[in] settings : Structure instance of bmp3_settings
*
*/
static void fill_odr_data(uint8_t *addr, uint8_t *reg_data, uint8_t *len, struct bmp3_settings *settings);
/*!
* @brief This internal API fills the register address and register data of the
* the filter settings for burst write operation.
*
* @param[out] addr : To store the address to fill in register buffer.
* @param[out] reg_data : To store the register data to set the filter.
* @param[out] len : To store the len for burst write.
* @param[in] settings : Structure instance of bmp3_settings
*
*/
static void fill_filter_data(uint8_t *addr, uint8_t *reg_data, uint8_t *len, const struct bmp3_settings *settings);
/*!
* @brief This internal API is used to validate the device pointer for
* null conditions.
*
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t null_ptr_check(const struct bmp3_dev *dev);
/*!
* @brief This internal API parse the power control(power mode, pressure enable
* and temperature enable), over sampling, ODR, filter and interrupt control
* settings and store in the device structure.
*
* @param[in] reg_data : Register data to be parsed.
* @param[in] settings : Structure instance of bmp3_settings
*/
static void parse_sett_data(const uint8_t *reg_data, struct bmp3_settings *settings);
/*!
* @brief This internal API parse the power control(power mode, pressure enable
* and temperature enable) settings and store in the device structure.
*
* @param[in] reg_data : Pointer variable which stores the register data to
* parse.
* @param[out] settings : Structure instance of bmp3_settings.
*/
static void parse_pwr_ctrl_settings(const uint8_t *reg_data, struct bmp3_settings *settings);
/*!
* @brief This internal API parse the over sampling, ODR and filter
* settings and store in the device structure.
*
* @param[in] reg_data : Pointer variable which stores the register data to
* parse.
* @param[out] settings : Structure instance of bmp3_odr_filter_settings.
*/
static void parse_odr_filter_settings(const uint8_t *reg_data, struct bmp3_odr_filter_settings *settings);
/*!
* @brief This internal API parse the interrupt control(output mode, level,
* latch and data ready) settings and store in the device structure.
*
* @param[in] reg_data : Pointer variable which stores the register data to
* parse.
* @param[out] settings : Structure instance of bmp3_int_ctrl_settings.
*/
static void parse_int_ctrl_settings(const uint8_t *reg_data, struct bmp3_int_ctrl_settings *settings);
/*!
* @brief This internal API parse the advance (i2c_wdt_en, i2c_wdt_sel)
* settings and store in the device structure.
*
* @param[in] reg_data : Pointer variable which stores the register data to
* parse.
* @param[out] settings : Structure instance of bmp3_adv_settings.
*/
static void parse_advance_settings(const uint8_t *reg_data, struct bmp3_adv_settings *settings);
/*!
* @brief This internal API validate the normal mode settings of the sensor.
*
* @param[out] settings : Structure instance of bmp3_settings.
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t validate_normal_mode_settings(struct bmp3_settings *settings, struct bmp3_dev *dev);
/*!
* @brief This internal API validate the over sampling, ODR settings of the
* sensor.
*
* @param[out] settings : Structure instance of bmp3_settings.
*
* @return Indicates whether ODR and OSR settings are valid or not.
* @retval 0 -> Success
* @retval <0 -> Fail
*/
static int8_t validate_osr_and_odr_settings(const struct bmp3_settings *settings);
/*!
* @brief This internal API calculates the pressure measurement duration of the
* sensor.
*
* @param[out] settings : Structure instance of bmp3_settings.
*
* @return Pressure measurement time
* @retval Pressure measurement time in microseconds
*/
static uint32_t calculate_press_meas_time(const struct bmp3_settings *settings);
/*!
* @brief This internal API calculates the temperature measurement duration of
* the sensor.
*
* @param[out] settings : Structure instance of bmp3_settings.
*
* @return Temperature measurement time
* @retval Temperature measurement time in microseconds
*/
static uint32_t calculate_temp_meas_time(const struct bmp3_settings *settings);
/*!
* @brief This internal API checks whether the measurement time and ODR duration
* of the sensor are proper.
*
* @param[in] meas_t : Pressure and temperature measurement time in microseconds.
* @param[in] odr_duration : Duration in microseconds corresponding to the ODR
* value.
*
* @return Indicates whether ODR and OSR settings are valid or not.
* @retval 0 -> Success
* @retval >0 -> Warning
*/
static int8_t verify_meas_time_and_odr_duration(uint32_t meas_t, uint32_t odr_duration);
/*!
* @brief This internal API puts the device to sleep mode.
*
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status.
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t put_device_to_sleep(struct bmp3_dev *dev);
/*!
* @brief This internal API sets the normal mode in the sensor.
*
* @param[in] settings : Structure instance of bmp3_settings.
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status.
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t set_normal_mode(struct bmp3_settings *settings, struct bmp3_dev *dev);
/*!
* @brief This internal API writes the power mode in the sensor.
*
* @param[out] settings : Structure instance of bmp3_settings.
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status.
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t write_power_mode(const struct bmp3_settings *settings, struct bmp3_dev *dev);
/*!
* @brief This internal API fills the fifo_config_1(fifo_mode,
* fifo_stop_on_full, fifo_time_en, fifo_press_en, fifo_temp_en) settings in the
* reg_data variable so as to burst write in the sensor.
*
* @param[in] desired_settings : Variable which specifies the settings which
* are to be set in the sensor.
* @param[in] fifo_settings : Structure instance of bmp3_fifo_settings
* @param[in] dev : Structure instance of bmp3_dev
*/
static int8_t fill_fifo_config_1(uint16_t desired_settings,
const struct bmp3_fifo_settings *fifo_settings,
struct bmp3_dev *dev);
/*!
* @brief This internal API fills the fifo_config_2(fifo_sub_sampling,
* data_select) settings in the reg_data variable so as to burst write
* in the sensor.
*
* @param[in] desired_settings : Variable which specifies the settings which
* are to be set in the sensor.
* @param[in] fifo_settings : Structure instance of bmp3_fifo_settings
* @param[in] dev : Structure instance of bmp3_dev
*/
static int8_t fill_fifo_config_2(uint16_t desired_settings,
const struct bmp3_fifo_settings *fifo_settings,
struct bmp3_dev *dev);
/*!
* @brief This internal API fills the fifo interrupt control(fwtm_en, ffull_en)
* settings in the reg_data variable so as to burst write in the sensor.
*
* @param[in] desired_settings : Variable which specifies the settings which
* are to be set in the sensor.
* @param[in] fifo_settings : Structure instance of bmp3_fifo_settings
* @param[in] dev : Structure instance of bmp3_dev
*/
static int8_t fill_fifo_int_ctrl(uint16_t desired_settings,
const struct bmp3_fifo_settings *fifo_settings,
struct bmp3_dev *dev);
/*!
* @brief This internal API is used to parse the fifo_config_1(fifo_mode,
* fifo_stop_on_full, fifo_time_en, fifo_press_en, fifo_temp_en),
* fifo_config_2(fifo_subsampling, data_select) and int_ctrl(fwtm_en, ffull_en)
* settings and store it in device structure
*
* @param[in] reg_data : Pointer variable which stores the fifo settings data
* read from the sensor.
* @param[out] fifo_settings : Structure instance of bmp3_fifo_settings which
* contains the fifo settings after parsing.
*/
static void parse_fifo_settings(const uint8_t *reg_data, struct bmp3_fifo_settings *fifo_settings);
/*!
* @brief This internal API parse the FIFO data frame from the fifo buffer and
* fills the byte count, uncompensated pressure and/or temperature data and no
* of parsed frames.
*
* @param[in] header : Pointer variable which stores the fifo settings data
* read from the sensor.
* @param[in,out] fifo : Structure instance of bmp3_fifo
* @param[out] byte_index : Byte count which is incremented according to the
* of data.
* @param[out] uncomp_data : Uncompensated pressure and/or temperature data
* which is stored after parsing fifo buffer data.
* @param[out] parsed_frames : Total number of parsed frames.
*
* @return Result of API execution status.
* @retval 0 -> Success
* @retval <0 -> Fail
*/
static uint8_t parse_fifo_data_frame(uint8_t header,
struct bmp3_fifo_data *fifo,
uint16_t *byte_index,
struct bmp3_uncomp_data *uncomp_data,
uint8_t *parsed_frames);
/*!
* @brief This internal API unpacks the FIFO data frame from the fifo buffer and
* fills the byte count, uncompensated pressure and/or temperature data.
*
* @param[out] byte_index : Byte count of fifo buffer.
* @param[in] fifo_buffer : FIFO buffer from where the temperature and pressure
* frames are unpacked.
* @param[out] uncomp_data : Uncompensated temperature and pressure data after
* unpacking from fifo buffer.
*/
static void unpack_temp_press_frame(uint16_t *byte_index,
const uint8_t *fifo_buffer,
struct bmp3_uncomp_data *uncomp_data);
/*!
* @brief This internal API unpacks the FIFO data frame from the fifo buffer and
* fills the byte count and uncompensated pressure data.
*
* @param[out] byte_index : Byte count of fifo buffer.
* @param[in] fifo_buffer : FIFO buffer from where the pressure frames are
* unpacked.
* @param[out] uncomp_data : Uncompensated pressure data after unpacking from
* fifo buffer.
*/
static void unpack_press_frame(uint16_t *byte_index, const uint8_t *fifo_buffer, struct bmp3_uncomp_data *uncomp_data);
/*!
* @brief This internal API unpacks the FIFO data frame from the fifo buffer and
* fills the byte count and uncompensated temperature data.
*
* @param[out] byte_index : Byte count of fifo buffer.
* @param[in] fifo_buffer : FIFO buffer from where the temperature frames
* are unpacked.
* @param[out] uncomp_data : Uncompensated temperature data after unpacking from
* fifo buffer.
*/
static void unpack_temp_frame(uint16_t *byte_index, const uint8_t *fifo_buffer, struct bmp3_uncomp_data *uncomp_data);
/*!
* @brief This internal API unpacks the time frame from the fifo data buffer and
* fills the byte count and update the sensor time variable.
*
* @param[out] byte_index : Byte count of fifo buffer.
* @param[in] fifo_buffer : FIFO buffer from where the sensor time frames
* are unpacked.
* @param[out] sensor_time : Variable used to store the sensor time.
*/
static void unpack_time_frame(uint16_t *byte_index, const uint8_t *fifo_buffer, uint32_t *sensor_time);
/*!
* @brief This internal API parses the FIFO buffer and gets the header info.
*
* @param[out] header : Variable used to store the fifo header data.
* @param[in] fifo_buffer : FIFO buffer from where the header data is retrieved.
* @param[out] byte_index : Byte count of fifo buffer.
*/
static void get_header_info(uint8_t *header, const uint8_t *fifo_buffer, uint16_t *byte_index);
/*!
* @brief This internal API parses the FIFO data frame from the fifo buffer and
* fills uncompensated temperature and/or pressure data.
*
* @param[in] sensor_comp : Variable used to select either temperature or
* pressure or both while parsing the fifo frames.
* @param[in] fifo_buffer : FIFO buffer where the temperature or pressure or
* both the data to be parsed.
* @param[out] uncomp_data : Uncompensated temperature or pressure or both the
* data after unpacking from fifo buffer.
*/
static void parse_fifo_sensor_data(uint8_t sensor_comp, const uint8_t *fifo_buffer,
struct bmp3_uncomp_data *uncomp_data);
/*!
* @brief This internal API resets the FIFO buffer, start index,
* parsed frame count, configuration change, configuration error and
* frame_not_available variables.
*
* @param[out] fifo : FIFO structure instance where the fifo related variables
* are reset.
*/
static void reset_fifo_index(struct bmp3_fifo_data *fifo);
/*!
* @brief This API gets the command ready, data ready for pressure and
* temperature, power on reset status from the sensor.
*
* @param[out] : Structure instance of bmp3_status
* @param[in] dev : Structure instance of bmp3_dev
*
* @return Result of API execution status.
* @retval 0 -> Success
* @retval <0 -> Fail
*/
static int8_t get_sensor_status(struct bmp3_status *status, struct bmp3_dev *dev);
/*!
* @brief This API gets the interrupt (fifo watermark, fifo full, data ready)
* status from the sensor.
*
* @param[out] : Structure instance of bmp3_status
* @param[in] dev : Structure instance of bmp3_dev
*
* @return Result of API execution status.
* @retval 0 -> Success
* @retval <0 -> Fail
*/
static int8_t get_int_status(struct bmp3_status *status, struct bmp3_dev *dev);
/*!
* @brief This API gets the fatal, command and configuration error
* from the sensor.
*
* @param[out] : Structure instance of bmp3_status
* @param[in] dev : Structure instance of bmp3_dev
*
* @return Result of API execution status.
* @retval 0 -> Success
* @retval <0 -> Fail
*/
static int8_t get_err_status(struct bmp3_status *status, struct bmp3_dev *dev);
/*!
* @brief This internal API converts the no. of frames required by the user to
* bytes so as to write in the watermark length register.
*
* @param[out] watermark_len : Pointer variable which contains the watermark
* length.
* @param[in] fifo : Structure instance of bmp3_fifo_data
* @param[in] fifo_settings : Structure instance of bmp3_fifo_settings
*
* @return Result of API execution status.
* @retval 0 -> Success
* @retval <0 -> Fail
*/
static int8_t convert_frames_to_bytes(uint16_t *watermark_len,
const struct bmp3_fifo_data *fifo,
const struct bmp3_fifo_settings *fifo_settings);
/****************** Global Function Definitions *******************************/
/*!
* @brief This API is the entry point.
* It performs the selection of I2C/SPI read mechanism according to the
* selected interface and reads the chip-id and calibration data of the sensor.
*/
int8_t bmp3_init(struct bmp3_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 == BMP3_OK)
{
/* Read mechanism according to selected interface */
if (dev->intf != BMP3_I2C_INTF)
{
/* If SPI interface is selected, read extra byte */
dev->dummy_byte = 1;
}
else
{
/* If I2C interface is selected, no need to read
* extra byte */
dev->dummy_byte = 0;
}
/* Read the chip-id of bmp3 sensor */
rslt = bmp3_get_regs(BMP3_REG_CHIP_ID, &chip_id, 1, dev);
/* Proceed if everything is fine until now */
if (rslt == BMP3_OK)
{
/* Check for chip id validity */
if ((chip_id == BMP3_CHIP_ID) || (chip_id == BMP390_CHIP_ID))
{
dev->chip_id = chip_id;
/* Reset the sensor */
rslt = bmp3_soft_reset(dev);
if (rslt == BMP3_OK)
{
/* Read the calibration data */
rslt = get_calib_data(dev);
}
}
else
{
rslt = BMP3_E_DEV_NOT_FOUND;
}
}
}
return rslt;
}
/*!
* @brief This API reads the data from the given register address of the sensor.
*/
int8_t bmp3_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bmp3_dev *dev)
{
int8_t rslt;
uint32_t idx;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if ((rslt == BMP3_OK) && (reg_data != NULL))
{
uint32_t temp_len = len + dev->dummy_byte;
uint8_t temp_buff[len + dev->dummy_byte];
/* If interface selected is SPI */
if (dev->intf != BMP3_I2C_INTF)
{
reg_addr = reg_addr | 0x80;
/* Read the data from the register */
dev->intf_rslt = dev->read(reg_addr, temp_buff, temp_len, dev->intf_ptr);
for (idx = 0; idx < len; idx++)
{
reg_data[idx] = temp_buff[idx + dev->dummy_byte];
}
}
else
{
/* Read the data using I2C */
dev->intf_rslt = dev->read(reg_addr, reg_data, len, dev->intf_ptr);
}
/* Check for communication error */
if (dev->intf_rslt != BMP3_INTF_RET_SUCCESS)
{
rslt = BMP3_E_COMM_FAIL;
}
}
else
{
rslt = BMP3_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API writes the given data to the register address
* of the sensor.
*/
int8_t bmp3_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint32_t len, struct bmp3_dev *dev)
{
int8_t rslt;
uint8_t temp_buff[len * 2];
uint32_t temp_len;
uint8_t reg_addr_cnt;
/* Check for null pointer in the device structure */
rslt = null_ptr_check(dev);
/* Check for arguments validity */
if ((rslt == BMP3_OK) && (reg_addr != NULL) && (reg_data != NULL))
{
if (len != 0)
{
temp_buff[0] = reg_data[0];
/* If interface selected is SPI */
if (dev->intf == BMP3_SPI_INTF)
{
for (reg_addr_cnt = 0; reg_addr_cnt < len; reg_addr_cnt++)
{
reg_addr[reg_addr_cnt] = reg_addr[reg_addr_cnt] & 0x7F;
}
}
/* Burst write mode */
if (len > 1)
{
/* Interleave register address w.r.t data for
* burst write*/
interleave_reg_addr(reg_addr, temp_buff, reg_data, len);
temp_len = len * 2;
}
else
{
temp_len = len;
}
dev->intf_rslt = dev->write(reg_addr[0], temp_buff, temp_len, dev->intf_ptr);
/* Check for communication error */
if (dev->intf_rslt != BMP3_INTF_RET_SUCCESS)
{
rslt = BMP3_E_COMM_FAIL;
}
}
else
{
rslt = BMP3_E_INVALID_LEN;
}
}
else
{
rslt = BMP3_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API sets the power control(pressure enable and
* temperature enable), over sampling, ODR and filter
* settings in the sensor.
*/
int8_t bmp3_set_sensor_settings(uint32_t desired_settings, struct bmp3_settings *settings, struct bmp3_dev *dev)
{
int8_t rslt = BMP3_OK;
if (settings != NULL)
{
if (are_settings_changed(BMP3_POWER_CNTL, desired_settings))
{
/* Set the power control settings */
rslt = set_pwr_ctrl_settings(desired_settings, settings, dev);
}
if (are_settings_changed(BMP3_ODR_FILTER, desired_settings))
{
/* Set the over sampling, ODR and filter settings */
rslt = set_odr_filter_settings(desired_settings, settings, dev);
}
if (are_settings_changed(BMP3_INT_CTRL, desired_settings))
{
/* Set the interrupt control settings */
rslt = set_int_ctrl_settings(desired_settings, settings, dev);
}
if (are_settings_changed(BMP3_ADV_SETT, desired_settings))
{
/* Set the advance settings */
rslt = set_advance_settings(desired_settings, settings, dev);
}
}
else
{
rslt = BMP3_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API gets the power control(power mode, pressure enable and
* temperature enable), over sampling, ODR, filter, interrupt control and
* advance settings from the sensor.
*/
int8_t bmp3_get_sensor_settings(struct bmp3_settings *settings, struct bmp3_dev *dev)
{
int8_t rslt;
uint8_t settings_data[BMP3_LEN_GEN_SETT];
if (settings != NULL)
{
rslt = bmp3_get_regs(BMP3_REG_INT_CTRL, settings_data, BMP3_LEN_GEN_SETT, dev);
if (rslt == BMP3_OK)
{
/* Parse the settings data */
parse_sett_data(settings_data, settings);
}
}
else
{
rslt = BMP3_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API sets the fifo_config_1(fifo_mode,
* fifo_stop_on_full, fifo_time_en, fifo_press_en, fifo_temp_en),
* fifo_config_2(fifo_subsampling, data_select) and int_ctrl(fwtm_en, ffull_en)
* settings in the sensor.
*/
int8_t bmp3_set_fifo_settings(uint16_t desired_settings,
const struct bmp3_fifo_settings *fifo_settings,
struct bmp3_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 == BMP3_OK) && (fifo_settings != NULL))
{
if (are_settings_changed(BMP3_FIFO_CONFIG_1, desired_settings))
{
/* Fill the FIFO config 1 register data */
rslt = fill_fifo_config_1(desired_settings, fifo_settings, dev);
}
if (are_settings_changed(desired_settings, BMP3_FIFO_CONFIG_2))