#include #include #include #include "bme680-driver-fork/bme680.h" static struct bme680_dev gas_sensor; static uint8_t spi_transfer(uint8_t out_data) { while(SPI1->SR & SPI_SR_BSY); SPI1->DR.DR8.DR8_1 = out_data; __DSB(); while((SPI1->SR & SPI_SR_BSY) || !(SPI1->SR & SPI_SR_TXE)); return (uint8_t)(SPI1->DR.DR8.DR8_1 & 0xFF); } static int8_t write_spi(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { (void)dev_id; int i; GPIOA->ODR &= ~(1U<<4); spi_transfer(reg_addr); for (i = 0; i < len; i++) spi_transfer(reg_data[i]); GPIOA->ODR |= (1U<<4); return 0; } static int8_t read_spi(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { (void)dev_id; int i; GPIOA->ODR &= ~(1U<<4); spi_transfer(reg_addr); for (i = 0; i < len; i++) reg_data[i] = spi_transfer(0x00); GPIOA->ODR |= (1U<<4); return 0; } int meas_init_without_gas(void) { int ret; gas_sensor.dev_id = 0; gas_sensor.intf = BME680_SPI_INTF; gas_sensor.read = read_spi; gas_sensor.write = write_spi; gas_sensor.delay_ms = delay_ms; gas_sensor.amb_temp = 25; ret = (int)bme680_init(&gas_sensor); if (ret) return ret; uint8_t set_required_settings; /* Set the temperature, pressure and humidity settings */ gas_sensor.tph_sett.os_hum = BME680_OS_8X; gas_sensor.tph_sett.os_pres = BME680_OS_8X; gas_sensor.tph_sett.os_temp = BME680_OS_8X; gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_7; /* Set the remaining gas sensor settings and link the heating profile */ gas_sensor.gas_sett.run_gas = BME680_DISABLE_GAS_MEAS; /* Create a ramp heat waveform in 3 steps */ gas_sensor.gas_sett.heatr_temp = 320; /* degree Celsius */ gas_sensor.gas_sett.heatr_dur = 150; /* milliseconds */ /* Select the power mode */ /* Must be set before writing the sensor configuration */ gas_sensor.power_mode = BME680_FORCED_MODE; /* Set the required sensor settings needed */ set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL | BME680_GAS_SENSOR_SEL; /* Set the desired sensor configuration */ ret = (int)bme680_set_sensor_settings(set_required_settings,&gas_sensor); return ret; } int meas_do(bool gas_enable, struct bme680_field_data *meas_data, int8_t ambient_temp) { uint16_t meas_duration; int res; if (!meas_data) return -100; gas_sensor.power_mode = BME680_FORCED_MODE; gas_sensor.gas_sett.run_gas = (gas_enable ? BME680_RUN_GAS_ENABLE : BME680_RUN_GAS_DISABLE); gas_sensor.amb_temp = ambient_temp; res = (int)bme680_set_sensor_settings(BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL | BME680_GAS_SENSOR_SEL, &gas_sensor); /* Set the power mode */ res = (int)bme680_set_sensor_mode(&gas_sensor); if (res) return res; bme680_get_profile_dur(&meas_duration, &gas_sensor); delay_ms(meas_duration); res = (int)bme680_get_sensor_data(meas_data, &gas_sensor); if (res) return res; gas_sensor.power_mode = BME680_SLEEP_MODE; res = (int)bme680_set_sensor_mode(&gas_sensor); return res; }