26
26
#include < Wire.h>
27
27
28
28
/* defines */
29
- #define AP_2SMPB02E_CHIP_ID 0x5C
29
+ #define BARO_2SMPB02E_CHIP_ID 0x5C
30
30
31
31
#define GPIO_LED_R_PIN 4
32
32
#define GPIO_LED_G_PIN 5
33
33
#define GPIO_LED_B_PIN 6
34
34
35
35
/* values */
36
- ap_2smpb02e_setting_t ap_2smpb02e_setting ;
36
+ baro_2smpb02e_setting_t baro_2smpb02e_setting ;
37
37
38
38
/* macros */
39
39
#define conv8s_s24_be (a, b, c ) \
40
40
(int32_t )((((uint32_t )a << 16 ) & 0x00FF0000 ) | \
41
41
(((uint32_t )b << 8 ) & 0x0000FF00 ) | \
42
42
((uint32_t )c & 0x000000FF ))
43
43
44
- #define ap_halt (a ) {Serial.println (a); while (1 ) {}}
44
+ #define baro_halt (a ) {Serial.println (a); while (1 ) {}}
45
45
46
46
47
47
/* I2C functions */
@@ -78,80 +78,82 @@ bool i2c_read_reg8(uint8_t slave_addr, uint8_t register_addr,
78
78
return false ;
79
79
}
80
80
81
- /* * <!-- ap_2smpb02e_setup {{{1 --> setup for 2SMPB-02E
82
- * 1. check CHIP_ID to I2C connections.
81
+
82
+ /* * <!-- baro_2smpb02e_setup {{{1 --> setup for 2SMPB-02E
83
+ * 1. check CHIP_ID to confirm I2C connections.
83
84
* 2. read coefficient values for compensations.
84
85
* 3. sensor setup and start to measurements.
85
86
*/
86
- void ap_2smpb02e_setup (void ) {
87
+ bool baro_2smpb02e_setup (void ) {
87
88
bool result;
88
89
uint8_t rbuf[32 ] = {0 };
89
90
uint8_t ex;
90
91
91
92
// 1.
92
- result = i2c_read_reg8 (AP_2SMPB02E_ADDRESS ,
93
- AP_2SMPB02E_REGI2C_CHIP_ID , rbuf, 1 );
94
- if (result || rbuf[0 ] != AP_2SMPB02E_CHIP_ID ) {
95
- ap_halt (" cannot find 2SMPB-02E sensor, halted..." );
93
+ result = i2c_read_reg8 (BARO_2SMPB02E_ADDRESS ,
94
+ BARO_2SMPB02E_REGI2C_CHIP_ID , rbuf, 1 );
95
+ if (result || rbuf[0 ] != BARO_2SMPB02E_CHIP_ID ) {
96
+ baro_halt (" cannot find 2SMPB-02E sensor, halted..." );
96
97
}
97
98
98
99
// 2.
99
- result = i2c_read_reg8 (AP_2SMPB02E_ADDRESS ,
100
- AP_2SMPB02E_REGI2C_COEFS , rbuf, 25 );
100
+ result = i2c_read_reg8 (BARO_2SMPB02E_ADDRESS ,
101
+ BARO_2SMPB02E_REGI2C_COEFS , rbuf, 25 );
101
102
if (result) {
102
- ap_halt (" failed to read 2SMPB-02E coeffients, halted..." );
103
+ baro_halt (" failed to read 2SMPB-02E coeffients, halted..." );
103
104
}
104
105
105
106
// pressure parameters
106
107
ex = (rbuf[24 ] & 0xf0 ) >> 4 ;
107
- ap_2smpb02e_setting ._B00 = ap_2smpb02e_conv20q4_dbl (rbuf, ex, 0 );
108
- ap_2smpb02e_setting ._BT1 = ap_2smpb02e_conv16_dbl (
109
- AP_2SMPB02E_COEFF_A_BT1, AP_2SMPB02E_COEFF_S_BT1 , rbuf, 2 );
110
- ap_2smpb02e_setting ._BT2 = ap_2smpb02e_conv16_dbl (
111
- AP_2SMPB02E_COEFF_A_BT2, AP_2SMPB02E_COEFF_S_BT2 , rbuf, 4 );
112
- ap_2smpb02e_setting ._BP1 = ap_2smpb02e_conv16_dbl (
113
- AP_2SMPB02E_COEFF_A_BP1, AP_2SMPB02E_COEFF_S_BP1 , rbuf, 6 );
114
- ap_2smpb02e_setting ._B11 = ap_2smpb02e_conv16_dbl (
115
- AP_2SMPB02E_COEFF_A_B11, AP_2SMPB02E_COEFF_S_B11 , rbuf, 8 );
116
- ap_2smpb02e_setting ._BP2 = ap_2smpb02e_conv16_dbl (
117
- AP_2SMPB02E_COEFF_A_BP2, AP_2SMPB02E_COEFF_S_BP2 , rbuf, 10 );
118
- ap_2smpb02e_setting ._B12 = ap_2smpb02e_conv16_dbl (
119
- AP_2SMPB02E_COEFF_A_B12, AP_2SMPB02E_COEFF_S_B12 , rbuf, 12 );
120
- ap_2smpb02e_setting ._B21 = ap_2smpb02e_conv16_dbl (
121
- AP_2SMPB02E_COEFF_A_B21, AP_2SMPB02E_COEFF_S_B21 , rbuf, 14 );
122
- ap_2smpb02e_setting ._BP3 = ap_2smpb02e_conv16_dbl (
123
- AP_2SMPB02E_COEFF_A_BP3, AP_2SMPB02E_COEFF_S_BP3 , rbuf, 16 );
108
+ baro_2smpb02e_setting ._B00 = baro_2smpb02e_conv20q4_dbl (rbuf, ex, 0 );
109
+ baro_2smpb02e_setting ._BT1 = baro_2smpb02e_conv16_dbl (
110
+ BARO_2SMPB02E_COEFF_A_BT1, BARO_2SMPB02E_COEFF_S_BT1 , rbuf, 2 );
111
+ baro_2smpb02e_setting ._BT2 = baro_2smpb02e_conv16_dbl (
112
+ BARO_2SMPB02E_COEFF_A_BT2, BARO_2SMPB02E_COEFF_S_BT2 , rbuf, 4 );
113
+ baro_2smpb02e_setting ._BP1 = baro_2smpb02e_conv16_dbl (
114
+ BARO_2SMPB02E_COEFF_A_BP1, BARO_2SMPB02E_COEFF_S_BP1 , rbuf, 6 );
115
+ baro_2smpb02e_setting ._B11 = baro_2smpb02e_conv16_dbl (
116
+ BARO_2SMPB02E_COEFF_A_B11, BARO_2SMPB02E_COEFF_S_B11 , rbuf, 8 );
117
+ baro_2smpb02e_setting ._BP2 = baro_2smpb02e_conv16_dbl (
118
+ BARO_2SMPB02E_COEFF_A_BP2, BARO_2SMPB02E_COEFF_S_BP2 , rbuf, 10 );
119
+ baro_2smpb02e_setting ._B12 = baro_2smpb02e_conv16_dbl (
120
+ BARO_2SMPB02E_COEFF_A_B12, BARO_2SMPB02E_COEFF_S_B12 , rbuf, 12 );
121
+ baro_2smpb02e_setting ._B21 = baro_2smpb02e_conv16_dbl (
122
+ BARO_2SMPB02E_COEFF_A_B21, BARO_2SMPB02E_COEFF_S_B21 , rbuf, 14 );
123
+ baro_2smpb02e_setting ._BP3 = baro_2smpb02e_conv16_dbl (
124
+ BARO_2SMPB02E_COEFF_A_BP3, BARO_2SMPB02E_COEFF_S_BP3 , rbuf, 16 );
124
125
125
126
// temperature parameters
126
127
ex = (rbuf[24 ] & 0x0f );
127
- ap_2smpb02e_setting ._A0 = ap_2smpb02e_conv20q4_dbl (rbuf, ex, 18 );
128
- ap_2smpb02e_setting ._A1 = ap_2smpb02e_conv16_dbl (
129
- AP_2SMPB02E_COEFF_A_A1, AP_2SMPB02E_COEFF_S_A1 , rbuf, 20 );
130
- ap_2smpb02e_setting ._A2 = ap_2smpb02e_conv16_dbl (
131
- AP_2SMPB02E_COEFF_A_A2, AP_2SMPB02E_COEFF_S_A2 , rbuf, 22 );
128
+ baro_2smpb02e_setting ._A0 = baro_2smpb02e_conv20q4_dbl (rbuf, ex, 18 );
129
+ baro_2smpb02e_setting ._A1 = baro_2smpb02e_conv16_dbl (
130
+ BARO_2SMPB02E_COEFF_A_A1, BARO_2SMPB02E_COEFF_S_A1 , rbuf, 20 );
131
+ baro_2smpb02e_setting ._A2 = baro_2smpb02e_conv16_dbl (
132
+ BARO_2SMPB02E_COEFF_A_A2, BARO_2SMPB02E_COEFF_S_A2 , rbuf, 22 );
132
133
133
134
// 3. setup a sensor at 125msec sampling and 32-IIR filter.
134
- rbuf[0 ] = AP_2SMPB02E_VAL_IOSETUP_STANDBY_0125MS ;
135
- i2c_write_reg8 (AP_2SMPB02E_ADDRESS, AP_2SMPB02E_REGI2C_IO_SETUP ,
135
+ rbuf[0 ] = BARO_2SMPB02E_VAL_IOSETUP_STANDBY_0125MS ;
136
+ i2c_write_reg8 (BARO_2SMPB02E_ADDRESS, BARO_2SMPB02E_REGI2C_IO_SETUP ,
136
137
rbuf, sizeof (rbuf));
137
138
138
- rbuf[0 ] = AP_2SMPB02E_VAL_IIR_32TIMES ;
139
- i2c_write_reg8 (AP_2SMPB02E_ADDRESS, AP_2SMPB02E_REGI2C_IIR ,
139
+ rbuf[0 ] = BARO_2SMPB02E_VAL_IIR_32TIMES ;
140
+ i2c_write_reg8 (BARO_2SMPB02E_ADDRESS, BARO_2SMPB02E_REGI2C_IIR ,
140
141
rbuf, sizeof (rbuf));
141
142
142
143
// then, start to measurements.
143
- result = ap_2smpb02e_trigger_measurement (
144
- AP_2SMPB02E_VAL_MEASMODE_ULTRAHIGH );
144
+ result = baro_2smpb02e_trigger_measurement (
145
+ BARO_2SMPB02E_VAL_MEASMODE_ULTRAHIGH );
145
146
if (result) {
146
- ap_halt (" failed to wake up 2SMPB-02E sensor, halted..." );
147
+ baro_halt (" failed to wake up 2SMPB-02E sensor, halted..." );
147
148
}
149
+ return false ;
148
150
}
149
151
150
- /* * <!-- ap_2smpb02e_conv16_dbl {{{1 --> convert bytes buffer to double.
152
+ /* * <!-- baro_2smpb02e_conv16_dbl {{{1 --> convert bytes buffer to double.
151
153
* bytes buffer format is a signed-16bit Big-Endian.
152
154
*/
153
- static double ap_2smpb02e_conv16_dbl (double a, double s,
154
- uint8_t * buf, int offset) {
155
+ static double baro_2smpb02e_conv16_dbl (double a, double s,
156
+ uint8_t * buf, int offset) {
155
157
uint16_t val;
156
158
int16_t ret;
157
159
@@ -165,7 +167,7 @@ static double ap_2smpb02e_conv16_dbl(double a, double s,
165
167
return a + (double )ret * s / 32767.0 ;
166
168
}
167
169
168
- /* * <!-- ap_2smpb02e_conv20q4_dbl {{{1 --> convert bytes buffer to double.
170
+ /* * <!-- baro_2smpb02e_conv20q4_dbl {{{1 --> convert bytes buffer to double.
169
171
* bytes buffer format is signed 20Q4, from -32768.0 to 32767.9375
170
172
*
171
173
* ### bit field of 20Q4
@@ -177,7 +179,8 @@ static double ap_2smpb02e_conv16_dbl(double a, double s,
177
179
* +-- Decimal point
178
180
* ```
179
181
*/
180
- static double ap_2smpb02e_conv20q4_dbl (uint8_t * buf, uint8_t ex, int offset) {
182
+ static double baro_2smpb02e_conv20q4_dbl (uint8_t * buf,
183
+ uint8_t ex, int offset) {
181
184
int32_t ret;
182
185
uint32_t val;
183
186
@@ -190,43 +193,44 @@ static double ap_2smpb02e_conv20q4_dbl(uint8_t* buf, uint8_t ex, int offset) {
190
193
return (double )ret / 16.0 ;
191
194
}
192
195
193
- /* * <!-- ap_2smpb02e_trigger_measurement {{{1 --> start the sensor
196
+ /* * <!-- baro_2smpb02e_trigger_measurement {{{1 --> start the sensor
194
197
*/
195
- static bool ap_2smpb02e_trigger_measurement (uint8_t mode) {
196
- uint8_t wbuf[1 ] = {mode | AP_2SMPB02E_VAL_POWERMODE_NORMAL};
198
+ static bool baro_2smpb02e_trigger_measurement (uint8_t mode) {
199
+ uint8_t wbuf[1 ] = {
200
+ (uint8_t )(mode | BARO_2SMPB02E_VAL_POWERMODE_NORMAL)};
197
201
198
- i2c_write_reg8 (AP_2SMPB02E_ADDRESS, AP_2SMPB02E_REGI2C_CTRL_MEAS ,
202
+ i2c_write_reg8 (BARO_2SMPB02E_ADDRESS, BARO_2SMPB02E_REGI2C_CTRL_MEAS ,
199
203
wbuf, sizeof (wbuf));
200
204
return false ;
201
205
}
202
206
203
- /* * <!-- ap_2smpb02e_read {{{1 --> read the sensor digit and convert to
207
+ /* * <!-- baro_2smpb02e_read {{{1 --> read the sensor digit and convert to
204
208
* physical values.
205
209
*/
206
- bool ap_2smpb02e_read (uint32_t * pres, int16_t * temp,
210
+ int baro_2smpb02e_read (uint32_t * pres, int16_t * temp,
207
211
uint32_t * dp, uint32_t * dt) {
208
212
bool ret;
209
213
uint8_t rbuf[6 ] = {0 };
210
214
uint32_t rawtemp, rawpres;
211
215
212
216
ret = i2c_read_reg8 (
213
- AP_2SMPB02E_ADDRESS, AP_2SMPB02E_REGI2C_PRES_TXD2 ,
217
+ BARO_2SMPB02E_ADDRESS, BARO_2SMPB02E_REGI2C_PRES_TXD2 ,
214
218
rbuf, sizeof (rbuf));
215
219
if (ret) {
216
- return true ;
220
+ return 1 ;
217
221
}
218
222
219
223
*dp = rawpres = conv8s_s24_be (rbuf[0 ], rbuf[1 ], rbuf[2 ]);
220
224
*dt = rawtemp = conv8s_s24_be (rbuf[3 ], rbuf[4 ], rbuf[5 ]);
221
- return ap_2smpb02e_output_compensation (rawtemp, rawpres, pres, temp);
225
+ return baro_2smpb02e_output_compensation (rawtemp, rawpres, pres, temp);
222
226
}
223
227
224
- /* * <!-- ap_2smpb02e_output_compensation {{{1 --> compensate sensors
228
+ /* * <!-- baro_2smpb02e_output_compensation {{{1 --> compensate sensors
225
229
* raw output digits to [Pa] and [degC].
226
230
*/
227
- bool ap_2smpb02e_output_compensation (uint32_t raw_temp_val,
228
- uint32_t raw_press_val,
229
- uint32_t * pres, int16_t * temp
231
+ bool baro_2smpb02e_output_compensation (uint32_t raw_temp_val,
232
+ uint32_t raw_press_val,
233
+ uint32_t * pres, int16_t * temp
230
234
) {
231
235
double Tr, Po;
232
236
double Dt, Dp;
@@ -235,7 +239,7 @@ bool ap_2smpb02e_output_compensation(uint32_t raw_temp_val,
235
239
Dp = (int32_t )raw_press_val - 0x800000 ;
236
240
237
241
// temperature compensation
238
- ap_2smpb02e_setting_t * c = &ap_2smpb02e_setting ;
242
+ baro_2smpb02e_setting_t * c = &baro_2smpb02e_setting ;
239
243
Tr = c->_A0 + c->_A1 * Dt + c->_A2 * (Dt * Dt);
240
244
241
245
// barometer compensation
@@ -269,7 +273,7 @@ void setup() {
269
273
Wire.begin (); // master
270
274
271
275
Serial.println (" sensor: barometer" );
272
- ap_2smpb02e_setup ();
276
+ baro_2smpb02e_setup ();
273
277
delay (32 );
274
278
}
275
279
@@ -280,23 +284,24 @@ void setup() {
280
284
*/
281
285
void loop () {
282
286
static bool blink = false ;
283
- uint32_t pres, pres_dp, pres_dt ;
284
- int16_t temp_pres ;
287
+ uint32_t pres, dp, dt ;
288
+ int16_t temp ;
285
289
286
290
blink = !blink;
287
291
digitalWrite (GPIO_LED_R_PIN, blink ? HIGH: LOW);
288
292
digitalWrite (GPIO_LED_G_PIN, blink ? HIGH: LOW);
289
293
digitalWrite (GPIO_LED_B_PIN, blink ? HIGH: LOW);
290
294
delay (900 );
291
- ap_2smpb02e_read (&pres, &temp_pres , &pres_dp , &pres_dt );
295
+ int ret = baro_2smpb02e_read (&pres, &temp , &dp , &dt );
292
296
Serial.print (" sensor output:" );
293
- Serial.print (pres);
294
- Serial.print (" ," );
295
- Serial.print (temp_pres);
296
- Serial.print (" ," );
297
- Serial.print (pres_dp);
298
- Serial.print (" ," );
299
- Serial.print (pres_dt);
300
- Serial.println (" " );
297
+ Serial.print (pres / 10.0 );
298
+ Serial.print (" [Pa], " );
299
+ Serial.print (temp / 100.0 );
300
+ Serial.print (" [degC], " );
301
+ Serial.print (dp);
302
+ Serial.print (" []," );
303
+ Serial.print (dt);
304
+ Serial.print (" [], retun code:" );
305
+ Serial.println (ret);
301
306
}
302
307
// vi: ft=arduino:fdm=marker:et:sw=4:tw=80
0 commit comments