BME280: Code refactor, added NORMAL mode.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
parent
242625c01b
commit
60df7deccb
|
@ -43,5 +43,5 @@ target_compile_definitions(${PROJECT_NAME}
|
|||
)
|
||||
|
||||
if(IMSENSORS_FLOAT_SUPPORT)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE ${IMSENSORS_FLOAT_DEFS})
|
||||
target_compile_definitions(${PROJECT_NAME} PUBLIC ${IMSENSORS_FLOAT_DEFS})
|
||||
endif()
|
|
@ -4,9 +4,9 @@
|
|||
#include "imsensors/common/sensors_common.h"
|
||||
|
||||
typedef enum {
|
||||
IMS_BME280_MODE_SLEEP,
|
||||
IMS_BME280_MODE_FORCED,
|
||||
IMS_BME280_MODE_NORMAL,
|
||||
IMS_BME280_MODE_SLEEP = 0x00U,
|
||||
IMS_BME280_MODE_FORCED = 0x01U,
|
||||
IMS_BME280_MODE_NORMAL = 0x03U,
|
||||
} ims_bme280_mode_t;
|
||||
|
||||
typedef enum {
|
||||
|
@ -26,11 +26,23 @@ typedef enum {
|
|||
IMS_BME280_FILTER_16,
|
||||
} ims_bme280_fiter_config_t;
|
||||
|
||||
typedef enum {
|
||||
IMS_BME280_STANDBY_TIME_0_5_MS = 0x00U,
|
||||
IMS_BME280_STANDBY_TIME_62_5_MS = 0x01U,
|
||||
IMS_BME280_STANDBY_TIME_125_MS = 0x02U,
|
||||
IMS_BME280_STANDBY_TIME_250_MS = 0x03U,
|
||||
IMS_BME280_STANDBY_TIME_500_MS = 0x04U,
|
||||
IMS_BME280_STANDBY_TIME_1_SEC = 0x05U,
|
||||
IMS_BME280_STANDBY_TIME_10_MS = 0x06U,
|
||||
IMS_BME280_STANDBY_TIME_20_MS = 0x07U,
|
||||
} ims_bme280_standby_time_t;
|
||||
|
||||
typedef enum {
|
||||
IMS_BME280_PRESET_WEATHER,
|
||||
IMS_BME280_PRESET_HUMIDITY,
|
||||
IMS_BME280_PRESET_INDOOR_NAV,
|
||||
IMS_BME280_PRESET_GAMING,
|
||||
IMS_BME280_PRESET_END,
|
||||
} ims_bme280_mode_preset_t;
|
||||
|
||||
typedef struct {
|
||||
|
@ -59,23 +71,21 @@ typedef struct {
|
|||
ims_bme280_osrs_config_t osrs_t;
|
||||
ims_bme280_osrs_config_t osrs_p;
|
||||
ims_bme280_osrs_config_t osrs_h;
|
||||
uint8_t t_sb;
|
||||
ims_bme280_standby_time_t t_sb;
|
||||
ims_bme280_fiter_config_t filter;
|
||||
} ims_bme280_config_t;
|
||||
|
||||
#ifdef IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
typedef struct {
|
||||
#ifdef IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
double temperature;
|
||||
double pressure;
|
||||
double humidity;
|
||||
} ims_bme280_result_t;
|
||||
#else
|
||||
typedef struct {
|
||||
uint32_t pressure; /* Pressure (Pa) multiplied by 256. */
|
||||
int32_t temperature; /* Temperature (C), multiplied by 100. */
|
||||
uint32_t pressure; /* Humidity (%), multiplied by 1024. */
|
||||
int32_t humidity; /* Pressure (Pa) multiplied by 256. */
|
||||
} ims_bme280_result_t;
|
||||
uint32_t humidity; /* Humidity (%), multiplied by 1024. */
|
||||
#endif
|
||||
} ims_bme280_result_t;
|
||||
|
||||
typedef struct {
|
||||
ims_i2c_xfer_t i2c_xfer;
|
||||
|
@ -91,12 +101,9 @@ typedef struct {
|
|||
|
||||
ims_ret_t ims_bme280_init(ims_bme280_t *bme);
|
||||
ims_ret_t ims_bme280_preset_config(ims_bme280_config_t *config, ims_bme280_mode_preset_t preset);
|
||||
ims_ret_t ims_bme280_apply_config(ims_bme280_t *bme, ims_bme280_config_t *config);
|
||||
ims_ret_t ims_bme280_config(ims_bme280_t *bme, ims_bme280_config_t *config);
|
||||
|
||||
#ifdef IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
ims_ret_t ims_bme280_measure(ims_bme280_t *bme, ims_bme280_result_t *result);
|
||||
#else
|
||||
ims_ret_t ims_bme280_measure(ims_bme280_t *bme, ims_bme280_result_t *result);
|
||||
#endif
|
||||
ims_ret_t ims_bme280_read(ims_bme280_t *bme, ims_bme280_result_t *result);
|
||||
ims_ret_t ims_bme280_read_forced(ims_bme280_t *bme, ims_bme280_result_t *result);
|
||||
|
||||
#endif // IMS_BME280_DHT_H
|
|
@ -3,6 +3,56 @@
|
|||
|
||||
#include "imsensors/common/sensors_common.h"
|
||||
|
||||
typedef enum {
|
||||
IMS_LSM6DSL_XL_ODR_POWER_DOWN = 0x00U, /* Power Down */
|
||||
IMS_LSM6DSL_XL_ODR_1_6_HZ = 0x0BU | (1U << 4U), /* 1.6Hz, low power mode only */
|
||||
IMS_LSM6DSL_XL_ODR_12_5_HZ = 0x01U, /* 12.5Hz */
|
||||
IMS_LSM6DSL_XL_ODR_26_HZ = 0x02U, /* 26Hz */
|
||||
IMS_LSM6DSL_XL_ODR_52_HZ = 0x03U, /* 52Hz */
|
||||
IMS_LSM6DSL_XL_ODR_104_HZ = 0x04U, /* 104Hz */
|
||||
IMS_LSM6DSL_XL_ODR_208_HZ = 0x05U, /* 208Hz */
|
||||
IMS_LSM6DSL_XL_ODR_416_HZ = 0x06U, /* 416Hz */
|
||||
IMS_LSM6DSL_XL_ODR_833_HZ = 0x07U, /* 833Hz */
|
||||
IMS_LSM6DSL_XL_ODR_1_66_KHZ = 0x08U, /* 1.66kHz */
|
||||
IMS_LSM6DSL_XL_ODR_3_33_KHZ = 0x09U, /* 3.3kHz */
|
||||
IMS_LSM6DSL_XL_ODR_6_66_KHZ = 0x0AU, /* 6.66kHz */
|
||||
} ims_lsm6dsl_xl_odr_t;
|
||||
|
||||
typedef enum {
|
||||
IMS_LSM6DSL_GYRO_ODR_POWER_DOWN = 0x00U, /* Power down */
|
||||
IMS_LSM6DSL_GYRO_ODR_12_5_HZ = 0x01U, /* 12.5Hz */
|
||||
IMS_LSM6DSL_GYRO_ODR_26_HZ = 0x02U, /* 26Hz */
|
||||
IMS_LSM6DSL_GYRO_ODR_52_HZ = 0x03U, /* 52Hz */
|
||||
IMS_LSM6DSL_GYRO_ODR_104_HZ = 0x04U, /* 104Hz */
|
||||
IMS_LSM6DSL_GYRO_ODR_208_HZ = 0x05U, /* 208Hz */
|
||||
IMS_LSM6DSL_GYRO_ODR_416_HZ = 0x06U, /* 416Hz */
|
||||
IMS_LSM6DSL_GYRO_ODR_833_HZ = 0x07U, /* 833Hz */
|
||||
IMS_LSM6DSL_GYRO_ODR_1_66_KHZ = 0x08U, /* 1.66kHz */
|
||||
IMS_LSM6DSL_GYRO_ODR_3_33_KHZ = 0x09U, /* 3.33kHz */
|
||||
IMS_LSM6DSL_GYRO_ODR_6_66_KHZ = 0x0AU, /* 6.66kHz */
|
||||
} ims_lsm6dsl_gyro_odr_t;
|
||||
|
||||
typedef enum {
|
||||
IMS_LSM6DSL_XL_LPF_BW_0 = 0x00U,
|
||||
IMS_LSM6DSL_XL_LPF_BW_1 = 0x01U,
|
||||
IMS_LSM6DSL_XL_LPF_BW_2 = 0x02U,
|
||||
IMS_LSM6DSL_XL_LPF_BW_3 = 0x03U,
|
||||
} ims_lsm6dsl_xl_lpf_config_t;
|
||||
|
||||
typedef struct {
|
||||
ims_lsm6dsl_xl_odr_t odr;
|
||||
ims_lsm6dsl_xl_lpf_config_t lpf_config;
|
||||
} ims_lsm6dsl_xl_config_t;
|
||||
|
||||
typedef struct {
|
||||
ims_lsm6dsl_gyro_odr_t odr;
|
||||
} ims_lsm6dsl_gyro_config_t;
|
||||
|
||||
typedef struct {
|
||||
ims_lsm6dsl_xl_config_t xl_config;
|
||||
ims_lsm6dsl_gyro_config_t gyro_config;
|
||||
} ims_lsm6dsl_config_t;
|
||||
|
||||
typedef struct {
|
||||
ims_i2c_xfer_t i2c_xfer;
|
||||
ims_delay_t delay;
|
||||
|
@ -14,6 +64,7 @@ typedef struct {
|
|||
} ims_lsm6dsl_t;
|
||||
|
||||
ims_ret_t ims_lsm6dsl_init(ims_lsm6dsl_t *imu);
|
||||
ims_ret_t ims_lsm6dsl_config(ims_lsm6dsl_t *imu, ims_lsm6dsl_config_t *config);
|
||||
ims_ret_t ims_lsm6dsl_read_temperature(ims_lsm6dsl_t *imu, int16_t *temp);
|
||||
|
||||
#endif // IMS_IMU_LSM6DSL_H
|
||||
|
|
|
@ -6,18 +6,305 @@
|
|||
/* Private */
|
||||
#include "dht_bme280.h"
|
||||
|
||||
#define IMS_BME280_REG_DIG_T1_L (0x88U)
|
||||
#define IMS_BME280_REG_DIG_T1_H (0x89U)
|
||||
#define IMS_BME280_REG_DIG_T2_L (0x8AU)
|
||||
#define IMS_BME280_REG_DIG_T2_H (0x8BU)
|
||||
#define IMS_BME280_REG_DIG_T3_L (0x8CU)
|
||||
#define IMS_BME280_REG_DIG_T3_H (0x8DU)
|
||||
#define IMS_BME280_REG_DIG_P1_L (0x8EU)
|
||||
#define IMS_BME280_REG_DIG_P1_H (0x8FU)
|
||||
#define IMS_BME280_REG_DIG_P2_L (0x90U)
|
||||
#define IMS_BME280_REG_DIG_P2_H (0x91U)
|
||||
#define IMS_BME280_REG_DIG_P3_L (0x92U)
|
||||
#define IMS_BME280_REG_DIG_P3_H (0x93U)
|
||||
#define IMS_BME280_REG_DIG_P4_L (0x94U)
|
||||
#define IMS_BME280_REG_DIG_P4_H (0x95U)
|
||||
#define IMS_BME280_REG_DIG_P5_L (0x96U)
|
||||
#define IMS_BME280_REG_DIG_P5_H (0x97U)
|
||||
#define IMS_BME280_REG_DIG_P6_L (0x98U)
|
||||
#define IMS_BME280_REG_DIG_P6_H (0x99U)
|
||||
#define IMS_BME280_REG_DIG_P7_L (0x9AU)
|
||||
#define IMS_BME280_REG_DIG_P7_H (0x9BU)
|
||||
#define IMS_BME280_REG_DIG_P8_L (0x9CU)
|
||||
#define IMS_BME280_REG_DIG_P8_H (0x9DU)
|
||||
#define IMS_BME280_REG_DIG_P9_L (0x9EU)
|
||||
#define IMS_BME280_REG_DIG_P9_H (0x9FU)
|
||||
#define IMS_BME280_REG_DIG_H1 (0xA1U)
|
||||
#define IMS_BME280_REG_DIG_H2_L (0xE1U)
|
||||
#define IMS_BME280_REG_DIG_H2_H (0xE2U)
|
||||
#define IMS_BME280_REG_DIG_H3 (0xE3U)
|
||||
#define IMS_BME280_REG_DIG_H4_H (0xE4U)
|
||||
#define IMS_BME280_REG_DIG_H4_L (0xE5U)
|
||||
#define IMS_BME280_REG_DIG_H5_L (0xE5U)
|
||||
#define IMS_BME280_REG_DIG_H5_H (0xE6U)
|
||||
#define IMS_BME280_REG_DIG_H6 (0xE7U)
|
||||
|
||||
#define IMS_BME280_REG_CTRL_HUM (0xF2U)
|
||||
#define IMS_BME280_REG_STATUS (0xF3U)
|
||||
#define IMS_BME280_REG_CTRL_MEAS (0xF4U)
|
||||
#define IMS_BME280_REG_CONFIG (0xF5U)
|
||||
#define IMS_BME280_REG_PRESS_MSB (0xF7U)
|
||||
|
||||
#define IMS_BME280_REG_STATUS_MEASURING_Pos (3U)
|
||||
#define IMS_BME280_REG_STATUS_MEASURING_Msk (1U << IMS_BME280_REG_STATUS_MEASURING_Pos)
|
||||
|
||||
#define IMS_BME280_REG_CTRL_MEAS_MODE_Pos (0U)
|
||||
#define IMS_BME280_REG_CTRL_MEAS_MODE_Msk (3U << IMS_BME280_REG_CTRL_MEAS_MODE_Pos)
|
||||
|
||||
#define IMS_BME280_FORCED_TIMEOUT_MS 100
|
||||
|
||||
#define IMS_BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)
|
||||
|
||||
static const ims_bme280_config_t ims_bme280_presets[] = {
|
||||
[IMS_BME280_PRESET_WEATHER] =
|
||||
{
|
||||
.mode = IMS_BME280_MODE_FORCED,
|
||||
.osrs_t = IMS_BME280_OSRS_1,
|
||||
.osrs_p = IMS_BME280_OSRS_1,
|
||||
.osrs_h = IMS_BME280_OSRS_1,
|
||||
.filter = IMS_BME280_FILTER_OFF,
|
||||
},
|
||||
[IMS_BME280_PRESET_HUMIDITY] =
|
||||
{
|
||||
.mode = IMS_BME280_MODE_FORCED,
|
||||
.osrs_t = IMS_BME280_OSRS_1,
|
||||
.osrs_p = IMS_BME280_OSRS_SKIP,
|
||||
.osrs_h = IMS_BME280_OSRS_1,
|
||||
.filter = IMS_BME280_FILTER_OFF,
|
||||
},
|
||||
[IMS_BME280_PRESET_INDOOR_NAV] =
|
||||
{
|
||||
.mode = IMS_BME280_MODE_NORMAL,
|
||||
.osrs_t = IMS_BME280_OSRS_2,
|
||||
.osrs_p = IMS_BME280_OSRS_16,
|
||||
.osrs_h = IMS_BME280_OSRS_1,
|
||||
.t_sb = IMS_BME280_STANDBY_TIME_0_5_MS,
|
||||
.filter = IMS_BME280_FILTER_16,
|
||||
},
|
||||
[IMS_BME280_PRESET_GAMING] =
|
||||
{
|
||||
.mode = IMS_BME280_MODE_NORMAL,
|
||||
.osrs_t = IMS_BME280_OSRS_1,
|
||||
.osrs_p = IMS_BME280_OSRS_4,
|
||||
.osrs_h = IMS_BME280_OSRS_SKIP,
|
||||
.t_sb = IMS_BME280_STANDBY_TIME_0_5_MS,
|
||||
.filter = IMS_BME280_FILTER_4,
|
||||
},
|
||||
};
|
||||
|
||||
static ims_ret_t ims_bme280_read_register(ims_bme280_t *bme, uint8_t reg, uint8_t *data, uint16_t len);
|
||||
static ims_ret_t ims_bme280_write_register(ims_bme280_t *bme, uint8_t reg, uint8_t data);
|
||||
|
||||
#ifdef IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
|
||||
static double ims_bme280_compensate_T_double(ims_bme280_t *bme, int32_t adc_T);
|
||||
static double ims_bme280_compensate_P_double(ims_bme280_t *bme, int32_t adc_P);
|
||||
static double ims_bme280_compensate_H_double(ims_bme280_t *bme, int32_t adc_H);
|
||||
|
||||
#else
|
||||
|
||||
static int32_t ims_bme280_compensate_T_int32(ims_bme280_t *bme, int32_t adc_T);
|
||||
static uint32_t ims_bme280_compensate_P_int64(ims_bme280_t *bme, int32_t adc_P);
|
||||
static uint32_t ims_bme280_compensate_H_int32(ims_bme280_t *bme, int32_t adc_H);
|
||||
|
||||
#endif
|
||||
|
||||
static ims_ret_t ims_bme280_reset(ims_bme280_t *bme) {
|
||||
return ims_bme280_write_register(bme, 0xE0, 0xB6);
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme280_read_trim_data(ims_bme280_t *bme) {
|
||||
ims_ret_t ret;
|
||||
uint8_t rx_buf[2];
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_T1_L, rx_buf, 0x02); // T1
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_T1 = IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_T2_L, rx_buf, 0x02); // T2
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_T2 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_T3_L, rx_buf, 0x02); // T3
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_T3 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_P1_L, rx_buf, 0x02); // P1
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P1 = IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_P2_L, rx_buf, 0x02); // P2
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P2 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_P3_L, rx_buf, 0x02); // P3
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P3 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_P4_L, rx_buf, 0x02); // P4
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P4 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_P5_L, rx_buf, 0x02); // P5
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P5 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_P6_L, rx_buf, 0x02); // P6
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P6 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_P7_L, rx_buf, 0x02); // P7
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P7 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_P8_L, rx_buf, 0x02); // P8
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P8 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_P9_L, rx_buf, 0x02); // P9
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P9 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_H1, rx_buf, 0x01); // H1
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H1 = rx_buf[0];
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_H2_L, rx_buf, 0x02); // H2
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H2 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_H3, rx_buf, 0x01); // H3
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H3 = (uint8_t)rx_buf[0];
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_H4_H, rx_buf, 0x02); // H4
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H4 = (int16_t)(((int8_t)rx_buf[0] << 0x04) | (rx_buf[1] & 0x0F));
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_H5_L, rx_buf, 0x02); // H5
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H5 = (int16_t)(((int8_t)rx_buf[1] << 0x04) | (rx_buf[0] >> 0x04));
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_DIG_H6, rx_buf, 0x01); // H6
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H6 = (int8_t)rx_buf[0];
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme280_set_mode(ims_bme280_t *bme, ims_bme280_mode_t mode) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t ctrl_meas;
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_CTRL_MEAS, &ctrl_meas, 1U);
|
||||
if (ret != IMS_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (((ctrl_meas & IMS_BME280_REG_CTRL_MEAS_MODE_Msk) >> IMS_BME280_REG_CTRL_MEAS_MODE_Pos) == 0x00U) {
|
||||
ctrl_meas |= ((mode << IMS_BME280_REG_CTRL_MEAS_MODE_Pos) & IMS_BME280_REG_CTRL_MEAS_MODE_Msk);
|
||||
}
|
||||
|
||||
ret = ims_bme280_write_register(bme, IMS_BME280_REG_CTRL_MEAS, ctrl_meas);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme_get_mode(ims_bme280_t *bme, ims_bme280_mode_t *mode) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t ctrl_meas;
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_CTRL_MEAS, &ctrl_meas, 1U);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
*mode = (ctrl_meas >> IMS_BME280_REG_CTRL_MEAS_MODE_Pos) & IMS_BME280_REG_CTRL_MEAS_MODE_Msk;
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme_wait_measure_complete(ims_bme280_t *bme, uint32_t timeout_ms) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t loop_count = 0U;
|
||||
|
||||
uint8_t status;
|
||||
do {
|
||||
bme->cb.delay(bme->pdev, 5);
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_STATUS, &status, 0x01);
|
||||
if (ret != IMS_SUCCESS) return IMS_FAIL;
|
||||
|
||||
loop_count++;
|
||||
|
||||
if (loop_count * 5 > timeout_ms) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
} while ((status & IMS_BME280_REG_STATUS_MEASURING_Msk) == 0U);
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme280_read_raw_data(ims_bme280_t *bme, uint32_t *raw_t, uint32_t *raw_p, uint32_t *raw_h) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t measured_data[8];
|
||||
|
||||
ret = ims_bme280_read_register(bme, IMS_BME280_REG_PRESS_MSB, measured_data, 0x08);
|
||||
if (ret != IMS_SUCCESS) return IMS_FAIL;
|
||||
|
||||
*raw_p = (measured_data[0] << 12) | (measured_data[1] << 0x04) | (measured_data[2] >> 0x04);
|
||||
*raw_t = (measured_data[3] << 12) | (measured_data[4] << 0x04) | (measured_data[5] >> 0x04);
|
||||
*raw_h = (measured_data[6] << 8) | (measured_data[7]);
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_bme280_init(ims_bme280_t *bme) {
|
||||
if (!bme) return IMS_FAIL;
|
||||
if (ims_bme280_reset(bme) == IMS_FAIL) return IMS_FAIL;
|
||||
bme->cb.delay(bme->pdev, 100);
|
||||
if (ims_bme280_read_trim_data(bme) == IMS_FAIL) return IMS_FAIL;
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_bme280_preset_config(ims_bme280_config_t *config, ims_bme280_mode_preset_t preset) {
|
||||
if (preset >= IMS_BME280_PRESET_END) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
memcpy(config, &ims_bme280_presets[preset], sizeof(ims_bme280_config_t));
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_bme280_config(ims_bme280_t *bme, ims_bme280_config_t *config) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint8_t reg_config = (config->filter << 0x02) | (config->t_sb << 0x05);
|
||||
uint8_t reg_ctrl_measure = (uint8_t)((config->osrs_t << 0x05) | (config->osrs_p << 0x02) | config->mode);
|
||||
uint8_t reg_ctrl_hum = config->osrs_h;
|
||||
|
||||
ims_bme280_mode_t mode;
|
||||
|
||||
ret = ims_bme_get_mode(bme, &mode);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
if (mode != IMS_BME280_MODE_SLEEP) {
|
||||
ret = ims_bme280_set_mode(bme, IMS_BME280_MODE_SLEEP);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
}
|
||||
|
||||
ret = ims_bme280_write_register(bme, IMS_BME280_REG_CONFIG, reg_config);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
ret = ims_bme280_write_register(bme, IMS_BME280_REG_CTRL_HUM, reg_ctrl_hum);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
ret = ims_bme280_write_register(bme, IMS_BME280_REG_CTRL_MEAS, reg_ctrl_measure);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme280_read_register(ims_bme280_t *bme, uint8_t reg, uint8_t *data, uint16_t len) {
|
||||
ims_i2c_xfer_desc_t xfer_desc = {
|
||||
.tx_size = 1U,
|
||||
|
@ -41,180 +328,84 @@ static ims_ret_t ims_bme280_write_register(ims_bme280_t *bme, uint8_t reg, uint8
|
|||
return bme->cb.i2c_xfer(bme->pdev, &xfer_desc);
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme280_reset(ims_bme280_t *bme) {
|
||||
return ims_bme280_write_register(bme, 0xE0, 0xB6);
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme280_read_trim_data(ims_bme280_t *bme) {
|
||||
ims_ret_t ims_bme280_read(ims_bme280_t *bme, ims_bme280_result_t *result) {
|
||||
ims_ret_t ret;
|
||||
uint8_t rx_buf[2];
|
||||
ret = ims_bme280_read_register(bme, 0x88, rx_buf, 0x02); // T1
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_T1 = IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x8A, rx_buf, 0x02); // T2
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_T2 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x8C, rx_buf, 0x02); // T3
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_T3 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x8E, rx_buf, 0x02); // P1
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P1 = IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x90, rx_buf, 0x02); // P2
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P2 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x92, rx_buf, 0x02); // P3
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P3 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x94, rx_buf, 0x02); // P4
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P4 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x96, rx_buf, 0x02); // P5
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P5 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x98, rx_buf, 0x02); // P5
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P6 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x9A, rx_buf, 0x02); // P7
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P7 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x9C, rx_buf, 0x02); // P8
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P8 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0x9E, rx_buf, 0x02); // P9
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_P9 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0xA1, rx_buf, 0x01); // H1
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H1 = rx_buf[0];
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0xE1, rx_buf, 0x02); // H2
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H2 = (int16_t)IMS_BME280_CONCAT_BYTES(rx_buf[1], rx_buf[0]);
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0xE3, rx_buf, 0x01); // H3
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H3 = (uint8_t)rx_buf[0];
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0xE4, rx_buf, 0x02); // H4
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H4 = (int16_t)(((int8_t)rx_buf[0] << 0x04) | (rx_buf[1] & 0x0F));
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0xE5, rx_buf, 0x02); // H5
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H5 = (int16_t)(((int8_t)rx_buf[1] << 0x04) | (rx_buf[0] >> 0x04));
|
||||
|
||||
ret = ims_bme280_read_register(bme, 0xE7, rx_buf, 0x01); // H6
|
||||
if (ret == IMS_FAIL) return ret;
|
||||
bme->trim.dig_H6 = (int8_t)rx_buf[0];
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static ims_ret_t ims_bme280_measure_raw(ims_bme280_t *bme, uint32_t *raw_t, uint32_t *raw_p, uint32_t *raw_h) {
|
||||
uint8_t measure_data[8];
|
||||
uint8_t status = 0;
|
||||
uint8_t ctrl_meas;
|
||||
uint8_t loop_count = 0;
|
||||
if (ims_bme280_read_register(bme, 0xF4, &ctrl_meas, 0x01) != IMS_SUCCESS) return IMS_FAIL;
|
||||
|
||||
ims_bme280_write_register(bme, 0xF4, ctrl_meas | IMS_BME280_MODE_FORCED);
|
||||
do {
|
||||
if (ims_bme280_read_register(bme, 0xF3, &status, 0x01) != IMS_SUCCESS) return IMS_FAIL;
|
||||
loop_count++;
|
||||
bme->cb.delay(bme->pdev, 5);
|
||||
} while (status & 0x08 && (loop_count < 12));
|
||||
|
||||
if (ims_bme280_read_register(bme, 0xF7, measure_data, 0x08) != IMS_SUCCESS) return IMS_FAIL;
|
||||
*raw_p =
|
||||
((uint32_t)measure_data[0] << 12) | ((uint32_t)measure_data[1] << 0x04) | ((uint32_t)measure_data[2] >> 0x04);
|
||||
*raw_t =
|
||||
((uint32_t)measure_data[3] << 12) | ((uint32_t)measure_data[4] << 0x04) | ((uint32_t)measure_data[5] >> 0x04);
|
||||
*raw_h = ((uint32_t)measure_data[6] << 8) | ((uint32_t)measure_data[7]);
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_bme280_init(ims_bme280_t *bme) {
|
||||
if (!bme) return IMS_FAIL;
|
||||
if (ims_bme280_reset(bme) == IMS_FAIL) return IMS_FAIL;
|
||||
bme->cb.delay(bme->pdev, 100);
|
||||
if (ims_bme280_read_trim_data(bme) == IMS_FAIL) return IMS_FAIL;
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_bme280_preset_config(ims_bme280_config_t *config, ims_bme280_mode_preset_t preset) {
|
||||
switch (preset) {
|
||||
case IMS_BME280_PRESET_WEATHER:
|
||||
config->mode = IMS_BME280_MODE_FORCED;
|
||||
config->osrs_t = IMS_BME280_OSRS_1;
|
||||
config->osrs_p = IMS_BME280_OSRS_1;
|
||||
config->osrs_h = IMS_BME280_OSRS_1;
|
||||
config->filter = IMS_BME280_FILTER_OFF;
|
||||
break;
|
||||
case IMS_BME280_PRESET_HUMIDITY:
|
||||
config->mode = IMS_BME280_MODE_FORCED;
|
||||
config->osrs_t = IMS_BME280_OSRS_1;
|
||||
config->osrs_p = IMS_BME280_OSRS_SKIP;
|
||||
config->osrs_h = IMS_BME280_OSRS_1;
|
||||
config->filter = IMS_BME280_FILTER_OFF;
|
||||
break;
|
||||
case IMS_BME280_PRESET_INDOOR_NAV:
|
||||
config->mode = IMS_BME280_MODE_NORMAL;
|
||||
config->osrs_t = IMS_BME280_OSRS_2;
|
||||
config->osrs_p = IMS_BME280_OSRS_16;
|
||||
config->osrs_h = IMS_BME280_OSRS_1;
|
||||
config->filter = IMS_BME280_FILTER_16;
|
||||
break;
|
||||
case IMS_BME280_PRESET_GAMING:
|
||||
config->mode = IMS_BME280_MODE_NORMAL;
|
||||
config->osrs_t = IMS_BME280_OSRS_1;
|
||||
config->osrs_p = IMS_BME280_OSRS_4;
|
||||
config->osrs_h = IMS_BME280_OSRS_SKIP;
|
||||
config->filter = IMS_BME280_FILTER_4;
|
||||
break;
|
||||
}
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_bme280_apply_config(ims_bme280_t *bme, ims_bme280_config_t *config) {
|
||||
uint8_t reg_config = config->filter << 0x02;
|
||||
uint8_t reg_ctrl_measure = (uint8_t)((config->osrs_t << 0x05) | (config->osrs_p << 0x02) | config->mode);
|
||||
uint8_t reg_ctrl_hum = config->osrs_h;
|
||||
ims_bme280_write_register(bme, 0xF5, reg_config);
|
||||
ims_bme280_write_register(bme, 0xF2, reg_ctrl_hum);
|
||||
ims_bme280_write_register(bme, 0xF4, reg_ctrl_measure);
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
#ifdef IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
|
||||
ims_ret_t ims_bme280_measure(ims_bme280_t *bme, ims_bme280_result_t *result) {
|
||||
uint32_t raw_P = 0x00;
|
||||
uint32_t raw_T = 0x00;
|
||||
uint32_t raw_H = 0x00;
|
||||
|
||||
if (ims_bme280_measure_raw(bme, &raw_T, &raw_P, &raw_H) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
ret = ims_bme280_read_raw_data(bme, &raw_T, &raw_P, &raw_H);
|
||||
if (ret != IMS_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
result->temperature = ims_bme280_compensate_T_double(bme, raw_T);
|
||||
result->pressure = ims_bme280_compensate_P_double(bme, raw_P);
|
||||
result->humidity = ims_bme280_compensate_H_double(bme, raw_H);
|
||||
#if IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
result->temperature = ims_bme280_compensate_T_double(bme, (int32_t)raw_T);
|
||||
result->pressure = ims_bme280_compensate_P_double(bme, (int32_t)raw_P);
|
||||
result->humidity = ims_bme280_compensate_H_double(bme, (int32_t)raw_H);
|
||||
#else
|
||||
result->temperature = ims_bme280_compensate_T_int32(bme, (int32_t)raw_T);
|
||||
result->pressure = ims_bme280_compensate_P_int64(bme, (int32_t)raw_P);
|
||||
result->humidity = ims_bme280_compensate_H_int32(bme, (int32_t)raw_H);
|
||||
#endif
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Force a new result to be sampled, or wait for a new result be available if the sensor is in NORMAL mode.
|
||||
* @param bme
|
||||
* @param result
|
||||
* @return
|
||||
*/
|
||||
ims_ret_t ims_bme280_read_forced(ims_bme280_t *bme, ims_bme280_result_t *result) {
|
||||
ims_ret_t ret;
|
||||
|
||||
uint32_t raw_P = 0x00;
|
||||
uint32_t raw_T = 0x00;
|
||||
uint32_t raw_H = 0x00;
|
||||
|
||||
ims_bme280_mode_t mode;
|
||||
|
||||
ret = ims_bme_get_mode(bme, &mode);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
|
||||
/*
|
||||
* If the sensor is in SLEEP mode, start a forced measurement;
|
||||
* if the sensor is measuring or in NORMAL mode, wait for a new result to be sampled.
|
||||
*/
|
||||
|
||||
if (mode == IMS_BME280_MODE_SLEEP) {
|
||||
ret = ims_bme280_set_mode(bme, IMS_BME280_MODE_FORCED);
|
||||
if (ret != IMS_SUCCESS) return ret;
|
||||
}
|
||||
|
||||
ret = ims_bme_wait_measure_complete(bme, IMS_BME280_FORCED_TIMEOUT_MS);
|
||||
if (ret != IMS_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = ims_bme280_read_raw_data(bme, &raw_T, &raw_P, &raw_H);
|
||||
if (ret != IMS_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
result->temperature = ims_bme280_compensate_T_double(bme, (int32_t)raw_T);
|
||||
result->pressure = ims_bme280_compensate_P_double(bme, (int32_t)raw_P);
|
||||
result->humidity = ims_bme280_compensate_H_double(bme, (int32_t)raw_H);
|
||||
#else
|
||||
result->temperature = ims_bme280_compensate_T_int32(bme, (int32_t)raw_T);
|
||||
result->pressure = ims_bme280_compensate_P_int64(bme, (int32_t)raw_P);
|
||||
result->humidity = ims_bme280_compensate_H_int32(bme, (int32_t)raw_H);
|
||||
#endif
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
#ifdef IMS_CFG_DHT_BME280_ENABLE_FLOAT
|
||||
|
||||
static double ims_bme280_compensate_T_double(ims_bme280_t *bme, int32_t adc_T) {
|
||||
double var1, var2, T;
|
||||
var1 = (((double)adc_T) / 16384.0 - ((double)bme->trim.dig_T1) / 1024.0) * ((double)bme->trim.dig_T2);
|
||||
|
@ -259,23 +450,9 @@ static double ims_bme280_compensate_H_double(ims_bme280_t *bme, int32_t adc_H) {
|
|||
var_H = 0.0;
|
||||
return var_H;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
ims_ret_t ims_bme280_measure(ims_bme280_t *bme, ims_bme280_result_t *result) {
|
||||
uint32_t raw_P = 0x00;
|
||||
uint32_t raw_T = 0x00;
|
||||
uint32_t raw_H = 0x00;
|
||||
|
||||
if (ims_bme280_measure_raw(bme, &raw_T, &raw_P, &raw_H) != IMS_SUCCESS) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
|
||||
result->temperature = ims_bme280_compensate_T_int32(bme, raw_T);
|
||||
result->pressure = ims_bme280_compensate_P_int64(bme, raw_P);
|
||||
result->humidity = ims_bme280_compensate_H_int32(bme, raw_H);
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
static int32_t ims_bme280_compensate_T_int32(ims_bme280_t *bme, int32_t adc_T) {
|
||||
int32_t var1, var2, T;
|
||||
var1 = ((((adc_T >> 3) - ((int32_t)bme->trim.dig_T1 << 1))) * ((int32_t)bme->trim.dig_T2)) >> 11;
|
||||
|
@ -324,4 +501,5 @@ static uint32_t ims_bme280_compensate_H_int32(ims_bme280_t *bme, int32_t adc_H)
|
|||
v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r);
|
||||
return (uint32_t)(v_x1_u32r >> 12);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -38,6 +38,11 @@ ims_ret_t ims_lsm6dsl_init(ims_lsm6dsl_t *imu) {
|
|||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_lsm6dsl_config(ims_lsm6dsl_t *imu, ims_lsm6dsl_config_t *config) {
|
||||
// TODO: Implement this.
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t ims_lsm6dsl_read_temperature(ims_lsm6dsl_t *imu, int16_t *temp) {
|
||||
ims_ret_t ret;
|
||||
|
||||
|
|
Loading…
Reference in New Issue