-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbm340_a14n.c
878 lines (814 loc) · 24.4 KB
/
fbm340_a14n.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
/*
* Copyright (C) 2015 Formosa Measurement Technology Inc. Ltd. All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
/* Driver description
*
* File Name :
* Authors : [email protected]
* Version : 1.1.0
* Date : 2019/3/11
* Description : FBM340-A14N pressure sensor API for MCU of ARM_M0 core
*
*/
/* Revised history
* 1.0.0: first release
* 1.1.0: To add a function calculating water depth
*
*/
#include "fbm340_a14n.h"
volatile uint32_t TMR0_Ticks;
volatile uint32_t fbm340_update_rdy;
static void fbm340_us_delay(uint32_t us);
#ifdef SPI
static uint8_t fbm340_spi_writeblock(uint8_t reg_addr, uint32_t cnt, const uint8_t *reg_data);
static uint8_t fbm340_spi_readblock(uint8_t reg_addr, uint32_t cnt, uint8_t *reg_data);
#else
static uint8_t fbm340_i2c_writeblock(uint8_t reg_addr, uint32_t cnt, const uint8_t *reg_data);
static uint8_t fbm340_i2c_readblock(uint8_t reg_addr, uint32_t cnt, uint8_t *reg_data);
#endif
static int32_t fbm340_startMeasure_temp(struct fbm340_data *barom);
static int32_t fbm340_get_raw_temperature(struct fbm340_data *barom);
static int32_t fbm340_startMeasure_press(struct fbm340_data *barom);
static int32_t fbm340_get_raw_pressure(struct fbm340_data *barom);
static int32_t fbm340_read_store_otp_data(struct fbm340_data *barom);
static int32_t fbm340_set_oversampling_rate(struct fbm340_data *barom
, enum fbm340_osr osr_setting);
static int32_t fbm340_chipid_check(struct fbm340_data *barom);
static int32_t fbm340_version_identification(struct fbm340_data *barom);
static int32_t fbm340_calculation(struct fbm340_data *barom);
static int32_t pressure_altitude_conversion(int32_t real_pressure);
static int32_t water_depth_calculation(int32_t real_pressure);
/**
* { pointer of fbm340 data }
*/
static struct fbm340_data fbm340_barom;
struct fbm340_data *barom = &fbm340_barom;
#ifdef SPI
static uint8_t fbm340_spi_writeblock(uint8_t reg_addr, uint32_t cnt, const uint8_t *reg_data)
{
/* This is just an example. This function have to
be implemented according to your platform. */
uint8_t cmd, i;
switch (cnt) {
case 1:
cmd = FBM340_SPI_WRITE | FBM340_SPI_1BYTE;
break;
case 2:
cmd = FBM340_SPI_WRITE | FBM340_SPI_2BYTE;
break;
case 3:
cmd = FBM340_SPI_WRITE | FBM340_SPI_3BYTE;
break;
default:
cmd = FBM340_SPI_WRITE | FBM340_SPI_4BYTE;
}
SPI_SET_SS0_LOW(SPI0);
/* Write to TX register */
SPI_WRITE_TX0(SPI0, cmd);
/* Trigger SPI data transfer */
SPI_TRIGGER(SPI0);
while (SPI_IS_BUSY(SPI0));
SPI_WRITE_TX0(SPI0, (reg_addr + (cnt - 1)));
SPI_TRIGGER(SPI0);
while (SPI_IS_BUSY(SPI0));
for (i = 0; i < cnt; i++) {
SPI_WRITE_TX0(SPI0, *(reg_data + i));
SPI_TRIGGER(SPI0);
/* Check SPI0 busy status */
while (SPI_IS_BUSY(SPI0));
}
SPI_SET_SS0_HIGH(SPI0);
return 0;
}
static uint8_t fbm340_spi_readblock(uint8_t reg_addr, uint32_t cnt, uint8_t *reg_data)
{
/* This is just an example. This function have to
be implemented according to your platform. */
int8_t i;
uint8_t cmd;
uint32_t tmp;
switch (cnt) {
case 1:
cmd = FBM340_SPI_READ | FBM340_SPI_1BYTE;
break;
case 2:
cmd = FBM340_SPI_READ | FBM340_SPI_2BYTE;
break;
case 3:
cmd = FBM340_SPI_READ | FBM340_SPI_3BYTE;
break;
default:
cmd = FBM340_SPI_READ | FBM340_SPI_4BYTE;
}
SPI_SET_SS0_LOW(SPI0);
/* Write to TX register */
SPI_WRITE_TX0(SPI0, cmd);
SPI_TRIGGER(SPI0);
while (SPI_IS_BUSY(SPI0));
SPI_WRITE_TX0(SPI0, reg_addr + (cnt - 1));
SPI_TRIGGER(SPI0);
while (SPI_IS_BUSY(SPI0));
for (i = (cnt - 1); i >= 0; i--) {
SPI_WRITE_TX0(SPI0, 0x00);//dummy clock
SPI_TRIGGER(SPI0);
while (SPI_IS_BUSY(SPI0));
tmp = SPI_READ_RX0(SPI0);
// printf("SPI read: %#x\n\r", tmp);
*(reg_data + i) = tmp;
}
SPI_SET_SS0_HIGH(SPI0);
return 0;
}
#endif
#ifdef I2C
static uint8_t fbm340_i2c_writeblock(uint8_t reg_addr, uint32_t cnt, const uint8_t *reg_data)
{
/* This is just an example. This function have to
be implemented according to your platform. */
uint8_t status;
uint32_t cnt_write;
cnt_write = I2C_WriteMultiBytesOneReg(I2C0, FBM340_I2C_SLAVE_ADDR, reg_addr \
, reg_data, cnt);
status = (cnt_write > 0) ? 0 : 1;
return status;
}
static uint8_t fbm340_i2c_readblock(uint8_t reg_addr, uint32_t cnt, uint8_t *reg_data)
{
/* This is just an example. This function have to
be implemented according to your platform. */
uint8_t status;
uint32_t cnt_read;
cnt_read = I2C_ReadMultiBytesOneReg(I2C0, FBM340_I2C_SLAVE_ADDR\
, reg_addr, reg_data, cnt);
status = (cnt_read > 0) ? 0 : 1;
return status;
}
#endif
/**
* @brief { API for fbm340 delay }
*
* @param[in] us { delay time in microseconds }
*/
static void fbm340_us_delay(uint32_t us)
{
/* This is just an example. This function have to
be implemented according to your platform. */
CLK_SysTickDelay(us);
}
/**
* @brief { API for assigning function pointers, as bus read/write
* and delay. }
*
* @return { 0, succeeded
* -1, failed }
*/
int8_t fbm340_init(void)
{
int32_t err;
uint8_t data_buf;
#ifdef SPI
fbm340_barom.bus_write = fbm340_spi_writeblock;
fbm340_barom.bus_read = fbm340_spi_readblock;
#else
fbm340_barom.bus_write = fbm340_i2c_writeblock;
fbm340_barom.bus_read = fbm340_i2c_readblock;
#endif
fbm340_barom.delay_usec = fbm340_us_delay;
/* The minimum start up time of fbm340 is 15ms */
barom->delay_usec(1000 * 15);
err = fbm340_chipid_check(barom);
if (err) {
err = -1;
goto err_chip_id_chk;
} else {
#ifdef DEBUG_FBM340
printf("%s:fbm340_chipid_check() passed!\n", __func__);
#endif
}
err = fbm340_version_identification(barom);
if (err) {
err = -2;
goto err_version_identification;
} else {
#ifdef DEBUG_FBM340
printf("%s:fbm340_version_identification() passed!\n", __func__);
#endif
}
err = fbm340_read_store_otp_data(barom);
if (err) {
err = -3;
goto err_read_otp;
} else {
#ifdef DEBUG_FBM340
printf("%s:fbm340_read_store_otp_data() passed!\n", __func__);
#endif
}
err = 0;
fbm340_set_oversampling_rate(barom, OVERSAMPLING_RATE_DEFAULT);
/* Setting the P_CONFIG_REG_GAIN */
#define P_CONFIG_REG_GAIN_SETTING FBM340_P_CONFIG_REG_GAIN_X32
barom->bus_read(FBM340_P_CONFIG_REG, sizeof(uint8_t), &data_buf);
data_buf &= ~(FBM340_P_CONFIG_REG_GAIN_MAK);
data_buf |= P_CONFIG_REG_GAIN_SETTING;
barom->bus_write(FBM340_P_CONFIG_REG, sizeof(uint8_t), &data_buf);
#ifdef DEBUG_FBM340
printf("%s:Setting of FBM340_P_CONFIG_REG_GAIN: %#x\n", __func__, P_CONFIG_REG_GAIN_SETTING);
#endif
#ifdef DEBUG_FBM340
printf("%s:fbm340_init() succeeded!\n", __func__);
#endif
return err;
err_chip_id_chk:
#ifdef DEBUG_FBM340
printf("%s:fbm340_init() failed!; fbm340_ID:%#x,err:%d\n", __func__, fbm340_barom.chip_id, err);
#endif
return err;
err_version_identification:
#ifdef DEBUG_FBM340
printf("%s:fbm340_init() failed!; fbm340 version:%#x,err:%d\n", __func__, fbm340_barom.hw_ver, err);
#endif
return err;
err_read_otp:
#ifdef DEBUG_FBM340
printf("%s:fbm340_init() failed!; fbm340 otp reading failed!,err:%d\n", __func__, err);
#endif
return err;
}
int32_t fbm340_read_raw_t(void)
{
return barom->raw_temperature;
}
/**
* @brief API for read real temperature value in unit of degree Celsius
*
* @return { temperature value in unit of degree Celsius }
*/
float fbm340_read_temperature(void)
{
fbm340_calculation(barom);
return barom->real_temperature * 0.01;
}
int32_t fbm340_read_raw_p(void)
{
return barom->raw_pressure;
}
/**
* @brief API for read real pressure value in unit of Pa
*
* @return { pressure value in unit of Pa }
*/
float fbm340_read_pressure(void)
{
fbm340_calculation(barom);
return barom->real_pressure;
}
/**
* @brief API for read real temperature and pressure values
* stored in fbm340_data structure
*
* @param real_pressure The pointer for saving real pressure value
* Pressure unit: Pa
* @param real_temperature The pointer for saving real temperature value
* Temperature unit: 0.01 degree Celsius
*/
void fbm340_read_data(int32_t *real_pressure, int32_t *real_temperature)
{
fbm340_calculation(barom);
*real_pressure = barom->real_pressure;
*real_temperature = barom->real_temperature;
return;
}
/**
* @brief { This api ignite a measurement procedure. It writes data into
* the register of FBM340_TAKE_MEAS_REG. }
*
* @param barom pointer of fbm340 data structure
*
* @return { return of bus_write() }
*/
static int fbm340_startMeasure_temp(struct fbm340_data *barom)
{
int err;
uint8_t bus_wr_data;
bus_wr_data = FBM340_MEAS_TEMP;
err = barom->bus_write(FBM340_TAKE_MEAS_REG, sizeof(uint8_t), &bus_wr_data);
return err;
}
/**
* @brief { This api gets the data from the registers of FBM340_READ_MEAS_REG_U
* , FBM340_READ_MEAS_REG_L and FBM340_READ_MEAS_REG_XL. And the data are
* stored in "barom->raw_temperature". }
*
* @param barom pointer of fbm340 data structure
*
* @return { return of bus_read() }
*/
static int fbm340_get_raw_temperature(struct fbm340_data *barom)
{
int err;
uint8_t buf[3] = {0};
err = barom->bus_read(FBM340_READ_MEAS_REG_U, 3 * sizeof(uint8_t), buf);
barom->raw_temperature = ((uint32_t)buf[0] << 16) + ((uint32_t)buf[1] << 8) + buf[2];
#ifdef DEBUG_FBM340
printf("%s: uncompensated temperature: %d\n", DEVICE_NAME, barom->raw_temperature);
#endif
return err;
}
/**
* @brief { This api ignite a measurement procedure. It writes data into
* the register of FBM340_TAKE_MEAS_REG. }
*
* @param barom pointer of fbm340 data structure
*
* @return { return of bus_write() }
*/
static int32_t fbm340_startMeasure_press(struct fbm340_data *barom)
{
int32_t err;
uint8_t bus_wr_data;
bus_wr_data = barom->cmd_start_p;
err = barom->bus_write(FBM340_TAKE_MEAS_REG, sizeof(uint8_t), &bus_wr_data);
return err;
}
/**
* @brief { This api gets the data from the registers of FBM340_READ_MEAS_REG_U
* , FBM340_READ_MEAS_REG_L and FBM340_READ_MEAS_REG_XL. And the data are
* stored in "barom->raw_temperature". }
*
* @param barom pointer of fbm340 data structure
*
* @return { return of bus_read() }
*/
static int32_t fbm340_get_raw_pressure(struct fbm340_data *barom)
{
int32_t err;
uint8_t buf[3] = {0};
err = barom->bus_read(FBM340_READ_MEAS_REG_U, 3 * sizeof(uint8_t), buf);
barom->raw_pressure = ((uint32_t)buf[0] << 16) + ((uint32_t)buf[1] << 8) + buf[2];
#ifdef DEBUG_FBM340
printf("%s: uncompensated pressure: %d\n", DEVICE_NAME, barom->raw_pressure);
#endif
return err;
}
/**
* @brief { API for reading calibration data saved in OTP memory }
*
* @param barom FBM340 data structure
*
* @return { description_of_the_return_value }
*/
static int32_t fbm340_read_store_otp_data(struct fbm340_data *barom)
{
struct fbm340_calibration_data *cali = &(barom->calibration);
int32_t status;
uint16_t R[10] = {0};
uint8_t tmp[FBM340_CALIBRATION_DATA_LENGTH] = {0};
status = barom->bus_read(FBM340_CALIBRATION_DATA_START0,
(FBM340_CALIBRATION_DATA_LENGTH - 2) * sizeof(uint8_t),
(uint8_t *)tmp);
if (status < 0)
goto exit;
status = barom->bus_read(FBM340_CALIBRATION_DATA_START1, sizeof(uint8_t), (uint8_t *)tmp + 18 );
if (status < 0)
goto exit;
status = barom->bus_read(FBM340_CALIBRATION_DATA_START2, sizeof(uint8_t), (uint8_t *)tmp + 19);
if (status < 0)
goto exit;
/* Read OTP data here */
R[0] = (tmp[0] << 8 | tmp[1]);
R[1] = (tmp[2] << 8 | tmp[3]);
R[2] = (tmp[4] << 8 | tmp[5]);
R[3] = (tmp[6] << 8 | tmp[7]);
R[4] = (tmp[8] << 8 | tmp[9]);
R[5] = (tmp[10] << 8 | tmp[11]);
R[6] = (tmp[12] << 8 | tmp[13]);
R[7] = (tmp[14] << 8 | tmp[15]);
R[8] = (tmp[16] << 8 | tmp[17]);
R[9] = (tmp[18] << 8 | tmp[19]);
/* Coefficient reconstruction */
switch (barom->hw_ver) {
case hw_ver_b0:
case hw_ver_b1:
case hw_ver_b3:
cali->C0 = R[0] >> 4;
cali->C1 = ((R[1] & 0xFF00) >> 5) | (R[2] & 7);
cali->C2 = ((R[1] & 0xFF) << 1) | (R[4] & 1);
cali->C3 = R[2] >> 3;
cali->C4 = ((uint32_t)R[3] << 2) | (R[0] & 3);
cali->C5 = R[4] >> 1;
cali->C6 = R[5] >> 3;
cali->C7 = ((uint32_t)R[6] << 3) | (R[5] & 7);
cali->C8 = R[7] >> 3;
cali->C9 = R[8] >> 2;
cali->C10 = ((R[9] & 0xFF00) >> 6) | (R[8] & 3);
cali->C11 = R[9] & 0xFF;
cali->C12 = ((R[0] & 0x0C) << 1) | (R[7] & 7);
break;
};
#if defined(DEBUG_FBM340) || defined(MSG_LOG)
printf("%s: R0= %#x\n", DEVICE_NAME, R[0]);
printf("%s: R1= %#x\n", DEVICE_NAME, R[1]);
printf("%s: R2= %#x\n", DEVICE_NAME, R[2]);
printf("%s: R3= %#x\n", DEVICE_NAME, R[3]);
printf("%s: R4= %#x\n", DEVICE_NAME, R[4]);
printf("%s: R5= %#x\n", DEVICE_NAME, R[5]);
printf("%s: R6= %#x\n", DEVICE_NAME, R[6]);
printf("%s: R7= %#x\n", DEVICE_NAME, R[7]);
printf("%s: R8= %#x\n", DEVICE_NAME, R[8]);
printf("%s: R9= %#x\n", DEVICE_NAME, R[9]);
printf("%s: C0= %d\n", DEVICE_NAME, cali->C0);
printf("%s: C1= %d\n", DEVICE_NAME, cali->C1);
printf("%s: C2= %d\n", DEVICE_NAME, cali->C2);
printf("%s: C3= %d\n", DEVICE_NAME, cali->C3);
printf("%s: C4= %d\n", DEVICE_NAME, cali->C4);
printf("%s: C5= %d\n", DEVICE_NAME, cali->C5);
printf("%s: C6= %d\n", DEVICE_NAME, cali->C6);
printf("%s: C7= %d\n", DEVICE_NAME, cali->C7);
printf("%s: C8= %d\n", DEVICE_NAME, cali->C8);
printf("%s: C9= %d\n", DEVICE_NAME, cali->C9);
printf("%s: C10= %d\n", DEVICE_NAME, cali->C10);
printf("%s: C11= %d\n", DEVICE_NAME, cali->C11);
printf("%s: C12= %d\n", DEVICE_NAME, cali->C12);
#endif
exit:
return status;
}
/**
* @brief { API for reading hardware version }
*
* @param barom FBM340 data structure
*
* @return { description_of_the_return_value }
*/
static int fbm340_version_identification(struct fbm340_data *barom)
{
int32_t err;
uint8_t buf[2] = {0};
uint8_t version = 0;
uint8_t bus_wr_data;
bus_wr_data = FBM340_SOFTRESET_CMD;
barom->bus_write(FBM340_SOFTRESET_REG, sizeof(uint8_t), &bus_wr_data);
barom->delay_usec(1000 * 15); /* The minimum start up time of fbm340 is
15ms */
err = barom->bus_read(FBM340_TAKE_MEAS_REG, sizeof(uint8_t), buf);
err = barom->bus_read(FBM340_VERSION_REG, sizeof(uint8_t), buf + 1);
version = ((buf[0] & 0xC0) >> 6) | ((buf[1] & 0x70) >> 2);
#if defined(DEBUG_FBM340) || defined(MSG_LOG)
printf("%s: The value of version: %#x\n", __func__, version);
#endif
switch (version) {
case hw_ver_b0:
barom->hw_ver = hw_ver_b0;
#if defined(DEBUG_FBM340) || defined(MSG_LOG)
printf("%s: The version of sensor is B0.\n", __func__);
#endif
break;
case hw_ver_b1:
barom->hw_ver = hw_ver_b1;
#if defined(DEBUG_FBM340) || defined(MSG_LOG)
printf("%s: The version of sensor is B1.\n", __func__);
#endif
break;
case hw_ver_b3:
barom->hw_ver = hw_ver_b3;
#if defined(DEBUG_FBM340) || defined(MSG_LOG)
printf("%s: The version of sensor is B3.\n", __func__);
#endif
break;
default:
barom->hw_ver = hw_ver_unknown;
#if defined(DEBUG_FBM340) || defined(MSG_LOG)
printf("%s: The version of sensor is unknown.\n", __func__);
#endif
break;
}
return err;
}
static int32_t fbm340_set_oversampling_rate(struct fbm340_data *barom
, enum fbm340_osr osr_setting)
{
uint8_t reg_addr;
uint8_t data_buf;
barom->oversampling_rate = osr_setting;
#ifdef DEBUG_FBM340
printf("%s:Setting of oversampling_rate:%#x\r\n", __func__,barom->oversampling_rate);
#endif
/* Setting conversion time for pressure measurement */
switch (osr_setting) {
case osr_1024:
barom->cnvTime_press = FBM340_CONVERSION_usTIME_OSR1024;
barom->cmd_start_p = FBM340_MEAS_PRESS_OVERSAMP_0;
break;
case osr_2048:
barom->cnvTime_press = FBM340_CONVERSION_usTIME_OSR2048;
barom->cmd_start_p = FBM340_MEAS_PRESS_OVERSAMP_1;
break;
case osr_4096:
barom->cnvTime_press = FBM340_CONVERSION_usTIME_OSR4096;
barom->cmd_start_p = FBM340_MEAS_PRESS_OVERSAMP_2;
break;
case osr_8192:
barom->cnvTime_press = FBM340_CONVERSION_usTIME_OSR8192;
barom->cmd_start_p = FBM340_MEAS_PRESS_OVERSAMP_3;
break;
case osr_16384:
barom->cnvTime_press = FBM340_CONVERSION_usTIME_OSR16384;
reg_addr = 0xa6;
barom->bus_read(reg_addr, sizeof(uint8_t), &data_buf);
data_buf &= 0xf8;
data_buf |= 0x6;
barom->bus_write(reg_addr, sizeof(uint8_t), &data_buf);
barom->cmd_start_p = FBM340_MEAS_PRESS_OVERSAMP_2;
barom->bus_read(0xA6, sizeof(uint8_t), &data_buf);
#ifdef DEBUG_FBM340
printf("reg_0xA6:%#x\n\r", data_buf);
#endif
break;
}
/* Setting covversion time for temperature measurement */
barom->cnvTime_temp = FBM340_CONVERSION_usTIME_OSR1024;
return 0;
}
static int32_t fbm340_chipid_check(struct fbm340_data *barom)
{
int32_t err;
uint8_t chip_id_read;
err = barom->bus_read(FBM340_CHIP_ID_REG, sizeof(uint8_t), &chip_id_read);
#ifdef DEBUG_FBM340
printf("%s: chip_id reading is %#x \n", __func__, chip_id_read);
#endif
if (chip_id_read != FBM340_CHIP_ID) {
err = -1;
return err;
} else {
barom->chip_id = chip_id_read;
return err = 0;
}
}
/**
* @brief { API for triggering measurement procedure and updating
* the temperature and pressure data in fbm340_data structure. }
*/
void fbm340_update_data(void)
{
static uint32_t t_start_flag = 0;
static uint32_t p_start_flag = 0;
static uint32_t tick_current;
static uint32_t tick_last;
static uint32_t tick_diff;
tick_current = TMR0_Ticks;
tick_diff = tick_current - tick_last;
if (t_start_flag == 0 && !fbm340_update_rdy) {
#ifdef DEBUG_FBM340
printf("start t_measurement\r\n");
#endif
fbm340_startMeasure_temp(barom);
t_start_flag = 1;
tick_last = TMR0_Ticks;
} else if ((tick_diff * 1000 > barom->cnvTime_temp ) && (p_start_flag == 0)) {
#ifdef DEBUG_FBM340
printf("start p_measurement\r\n");
#endif
fbm340_get_raw_temperature(barom);
fbm340_startMeasure_press(barom);
p_start_flag = 1;
tick_last = TMR0_Ticks;
} else if (tick_diff * 1000 > barom->cnvTime_press ) {
#ifdef DEBUG_FBM340
printf("read pressure\r\n");
#endif
fbm340_get_raw_pressure(barom);
t_start_flag = 0;
p_start_flag = 0;
tick_current = 0;
tick_last = 0;
TMR0_Ticks = 0;
fbm340_update_rdy = 1;
}
#ifdef DEBUG_FBM340
printf("tick_current:%d\r\n", tick_current);
printf("tick_last:%d\r\n", tick_last);
printf("FBM340 is updating %d\r\n", TMR0_Ticks);
#endif
return ;
}
/**
* @brief { API for calculating real temperature and pressure values.
* The results are stored in fbm340_data structure.
* "barom->real_temperature" is represented real temperature value.
* "barom->real_temperature" is in uint of 0.01 drgree Celsius.
* "barom->real_pressure" is represented real pressure value.
* "barom->real_pressure" is in unit of Pa. }
*
* @param barom pointer of fbm340 data structure
*
* @return { description_of_the_return_value }
*/
int fbm340_calculation(struct fbm340_data *barom)
{
struct fbm340_calibration_data *cali = &barom->calibration;
int32_t X01, X02, X03, X11, X12, X13, X21, X22, X23, X24, X25, X26, X31, X32;
int32_t PP1, PP2, PP3, PP4, CF;
int32_t RT, RP, UT, UP, DT, DT2;
switch (barom->hw_ver) {
case hw_ver_b0:
case hw_ver_b1:
case hw_ver_b3:
/* calculation for real temperature value*/
UT = barom->raw_temperature;
DT = ((UT - 8388608) >> 4) + (cali->C0 << 4);
X01 = (cali->C1 + 4498L) * DT >> 1;
X02 = ((((cali->C2 - 256L) * DT) >> 14) * DT) >> 4;
X03 = (((((cali->C3 * DT) >> 18) * DT) >> 18) * DT);
DT2 = (X01 + X02 + X03) >> 12;
RT = ((2500L << 15) - X01 - X02 - X03) >> 15;
/* calculation for real pressure value*/
UP = barom->raw_pressure;
X11 = ((cali->C5 - 15446L) * DT2);
X12 = ((((cali->C6 - 4096L) * DT2) >> 16) * DT2) >> 4;
X13 = ((X11 + X12) >> 11) + ((cali->C4 - 122684) << 4);
X21 = ((cali->C8 + 1528L) * DT2) >> 11;
X22 = (((cali->C9 * DT2) >> 17) * DT2) >> 13;
X23 = (X22 >= X21) ? (X22 - X21) : (X21 - X22);
X24 = (X23 >> 11) * (cali->C7 + 596352);
X25 = ((X23 & 0x7FF) * (cali->C7 + 596352)) >> 11;
X26 = (X21 >= X22 ? ((0 - X24 - X25) >> 9) : ((X24 + X25) >> 9)) + cali->C7 + 596352;
PP1 = (((UP - 8388608) >> 1) - X13) >> 4;
PP2 = ((X26 >> 12) * PP1) >> 1;
PP3 = ((X26 & 0xFFF) * PP1) >> 13;
PP4 = (PP2 + PP3) >> 3;
CF = (2097152 + cali->C12 * DT2) >> 2;
X31 = (((CF * cali->C10) >> 22) * PP4) >> 6;
X32 = (((((CF * cali->C11) >> 20) * PP4) >> 22) * PP4);
RP = ((X31 + X32) >> 11) + PP4 + 100000;
break;
};
barom->real_temperature = RT; //uint:0.01 degree Celsius
barom->real_pressure = RP; //uint: Pa
#ifdef DEBUG_FBM340
printf("%s: calibrated pressure: %d\n", DEVICE_NAME, RP);
#endif
return 0;
}
/**
* @brief { API for converting pressure value to altitude }
*
* @param[in] pressure_input The pressure_input is in unit of Pa.
*
* @return { The altitude value is in unit millimeter(mm) }
*/
int32_t fbm340_get_altitude(int32_t pressure_input)
{
return pressure_altitude_conversion(pressure_input * 8);
}
/**
* @brief { This function is used for converting pressure value to altitude.
* The standard sea-level pressure is 1013.25 hPa. }
*
* @param[in] real_pressure The real pressure is in unit of 0.125 Pa
*
* @return { The altitude value is in unit of millimeter(mm) }
*/
static int32_t pressure_altitude_conversion(int32_t real_pressure)
{
int32_t RP, h0, hs0, HP1, HP2, RH;
int16_t hs1, dP0;
int8_t P0;
RP = real_pressure;
if ( RP >= 824000 ) {
P0 = 103 ;
h0 = -138507 ;
hs0 = -5252 ;
hs1 = 311 ;
} else if ( RP >= 784000 ) {
P0 = 98 ;
h0 = 280531 ;
hs0 = -5468 ;
hs1 = 338 ;
} else if ( RP >= 744000 ) {
P0 = 93 ;
h0 = 717253 ;
hs0 = -5704 ;
hs1 = 370 ;
} else if ( RP >= 704000 ) {
P0 = 88 ;
h0 = 1173421 ;
hs0 = -5964 ;
hs1 = 407 ;
} else if ( RP >= 664000 ) {
P0 = 83 ;
h0 = 1651084 ;
hs0 = -6252 ;
hs1 = 450 ;
} else if ( RP >= 624000 ) {
P0 = 78 ;
h0 = 2152645 ;
hs0 = -6573 ;
hs1 = 501 ;
} else if ( RP >= 584000 ) {
P0 = 73 ;
h0 = 2680954 ;
hs0 = -6934 ;
hs1 = 560 ;
} else if ( RP >= 544000 ) {
P0 = 68 ;
h0 = 3239426 ;
hs0 = -7342 ;
hs1 = 632 ;
} else if ( RP >= 504000 ) {
P0 = 63 ;
h0 = 3832204 ;
hs0 = -7808 ;
hs1 = 719 ;
} else if ( RP >= 464000 ) {
P0 = 58 ;
h0 = 4464387 ;
hs0 = -8345 ;
hs1 = 826 ;
} else if ( RP >= 424000 ) {
P0 = 53 ;
h0 = 5142359 ;
hs0 = -8972 ;
hs1 = 960 ;
} else if ( RP >= 384000 ) {
P0 = 48 ;
h0 = 5874268 ;
hs0 = -9714 ;
hs1 = 1131 ;
} else if ( RP >= 344000 ) {
P0 = 43 ;
h0 = 6670762 ;
hs0 = -10609 ;
hs1 = 1354 ;
} else if ( RP >= 304000 ) {
P0 = 38 ;
h0 = 7546157 ;
hs0 = -11711 ;
hs1 = 1654 ;
} else if ( RP >= 264000 ) {
P0 = 33 ;
h0 = 8520395 ;
hs0 = -13103 ;
hs1 = 2072 ;
} else {
P0 = 28 ;
h0 = 9622536 ;
hs0 = -14926 ;
hs1 = 2682 ;
}
dP0 = RP - P0 * 8000;
HP1 = ( hs0 * dP0 ) >> 1;
HP2 = ((( hs1 * dP0 ) >> 14 ) * dP0 ) >> 4;
RH = (( HP1 + HP2 ) >> 8 ) + h0;
return RH;
}
/**
* @brief { API for calculating water depth }
*
* @param[in] pressure_input: unit: Pa
*
* @return { water depth:
* The unit of water depth is in millimeter(mm). }
*/
int32_t fbm340_get_water_depth(int32_t pressure_input)
{
return water_depth_calculation(pressure_input);
}
/**
* @brief { This function is used for calculating water depth }
*
* @param[in] real_pressure: The real pressure, unit: Pa
*
* @return { water_depth: water depth
* The unit of water_depth is in millimeter(mm). }
*/
static int32_t water_depth_calculation(int32_t real_pressure)
{
#define WATER_DENSITY 1025L /* unit:kg/m^3, range: 1000 to 1050 */
#define SEA_LEVEL_PRESSURE 101325 /* uint: Pa */
int32_t pressure, water_depth, d1, hc1;
pressure = real_pressure;
d1 = WATER_DENSITY - 1025L;
hc1 = (((d1 * 27325752) >> 5) * d1) >> 5;
hc1 = ((((d1 * 27348101) - hc1) >> 10) - 27364648) >> 16;
/* The unit of water_depth is millimeter(mm). */
water_depth = ((((SEA_LEVEL_PRESSURE - pressure) * hc1) >> 10) * 1000) >> 12;
#undef WATER_DENSITY
#undef SEA_LEVEL_PRESSURE
return water_depth;
}