MYNEWT-748 SensorAPI: Add BME280 support SensorAPI support for pressure, temperature and humidity
- Add temperature, pressure, humidity support - change temperature handling for bno055 - fix bugs with register values Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/1dc16358 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/1dc16358 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/1dc16358 Branch: refs/heads/bluetooth5 Commit: 1dc1635810bd5b0eea1c3faed1675d9139e2a32d Parents: 8d98e07 Author: Vipul Rahane <[email protected]> Authored: Tue May 2 18:52:45 2017 -0700 Committer: Vipul Rahane <[email protected]> Committed: Fri May 12 17:08:16 2017 -0700 ---------------------------------------------------------------------- apps/sensors_test/src/main.c | 10 + .../sensors/bme280/include/bme280/bme280.h | 19 + hw/drivers/sensors/bme280/src/bme280.c | 446 +++++++++++++++++-- hw/drivers/sensors/bme280/src/bme280_priv.h | 15 +- hw/drivers/sensors/bme280/src/bme280_shell.c | 16 +- .../sensors/bno055/include/bno055/bno055.h | 2 +- hw/drivers/sensors/bno055/src/bno055.c | 32 +- hw/sensor/include/sensor/humidity.h | 45 ++ hw/sensor/include/sensor/pressure.h | 45 ++ hw/sensor/include/sensor/temperature.h | 45 ++ hw/sensor/src/sensor_shell.c | 23 +- 11 files changed, 649 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/apps/sensors_test/src/main.c ---------------------------------------------------------------------- diff --git a/apps/sensors_test/src/main.c b/apps/sensors_test/src/main.c index ce5f4bd..ace40af 100755 --- a/apps/sensors_test/src/main.c +++ b/apps/sensors_test/src/main.c @@ -400,6 +400,16 @@ config_sensor(void) memset(&bmecfg, 0, sizeof(bmecfg)); + bmecfg.bc_mode = BME280_MODE_NORMAL; + bmecfg.bc_iir = BME280_FILTER_OFF; + bmecfg.bc_sby_dur = BME280_STANDBY_MS_1000; + bmecfg.bc_boc[0].boc_type = SENSOR_TYPE_RELATIVE_HUMIDITY; + bmecfg.bc_boc[1].boc_type = SENSOR_TYPE_PRESSURE; + bmecfg.bc_boc[2].boc_type = SENSOR_TYPE_TEMPERATURE; + bmecfg.bc_boc[0].boc_oversample = BME280_SAMPLING_X1; + bmecfg.bc_boc[1].boc_oversample = BME280_SAMPLING_X1; + bmecfg.bc_boc[2].boc_oversample = BME280_SAMPLING_X1; + rc = bme280_config((struct bme280 *)dev, &bmecfg); if (rc) { os_dev_close(dev); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/drivers/sensors/bme280/include/bme280/bme280.h ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bme280/include/bme280/bme280.h b/hw/drivers/sensors/bme280/include/bme280/bme280.h index 4ef5d78..ff3daa3 100644 --- a/hw/drivers/sensors/bme280/include/bme280/bme280.h +++ b/hw/drivers/sensors/bme280/include/bme280/bme280.h @@ -93,6 +93,7 @@ struct bme280_cfg { uint8_t bc_iir; struct bme280_over_cfg bc_boc[3]; uint8_t bc_mode; + uint8_t bc_sby_dur; }; struct bme280 { @@ -234,6 +235,24 @@ int bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg); int bme280_get_chipid(uint8_t *chipid); +/** + * Set the standy duration setting + * + * @param duration + * @return 0 on success, non-zero on failure + */ +int +bme280_set_sby_duration(uint8_t dur); + +/** + * Get the standy duration setting + * + * @param ptr to duration + * @return 0 on success, non-zero on failure + */ +int +bme280_get_sby_duration(uint8_t *dur); + #if MYNEWT_VAL(BME280_CLI) int bme280_shell_init(void); #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/drivers/sensors/bme280/src/bme280.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bme280/src/bme280.c b/hw/drivers/sensors/bme280/src/bme280.c index e31cffc..7c66307 100644 --- a/hw/drivers/sensors/bme280/src/bme280.c +++ b/hw/drivers/sensors/bme280/src/bme280.c @@ -28,6 +28,9 @@ #include "hal/hal_spi.h" #include "sensor/sensor.h" #include "bme280/bme280.h" +#include "sensor/humidity.h" +#include "sensor/temperature.h" +#include "sensor/pressure.h" #include "bme280_priv.h" #include "hal/hal_gpio.h" @@ -46,6 +49,8 @@ static struct hal_spi_settings spi_bme280_settings = { .word_size = HAL_SPI_WORD_SIZE_8BIT, }; +struct bme280_calib_data bcd; + #if MYNEWT_VAL(BME280_STATS) /* Define the stats section and records */ STATS_SECT_START(bme280_stat_section) @@ -85,6 +90,8 @@ static const struct sensor_driver g_bme280_sensor_driver = { bme280_sensor_get_config }; +float g_t_fine; + static int bme280_default_cfg(struct bme280_cfg *cfg) { @@ -186,13 +193,238 @@ bme280_sensor_get_interface(struct sensor *sensor, sensor_type_t type) return (NULL); } +#if MYNEWT_VAL(BME280_SPEC_CALC) +/** + * Returns temperature in DegC, as float + * Output value of â51.23â equals 51.23 DegC. + * + * @param uncompensated raw temperature value + * @return 0 on success, non-zero on failure + */ +static float +bme280_compensate_temperature(uint32_t rawtemp, struct bme280_calib_data *bcd) +{ + float var1, var2, comptemp; + + var1 = (((float)rawtemp)/16384.0 â ((float)bcd->bcd_dig_T1)/1024.0) * + ((float)bcd->bcd_dig_T2); + var2 = ((((float)rawtemp)/131072.0 â ((float)bcd->bcd_dig_T1)/8192.0) * + (((float)rawtemp)/131072.0 â ((float)bcd->bcd_dig_T1)/8192.0)) * + ((float)bcd->bcd_dig_T3); + + g_t_fine = var1 + var2; + + comptemp = (var1 + var2) / 5120.0; + + return comptemp; +} + +/** + * Returns pressure in Pa as float. + * Output value of â96386.2â equals 96386.2 Pa = 963.862 hPa + * + * @param uncompensated raw pressure value + * @return 0 on success, non-zero on failure + */ +static float +bme280_compensate_pressure(uint32_t rawpress, struct bme280_calib_data *bcd) +{ + float var1, var2, p; + uint32_t temp; + + if (!g_t_fine) { + if(!bme280_get_temperature(&temp)) { + (void)bme280_compensate_temperature(temp); + } + } + + var1 = ((float)g_t_fine/2.0) â 64000.0; + var2 = var1 * var1 * ((float)bcd->bcd_dig_P6) / 32768.0; + var2 = var2 + var1 * ((float)bcd->bcd_dig_P5) * 2.0; + var2 = (var2/4.0)+(((float)bcd->bcd_dig_P4) * 65536.0); + var1 = (((float)bcd->bcd_dig_P3) * var1 * var1 / 524288.0 + + ((float)bcd->bcd_dig_P2) * var1) / 524288.0; + var1 = (1.0 + var1 / 32768.0)*((float)bcd->bcd_dig_P1); + + if (var1 == 0.0) + { + return 0; + } + + p = 1048576.0 â (float)rawpress; + p = (p â (var2 / 4096.0)) * 6250.0 / var1; + + var1 = ((float)bcd->bcd_dig_P9) * p * p / 2147483648.0; + var2 = p * ((float)bcd->bcd_dig_P8) / 32768.0; + + p = p + (var1 + var2 + ((float)bcd->bcd_dig_P7)) / 16.0; + + return p; +} + +/** + * Returns humidity in %rH as float. + * Output value of â46.332â represents 46.332 %rH + * + * @param uncompensated raw humidity value + * @return 0 on success, non-zero on failure + */ +static float +bme280_compensate_humidity(uint32_t rawhumid, struct bme280_calib_data *bcd) +{ + float h; + uint32_t temp; + + if (!g_t_fine) { + if(!bme280_get_temperature(&temp)) { + (void)bme280_compensate_temperature(temp, bcd); + } + } + + h = (((float)g_t_fine) â 76800.0); + h = (rawhumid â (((float)bcd->bcd_dig_H4) * 64.0 + + ((float)bcd->bcd_dig_H5) / 16384.0 * h)) * + (((float)bcd->bcd_dig_H2) / 65536.0 * (1.0 + + ((float)bcd->bcd_dig_H6) / 67108864.0 * h * + (1.0 + ((float)bcd->bcd_dig_H3) / 67108864.0 * h))); + + h = h * (1.0 â ((float)bcd->bcd_dig_H1) * h / 524288.0); + if (h > 100.0) { + h = 100.0; + } else if (h < 0.0) { + h = 0.0; + } + + return h; +} + +#else + +/** + * Returns temperature in DegC, as float + * Output value of â51.23â equals 51.23 DegC. + * + * @param uncompensated raw temperature value + * @return 0 on success, non-zero on failure + */ +static float +bme280_compensate_temperature(uint32_t rawtemp, struct bme280_calib_data *bcd) +{ + float var1, var2, comptemp; + + rawtemp >>= 4; + + var1 = ((((rawtemp>>3) - ((int32_t)bcd->bcd_dig_T1 <<1))) * + ((int32_t)bcd->bcd_dig_T2)) >> 11; + + var2 = (((((rawtemp>>4) - ((int32_t)bcd->bcd_dig_T1)) * + ((rawtemp>>4) - ((int32_t)bcd->bcd_dig_T1))) >> 12) * + ((int32_t)bcd->bcd_dig_T3)) >> 14; + + g_t_fine = var1 + var2; + + comptemp = ((int32_t)(g_t_fine * 5 + 128)) >> 8; + + return comptemp/100; +} + +/** + * Returns pressure in Pa as float. + * Output value of â96386.2â equals 96386.2 Pa = 963.862 hPa + * + * @param uncompensated raw pressure value + * @return 0 on success, non-zero on failure + */ +static float +bme280_compensate_pressure(uint32_t rawpress, struct bme280_calib_data *bcd) +{ + float var1, var2, p; + uint32_t temp; + + if (!g_t_fine) { + if(!bme280_get_temperature(&temp)) { + (void)bme280_compensate_temperature(temp, bcd); + } + } + + rawpress >>= 4; + + var1 = ((int64_t)g_t_fine) - 128000; + var2 = var1 * var1 * (int64_t)bcd->bcd_dig_P6; + var2 = var2 + ((int64_t)(var1*(int64_t)bcd->bcd_dig_P5) << 17); + var2 = var2 + (((int64_t)bcd->bcd_dig_P4) << 35); + var1 = ((int64_t)(var1 * var1 * (int64_t)bcd->bcd_dig_P3) >> 8) + + ((int64_t)(var1 * (int64_t)bcd->bcd_dig_P2) << 12); + var1 = (int64_t)((((((int64_t)1) << 47)+var1))*((int64_t)bcd->bcd_dig_P1)) >> 33; + + if (var1 == 0) { + /* Avoid exception caused by division by zero */ + return 0; + } + + p = 1048576 - rawpress; + p = ((((int64_t)p << 31) - var2) * 3125) / var1; + + var1 = (int64_t)(((int64_t)bcd->bcd_dig_P9) * ((int64_t)p >> 13) * ((int64_t)p >> 13)) >> 25; + var2 = (int64_t)(((int64_t)bcd->bcd_dig_P8) * (int64_t)p) >> 19; + + p = ((int64_t)(p + var1 + var2) >> 8) + (((int64_t)bcd->bcd_dig_P7) << 4); + + return (float)p/256; +} + +/** + * Returns humidity in %rH as float. + * Output value of â46.332â represents 46.332 %rH + * + * @param uncompensated raw humidity value + * @return 0 on success, non-zero on failure + */ +static float +bme280_compensate_humidity(uint32_t rawhumid, struct bme280_calib_data *bcd) +{ + float h; + uint32_t temp; + int32_t tmp32; + + if (!g_t_fine) { + if(!bme280_get_temperature(&temp)) { + (void)bme280_compensate_temperature(temp, bcd); + } + } + + tmp32 = (g_t_fine - ((int32_t)76800)); + + tmp32 = (((((rawhumid << 14) - (((int32_t)bcd->bcd_dig_H4) << 20) - + (((int32_t)bcd->bcd_dig_H5) * tmp32)) + ((int32_t)16384)) >> 15) * + (((((((tmp32 * ((int32_t)bcd->bcd_dig_H6)) >> 10) * + (((tmp32 * ((int32_t)bcd->bcd_dig_H3)) >> 11) + ((int32_t)32768))) >> 10) + + ((int32_t)2097152)) * ((int32_t)bcd->bcd_dig_H2) + 8192) >> 14)); + + tmp32 = (tmp32 - (((((tmp32 >> 15) * (tmp32 >> 15)) >> 7) * + ((int32_t)bcd->bcd_dig_H1)) >> 4)); + + tmp32 = (tmp32 < 0) ? 0 : tmp32; + + tmp32 = (tmp32 > 419430400) ? 419430400 : tmp32; + + h = (tmp32 >> 12); + + return h / 1024.0; +} + +#endif + static int bme280_sensor_read(struct sensor *sensor, sensor_type_t type, sensor_data_func_t data_func, void *data_arg, uint32_t timeout) { - uint32_t temp; - uint32_t press; - uint32_t humid; + uint32_t rawtemp; + uint32_t rawpress; + uint32_t rawhumid; + struct sensor_temp_data std; + struct sensor_press_data spd; + struct sensor_humid_data shd; int rc; if (!(type & SENSOR_TYPE_PRESSURE) && @@ -202,19 +434,20 @@ bme280_sensor_read(struct sensor *sensor, sensor_type_t type, goto err; } - temp = press = humid = 0; + rawtemp = rawpress = rawhumid = 0; /* Get a new pressure sample */ if (type & SENSOR_TYPE_PRESSURE) { - rc = bme280_get_pressure(&press); + rc = bme280_get_pressure(&rawpress); if (rc) { goto err; } - //lux = bme280_calculate_lux(full, ir, &(bme280->cfg)); + spd.spd_press = bme280_compensate_pressure(rawpress, &bcd); + spd.spd_press_is_valid = 1; /* Call data function */ - rc = data_func(sensor, data_arg, &press); + rc = data_func(sensor, data_arg, &spd); if (rc) { goto err; } @@ -222,15 +455,16 @@ bme280_sensor_read(struct sensor *sensor, sensor_type_t type, /* Get a new temperature sample */ if (type & SENSOR_TYPE_TEMPERATURE) { - rc = bme280_get_temperature(&temp); + rc = bme280_get_temperature(&rawtemp); if (rc) { goto err; } - //lux = bme280_calculate_lux(full, ir, &(bme280->cfg)); + std.std_temp = bme280_compensate_temperature(rawtemp, &bcd); + std.std_temp_is_valid = 1; /* Call data function */ - rc = data_func(sensor, data_arg, &temp); + rc = data_func(sensor, data_arg, &std); if (rc) { goto err; } @@ -238,15 +472,16 @@ bme280_sensor_read(struct sensor *sensor, sensor_type_t type, /* Get a new relative humidity sample */ if (type & SENSOR_TYPE_RELATIVE_HUMIDITY) { - rc = bme280_get_humidity(&humid); + rc = bme280_get_humidity(&rawhumid); if (rc) { goto err; } - //lux = bme280_calculate_lux(full, ir, &(bme280->cfg)); + shd.shd_humid = bme280_compensate_humidity(rawhumid, &bcd); + shd.shd_humid_is_valid = 1; /* Call data function */ - rc = data_func(sensor, data_arg, &humid); + rc = data_func(sensor, data_arg, &shd); if (rc) { goto err; } @@ -270,7 +505,7 @@ bme280_sensor_get_config(struct sensor *sensor, sensor_type_t type, goto err; } - cfg->sc_valtype = SENSOR_VALUE_TYPE_INT32; + cfg->sc_valtype = SENSOR_VALUE_TYPE_FLOAT; return (0); err: @@ -278,6 +513,72 @@ err: } /** + * Check status to see if the sensor is reading calibration + * + * @return 0 on success, non-zero on failure + */ +int +bme280_is_calibrating(uint8_t *calibrating) +{ + uint8_t status; + int rc; + + rc = bme280_readlen(BME280_REG_ADDR_STATUS, &status, 1); + if (rc) { + goto err; + } + + *calibrating = (status & BME280_REG_STATUS_IM_UP) != 0; + + return 0; +err: + return rc; +} + +/** + * Get calibration info from the sensor + * + * @param ptr to the calib data info + * @return 0 in success, non-zero on failure + */ +static int +bme280_get_calibinfo(struct bme280_calib_data *bcd) +{ + int rc; + uint8_t payload[33]; + + rc = bme280_readlen(BME280_REG_ADDR_DIG_T1, payload, sizeof(payload)); + if (rc) { + goto err; + } + + bcd->bcd_dig_T1 = payload[0] | payload[1] << 8; + bcd->bcd_dig_T2 = payload[3] | payload[2] << 8; + bcd->bcd_dig_T3 = payload[5] | payload[4] << 8; + + bcd->bcd_dig_P1 = payload[7] | payload[6] << 8; + bcd->bcd_dig_P2 = payload[9] | payload[8] << 8; + bcd->bcd_dig_P3 = payload[11] | payload[10] << 8; + bcd->bcd_dig_P4 = payload[13] | payload[12] << 8; + bcd->bcd_dig_P5 = payload[15] | payload[14] << 8; + bcd->bcd_dig_P6 = payload[17] | payload[16] << 8; + bcd->bcd_dig_P7 = payload[19] | payload[18] << 8; + bcd->bcd_dig_P8 = payload[21] | payload[20] << 8; + bcd->bcd_dig_P9 = payload[23] | payload[22] << 8; + + bcd->bcd_dig_H1 = payload[24]; + bcd->bcd_dig_H2 = payload[26] | payload[25] << 8; + bcd->bcd_dig_H3 = payload[27]; + bcd->bcd_dig_H4 = (payload[28] << 4) | (payload[29] & 0xF); + bcd->bcd_dig_H5 = (payload[31] << 4) | (payload[30] >> 4); + bcd->bcd_dig_H6 = (int8_t)payload[32]; + + return 0; +err: + return rc; +} + +/** * Configure BME280 sensor * * @param Sensor device BME280 structure @@ -290,6 +591,7 @@ bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg) { int rc; uint8_t id; + uint8_t calibrating; /* Check if we can read the chip address */ rc = bme280_get_chipid(&id); @@ -311,11 +613,34 @@ bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg) } } + rc = bme280_reset(); + if (rc) { + goto err; + } + + os_time_delay((OS_TICKS_PER_SEC * 300)/1000 + 1); + + calibrating = 1; + + while(calibrating) { + rc = bme280_is_calibrating(&calibrating); + if (rc) { + goto err; + } + } + + rc = bme280_get_calibinfo(&bcd); + if (rc) { + goto err; + } + rc = bme280_set_iir(cfg->bc_iir); if (rc) { goto err; } + os_time_delay((OS_TICKS_PER_SEC * 200)/1000 + 1); + bme280->cfg.bc_iir = cfg->bc_iir; rc = bme280_set_mode(cfg->bc_mode); @@ -323,9 +648,20 @@ bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg) goto err; } + os_time_delay((OS_TICKS_PER_SEC * 200)/1000 + 1); + bme280->cfg.bc_mode = cfg->bc_mode; - if (!cfg->bc_boc[0].boc_type) { + rc = bme280_set_sby_duration(cfg->bc_sby_dur); + if (rc) { + goto err; + } + + os_time_delay((OS_TICKS_PER_SEC * 200)/1000 + 1); + + bme280->cfg.bc_sby_dur = cfg->bc_sby_dur; + + if (cfg->bc_boc[0].boc_type) { rc = bme280_set_oversample(cfg->bc_boc[0].boc_type, cfg->bc_boc[0].boc_oversample); if (rc) { @@ -336,7 +672,7 @@ bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg) bme280->cfg.bc_boc[0].boc_type = cfg->bc_boc[0].boc_type; bme280->cfg.bc_boc[0].boc_oversample = cfg->bc_boc[0].boc_oversample; - if (!cfg->bc_boc[1].boc_type) { + if (cfg->bc_boc[1].boc_type) { rc = bme280_set_oversample(cfg->bc_boc[1].boc_type, cfg->bc_boc[1].boc_oversample); if (rc) { @@ -347,7 +683,7 @@ bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg) bme280->cfg.bc_boc[1].boc_type = cfg->bc_boc[1].boc_type; bme280->cfg.bc_boc[1].boc_oversample = cfg->bc_boc[1].boc_oversample; - if (!cfg->bc_boc[2].boc_type) { + if (cfg->bc_boc[2].boc_type) { rc = bme280_set_oversample(cfg->bc_boc[2].boc_type, cfg->bc_boc[2].boc_oversample); if (rc) { @@ -358,6 +694,9 @@ bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg) bme280->cfg.bc_boc[2].boc_type = cfg->bc_boc[2].boc_type; bme280->cfg.bc_boc[2].boc_oversample = cfg->bc_boc[2].boc_oversample; + os_time_delay((OS_TICKS_PER_SEC * 200)/1000 + 1); + + return 0; err: return (rc); } @@ -426,7 +765,8 @@ bme280_writelen(uint8_t addr, uint8_t *payload, uint8_t len) hal_gpio_write(MYNEWT_VAL(BME280_CSPIN), 0); /* Send the address */ - rc = hal_spi_tx_val(MYNEWT_VAL(BME280_SPINUM), addr | BME280_SPI_READ_CMD_BIT); + rc = hal_spi_tx_val(MYNEWT_VAL(BME280_SPINUM), + addr & ~BME280_SPI_READ_CMD_BIT); if (rc == 0xFFFF) { rc = SYS_EINVAL; goto err; @@ -531,7 +871,7 @@ bme280_reset(void) { uint8_t txdata; - txdata = 1; + txdata = 0xB6; return bme280_writelen(BME280_REG_ADDR_RESET, &txdata, 1); } @@ -632,9 +972,9 @@ bme280_set_mode(uint8_t mode) goto err; } - mode = cfg | (mode & BME280_REG_CTRL_MEAS_MODE); + cfg = cfg | (mode & BME280_REG_CTRL_MEAS_MODE); - rc = bme280_writelen(BME280_REG_ADDR_CTRL_MEAS, &mode, 1); + rc = bme280_writelen(BME280_REG_ADDR_CTRL_MEAS, &cfg, 1); if (rc) { goto err; } @@ -706,14 +1046,14 @@ bme280_set_oversample(sensor_type_t type, uint8_t oversample) } if (type & SENSOR_TYPE_TEMPERATURE) { - oversample = cfg | ((oversample << 5) & BME280_REG_CTRL_MEAS_TOVER); + cfg = cfg | ((oversample << 5) & BME280_REG_CTRL_MEAS_TOVER); } if (type & SENSOR_TYPE_PRESSURE) { - oversample = cfg | ((oversample << 3) & BME280_REG_CTRL_MEAS_POVER); + cfg = cfg | ((oversample << 2) & BME280_REG_CTRL_MEAS_POVER); } - rc = bme280_writelen(BME280_REG_ADDR_CTRL_MEAS, &oversample, 1); + rc = bme280_writelen(BME280_REG_ADDR_CTRL_MEAS, &cfg, 1); if (rc) { goto err; } @@ -725,9 +1065,9 @@ bme280_set_oversample(sensor_type_t type, uint8_t oversample) goto err; } - oversample = cfg | (oversample & BME280_REG_CTRL_HUM_HOVER); + cfg = cfg | (oversample & BME280_REG_CTRL_HUM_HOVER); - rc = bme280_writelen(BME280_REG_ADDR_CTRL_HUM, &oversample, 1); + rc = bme280_writelen(BME280_REG_ADDR_CTRL_HUM, &cfg, 1); if (rc) { goto err; } @@ -761,3 +1101,57 @@ bme280_get_chipid(uint8_t *chipid) err: return rc; } + +/** + * Set the standy duration setting + * + * @param duration + * @return 0 on success, non-zero on failure + */ +int +bme280_set_sby_duration(uint8_t dur) +{ + int rc; + uint8_t cfg; + + rc = bme280_readlen(BME280_REG_ADDR_CONFIG, &cfg, 1); + if (rc) { + goto err; + } + + cfg = cfg | ((dur << 5) & BME280_REG_CONFIG_STANDBY); + + rc = bme280_writelen(BME280_REG_ADDR_CONFIG, &cfg, 1); + if (rc) { + goto err; + } + + return 0; +err: + return rc; +} + +/** + * Get the standy duration setting + * + * @param ptr to duration + * @return 0 on success, non-zero on failure + */ +int +bme280_get_sby_duration(uint8_t *dur) +{ + int rc; + uint8_t tmp; + + rc = bme280_readlen(BME280_REG_ADDR_CONFIG, &tmp, 1); + if (rc) { + goto err; + } + + *dur = tmp & BME280_REG_CONFIG_STANDBY; + + return 0; +err: + return rc; +} + http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/drivers/sensors/bme280/src/bme280_priv.h ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bme280/src/bme280_priv.h b/hw/drivers/sensors/bme280/src/bme280_priv.h index 23d0aaf..4b01e98 100644 --- a/hw/drivers/sensors/bme280/src/bme280_priv.h +++ b/hw/drivers/sensors/bme280/src/bme280_priv.h @@ -45,24 +45,23 @@ #define BME280_REG_ADDR_VERSION 0xD1 #define BME280_REG_ADDR_SOFTRESET 0xE0 -#define BME280_REG_ADDR_CAL26 0xE1 // R calibration stored in 0xE1-0xF0 +#define BME280_REG_ADDR_CAL26 0xE1 /* R calibration stored in 0xE1-0xF0 */ #define BME280_REG_ADDR_CTRL_HUM 0xF2 -#define BME280_REG_CTRL_HUM_NONE (0x1F) -#define BME280_REG_CTRL_HUM_HOVER (0x11) +#define BME280_REG_CTRL_HUM_HOVER (0x7) #define BME280_REG_ADDR_STATUS 0XF3 #define BME280_REG_STATUS_MEAS 0x04 #define BME280_REG_STATUS_IM_UP 0x01 #define BME280_REG_ADDR_CTRL_MEAS 0xF4 -#define BME280_REG_CTRL_MEAS_TOVER (0x11 << 5) -#define BME280_REG_CTRL_MEAS_POVER (0x11 << 3) -#define BME280_REG_CTRL_MEAS_MODE (0x11) +#define BME280_REG_CTRL_MEAS_TOVER (0x7 << 5) +#define BME280_REG_CTRL_MEAS_POVER (0x7 << 2) +#define BME280_REG_CTRL_MEAS_MODE (0x3) #define BME280_REG_ADDR_CONFIG 0xF5 -#define BME280_REG_CONFIG_STANDBY (0x11 << 5) -#define BME280_REG_CONFIG_FILTER (0x11 << 3) +#define BME280_REG_CONFIG_STANDBY (0x7 << 5) +#define BME280_REG_CONFIG_FILTER (0x7 << 3) #define BME280_REG_CONFIG_SPI3_EN (0x1) #define BME280_REG_ADDR_PRESS 0xF7 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/drivers/sensors/bme280/src/bme280_shell.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bme280/src/bme280_shell.c b/hw/drivers/sensors/bme280/src/bme280_shell.c index 78f5d2e..aeea071 100644 --- a/hw/drivers/sensors/bme280/src/bme280_shell.c +++ b/hw/drivers/sensors/bme280/src/bme280_shell.c @@ -82,12 +82,12 @@ bme280_shell_help(void) console_printf("%s cmd [flags...]\n", bme280_shell_cmd_struct.sc_cmd); console_printf("cmd:\n"); console_printf("\tr [n_samples]\n"); - console_printf("\tmode [0-sleep | 1-forced | 3-normal]\n"); - console_printf("\tiir [1-enabled | 0-disabled]"); + console_printf("\tmode [0-sleep | 1/2-forced | 3-normal]\n"); + console_printf("\tiir [1-enabled | 0-disabled]\n"); console_printf("\toversample [type 5-temperature | 6-pressure | 8-humidity]\n" " [0-none | 1-x1 | 2-x2 | 3-x4 | 4-x8 | 5-x16]\n"); console_printf("\treset\n"); - console_printf("\tchip_id"); + console_printf("\tchipid\n"); console_printf("\tdump\n"); return 0; @@ -104,7 +104,7 @@ bme280_shell_cmd_read_chipid(int argc, char **argv) goto err; } - console_printf("CHIP_ID:%02X", chipid); + console_printf("CHIP_ID:0x%02X\n", chipid); return 0; err: @@ -159,7 +159,7 @@ bme280_shell_cmd_read(int argc, char **argv) return rc; } - console_printf("temperature: %u\tpressure: %u\thumidity: %u\n", + console_printf("temperature: %u pressure: %u\thumidity: %u\n", (unsigned int)temp, (unsigned int)press, (unsigned int)humid); } @@ -235,9 +235,9 @@ bme280_shell_cmd_mode(int argc, char **argv) console_printf("mode: %u", mode); } - /* Chaneg mode */ + /* Change mode */ if (argc == 3) { - if (bme280_shell_stol(argv[2], 0, 1, &val)) { + if (bme280_shell_stol(argv[2], 0, 3, &val)) { return bme280_shell_err_invalid_arg(argv[2]); } rc = bme280_set_mode(val); @@ -268,7 +268,7 @@ bme280_shell_cmd_iir(int argc, char **argv) if (rc) { goto err; } - console_printf("IIR: %02X", iir); + console_printf("IIR: 0x%02X", iir); } /* Enable/disable iir*/ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/drivers/sensors/bno055/include/bno055/bno055.h ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bno055/include/bno055/bno055.h b/hw/drivers/sensors/bno055/include/bno055/bno055.h index af1c836..f453871 100644 --- a/hw/drivers/sensors/bno055/include/bno055/bno055.h +++ b/hw/drivers/sensors/bno055/include/bno055/bno055.h @@ -223,7 +223,7 @@ bno055_get_quat_data(void *sqd); * @return temperature in degree celcius */ int -bno055_get_temp(int8_t *temp); +bno055_get_temp(uint8_t *temp); /** * Gets current calibration status http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/drivers/sensors/bno055/src/bno055.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bno055/src/bno055.c b/hw/drivers/sensors/bno055/src/bno055.c index 7502c5b..49eeb6e 100644 --- a/hw/drivers/sensors/bno055/src/bno055.c +++ b/hw/drivers/sensors/bno055/src/bno055.c @@ -30,6 +30,7 @@ #include "sensor/mag.h" #include "sensor/quat.h" #include "sensor/euler.h" +#include "sensor/temperature.h" #include "bno055/bno055.h" #include "bno055_priv.h" @@ -1018,13 +1019,13 @@ err: * @return 0 on success, non-zero on error */ int -bno055_get_temp(int8_t *temp) +bno055_get_temp(uint8_t *temp) { int rc; uint8_t units; uint8_t div; - rc = bno055_read8(BNO055_TEMP_ADDR, (uint8_t *)temp); + rc = bno055_read8(BNO055_TEMP_ADDR, temp); if (rc) { goto err; } @@ -1044,6 +1045,31 @@ err: } /** + * Get temperature data from bno055 sensor and mark it valid + * + * @param pointer to the temperature data structure + * @return 0 on success, non-zero on error + */ +static int +bno055_get_temp_data(struct sensor_temp_data *std) +{ + int rc; + uint8_t temp; + + rc = bno055_get_temp(&temp); + if (rc) { + goto err; + } + + std->std_temp = temp; + std->std_temp_is_valid = 1; + + return 0; +err: + return rc; +} + +/** * Get sensor data of specific type. This function also allocates a buffer * to fill up the data in. * @@ -1071,7 +1097,7 @@ bno055_sensor_read(struct sensor *sensor, sensor_type_t type, goto err; } } else if (type == SENSOR_TYPE_TEMPERATURE) { - rc = bno055_get_temp(databuf); + rc = bno055_get_temp_data(databuf); if (rc) { goto err; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/sensor/include/sensor/humidity.h ---------------------------------------------------------------------- diff --git a/hw/sensor/include/sensor/humidity.h b/hw/sensor/include/sensor/humidity.h new file mode 100644 index 0000000..40f5350 --- /dev/null +++ b/hw/sensor/include/sensor/humidity.h @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef __SENSOR_HUMIDITY_H__ +#define __SENSOR_HUMIDITY_H__ + +#include "os/os.h" +#include "os/os_dev.h" +#include "sensor/sensor.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Data representing a singular read from a pressure sensor + * All values are in %rH + */ +struct sensor_humid_data { + float shd_humid; + + /* Validity */ + uint8_t shd_humid_is_valid:1; +} __attribute__((packed)); + +#ifdef __cplusplus +} +#endif + +#endif /* __SENSOR_HUMIDITY_H__ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/sensor/include/sensor/pressure.h ---------------------------------------------------------------------- diff --git a/hw/sensor/include/sensor/pressure.h b/hw/sensor/include/sensor/pressure.h new file mode 100644 index 0000000..425fd91 --- /dev/null +++ b/hw/sensor/include/sensor/pressure.h @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef __SENSOR_PRESSURE_H__ +#define __SENSOR_PRESSURE_H__ + +#include "os/os.h" +#include "os/os_dev.h" +#include "sensor/sensor.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Data representing a singular read from a pressure sensor + * All values are in Pa + */ +struct sensor_press_data { + float spd_press; + + /* Validity */ + uint8_t spd_press_is_valid:1; +} __attribute__((packed)); + +#ifdef __cplusplus +} +#endif + +#endif /* __SENSOR_PRESSURE_H__ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/sensor/include/sensor/temperature.h ---------------------------------------------------------------------- diff --git a/hw/sensor/include/sensor/temperature.h b/hw/sensor/include/sensor/temperature.h new file mode 100644 index 0000000..247631f --- /dev/null +++ b/hw/sensor/include/sensor/temperature.h @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef __SENSOR_TEMPERATURE_H__ +#define __SENSOR_TEMPERATURE_H__ + +#include "os/os.h" +#include "os/os_dev.h" +#include "sensor/sensor.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Data representing a singular read from a temperature sensor + * All values are in Deg C + */ +struct sensor_temp_data { + float std_temp; + + /* Validity */ + uint8_t std_temp_is_valid:1; +} __attribute__((packed)); + +#ifdef __cplusplus +} +#endif + +#endif /* __SENSOR_TEMPERATURE_H__ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1dc16358/hw/sensor/src/sensor_shell.c ---------------------------------------------------------------------- diff --git a/hw/sensor/src/sensor_shell.c b/hw/sensor/src/sensor_shell.c index ebe3b1b..69c1b94 100644 --- a/hw/sensor/src/sensor_shell.c +++ b/hw/sensor/src/sensor_shell.c @@ -37,6 +37,9 @@ #include "sensor/quat.h" #include "sensor/euler.h" #include "sensor/color.h" +#include "sensor/temperature.h" +#include "sensor/pressure.h" +#include "sensor/humidity.h" #include "console/console.h" #include "shell/shell.h" #include "hal/hal_i2c.h" @@ -230,6 +233,9 @@ sensor_shell_read_listener(struct sensor *sensor, void *arg, void *data) struct sensor_euler_data *sed; struct sensor_quat_data *sqd; struct sensor_color_data *scd; + struct sensor_temp_data *std; + struct sensor_press_data *spd; + struct sensor_humid_data *shd; char tmpstr[13]; ctx = (struct sensor_shell_read_ctx *) arg; @@ -287,7 +293,10 @@ sensor_shell_read_listener(struct sensor *sensor, void *arg, void *data) } if (ctx->type == SENSOR_TYPE_TEMPERATURE) { - console_printf("temprature = %d", *(int *)data); + std = (struct sensor_temp_data *) data; + if (std->std_temp_is_valid) { + console_printf("temprature = %s", sensor_ftostr(std->std_temp, tmpstr, 13)); + } console_printf("\n"); } @@ -365,12 +374,20 @@ sensor_shell_read_listener(struct sensor *sensor, void *arg, void *data) } if (ctx->type == SENSOR_TYPE_PRESSURE) { - console_printf("pressure = %d", *(int *)data); + spd = (struct sensor_press_data *) data; + if (spd->spd_press_is_valid) { + console_printf("pressure = %s Pa", + sensor_ftostr(spd->spd_press, tmpstr, 13)); + } console_printf("\n"); } if (ctx->type == SENSOR_TYPE_RELATIVE_HUMIDITY) { - console_printf("relative humidity = %d", *(int *)data); + shd = (struct sensor_humid_data *) data; + if (shd->shd_humid_is_valid) { + console_printf("relative humidity = %s", + sensor_ftostr(shd->shd_humid, tmpstr, 13)); + } console_printf("\n"); }
