update
This commit is contained in:
parent
71381bb467
commit
793eb33f67
4 changed files with 142 additions and 112 deletions
|
@ -1,126 +1,145 @@
|
|||
// #include <stdio.h>
|
||||
|
||||
// void app_main(void)
|
||||
// {
|
||||
|
||||
// }
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/i2c.h"
|
||||
#include "bme680.h"
|
||||
#include "bme68x.h"
|
||||
|
||||
/* ----- I2C pins (ESP-01S: SDA=GPIO0, SCL=GPIO2) ----- */
|
||||
#define I2C_PORT I2C_NUM_0
|
||||
#define I2C_SDA_PIN 0 // GPIO0
|
||||
#define I2C_SCL_PIN 2 // GPIO2
|
||||
#define I2C_FREQ_HZ 100000
|
||||
#define BME680_ADDR BME680_I2C_ADDR_PRIMARY // 0x76 (SDO=GND)
|
||||
// #define BME680_ADDR BME680_I2C_ADDR_SECONDARY // 0x77 (SDO=VCC)
|
||||
#define I2C_SDA_PIN 0
|
||||
#define I2C_SCL_PIN 2
|
||||
#define I2C_TIMEOUT_MS 1000
|
||||
|
||||
// --- I2C helpers (ESP-IDF style) ---
|
||||
static int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) {
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (dev_id << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(cmd, reg_addr, true);
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (dev_id << 1) | I2C_MASTER_READ, true);
|
||||
if (len > 1) {
|
||||
i2c_master_read(cmd, data, len - 1, I2C_MASTER_ACK);
|
||||
}
|
||||
i2c_master_read_byte(cmd, data + len - 1, I2C_MASTER_NACK);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(I2C_PORT, cmd, pdMS_TO_TICKS(1000));
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return (ret == ESP_OK) ? BME680_OK : BME680_E_COM_FAIL;
|
||||
}
|
||||
/* BME68x address: SDO=GND -> LOW(0x76), SDO=VCC -> HIGH(0x77) */
|
||||
#define BME68X_ADDR BME68X_I2C_ADDR_LOW
|
||||
|
||||
static int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, const uint8_t *data, uint16_t len) {
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (dev_id << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(cmd, reg_addr, true);
|
||||
i2c_master_write(cmd, (uint8_t*)data, len, true);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(I2C_PORT, cmd, pdMS_TO_TICKS(1000));
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return (ret == ESP_OK) ? BME680_OK : BME680_E_COM_FAIL;
|
||||
}
|
||||
|
||||
static void delay_ms(uint32_t period_ms) { vTaskDelay(pdMS_TO_TICKS(period_ms)); }
|
||||
|
||||
static void i2c_init(void) {
|
||||
/* --------- ESP8266 I2C init (note: clk_stretch_tick field) --------- */
|
||||
static void i2c_init(void)
|
||||
{
|
||||
i2c_config_t conf = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = I2C_SDA_PIN,
|
||||
.scl_io_num = I2C_SCL_PIN,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = I2C_FREQ_HZ,
|
||||
.clk_stretch_tick = 300, // reasonable default on ESP8266
|
||||
};
|
||||
i2c_param_config(I2C_PORT, &conf);
|
||||
i2c_driver_install(I2C_PORT, conf.mode, 0, 0, 0);
|
||||
i2c_driver_install(I2C_PORT, conf.mode); // ESP8266 signature: (port, mode)
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
/* ------------- BME68x I2C read/write helpers ------------- */
|
||||
static int8_t bme68x_i2c_read(uint8_t reg, uint8_t *data, uint32_t len, void *intf_ptr)
|
||||
{
|
||||
uint8_t addr = *(uint8_t *)intf_ptr; // 7-bit address
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(cmd, reg, true);
|
||||
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_READ, true);
|
||||
if (len > 1) i2c_master_read(cmd, data, len - 1, I2C_MASTER_ACK);
|
||||
i2c_master_read_byte(cmd, data + len - 1, I2C_MASTER_NACK);
|
||||
i2c_master_stop(cmd);
|
||||
|
||||
esp_err_t ret = i2c_master_cmd_begin(I2C_PORT, cmd, I2C_TIMEOUT_MS / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return (ret == ESP_OK) ? BME68X_OK : BME68X_E_COM_FAIL;
|
||||
}
|
||||
|
||||
static int8_t bme68x_i2c_write(uint8_t reg, const uint8_t *data, uint32_t len, void *intf_ptr)
|
||||
{
|
||||
uint8_t addr = *(uint8_t *)intf_ptr;
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (addr << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(cmd, reg, true);
|
||||
i2c_master_write(cmd, (uint8_t*)data, len, true);
|
||||
i2c_master_stop(cmd);
|
||||
|
||||
esp_err_t ret = i2c_master_cmd_begin(I2C_PORT, cmd, I2C_TIMEOUT_MS / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return (ret == ESP_OK) ? BME68X_OK : BME68X_E_COM_FAIL;
|
||||
}
|
||||
|
||||
static void bme68x_delay_us(uint32_t period_us, void *intf_ptr)
|
||||
{
|
||||
(void)intf_ptr;
|
||||
/* Delay granularity is ms on FreeRTOS; round up */
|
||||
vTaskDelay((period_us + 999) / 1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
/* ----------------------- App ----------------------- */
|
||||
void app_main(void)
|
||||
{
|
||||
i2c_init();
|
||||
|
||||
struct bme680_dev dev = {0};
|
||||
dev.dev_id = BME680_ADDR;
|
||||
dev.intf = BME680_I2C_INTF;
|
||||
dev.read = i2c_read;
|
||||
dev.write = i2c_write;
|
||||
dev.delay_ms = delay_ms;
|
||||
struct bme68x_dev dev = {0};
|
||||
uint8_t i2c_addr = BME68X_ADDR;
|
||||
|
||||
if (bme680_init(&dev) != BME680_OK) {
|
||||
printf("BME680 init failed\n");
|
||||
dev.intf = BME68X_I2C_INTF;
|
||||
dev.read = bme68x_i2c_read;
|
||||
dev.write = bme68x_i2c_write;
|
||||
dev.delay_us = bme68x_delay_us;
|
||||
dev.intf_ptr = &i2c_addr;
|
||||
|
||||
if (bme68x_init(&dev) != BME68X_OK) {
|
||||
printf("BME68x init failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Oversampling / filter
|
||||
dev.tph_sett.os_hum = BME680_OS_2X;
|
||||
dev.tph_sett.os_pres = BME680_OS_4X;
|
||||
dev.tph_sett.os_temp = BME680_OS_8X;
|
||||
dev.tph_sett.filter = BME680_FILTER_SIZE_3;
|
||||
/* Oversampling + filter */
|
||||
struct bme68x_conf conf = {
|
||||
.os_hum = BME68X_OS_2X,
|
||||
.os_temp = BME68X_OS_8X,
|
||||
.os_pres = BME68X_OS_4X,
|
||||
.filter = BME68X_FILTER_SIZE_3,
|
||||
.odr = BME68X_ODR_NONE
|
||||
};
|
||||
if (bme68x_set_conf(&conf, &dev) != BME68X_OK) {
|
||||
printf("set_conf failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Gas sensor
|
||||
dev.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
|
||||
dev.gas_sett.heatr_temp = 320; // °C
|
||||
dev.gas_sett.heatr_dur = 150; // ms
|
||||
|
||||
uint8_t set_required_settings = BME680_OST_SEL | BME680_OSP_SEL |
|
||||
BME680_OSH_SEL | BME680_FILTER_SEL |
|
||||
BME680_GAS_SENSOR_SEL;
|
||||
|
||||
if (bme680_set_sensor_settings(set_required_settings, &dev) != BME680_OK) {
|
||||
printf("Sensor settings failed\n");
|
||||
/* Gas heater config for one-shot */
|
||||
struct bme68x_heatr_conf hconf = {
|
||||
.enable = BME68X_ENABLE,
|
||||
.heatr_temp = 320, // °C
|
||||
.heatr_dur = 150 // ms
|
||||
};
|
||||
if (bme68x_set_heatr_conf(BME68X_FORCED_MODE, &hconf, &dev) != BME68X_OK) {
|
||||
printf("set_heatr_conf failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
// Force mode, one-shot
|
||||
if (bme680_set_sensor_mode(&dev) != BME680_OK) {
|
||||
printf("Set mode failed\n");
|
||||
if (bme68x_set_op_mode(BME68X_FORCED_MODE, &dev) != BME68X_OK) {
|
||||
printf("set_op_mode failed\n");
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Wait for measurement time
|
||||
uint16_t meas_period;
|
||||
bme680_get_profile_dur(&meas_period, &dev);
|
||||
vTaskDelay(pdMS_TO_TICKS(meas_period));
|
||||
/* Calculate measurement duration and wait */
|
||||
uint32_t dur_us = bme68x_get_meas_dur(BME68X_FORCED_MODE, &conf, &dev) + (hconf.heatr_dur * 1000);
|
||||
bme68x_delay_us(dur_us, NULL);
|
||||
|
||||
struct bme68x_data data;
|
||||
uint8_t n_fields = 0;
|
||||
|
||||
if (bme68x_get_data(BME68X_FORCED_MODE, &data, &n_fields, &dev) == BME68X_OK && n_fields > 0) {
|
||||
/* Note: API returns fixed-point values; divide per defs */
|
||||
float temp_c = data.temperature / 100.0f; // °C
|
||||
float press_hpa = data.pressure / 100.0f; // hPa
|
||||
float hum_pct = data.humidity / 1000.0f; // %RH
|
||||
float gas_kohm = data.gas_resistance / 1000.0f;
|
||||
|
||||
struct bme680_field_data data;
|
||||
if (bme680_get_sensor_data(&data, &dev) == BME680_OK) {
|
||||
printf("T=%.2f °C RH=%.2f %% P=%.2f hPa Gas=%.2f kΩ\n",
|
||||
data.temperature / 100.0f,
|
||||
data.humidity / 1000.0f,
|
||||
data.pressure / 100.0f,
|
||||
data.gas_resistance / 1000.0f);
|
||||
temp_c, hum_pct, press_hpa, gas_kohm);
|
||||
} else {
|
||||
printf("Read failed\n");
|
||||
printf("read failed\n");
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue