33#include " bar.h"
44#include " ../hal/MF_I2C.h"
55
6+ /*
7+ OSR T[ms] ADC_CVT
8+ 128 4.1 0b01010100
9+ 256 8.2 0b01010000
10+ 512 16.4 0b01001100
11+ 1024 32.8 0b01001000
12+ 2048 65.6 0b01000100
13+ 4096 131.1 0b01000000
14+ */
15+
16+ #define HP203B_SOFT_RST 0x06 // SOFT_RST soft reset
17+ #define HP203B_ANA_CAL 0x28 // ANA_CAL Re-calibrate the Internal analog Blocks
18+ #define HP203B_ADC_CVT_256 0b01010000 // start conversion: ADC_CVT = 010, 100 OSR=256 (8.2ms), 00 press+temp
19+ #define HP203B_READ_PT 0x10 // READ_PT pressure [Pa] + temp [0.01*C]
20+ #define HP203B_READ_AT 0x11 // READ_AT altitude [0.01*m] + temp [0.01*C]
21+
22+ #define HP203B_INT_SRC 0x8D
23+ #define HP203B_INT_SRC_DEV_RDY (1 <<6 ) // only access device when 1
24+ #define HP203B_INT_SRC_PA_RDY (1 <<5 ) // press/alt conversion ready
25+
26+
627class BarGizmoHP203B : public BarGizmo {
728private:
829 MF_I2CDevice *dev;
@@ -12,35 +33,30 @@ class BarGizmoHP203B: public BarGizmo {
1233 if (i2c_adr == 0 ) i2c_adr = 0x76 ; // fixed: 0x76 or 0x77
1334 this ->dev = new MF_I2CDevice (i2c, i2c_adr);
1435
15- dev->writeReg (0x06 , nullptr , 0 ); // SOFT_RST soft reset
16- dev->writeReg (0x28 , nullptr , 0 ); // ANA_CAL Re-calibrate the Internal analog Blocks
17- dev->writeReg (0b01010000 , nullptr , 0 ); // start conversion: ADC_CVT = 010, 100 OSR=256 (8.2ms), 00 press+temp
36+ dev->writeReg (HP203B_SOFT_RST, nullptr , 0 ); // SOFT_RST soft reset
37+ delay (1 ); // FIXME: should check HP203B_INT_SRC_DEV_RDY
38+ dev->writeReg (HP203B_ANA_CAL, nullptr , 0 ); // ANA_CAL Re-calibrate the Internal analog Blocks
39+ delay (1 ); // FIXME: should check HP203B_INT_SRC_DEV_RDY
40+ dev->writeReg (HP203B_ADC_CVT_256, nullptr , 0 ); // start conversion: ADC_CVT = 010, 100 OSR=256 (8.2ms), 00 press+temp
1841 }
1942
2043 ~BarGizmoHP203B () {
2144 delete dev;
2245 }
2346
24-
25-
26- /*
27- OSR T[ms] ADC_CVT
28- 128 4.1 0b01010100
29- 256 8.2 0b01010000
30- 512 16.4 0b01001100
31- 1024 32.8 0b01001000
32- 2048 65.6 0b01000100
33- 4096 131.1 0b01000000
34- */
3547 bool update (float *press, float *temp) override {
48+ // only read device when dev_rdy and pa_rdy
49+ // FIXME: put timeout on waiting for pa_rdy
50+ const uint8_t mask = HP203B_INT_SRC_DEV_RDY | HP203B_INT_SRC_DEV_RDY;
51+ if ( dev->readReg (HP203B_INT_SRC) & mask != mask) return false ;
52+
3653 uint8_t d[6 ];
37- dev->readReg (0x10 , d, 6 ); // READ_PT pressure [Pa] + temp [0.01*C]
38- // dev->readReg(0x11, d, 6); //READ_AT altitude [0.01*m] + temp [0.01*C]
39- dev->writeReg (0b01010000 , nullptr , 0 ); // start conversion: ADC_CVT = 010, 100 OSR=256 (8.2ms), 00 press+temp
54+ dev->readReg (HP203B_READ_PT, d, 6 ); // READ_PT pressure [Pa] + temp [0.01*C]
55+ dev->writeReg (HP203B_ADC_CVT_256, nullptr , 0 ); // start conversion: ADC_CVT = 010, 100 OSR=256 (8.2ms), 00 press+temp
4056
4157 int32_t t = ((d[0 ] << 16 ) | (d[1 ] << 8 ) | d[2 ]) & 0x000fffff ; // 20 bit big-endian signed
4258 if (t & 0x00080000 ) t |= 0xfff00000 ; // 20bit sign expansion
43- uint32_t p = ((d[3 ] << 16 ) | (d[4 ] << 8 ) | d[5 ]) & 0x000fffff ; // 20 bit big-endian unsigned
59+ int32_t p = ((d[3 ] << 16 ) | (d[4 ] << 8 ) | d[5 ]) & 0x000fffff ; // 20 bit big-endian unsigned
4460
4561 *temp = t * 0.01 ; // temperature in [C]
4662 *press = p; // pressure in [Pa]
0 commit comments