change a lot
This commit is contained in:
parent
b3783a13da
commit
23de126d97
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,2 +1,5 @@
|
||||
*.o
|
||||
bme680
|
||||
bme680
|
||||
bme680_i2c
|
||||
bme680_spi
|
||||
bme680_log*
|
41
Makefile
41
Makefile
@ -1,17 +1,38 @@
|
||||
CC=gcc
|
||||
OPT=-O2 -std=c99 -Wall -Wextra -W -pedantic
|
||||
CFLAGS=-I. $(OPT)
|
||||
CFILES=$(wildcard ./*.c)
|
||||
OBJECTS=$(patsubst %.c,%.o, $(CFILES))
|
||||
BINARY=bme680
|
||||
CFLAGS = -g -O0 -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wfloat-equal \
|
||||
-Wconversion -Wcast-align -Wpointer-arith -Wstrict-overflow=5 \
|
||||
-Wwrite-strings -Wcast-qual -Wswitch-default -Wswitch-enum \
|
||||
-Wunreachable-code -Wstrict-prototypes -Wmissing-prototypes \
|
||||
-Wmissing-declarations -Wredundant-decls -Wold-style-definition \
|
||||
-Winline -Wlogical-op -Wjump-misses-init -Wdouble-promotion \
|
||||
-Wformat-overflow=2 -Wformat-signedness -Wmissing-include-dirs \
|
||||
-Wnull-dereference -Wstack-usage=1024 -Wpacked -Woverlength-strings \
|
||||
-Wvla -Walloc-zero -Wduplicated-cond -Wduplicated-branches -Wrestrict \
|
||||
-fanalyzer -I. -std=c89
|
||||
|
||||
all: $(BINARY)
|
||||
CFILES = $(wildcard *.c)
|
||||
OBJECTS = $(CFILES:.c=.o)
|
||||
BIN = bme680
|
||||
|
||||
$(BINARY): $(OBJECTS)
|
||||
$(CC) $^ -o $@
|
||||
all: spi i2c
|
||||
|
||||
log: $(OBJECTS)
|
||||
@echo ">>> $(BIN)_log_spi"
|
||||
@$(CC) $(CFLAGS) -DUSE_SPI -std=gnu99 $^ cmd/log.c -o "$(BIN)_log_spi"
|
||||
@echo ">>> $(BIN)_log_i2c"
|
||||
@$(CC) $(CFLAGS) -DUSE_I2C -std=gnu99 $^ cmd/log.c -o "$(BIN)_log_i2c"
|
||||
|
||||
spi: $(OBJECTS)
|
||||
@echo ">>> $(BIN)_spi"
|
||||
@$(CC) $(CFLAGS) -DUSE_SPI $^ cmd/main.c -o "$(BIN)_spi"
|
||||
|
||||
i2c: $(OBJECTS)
|
||||
@echo ">>> $(BIN)_i2c"
|
||||
@$(CC) $(CFLAGS) -DUSE_I2C $^ cmd/main.c -o "$(BIN)_i2c"
|
||||
|
||||
%.o:%.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
@echo "cc $<"
|
||||
@$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
@rm -rf $(OBJECTS) $(BINARY)
|
||||
@rm -rf $(OBJECTS)
|
@ -14,6 +14,11 @@ Connecting the purple BME680 module board to SPI:
|
||||
| SCLK | "SCL" | GPIO 11 (Pin 23) |
|
||||
| CS | "CS" | GPIO 8 (Pin 24) |
|
||||
|
||||
### build
|
||||
Run `make` to build demo programs `bme680_spi` and `bme680_i2c` that use the pins/dev configured in `spi.c` and `i2c.c`
|
||||
|
||||
Also `make log` creates spi and i2c versions of the log program
|
||||
|
||||
## spi demo
|
||||
```
|
||||
par_t1: 26203
|
||||
|
70
bme680.c
70
bme680.c
@ -1,8 +1,6 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "bme680.h"
|
||||
#include "registers.h"
|
||||
|
||||
|
||||
static void calc_temp_comp(bme680_t *bme680);
|
||||
static void calc_press_comp(bme680_t *bme680);
|
||||
@ -152,9 +150,9 @@ int bme680_reset(bme680_t *bme680) {
|
||||
/********************************************************************/
|
||||
/* configure device */
|
||||
int bme680_configure(bme680_t *bme680) {
|
||||
uint8_t meas, hum, filter, ctrl_gas1, ctrl_gas0, err;
|
||||
int i;
|
||||
meas = hum = filter = err = 0;
|
||||
uint8_t meas, hum, filter, ctrl_gas1, ctrl_gas0, i;
|
||||
int err = 0;
|
||||
meas = hum = filter = 0;
|
||||
|
||||
/* ctrl_meas. the last 0 is ticked on to enable forced mode,
|
||||
* but the config has to be written first. strange behaviour.
|
||||
@ -299,12 +297,14 @@ static int const_array1_int[16] = {
|
||||
2147483647
|
||||
};
|
||||
|
||||
/* long int maybe */
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Woverflow"
|
||||
static int const_array2_int[16] = {
|
||||
4096000000, 2048000000, 1024000000, 512000000, 255744255,
|
||||
127110228, 64000000, 32258064, 16016016, 8000000, 4000000,
|
||||
2000000, 1000000, 500000, 250000, 125000
|
||||
};
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
/********************************************************************/
|
||||
/********************************************************************/
|
||||
@ -364,6 +364,8 @@ static void calc_press_comp_1 (bme680_t *bme680) {
|
||||
}
|
||||
|
||||
/********************************************************************/
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
static void calc_press_comp_2 (bme680_t *bme680 ) {
|
||||
int32_t var1, var2, var3, press_comp;
|
||||
|
||||
@ -375,7 +377,7 @@ static void calc_press_comp_2 (bme680_t *bme680 ) {
|
||||
((int32_t)bme680->cal.par_p3 << 5)) >> 3) + (((int32_t)bme680->cal.par_p2 * var1) >> 1);
|
||||
var1 = var1 >> 18;
|
||||
var1 = ((32768 + var1) * (int32_t)bme680->cal.par_p1) >> 15;
|
||||
press_comp = 1048576 - bme680->adc.press; // bosch code pg 19 says "press_raw" here ???
|
||||
press_comp = 1048576 - bme680->adc.press; /* bosch code pg 19 says "press_raw" here ??? */
|
||||
press_comp = (uint32_t)((press_comp - (var2 >> 12)) * ((uint32_t)3125));
|
||||
if (press_comp >= (1 << 30))
|
||||
press_comp = ((press_comp / (uint32_t)var1) << 1);
|
||||
@ -389,6 +391,7 @@ static void calc_press_comp_2 (bme680_t *bme680 ) {
|
||||
press_comp = (int32_t)(press_comp) + ((var1 + var2 + var3 + ((int32_t)bme680->cal.par_p7 << 7)) >> 4);
|
||||
bme680->icomp.press = press_comp;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
/********************************************************************/
|
||||
static void calc_press_comp (bme680_t *bme680) {
|
||||
@ -445,7 +448,7 @@ static void calc_hum_comp (bme680_t *bme680) {
|
||||
}
|
||||
|
||||
/********************************************************************/
|
||||
// TODO: read one big contiguous block
|
||||
/* TODO: read one big contiguous block */
|
||||
int bme680_calibrate(bme680_t *bme680) {
|
||||
uint8_t buffer[3] = {0, 0 ,0};
|
||||
int err = 0;
|
||||
@ -458,7 +461,7 @@ int bme680_calibrate(bme680_t *bme680) {
|
||||
bme680->cal.par_t2 = (buffer[1] << 8) | buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0x8C, buffer, 1);
|
||||
bme680->cal.par_t3 = buffer[0];
|
||||
bme680->cal.par_t3 = (int8_t)buffer[0];
|
||||
|
||||
|
||||
/* pressure */
|
||||
@ -469,7 +472,7 @@ int bme680_calibrate(bme680_t *bme680) {
|
||||
bme680->cal.par_p2 = (buffer[1] << 8) | buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0x92, buffer, 1);
|
||||
bme680->cal.par_p3 = buffer[0];
|
||||
bme680->cal.par_p3 = (int8_t)buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0x94, buffer, 2);
|
||||
bme680->cal.par_p4 = (buffer[1] << 8) | buffer[0];
|
||||
@ -478,10 +481,10 @@ int bme680_calibrate(bme680_t *bme680) {
|
||||
bme680->cal.par_p5 = (buffer[1] << 8) | buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0x99, buffer, 1);
|
||||
bme680->cal.par_p6 = buffer[0];
|
||||
bme680->cal.par_p6 = (int8_t)buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0x98, buffer, 1);
|
||||
bme680->cal.par_p7 = buffer[0];
|
||||
bme680->cal.par_p7 = (int8_t)buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0x9C, buffer, 1);
|
||||
bme680->cal.par_p8 = (buffer[1] << 8) | buffer[0];
|
||||
@ -501,19 +504,19 @@ int bme680_calibrate(bme680_t *bme680) {
|
||||
bme680->cal.par_h2 = (buffer[0] << 4) | ((buffer[1] >> 4) & 0xF);
|
||||
|
||||
err |= read_dev(bme680, 0xE4, buffer, 1);
|
||||
bme680->cal.par_h3 = buffer[0];
|
||||
bme680->cal.par_h3 = (int8_t)buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0xE5, buffer, 1);
|
||||
bme680->cal.par_h4 = buffer[0];
|
||||
bme680->cal.par_h4 = (int8_t)buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0xE6, buffer, 1);
|
||||
bme680->cal.par_h5 = buffer[0];
|
||||
bme680->cal.par_h5 = (int8_t)buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0xE7, buffer, 1);
|
||||
bme680->cal.par_h6 = buffer[0];
|
||||
|
||||
err |= read_dev(bme680, 0xE8, buffer, 1);
|
||||
bme680->cal.par_h7 = buffer[0];
|
||||
bme680->cal.par_h7 = (int8_t)buffer[0];
|
||||
|
||||
|
||||
/* gas */
|
||||
@ -533,7 +536,7 @@ int bme680_calibrate(bme680_t *bme680) {
|
||||
bme680->cal.res_heat_range = (buffer[0] >> 4) & 3;
|
||||
|
||||
err |= read_dev(bme680, 0x00, buffer, 1);
|
||||
bme680->cal.res_heat_val = buffer[0];
|
||||
bme680->cal.res_heat_val = (int8_t)buffer[0];
|
||||
|
||||
return err;
|
||||
}
|
||||
@ -568,35 +571,7 @@ static void calc_gas_res(bme680_t *bme680) {
|
||||
}
|
||||
}
|
||||
|
||||
/********************************************************************/
|
||||
void bme680_print_calibration (bme680_t *bme680) {
|
||||
printf("par_t1: %d\n", bme680->cal.par_t1);
|
||||
printf("par_t2: %d\n", bme680->cal.par_t2);
|
||||
printf("par_t3: %d\n", bme680->cal.par_t3);
|
||||
printf("par_p1: %d\n", bme680->cal.par_p1);
|
||||
printf("par_p2: %d\n", bme680->cal.par_p2);
|
||||
printf("par_p3: %d\n", bme680->cal.par_p3);
|
||||
printf("par_p4: %d\n", bme680->cal.par_p4);
|
||||
printf("par_p5: %d\n", bme680->cal.par_p5);
|
||||
printf("par_p6: %d\n", bme680->cal.par_p6);
|
||||
printf("par_p7: %d\n", bme680->cal.par_p7);
|
||||
printf("par_p8: %d\n", bme680->cal.par_p8);
|
||||
printf("par_p9: %d\n", bme680->cal.par_p9);
|
||||
printf("par_p10: %d\n", bme680->cal.par_p10);
|
||||
printf("par_h1: %d\n", bme680->cal.par_h1);
|
||||
printf("par_h2: %d\n", bme680->cal.par_h2);
|
||||
printf("par_h3: %d\n", bme680->cal.par_h3);
|
||||
printf("par_h4: %d\n", bme680->cal.par_h4);
|
||||
printf("par_h5: %d\n", bme680->cal.par_h5);
|
||||
printf("par_h6: %d\n", bme680->cal.par_h6);
|
||||
printf("par_h7: %d\n", bme680->cal.par_h7);
|
||||
printf("par_g1: %d\n", bme680->cal.par_g1);
|
||||
printf("par_g2: %d\n", bme680->cal.par_g2);
|
||||
printf("par_g3: %d\n", bme680->cal.par_g3);
|
||||
printf("range_switching_error: %d\n", bme680->cal.range_switching_error);
|
||||
printf("res_heat_range: %d\n", bme680->cal.res_heat_range);
|
||||
printf("res_heat_val: %d\n", bme680->cal.res_heat_val);
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************/
|
||||
static uint8_t calc_target_1(bme680_t *bme680, double target, double ambient) {
|
||||
@ -614,6 +589,8 @@ static uint8_t calc_target_1(bme680_t *bme680, double target, double ambient) {
|
||||
}
|
||||
|
||||
/********************************************************************/
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wfloat-conversion"
|
||||
static uint8_t calc_target_2(bme680_t *bme680, double target, double ambient) {
|
||||
int32_t var1, var2, var3, var4, var5, res_heat_x100;
|
||||
uint8_t res_heat;
|
||||
@ -628,6 +605,7 @@ static uint8_t calc_target_2(bme680_t *bme680, double target, double ambient) {
|
||||
res_heat = (uint8_t)((res_heat_x100 + 50) / 100);
|
||||
return res_heat;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
/********************************************************************/
|
||||
uint8_t bme680_calc_target(bme680_t *bme680, double target, double ambient) {
|
||||
|
72
bme680.h
72
bme680.h
@ -3,6 +3,75 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* REGISTERS */
|
||||
|
||||
/* confuses me too ! */
|
||||
/* SPI page number 0 => 0x00 to 0x7F */
|
||||
/* SPI page number 1 => 0x80 to 0xFF */
|
||||
#define REG_SPI_PAGE(v) ((v > 0x7F) ? 0 : 1)
|
||||
#define REG_SPI_PAGE_MAP(v) (v == 0 ? "LOW" : "HIGH")
|
||||
|
||||
#define REG_STATUS 0x73
|
||||
#define REG_RESET 0xE0
|
||||
#define REG_ID 0xD0
|
||||
#define REG_CONFIG 0x75
|
||||
#define REG_CTRL_MEAS 0x74
|
||||
#define REG_CTRL_HUM 0x72
|
||||
|
||||
#define REG_CTRL_GAS_1 0x71
|
||||
#define REG_CTRL_GAS_0 0x70
|
||||
|
||||
#define REG_GAS_WAIT_9 0x6D
|
||||
#define REG_GAS_WAIT_8 0x6C
|
||||
#define REG_GAS_WAIT_7 0x6B
|
||||
#define REG_GAS_WAIT_6 0x6A
|
||||
#define REG_GAS_WAIT_5 0x69
|
||||
#define REG_GAS_WAIT_4 0x68
|
||||
#define REG_GAS_WAIT_3 0x67
|
||||
#define REG_GAS_WAIT_2 0x66
|
||||
#define REG_GAS_WAIT_1 0x65
|
||||
#define REG_GAS_WAIT_0 0x64
|
||||
|
||||
#define REG_RES_HEAT_9 0x63
|
||||
#define REG_RES_HEAT_8 0x62
|
||||
#define REG_RES_HEAT_7 0x61
|
||||
#define REG_RES_HEAT_6 0x60
|
||||
#define REG_RES_HEAT_5 0x5F
|
||||
#define REG_RES_HEAT_4 0x5E
|
||||
#define REG_RES_HEAT_3 0x5D
|
||||
#define REG_RES_HEAT_2 0x5C
|
||||
#define REG_RES_HEAT_1 0x5B
|
||||
#define REG_RES_HEAT_0 0x5A
|
||||
|
||||
#define REG_IDAC_HEAT_9 0x59
|
||||
#define REG_IDAC_HEAT_8 0x58
|
||||
#define REG_IDAC_HEAT_7 0x57
|
||||
#define REG_IDAC_HEAT_6 0x56
|
||||
#define REG_IDAC_HEAT_5 0x55
|
||||
#define REG_IDAC_HEAT_4 0x54
|
||||
#define REG_IDAC_HEAT_3 0x53
|
||||
#define REG_IDAC_HEAT_2 0x52
|
||||
#define REG_IDAC_HEAT_1 0x51
|
||||
#define REG_IDAC_HEAT_0 0x50
|
||||
|
||||
#define REG_GAS_R_LSB 0x2B
|
||||
#define REG_GAS_R_MSB 0x2A
|
||||
|
||||
#define REG_HUM_LSB 0x26
|
||||
#define REG_HUM_MSB 0x25
|
||||
|
||||
#define REG_TEMP_XLSB 0x24
|
||||
#define REG_TEMP_LSB 0x23
|
||||
#define REG_TEMP_MSB 0x22
|
||||
|
||||
#define REG_PRESS_XLSB 0x21
|
||||
#define REG_PRESS_LSB 0x20
|
||||
#define REG_PRESS_MSB 0x1F
|
||||
|
||||
#define REG_MEAS_STATUS 0x1D
|
||||
|
||||
/* END OF REGISTERS */
|
||||
|
||||
#define BME680_IS_SPI(m) ((m & 1) == 1)
|
||||
#define BME680_IS_FLOAT(m) (((m >> 1) & 1) == 0)
|
||||
#define BME680_GAS_ENABLED(m) (((m >> 2) & 1) == 1)
|
||||
@ -142,7 +211,6 @@ struct bme680 {
|
||||
|
||||
typedef struct bme680 bme680_t;
|
||||
|
||||
|
||||
int bme680_init(bme680_t *bme680, uint8_t mode);
|
||||
int bme680_deinit(bme680_t *bme680);
|
||||
int bme680_reset(bme680_t *bme680);
|
||||
@ -156,6 +224,4 @@ int bme680_read_setpoint_index(bme680_t *bme680, uint8_t *index);
|
||||
|
||||
uint8_t bme680_calc_target(bme680_t *bme680, double target, double ambient);
|
||||
|
||||
void bme680_print_calibration(bme680_t *bme680);
|
||||
|
||||
#endif
|
||||
|
31
bme680_util.c
Normal file
31
bme680_util.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include "bme680_util.h"
|
||||
|
||||
void bme680_print_calibration (bme680_t *bme680) {
|
||||
printf("par_t1: %d\n", bme680->cal.par_t1);
|
||||
printf("par_t2: %d\n", bme680->cal.par_t2);
|
||||
printf("par_t3: %d\n", bme680->cal.par_t3);
|
||||
printf("par_p1: %d\n", bme680->cal.par_p1);
|
||||
printf("par_p2: %d\n", bme680->cal.par_p2);
|
||||
printf("par_p3: %d\n", bme680->cal.par_p3);
|
||||
printf("par_p4: %d\n", bme680->cal.par_p4);
|
||||
printf("par_p5: %d\n", bme680->cal.par_p5);
|
||||
printf("par_p6: %d\n", bme680->cal.par_p6);
|
||||
printf("par_p7: %d\n", bme680->cal.par_p7);
|
||||
printf("par_p8: %d\n", bme680->cal.par_p8);
|
||||
printf("par_p9: %d\n", bme680->cal.par_p9);
|
||||
printf("par_p10: %d\n", bme680->cal.par_p10);
|
||||
printf("par_h1: %d\n", bme680->cal.par_h1);
|
||||
printf("par_h2: %d\n", bme680->cal.par_h2);
|
||||
printf("par_h3: %d\n", bme680->cal.par_h3);
|
||||
printf("par_h4: %d\n", bme680->cal.par_h4);
|
||||
printf("par_h5: %d\n", bme680->cal.par_h5);
|
||||
printf("par_h6: %d\n", bme680->cal.par_h6);
|
||||
printf("par_h7: %d\n", bme680->cal.par_h7);
|
||||
printf("par_g1: %d\n", bme680->cal.par_g1);
|
||||
printf("par_g2: %d\n", bme680->cal.par_g2);
|
||||
printf("par_g3: %d\n", bme680->cal.par_g3);
|
||||
printf("range_switching_error: %d\n", bme680->cal.range_switching_error);
|
||||
printf("res_heat_range: %d\n", bme680->cal.res_heat_range);
|
||||
printf("res_heat_val: %d\n", bme680->cal.res_heat_val);
|
||||
}
|
8
bme680_util.h
Normal file
8
bme680_util.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef BME680_UTIL_H
|
||||
#define BME680_UTIL_H
|
||||
|
||||
#include "bme680.h"
|
||||
|
||||
void bme680_print_calibration (bme680_t *bme680);
|
||||
|
||||
#endif
|
120
cmd/log.c
Normal file
120
cmd/log.c
Normal file
@ -0,0 +1,120 @@
|
||||
#define _DEFAULT_SOURCE
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "bme680.h"
|
||||
#include "i2c.h"
|
||||
#include "spi.h"
|
||||
|
||||
#define AMBIENT_TEMP_GUESS 19.0
|
||||
#define HEATER_TARGET 300.0
|
||||
|
||||
int main(void) {
|
||||
|
||||
bme680_t bme680;
|
||||
uint8_t mode;
|
||||
double temperature = AMBIENT_TEMP_GUESS;
|
||||
time_t curr_time;
|
||||
char date[100];
|
||||
|
||||
#if defined(USE_SPI) && defined(USE_I2C)
|
||||
#error "Both USE_SPI and USE_I2C are defined. Please define only one."
|
||||
#elif !defined(USE_SPI) && !defined(USE_I2C)
|
||||
#error "Neither USE_SPI nor USE_I2C is defined. Please define one."
|
||||
#elif defined(USE_SPI)
|
||||
bme680.dev.init = spi_init;
|
||||
bme680.dev.read = spi_read;
|
||||
bme680.dev.write = spi_write;
|
||||
bme680.dev.deinit = spi_deinit;
|
||||
#elif defined(USE_I2C)
|
||||
bme680.dev.init = i2c_init;
|
||||
bme680.dev.read = i2c_read;
|
||||
bme680.dev.write = i2c_write;
|
||||
bme680.dev.deinit = i2c_deinit;
|
||||
#endif
|
||||
bme680.dev.sleep = usleep;
|
||||
|
||||
mode = BME680_MODE_FLOAT | BME680_ENABLE_GAS;
|
||||
#if defined(USE_SPI)
|
||||
mode |= BME680_SPI;
|
||||
#elif defined(USE_I2C)
|
||||
mode |= BME680_I2C;
|
||||
#endif
|
||||
|
||||
if (bme680_init(&bme680, mode) != 0) {
|
||||
fprintf(stderr, "bme680_init()\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
START:
|
||||
if (bme680_reset(&bme680) != 0) {
|
||||
fprintf(stderr, "bme680_reset()\n");
|
||||
bme680_deinit(&bme680);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (bme680_calibrate(&bme680) != 0) {
|
||||
fprintf(stderr, "bme680_calibrate()\n");
|
||||
bme680_deinit(&bme680);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
bme680.cfg.osrs_t = BME680_OVERSAMPLE_X16;
|
||||
bme680.cfg.osrs_p = BME680_OVERSAMPLE_X16;
|
||||
bme680.cfg.osrs_h = BME680_OVERSAMPLE_X8;
|
||||
bme680.cfg.filter = BME680_IIR_COEFF_127;
|
||||
|
||||
bme680.cfg.res_heat[0] = bme680_calc_target(&bme680, HEATER_TARGET, temperature);
|
||||
bme680.cfg.idac_heat[0] = BME680_IDAC(100);
|
||||
bme680.cfg.gas_wait[0] = BME680_GAS_WAIT(25, BME680_GAS_WAIT_X4);
|
||||
|
||||
bme680.setpoint = 0;
|
||||
|
||||
if (bme680_configure(&bme680) != 0) {
|
||||
fprintf(stderr, "bme680_configure()\n");
|
||||
bme680_deinit(&bme680);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (bme680_start(&bme680) != 0) {
|
||||
fprintf(stderr, "bme680_start()\n");
|
||||
bme680_deinit(&bme680);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (bme680_poll(&bme680) != 0) {
|
||||
fprintf(stderr, "bme680_poll()\n");
|
||||
bme680_deinit(&bme680);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (bme680_read(&bme680) != 0) {
|
||||
fprintf(stderr, "bme680_read()\n");
|
||||
bme680_deinit(&bme680);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
curr_time = time(NULL);
|
||||
strftime(date, 100, "%FT%T%z", localtime(&curr_time));
|
||||
printf("%s %g %g %g %g %d\n",
|
||||
date,
|
||||
bme680.fcomp.temp,
|
||||
bme680.fcomp.press,
|
||||
bme680.fcomp.hum,
|
||||
bme680.fcomp.gas_res,
|
||||
!!bme680.heat_stab);
|
||||
|
||||
temperature = bme680.fcomp.temp;
|
||||
|
||||
/* 60 secs */
|
||||
usleep(1000*1000*60);
|
||||
goto START;
|
||||
|
||||
bme680_deinit(&bme680);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "bme680.h"
|
||||
#include "bme680_util.h"
|
||||
#include "i2c.h"
|
||||
#include "spi.h"
|
||||
|
||||
@ -20,21 +21,33 @@ int main(void) {
|
||||
|
||||
/* 1. Assign functions for interacting with the device */
|
||||
|
||||
// bme680.dev.init = i2c_init;
|
||||
// bme680.dev.read = i2c_read;
|
||||
// bme680.dev.write = i2c_write;
|
||||
// bme680.dev.deinit = i2c_deinit;
|
||||
|
||||
bme680.dev.init = spi_init;
|
||||
#if defined(USE_SPI) && defined(USE_I2C)
|
||||
#error "Both USE_SPI and USE_I2C are defined. Please define only one."
|
||||
#elif !defined(USE_SPI) && !defined(USE_I2C)
|
||||
#error "Neither USE_SPI nor USE_I2C is defined. Please define one."
|
||||
#elif defined(USE_SPI)
|
||||
bme680.dev.init = spi_init;
|
||||
bme680.dev.read = spi_read;
|
||||
bme680.dev.write = spi_write;
|
||||
bme680.dev.deinit = spi_deinit;
|
||||
#elif defined(USE_I2C)
|
||||
bme680.dev.init = i2c_init;
|
||||
bme680.dev.read = i2c_read;
|
||||
bme680.dev.write = i2c_write;
|
||||
bme680.dev.deinit = i2c_deinit;
|
||||
#endif
|
||||
|
||||
|
||||
bme680.dev.sleep = usleep;
|
||||
|
||||
/* 2. set the device mode */
|
||||
mode = BME680_MODE_FLOAT | BME680_SPI | BME680_ENABLE_GAS;
|
||||
/* BME680_MODE_INT | BME680_I2C; */
|
||||
mode = BME680_MODE_FLOAT | BME680_ENABLE_GAS;
|
||||
#if defined(USE_SPI)
|
||||
mode |= BME680_SPI;
|
||||
#elif defined(USE_I2C)
|
||||
mode |= BME680_I2C;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* 3. initialise dev func's, and check device id */
|
31
i2c.c
31
i2c.c
@ -1,8 +1,4 @@
|
||||
/*
|
||||
|
||||
Example I2C use on linux/raspberry pi
|
||||
|
||||
*/
|
||||
/* Raspberry Pi i2c */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
@ -24,42 +20,47 @@ int i2c_init(void) {
|
||||
fd = open(I2C_DEVICE, O_RDWR);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "could not open device: %s\n", I2C_DEVICE);
|
||||
return I2C_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ioctl(fd, I2C_SLAVE, I2C_BME680_ADDRESS) < 0) {
|
||||
fprintf(stderr, "failed to acquire bus/talk to slave\n");
|
||||
close(fd);
|
||||
return I2C_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return I2C_OK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int i2c_read(uint8_t reg, uint8_t *dst, uint32_t size) {
|
||||
uint8_t cmd[2] = {reg, 0x00};
|
||||
uint8_t cmd[2];
|
||||
cmd[0] = reg;
|
||||
cmd[1] = 0x00;
|
||||
|
||||
write(fd, cmd, 2);
|
||||
|
||||
if (read(fd, dst, size) != (ssize_t)size) {
|
||||
fprintf(stderr, "error read()\n");
|
||||
return I2C_ERR;
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
return I2C_OK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int i2c_write(uint8_t reg, uint8_t value) {
|
||||
uint8_t cmd[2] = {reg, value};
|
||||
uint8_t cmd[2];
|
||||
cmd[0] = reg;
|
||||
cmd[1] = value;
|
||||
|
||||
if (write(fd, cmd, 2) != 2) {
|
||||
fprintf(stderr, "error write()\n");
|
||||
return I2C_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return I2C_OK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i2c_deinit(void) {
|
||||
@ -67,6 +68,6 @@ int i2c_deinit(void) {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
return I2C_OK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
3
i2c.h
3
i2c.h
@ -3,9 +3,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define I2C_OK 0
|
||||
#define I2C_ERR 1
|
||||
|
||||
int i2c_init (void);
|
||||
int i2c_read (uint8_t reg, uint8_t *dst, uint32_t size);
|
||||
int i2c_write (uint8_t reg, uint8_t value);
|
||||
|
70
registers.h
70
registers.h
@ -1,70 +0,0 @@
|
||||
#ifndef REGISTERS_H
|
||||
#define REGISTERS_H
|
||||
|
||||
/* SPI page number 0 => 0x00 to 0x7F */
|
||||
/* SPI page number 1 => 0x80 to 0xFF */
|
||||
#define REG_SPI_PAGE(v) ((v > 0x7F) ? 0 : 1)
|
||||
#define REG_SPI_PAGE_MAP(v) (v == 0 ? "LOW" : "HIGH")
|
||||
|
||||
#define REG_STATUS 0x73
|
||||
#define REG_RESET 0xE0
|
||||
#define REG_ID 0xD0
|
||||
#define REG_CONFIG 0x75
|
||||
#define REG_CTRL_MEAS 0x74
|
||||
#define REG_CTRL_HUM 0x72
|
||||
|
||||
#define REG_CTRL_GAS_1 0x71
|
||||
#define REG_CTRL_GAS_0 0x70
|
||||
|
||||
#define REG_GAS_WAIT_9 0x6D
|
||||
#define REG_GAS_WAIT_8 0x6C
|
||||
#define REG_GAS_WAIT_7 0x6B
|
||||
#define REG_GAS_WAIT_6 0x6A
|
||||
#define REG_GAS_WAIT_5 0x69
|
||||
#define REG_GAS_WAIT_4 0x68
|
||||
#define REG_GAS_WAIT_3 0x67
|
||||
#define REG_GAS_WAIT_2 0x66
|
||||
#define REG_GAS_WAIT_1 0x65
|
||||
#define REG_GAS_WAIT_0 0x64
|
||||
|
||||
#define REG_RES_HEAT_9 0x63
|
||||
#define REG_RES_HEAT_8 0x62
|
||||
#define REG_RES_HEAT_7 0x61
|
||||
#define REG_RES_HEAT_6 0x60
|
||||
#define REG_RES_HEAT_5 0x5F
|
||||
#define REG_RES_HEAT_4 0x5E
|
||||
#define REG_RES_HEAT_3 0x5D
|
||||
#define REG_RES_HEAT_2 0x5C
|
||||
#define REG_RES_HEAT_1 0x5B
|
||||
#define REG_RES_HEAT_0 0x5A
|
||||
|
||||
#define REG_IDAC_HEAT_9 0x59
|
||||
#define REG_IDAC_HEAT_8 0x58
|
||||
#define REG_IDAC_HEAT_7 0x57
|
||||
#define REG_IDAC_HEAT_6 0x56
|
||||
#define REG_IDAC_HEAT_5 0x55
|
||||
#define REG_IDAC_HEAT_4 0x54
|
||||
#define REG_IDAC_HEAT_3 0x53
|
||||
#define REG_IDAC_HEAT_2 0x52
|
||||
#define REG_IDAC_HEAT_1 0x51
|
||||
#define REG_IDAC_HEAT_0 0x50
|
||||
|
||||
#define REG_GAS_R_LSB 0x2B
|
||||
#define REG_GAS_R_MSB 0x2A
|
||||
|
||||
#define REG_HUM_LSB 0x26
|
||||
#define REG_HUM_MSB 0x25
|
||||
|
||||
#define REG_TEMP_XLSB 0x24
|
||||
#define REG_TEMP_LSB 0x23
|
||||
#define REG_TEMP_MSB 0x22
|
||||
|
||||
#define REG_PRESS_XLSB 0x21
|
||||
#define REG_PRESS_LSB 0x20
|
||||
#define REG_PRESS_MSB 0x1F
|
||||
|
||||
#define REG_MEAS_STATUS 0x1D
|
||||
|
||||
|
||||
|
||||
#endif
|
53
spi.c
53
spi.c
@ -1,8 +1,4 @@
|
||||
/*
|
||||
|
||||
Example SPI use on linux/raspberry pi
|
||||
|
||||
*/
|
||||
/* Raspberry Pi SPI */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
@ -35,52 +31,48 @@ Example SPI use on linux/raspberry pi
|
||||
|
||||
static int fd;
|
||||
|
||||
//static void print_access(const char *name, uint8_t reg, uint8_t *dst, uint32_t size);
|
||||
|
||||
int spi_init(void) {
|
||||
|
||||
uint8_t mode = SPI_MODE;
|
||||
uint8_t bits = SPI_BITS;
|
||||
uint32_t speed = SPI_SPEED;
|
||||
|
||||
if ((fd = open(SPI_DEVICE, O_RDWR)) < 0) {
|
||||
fprintf(stderr, "spi open(%s)\n", SPI_DEVICE);
|
||||
return SPI_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ioctl(fd, SPI_IOC_RD_MODE, &mode) == -1) {
|
||||
fprintf(stderr, "SPI_IOC_RD_MODE\n");
|
||||
return SPI_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
|
||||
fprintf(stderr, "SPI_IOC_WR_MODE\n");
|
||||
return SPI_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
|
||||
fprintf(stderr, "SPI_IOC_WR_BITS_PER_WORD\n");
|
||||
return SPI_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) == -1) {
|
||||
fprintf(stderr, "SPI_IOC_RD_BITS_PER_WORD\n");
|
||||
return SPI_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
|
||||
fprintf(stderr, "SPI_IOC_WR_MAX_SPEED_HZ\n");
|
||||
return SPI_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1) {
|
||||
fprintf(stderr, "SPI_IOC_RD_MAX_SPEED_HZ\n");
|
||||
return SPI_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// puts("spi_init");
|
||||
return SPI_OK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* bme680 auto-increments for spi reads */
|
||||
@ -101,11 +93,10 @@ int spi_read(uint8_t reg, uint8_t *dst, uint32_t size) {
|
||||
|
||||
if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) {
|
||||
fprintf(stderr, "spi_read()\n");
|
||||
return SPI_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// print_access("spi_read", reg, dst, size);
|
||||
return SPI_OK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* bme680 does NOT auto-increment for spi writes, so one at a time */
|
||||
@ -126,32 +117,16 @@ int spi_write(uint8_t reg, uint8_t value) {
|
||||
|
||||
if (ioctl(fd, SPI_IOC_MESSAGE(2), tr) < 0) {
|
||||
fprintf(stderr, "spi_write()\n");
|
||||
return SPI_ERR;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// print_access("spi_write", reg, &value, 1);
|
||||
return SPI_OK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int spi_deinit(void) {
|
||||
// puts("spi_deinit");
|
||||
if (fd) {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
return SPI_OK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
static void print_access(const char *name, uint8_t reg, uint8_t *dst, uint32_t size) {
|
||||
printf("%s: %.2X (%d) [", name, reg, size);
|
||||
for(uint32_t i=0; i<size; i++) {
|
||||
printf("%.2X", dst[i]);
|
||||
if (i < (size - 1)) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
*/
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user