bme680/bme680.h

162 lines
3.5 KiB
C
Raw Normal View History

2023-11-04 03:42:14 +00:00
#ifndef BME680_H
#define BME680
2023-09-17 20:31:02 +00:00
#include <stdint.h>
2023-11-04 17:08:47 +00:00
#define BME680_IS_SPI(m) ((m & 1) == 1)
2023-11-05 02:58:23 +00:00
#define BME680_IS_FLOAT(m) (((m >> 1) & 1) == 0)
#define BME680_GAS_ENABLED(m) (((m >> 2) & 1) == 1)
#define BME680_IDAC(c) (c << 1)
#define BME680_GAS_WAIT(val, scal) ((uint8_t)(((scal & 0b11) << 6) | (val & 0b111111)))
2023-11-04 17:08:47 +00:00
2023-11-04 03:42:14 +00:00
/* connection modes */
#define BME680_SPI 1
#define BME680_I2C 0
/* calculation modes; int or float calc */
#define BME680_MODE_INT 2
#define BME680_MODE_FLOAT 0
2023-11-05 02:58:23 +00:00
/* to enable gas conversion OR this into mode byte */
#define BME680_ENABLE_GAS 4
2023-11-04 03:42:14 +00:00
/* config values */
2023-11-04 20:37:39 +00:00
#define BME680_OVERSAMPLE_X1 0b001
#define BME680_OVERSAMPLE_X2 0b010
#define BME680_OVERSAMPLE_X4 0b011
#define BME680_OVERSAMPLE_X8 0b100
#define BME680_OVERSAMPLE_X16 0b101
2023-11-04 03:42:14 +00:00
/* IIR filter */
#define BME680_IIR_COEFF_0 0b000
#define BME680_IIR_COEFF_1 0b001
#define BME680_IIR_COEFF_3 0b010
#define BME680_IIR_COEFF_7 0b011
#define BME680_IIR_COEFF_15 0b100
#define BME680_IIR_COEFF_31 0b101
#define BME680_IIR_COEFF_63 0b110
#define BME680_IIR_COEFF_127 0b111
2023-11-05 02:58:23 +00:00
/* gas related values */
#define BME680_GAS_WAIT_X1 0b00
#define BME680_GAS_WAIT_X4 0b01
#define BME680_GAS_WAIT_X16 0b10
#define BME680_GAS_WAIT_X64 0b11
2023-11-04 03:42:14 +00:00
/* user supplied spi/i2c functions */
struct bme680_dev {
int (*init) (void);
int (*read) (uint8_t reg, uint8_t *dst, uint32_t size);
2023-11-04 20:37:39 +00:00
int (*write) (uint8_t reg, uint8_t value);
int (*sleep) (uint32_t dur_ms);
2023-11-04 03:42:14 +00:00
int (*deinit) (void);
};
struct bme680_cal {
/* temp calibration */
2023-09-17 20:31:02 +00:00
uint16_t par_t1;
2023-11-04 03:42:14 +00:00
int16_t par_t2;
int8_t par_t3;
2023-11-04 03:42:14 +00:00
/* press calibration */
2023-09-17 20:31:02 +00:00
uint16_t par_p1;
2023-11-04 03:42:14 +00:00
int16_t par_p2;
int8_t par_p3;
int16_t par_p4;
int16_t par_p5;
int8_t par_p6;
int8_t par_p7;
int16_t par_p8;
int16_t par_p9;
uint8_t par_p10;
/* humidity calibration */
2023-09-17 20:31:02 +00:00
uint16_t par_h1;
uint16_t par_h2;
2023-11-04 03:42:14 +00:00
int8_t par_h3;
int8_t par_h4;
int8_t par_h5;
uint8_t par_h6;
int8_t par_h7;
2023-11-05 02:58:23 +00:00
/* heater + gas */
2023-09-17 20:31:02 +00:00
uint16_t par_g1;
uint16_t par_g2;
uint16_t par_g3;
uint16_t range_switching_error;
2023-11-05 02:58:23 +00:00
uint8_t res_heat_range;
int8_t res_heat_val;
2023-11-04 03:42:14 +00:00
};
struct bme680_config {
uint8_t osrs_t;
uint8_t osrs_p;
uint8_t osrs_h;
2023-11-04 17:08:47 +00:00
uint8_t filter;
2023-11-05 02:58:23 +00:00
uint8_t idac_heat[10];
uint8_t res_heat[10];
uint8_t gas_wait[10];
uint8_t meas; /* required because you can't read back ctrl regs */
2023-11-04 03:42:14 +00:00
};
2023-09-17 20:31:02 +00:00
2023-11-04 03:42:14 +00:00
struct bme680_comp_float {
double tfine;
double temp;
double press;
double hum;
2023-11-05 02:58:23 +00:00
double gas_res;
2023-11-04 03:42:14 +00:00
};
2023-09-17 20:31:02 +00:00
2023-11-04 03:42:14 +00:00
struct bme680_comp_int {
int32_t tfine;
int32_t temp;
int32_t press;
int32_t hum;
2023-11-05 02:58:23 +00:00
int32_t gas_res;
2023-11-04 03:42:14 +00:00
};
2023-09-17 20:31:02 +00:00
2023-11-04 03:42:14 +00:00
struct bme680_adc {
uint32_t temp;
uint32_t press;
uint32_t hum;
2023-11-05 02:58:23 +00:00
uint32_t gas;
uint32_t gas_range;
2023-11-04 03:42:14 +00:00
};
2023-09-17 20:31:02 +00:00
2023-11-04 03:42:14 +00:00
struct bme680 {
struct bme680_comp_float fcomp;
struct bme680_comp_int icomp;
struct bme680_cal cal;
struct bme680_dev dev;
struct bme680_config cfg;
struct bme680_adc adc;
uint8_t mode;
2023-11-07 16:24:40 +00:00
uint8_t setpoint;
2023-11-04 17:08:47 +00:00
uint8_t spi_page;
uint8_t gas_valid;
uint8_t heat_stab;
2023-11-04 03:42:14 +00:00
};
2023-09-17 20:31:02 +00:00
2023-11-04 03:42:14 +00:00
typedef struct bme680 bme680_t;
2023-09-17 20:31:02 +00:00
2023-11-04 17:08:47 +00:00
int bme680_init(bme680_t *bme680, uint8_t mode);
int bme680_deinit(bme680_t *bme680);
int bme680_reset(bme680_t *bme680);
2023-11-04 03:42:14 +00:00
int bme680_calibrate(bme680_t *bme680);
2023-11-05 02:58:23 +00:00
int bme680_configure(bme680_t *bme680);
2023-11-04 17:08:47 +00:00
int bme680_start(bme680_t *bme680);
int bme680_poll(bme680_t *bme680);
int bme680_read(bme680_t *bme680);
2023-11-07 16:24:40 +00:00
int bme680_write_setpoint_index(bme680_t *bme680);
int bme680_read_setpoint_index(bme680_t *bme680, uint8_t *index);
2023-09-18 03:54:42 +00:00
2023-11-05 02:58:23 +00:00
uint8_t bme680_calc_target(bme680_t *bme680, double target, double ambient);
2023-09-18 03:54:42 +00:00
2023-11-04 17:08:47 +00:00
void bme680_print_calibration(bme680_t *bme680);
2023-09-18 03:54:42 +00:00
2023-11-04 03:42:14 +00:00
#endif