42#define CHIP_NAME "Bosch BMP280"
43#define MANUFACTURER_NAME "Bosch"
44#define SUPPLY_VOLTAGE_MIN 1.71f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 1.12f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
54#define BMP280_REG_NVM_PAR_T1_L 0x88
55#define BMP280_REG_NVM_PAR_T1_H 0x89
56#define BMP280_REG_NVM_PAR_T2_L 0x8A
57#define BMP280_REG_NVM_PAR_T2_H 0x8B
58#define BMP280_REG_NVM_PAR_T3_L 0x8C
59#define BMP280_REG_NVM_PAR_T3_H 0x8D
60#define BMP280_REG_NVM_PAR_P1_L 0x8E
61#define BMP280_REG_NVM_PAR_P1_H 0x8F
62#define BMP280_REG_NVM_PAR_P2_L 0x90
63#define BMP280_REG_NVM_PAR_P2_H 0x91
64#define BMP280_REG_NVM_PAR_P3_L 0x92
65#define BMP280_REG_NVM_PAR_P3_H 0x93
66#define BMP280_REG_NVM_PAR_P4_L 0x94
67#define BMP280_REG_NVM_PAR_P4_H 0x95
68#define BMP280_REG_NVM_PAR_P5_L 0x96
69#define BMP280_REG_NVM_PAR_P5_H 0x97
70#define BMP280_REG_NVM_PAR_P6_L 0x98
71#define BMP280_REG_NVM_PAR_P6_H 0x99
72#define BMP280_REG_NVM_PAR_P7_L 0x9A
73#define BMP280_REG_NVM_PAR_P7_H 0x9B
74#define BMP280_REG_NVM_PAR_P8_L 0x9C
75#define BMP280_REG_NVM_PAR_P8_H 0x9D
76#define BMP280_REG_NVM_PAR_P9_L 0x9E
77#define BMP280_REG_NVM_PAR_P9_H 0x9F
78#define BMP280_REG_TEMP_XLSB 0xFC
79#define BMP280_REG_TEMP_LSB 0xFB
80#define BMP280_REG_TEMP_MSB 0xFA
81#define BMP280_REG_PRESS_XLSB 0xF9
82#define BMP280_REG_PRESS_LSB 0xF8
83#define BMP280_REG_PRESS_MSB 0xF7
84#define BMP280_REG_CONFIG 0xF5
85#define BMP280_REG_CTRL_MEAS 0xF4
86#define BMP280_REG_STATUS 0xF3
87#define BMP280_REG_RESET 0xE0
88#define BMP280_REG_ID 0xD0
101static uint8_t a_bmp280_iic_spi_read(
bmp280_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
115 if (handle->
spi_read(reg, buf, len) != 0)
135static uint8_t a_bmp280_iic_spi_write(
bmp280_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
149 if (handle->
spi_write(reg, buf, len) != 0)
172 handle->
debug_print(
"bmp280: get calibration data failed.\n");
176 handle->
t1 = (uint16_t)buf[1] << 8 | buf[0];
179 handle->
debug_print(
"bmp280: get calibration data failed.\n");
183 handle->
t2 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
186 handle->
debug_print(
"bmp280: get calibration data failed.\n");
190 handle->
t3 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
193 handle->
debug_print(
"bmp280: get calibration data failed.\n");
197 handle->
p1 = (uint16_t)buf[1] << 8 | buf[0];
200 handle->
debug_print(
"bmp280: get calibration data failed.\n");
204 handle->
p2 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
207 handle->
debug_print(
"bmp280: get calibration data failed.\n");
211 handle->
p3 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
214 handle->
debug_print(
"bmp280: get calibration data failed.\n");
218 handle->
p4 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
221 handle->
debug_print(
"bmp280: get calibration data failed.\n");
225 handle->
p5 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
228 handle->
debug_print(
"bmp280: get calibration data failed.\n");
232 handle->
p6 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
235 handle->
debug_print(
"bmp280: get calibration data failed.\n");
239 handle->
p7 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
242 handle->
debug_print(
"bmp280: get calibration data failed.\n");
246 handle->
p8 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
249 handle->
debug_print(
"bmp280: get calibration data failed.\n");
253 handle->
p9 = (int16_t)((uint16_t)buf[1] << 8 | buf[0]);
269static uint8_t a_bmp280_compensate_temperature(
bmp280_handle_t *handle, uint32_t raw,
float *output)
276 var1 = (((float)raw) / 16384.0f - ((float)handle->
t1) / 1024.0f) * ((
float)handle->
t2);
277 var2 = ((((float)raw) / 131072.0f - ((float)handle->
t1) / 8192.0f) *
278 (((float)raw) / 131072.0f - ((float)handle->
t1) / 8192.0f)) *
280 handle->
t_fine = (int32_t)(var1 + var2);
281 temperature = (var1 + var2) / 5120.0f;
283 if (temperature < -40.0f)
285 temperature = -40.0f;
288 if (temperature > 85.0f)
293 (*output) = temperature;
308static uint8_t a_bmp280_compensate_pressure(
bmp280_handle_t *handle, uint32_t raw,
float *output)
315 var1 = ((float)handle->
t_fine / 2.0f) - 64000.0f;
316 var2 = var1 * var1 * ((float)handle->
p6) / 32768.0f;
317 var2 = var2 + var1 * ((float)handle->
p5) * 2.0f;
318 var2 = (var2 / 4.0f) + (((
float)handle->
p4) * 65536.0f);
319 var1 = (((float)handle->
p3) * var1 * var1 / 524288.0f +
320 ((float)handle->
p2) * var1) / 524288.0f;
321 var1 = (1.0f + var1 / 32768.0f) * ((
float)handle->
p1);
323 if (var1 < 0.0f || var1 > 0.0f)
325 pressure = 1048576.0f - (float)raw;
326 pressure = (pressure - (var2 / 4096.0f)) * 6250.0f / var1;
327 var1 = ((float)handle->
p9) * pressure * pressure / 2147483648.0f;
328 var2 = pressure * ((float)handle->
p8) / 32768.0f;
329 pressure = pressure + (var1 + var2 + ((float)handle->
p7)) / 16.0f;
331 if (pressure < 30000.0f)
336 if (pressure > 110000.0f)
338 pressure = 110000.0f;
342 (*output) = pressure;
349 (*output) = pressure;
371 handle->
iic_addr = (uint8_t)addr_pin;
413 handle->
iic_spi = (uint8_t)interface;
467 handle->
debug_print(
"bmp280: iic_init is null.\n");
473 handle->
debug_print(
"bmp280: iic_deinit is null.\n");
479 handle->
debug_print(
"bmp280: iic_read is null.\n");
485 handle->
debug_print(
"bmp280: iic_write is null.\n");
491 handle->
debug_print(
"bmp280: spi_init is null.\n");
497 handle->
debug_print(
"bmp280: spi_deinit is null.\n");
503 handle->
debug_print(
"bmp280: spi_read is null.\n");
509 handle->
debug_print(
"bmp280: spi_write is null.\n");
515 handle->
debug_print(
"bmp280: delay_ms is null.\n");
539 if (a_bmp280_iic_spi_read(handle,
BMP280_REG_ID, (uint8_t *)&
id, 1) != 0)
562 if (a_bmp280_get_nvm_calibration(handle) != 0)
599 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
607 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
615 handle->
debug_print(
"bmp280: iic deinit failed.\n");
624 handle->
debug_print(
"bmp280: spi deinit failed.\n");
693 handle->
debug_print(
"bmp280: read status failed.\n");
727 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
732 prev |= oversampling << 5;
735 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
769 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
804 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
809 prev |= oversampling << 2;
812 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
846 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
881 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
889 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
923 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
958 handle->
debug_print(
"bmp280: read config failed.\n");
963 prev |= standby_time << 5;
966 handle->
debug_print(
"bmp280: write config failed.\n");
1000 handle->
debug_print(
"bmp280: read config failed.\n");
1035 handle->
debug_print(
"bmp280: read config failed.\n");
1040 prev |= (filter & 0x07) << 2;
1043 handle->
debug_print(
"bmp280: write config failed.\n");
1077 handle->
debug_print(
"bmp280: read config failed.\n");
1112 handle->
debug_print(
"bmp280: read config failed.\n");
1120 handle->
debug_print(
"bmp280: write config failed.\n");
1154 handle->
debug_print(
"bmp280: read config failed.\n");
1182 uint32_t temperature_raw;
1183 float temperature_c;
1197 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1201 if ((prev & 0x3) == 3)
1210 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1211 (((uint32_t)(buf[4])) << 4) |
1212 ((uint32_t)buf[5] >> 4));
1213 res = a_bmp280_compensate_temperature(handle, temperature_raw, &temperature_c);
1216 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1220 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
1221 (((int32_t)(buf[1])) << 4) |
1222 (((int32_t)(buf[2])) >> 4));
1223 res = a_bmp280_compensate_pressure(handle, *pressure_raw, pressure_pa);
1226 handle->
debug_print(
"bmp280: compensate pressure failed.\n");
1235 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1243 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
1247 timeout = 10 * 1000;
1248 while (timeout != 0)
1252 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1256 if ((prev & 0x03) == 0)
1276 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1277 (((uint32_t)(buf[4])) << 4) |
1278 ((uint32_t)buf[5] >> 4));
1279 res = a_bmp280_compensate_temperature(handle, temperature_raw, &temperature_c);
1282 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1286 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
1287 (((int32_t)(buf[1])) << 4) |
1288 (((int32_t)(buf[2])) >> 4));
1289 res = a_bmp280_compensate_pressure(handle, *pressure_raw, pressure_pa);
1292 handle->
debug_print(
"bmp280: compensate pressure failed.\n");
1333 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1337 if ((prev & 0x3) == 3)
1346 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1347 (((uint32_t)(buf[4])) << 4) |
1348 ((uint32_t)buf[5] >> 4));
1349 res = a_bmp280_compensate_temperature(handle, *temperature_raw, temperature_c);
1352 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1361 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1369 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
1373 timeout = 10 * 1000;
1374 while (timeout != 0)
1378 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1382 if ((prev & 0x03) == 0)
1402 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1403 (((uint32_t)(buf[4])) << 4) |
1404 ((uint32_t)buf[5] >> 4));
1405 res = a_bmp280_compensate_temperature(handle, *temperature_raw, temperature_c);
1408 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1434 uint32_t *pressure_raw,
float *pressure_pa)
1452 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1456 if ((prev & 0x3) == 3)
1465 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1466 (((uint32_t)(buf[4])) << 4) |
1467 ((uint32_t)buf[5] >> 4));
1468 res = a_bmp280_compensate_temperature(handle, *temperature_raw, temperature_c);
1471 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1475 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
1476 (((int32_t)(buf[1])) << 4) |
1477 (((int32_t)(buf[2])) >> 4));
1478 res = a_bmp280_compensate_pressure(handle, *pressure_raw, pressure_pa);
1481 handle->
debug_print(
"bmp280: compensate pressure failed.\n");
1490 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1498 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
1502 timeout = 10 * 1000;
1503 while (timeout != 0)
1507 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1511 if ((prev & 0x03) == 0)
1531 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1532 (((uint32_t)(buf[4])) << 4) |
1533 ((uint32_t)buf[5] >> 4));
1534 res = a_bmp280_compensate_temperature(handle, *temperature_raw, temperature_c);
1537 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1541 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
1542 (((int32_t)(buf[1])) << 4) |
1543 (((int32_t)(buf[2])) >> 4));
1544 res = a_bmp280_compensate_pressure(handle, *pressure_raw, pressure_pa);
1547 handle->
debug_print(
"bmp280: compensate pressure failed.\n");
1579 return a_bmp280_iic_spi_write(handle, reg, &value, 1);
1605 return a_bmp280_iic_spi_read(handle, reg, value, 1);
#define BMP280_REG_NVM_PAR_P2_L
#define BMP280_REG_NVM_PAR_P6_L
#define BMP280_REG_NVM_PAR_P3_L
#define BMP280_REG_NVM_PAR_P4_L
#define BMP280_REG_CONFIG
#define BMP280_REG_NVM_PAR_P5_L
#define BMP280_REG_STATUS
#define BMP280_REG_NVM_PAR_P8_L
#define SUPPLY_VOLTAGE_MAX
#define BMP280_REG_NVM_PAR_T2_L
#define BMP280_REG_NVM_PAR_P1_L
#define BMP280_REG_NVM_PAR_P9_L
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define BMP280_REG_NVM_PAR_T3_L
#define BMP280_REG_CTRL_MEAS
#define BMP280_REG_NVM_PAR_P7_L
#define CHIP_NAME
chip information definition
#define BMP280_REG_NVM_PAR_T1_L
chip register definition
#define BMP280_REG_PRESS_MSB
driver bmp280 header file
uint8_t bmp280_soft_reset(bmp280_handle_t *handle)
soft reset
uint8_t bmp280_set_filter(bmp280_handle_t *handle, bmp280_filter_t filter)
set filter
uint8_t bmp280_get_pressure_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t *oversampling)
get pressure oversampling
bmp280_spi_wire_t
bmp280 spi wire enumeration definition
uint8_t bmp280_info(bmp280_info_t *info)
get chip's information
uint8_t bmp280_set_mode(bmp280_handle_t *handle, bmp280_mode_t mode)
set mode
uint8_t bmp280_get_mode(bmp280_handle_t *handle, bmp280_mode_t *mode)
get mode
uint8_t bmp280_read_temperature_pressure(bmp280_handle_t *handle, uint32_t *temperature_raw, float *temperature_c, uint32_t *pressure_raw, float *pressure_pa)
read the temperature and pressure data
bmp280_filter_t
bmp280 filter enumeration definition
uint8_t bmp280_get_temperatue_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t *oversampling)
get temperatue oversampling
uint8_t bmp280_deinit(bmp280_handle_t *handle)
close the chip
bmp280_mode_t
bmp280 mode enumeration definition
uint8_t bmp280_get_filter(bmp280_handle_t *handle, bmp280_filter_t *filter)
get filter
uint8_t bmp280_get_addr_pin(bmp280_handle_t *handle, bmp280_address_t *addr_pin)
get the iic address pin
bmp280_interface_t
bmp280 interface enumeration definition
uint8_t bmp280_set_interface(bmp280_handle_t *handle, bmp280_interface_t interface)
set the interface
uint8_t bmp280_get_standby_time(bmp280_handle_t *handle, bmp280_standby_time_t *standby_time)
get standby time
uint8_t bmp280_set_spi_wire(bmp280_handle_t *handle, bmp280_spi_wire_t spi)
set spi wire
uint8_t bmp280_read_pressure(bmp280_handle_t *handle, uint32_t *pressure_raw, float *pressure_pa)
read the pressure data
uint8_t bmp280_get_spi_wire(bmp280_handle_t *handle, bmp280_spi_wire_t *spi)
get spi wire
uint8_t bmp280_set_pressure_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t oversampling)
set pressure oversampling
uint8_t bmp280_set_addr_pin(bmp280_handle_t *handle, bmp280_address_t addr_pin)
set the iic address pin
bmp280_address_t
bmp280 address enumeration definition
struct bmp280_info_s bmp280_info_t
bmp280 information structure definition
uint8_t bmp280_get_interface(bmp280_handle_t *handle, bmp280_interface_t *interface)
get the interface
uint8_t bmp280_set_standby_time(bmp280_handle_t *handle, bmp280_standby_time_t standby_time)
set standby time
bmp280_oversampling_t
bmp280 oversampling enumeration definition
uint8_t bmp280_get_status(bmp280_handle_t *handle, uint8_t *status)
get status
uint8_t bmp280_init(bmp280_handle_t *handle)
initialize the chip
struct bmp280_handle_s bmp280_handle_t
bmp280 handle structure definition
uint8_t bmp280_read_temperature(bmp280_handle_t *handle, uint32_t *temperature_raw, float *temperature_c)
read the temperature data
bmp280_standby_time_t
bmp280 standby time enumeration definition
uint8_t bmp280_set_temperatue_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t oversampling)
set temperatue oversampling
uint8_t bmp280_set_reg(bmp280_handle_t *handle, uint8_t reg, uint8_t value)
set the chip register
uint8_t bmp280_get_reg(bmp280_handle_t *handle, uint8_t reg, uint8_t *value)
get the chip register
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* spi_read)(uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* spi_write)(uint8_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* spi_deinit)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v