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)
583 if (a_bmp280_get_nvm_calibration(handle) != 0)
627 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
635 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
643 handle->
debug_print(
"bmp280: iic deinit failed.\n");
652 handle->
debug_print(
"bmp280: spi deinit failed.\n");
721 handle->
debug_print(
"bmp280: read status failed.\n");
755 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
760 prev |= oversampling << 5;
763 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
797 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
832 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
837 prev |= oversampling << 2;
840 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
874 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
909 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
917 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
951 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
986 handle->
debug_print(
"bmp280: read config failed.\n");
991 prev |= standby_time << 5;
994 handle->
debug_print(
"bmp280: write config failed.\n");
1028 handle->
debug_print(
"bmp280: read config failed.\n");
1063 handle->
debug_print(
"bmp280: read config failed.\n");
1068 prev |= (filter & 0x07) << 2;
1071 handle->
debug_print(
"bmp280: write config failed.\n");
1105 handle->
debug_print(
"bmp280: read config failed.\n");
1140 handle->
debug_print(
"bmp280: read config failed.\n");
1148 handle->
debug_print(
"bmp280: write config failed.\n");
1182 handle->
debug_print(
"bmp280: read config failed.\n");
1210 uint32_t temperature_raw;
1211 float temperature_c;
1225 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1229 if ((prev & 0x3) == 3)
1238 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1239 (((uint32_t)(buf[4])) << 4) |
1240 ((uint32_t)buf[5] >> 4));
1241 res = a_bmp280_compensate_temperature(handle, temperature_raw, &temperature_c);
1244 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1248 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
1249 (((int32_t)(buf[1])) << 4) |
1250 (((int32_t)(buf[2])) >> 4));
1251 res = a_bmp280_compensate_pressure(handle, *pressure_raw, pressure_pa);
1254 handle->
debug_print(
"bmp280: compensate pressure failed.\n");
1263 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1271 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
1275 timeout = 10 * 1000;
1276 while (timeout != 0)
1280 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1284 if ((prev & 0x03) == 0)
1304 temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1305 (((uint32_t)(buf[4])) << 4) |
1306 ((uint32_t)buf[5] >> 4));
1307 res = a_bmp280_compensate_temperature(handle, temperature_raw, &temperature_c);
1310 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1314 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
1315 (((int32_t)(buf[1])) << 4) |
1316 (((int32_t)(buf[2])) >> 4));
1317 res = a_bmp280_compensate_pressure(handle, *pressure_raw, pressure_pa);
1320 handle->
debug_print(
"bmp280: compensate pressure failed.\n");
1361 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1365 if ((prev & 0x3) == 3)
1374 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1375 (((uint32_t)(buf[4])) << 4) |
1376 ((uint32_t)buf[5] >> 4));
1377 res = a_bmp280_compensate_temperature(handle, *temperature_raw, temperature_c);
1380 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1389 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1397 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
1401 timeout = 10 * 1000;
1402 while (timeout != 0)
1406 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1410 if ((prev & 0x03) == 0)
1430 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1431 (((uint32_t)(buf[4])) << 4) |
1432 ((uint32_t)buf[5] >> 4));
1433 res = a_bmp280_compensate_temperature(handle, *temperature_raw, temperature_c);
1436 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1462 uint32_t *pressure_raw,
float *pressure_pa)
1480 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1484 if ((prev & 0x3) == 3)
1493 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1494 (((uint32_t)(buf[4])) << 4) |
1495 ((uint32_t)buf[5] >> 4));
1496 res = a_bmp280_compensate_temperature(handle, *temperature_raw, temperature_c);
1499 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1503 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
1504 (((int32_t)(buf[1])) << 4) |
1505 (((int32_t)(buf[2])) >> 4));
1506 res = a_bmp280_compensate_pressure(handle, *pressure_raw, pressure_pa);
1509 handle->
debug_print(
"bmp280: compensate pressure failed.\n");
1518 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1526 handle->
debug_print(
"bmp280: write ctrl meas failed.\n");
1530 timeout = 10 * 1000;
1531 while (timeout != 0)
1535 handle->
debug_print(
"bmp280: read ctrl meas failed.\n");
1539 if ((prev & 0x03) == 0)
1559 *temperature_raw = ((((uint32_t)(buf[3])) << 12) |
1560 (((uint32_t)(buf[4])) << 4) |
1561 ((uint32_t)buf[5] >> 4));
1562 res = a_bmp280_compensate_temperature(handle, *temperature_raw, temperature_c);
1565 handle->
debug_print(
"bmp280: compensate temperature failed.\n");
1569 *pressure_raw = ((((int32_t)(buf[0])) << 12) |
1570 (((int32_t)(buf[1])) << 4) |
1571 (((int32_t)(buf[2])) >> 4));
1572 res = a_bmp280_compensate_pressure(handle, *pressure_raw, pressure_pa);
1575 handle->
debug_print(
"bmp280: compensate pressure failed.\n");
1607 return a_bmp280_iic_spi_write(handle, reg, &value, 1);
1633 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_get_temperature_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t *oversampling)
get temperature oversampling
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_temperature_oversampling(bmp280_handle_t *handle, bmp280_oversampling_t oversampling)
set temperature oversampling
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_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_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