MYNEWT-748 SensorAPI: Add BME280 support - SensorAPI pressure, temperature and humidity support - Compiles
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/ad7cdf09 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/ad7cdf09 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/ad7cdf09 Branch: refs/heads/bluetooth5 Commit: ad7cdf093a9cf325df02b4a27bcf1d70529dce9f Parents: afc404f Author: Vipul Rahane <[email protected]> Authored: Mon Apr 24 11:54:02 2017 -0700 Committer: Vipul Rahane <[email protected]> Committed: Fri May 12 17:08:08 2017 -0700 ---------------------------------------------------------------------- apps/sensors_test/pkg.yml | 1 + apps/sensors_test/src/main.c | 20 + apps/sensors_test/syscfg.yml | 10 +- hw/bsp/nrf52dk/src/hal_bsp.c | 12 + .../sensors/bme280/include/bme280/bme280.h | 245 +++++++ hw/drivers/sensors/bme280/pkg.yml | 32 + hw/drivers/sensors/bme280/src/bme280.c | 714 +++++++++++++++++++ hw/drivers/sensors/bme280/src/bme280_priv.h | 110 +++ hw/drivers/sensors/bme280/src/bme280_shell.c | 364 ++++++++++ hw/drivers/sensors/bme280/syscfg.yml | 38 + hw/sensor/src/sensor_shell.c | 14 +- 11 files changed, 1554 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/apps/sensors_test/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/sensors_test/pkg.yml b/apps/sensors_test/pkg.yml index 4f8e25c..9f56e7b 100644 --- a/apps/sensors_test/pkg.yml +++ b/apps/sensors_test/pkg.yml @@ -33,6 +33,7 @@ pkg.deps: - hw/drivers/sensors/tsl2561 - hw/drivers/sensors/bno055 - hw/drivers/sensors/tcs34725 + - hw/drivers/sensors/bme280 - boot/bootutil - sys/shell - sys/config http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/apps/sensors_test/src/main.c ---------------------------------------------------------------------- diff --git a/apps/sensors_test/src/main.c b/apps/sensors_test/src/main.c index d05cb94..152d48e 100755 --- a/apps/sensors_test/src/main.c +++ b/apps/sensors_test/src/main.c @@ -34,6 +34,7 @@ #include <tsl2561/tsl2561.h> #include <tcs34725/tcs34725.h> #include <bno055/bno055.h> +#include <bme280/bme280.h> #include "flash_map/flash_map.h" #include <hal/hal_system.h> #if MYNEWT_VAL(SPLIT_LOADER) @@ -386,6 +387,25 @@ config_sensor(void) struct os_dev *dev; int rc; +#if MYNEWT_VAL(BME280_PRESENT) + struct bme280_cfg bmecfg; + + dev = (struct os_dev *) os_dev_open("bme280", OS_TIMEOUT_NEVER, NULL); + assert(dev != NULL); + rc = bme280_init(dev, NULL); + if (rc) { + os_dev_close(dev); + goto err; + } + + rc = bme280_config((struct bme280 *)dev, &bmecfg); + if (rc) { + os_dev_close(dev); + goto err; + } + os_dev_close(dev); +#endif + #if MYNEWT_VAL(TCS34725_PRESENT) struct tcs34725_cfg tcscfg; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/apps/sensors_test/syscfg.yml ---------------------------------------------------------------------- diff --git a/apps/sensors_test/syscfg.yml b/apps/sensors_test/syscfg.yml index 81876bc..8172a62 100644 --- a/apps/sensors_test/syscfg.yml +++ b/apps/sensors_test/syscfg.yml @@ -42,8 +42,9 @@ syscfg.vals: CONFIG_NEWTMGR: 0 TSL2561_CLI: 0 - BNO055_CLI: 1 - TCS34725_CLI: 1 + BNO055_CLI: 0 + TCS34725_CLI: 0 + BME280_CLI: 0 # Setup Sensor BLE OIC GATT Server SENSOR_OIC: 1 @@ -65,10 +66,13 @@ syscfg.defs: value : 0 BNO055_PRESENT: description: 'BNO055 is present' + value : 0 + BME280_PRESENT: + description: 'BME280 is present' value : 1 TCS34725_PRESENT: description: 'TCS34725 is present' - value : 1 + value : 0 SIM_ACCEL_PRESENT: description: 'SIM ACCEL is present' value : 0 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/hw/bsp/nrf52dk/src/hal_bsp.c ---------------------------------------------------------------------- diff --git a/hw/bsp/nrf52dk/src/hal_bsp.c b/hw/bsp/nrf52dk/src/hal_bsp.c index c0031b1..46c084d 100644 --- a/hw/bsp/nrf52dk/src/hal_bsp.c +++ b/hw/bsp/nrf52dk/src/hal_bsp.c @@ -55,6 +55,9 @@ static struct lsm303dlhc lsm303dlhc; #if MYNEWT_VAL(TCS34725_PRESENT) #include <tcs34725/tcs34725.h> #endif +#if MYNEWT_VAL(BME280_PRESENT) +#include <bme280/bme280.h> +#endif #if MYNEWT_VAL(LSM303DLHC_PRESENT) static struct lsm303dlhc lsm303dlhc; @@ -72,6 +75,10 @@ static struct tsl2561 tsl2561; static struct tcs34725 tcs34725; #endif +#if MYNEWT_VAL(BME280_PRESENT) +static struct bme280 bme280; +#endif + #if MYNEWT_VAL(UART_0) static struct uart_dev os_bsp_uart0; static const struct nrf52_uart_cfg os_bsp_uart0_cfg = { @@ -234,6 +241,11 @@ sensor_dev_create(void) assert(rc == 0); #endif +#if MYNEWT_VAL(BME280_PRESENT) + rc = os_dev_create((struct os_dev *) &bme280, "bme280", + OS_DEV_INIT_PRIMARY, 0, color_init, NULL); + assert(rc == 0); +#endif } void http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/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 new file mode 100644 index 0000000..f5ea131 --- /dev/null +++ b/hw/drivers/sensors/bme280/include/bme280/bme280.h @@ -0,0 +1,245 @@ +/* + * 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 __BME280_H__ +#define __BME280_H__ + +#include <os/os.h> +#include "os/os_dev.h" +#include "sensor/sensor.h" + +#define BME280_SPI_READ_CMD_BIT 0x80 + +/* Sampling */ +#define BME280_SAMPLING_NONE 0x0 +#define BME280_SAMPLING_X1 0x1 +#define BME280_SAMPLING_X2 0x2 +#define BME280_SAMPLING_X4 0x3 +#define BME280_SAMPLING_X8 0x4 +#define BME280_SAMPLING_X16 0x5 + +/* Operating modes */ +#define BME280_MODE_SLEEP 0x0 +#define BME280_MODE_FORCED 0x1 +#define BME280_MODE_NORMAL 0x3 + +/* Filter settings */ +#define BME280_FILTER_OFF 0x0 +#define BME280_FILTER_X2 0x1 +#define BME280_FILTER_X4 0x2 +#define BME280_FILTER_X8 0x3 +#define BME280_FILTER_X16 0x4 + +/* Standby durations in ms */ +#define BME280_STANDBY_MS_0_5 0x0 +#define BME280_STANDBY_MS_10 0x6 +#define BME280_STANDBY_MS_20 0x7 +#define BME280_STANDBY_MS_62_5 0x1 +#define BME280_STANDBY_MS_125 0x2 +#define BME280_STANDBY_MS_250 0x3 +#define BME280_STANDBY_MS_500 0x4 +#define BME280_STANDBY_MS_1000 0x5 + +#ifdef __cplusplus +extern "C" { +#endif + +struct bme280_calib_data { + uint16_t bcd_dig_T1; + int16_t bcd_dig_T2; + int16_t bcd_dig_T3; + + uint16_t bcd_dig_P1; + int16_t bcd_dig_P2; + int16_t bcd_dig_P3; + int16_t bcd_dig_P4; + int16_t bcd_dig_P5; + int16_t bcd_dig_P6; + int16_t bcd_dig_P7; + int16_t bcd_dig_P8; + int16_t bcd_dig_P9; + + uint8_t bcd_dig_H1; + int16_t bcd_dig_H2; + uint8_t bcd_dig_H3; + int16_t bcd_dig_H4; + int16_t bcd_dig_H5; + int8_t bcd_dig_H6; +}; + +struct bme280_over_cfg { + sensor_type_t boc_type; + uint8_t boc_oversample; +}; + +struct bme280_cfg { + uint8_t bc_iir; + struct bme280_over_cfg bc_boc[3]; + uint8_t bc_mode; +}; + +struct bme280 { + struct os_dev dev; + struct sensor sensor; + struct bme280_cfg cfg; + os_time_t last_read_time; +}; + +/** + * Initialize the bme280. + * + * @param dev Pointer to the bme280_dev device descriptor + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_init(struct os_dev *dev, void *arg); + +/** + * Sets IIR filter + * + * @param filter setting + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_set_iir(uint8_t iir); + +/** + * Get IIR filter setting + * + * @param ptr to fill up iir setting into + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_get_iir(uint8_t *iir); + +/** + * Gets a new data sample from the sensor. + * + * @param temperature sensor output + * @param pressure sensor output + * @param humidity sensor output + * @param bme280 config and OS device structure + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_get_data(uint32_t *temp, uint32_t *press, uint32_t *humidity, + struct bme280 *bme280); + +/** + * Gets temperature + * + * @param temperature + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_get_temperature(uint32_t *temp); + +/** + * Gets pressure + * + * @param pressure + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_get_pressure(uint32_t *press); + +/** + * Gets humidity + * + * @param humidity + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_get_humidity(uint32_t *humidity); + +/** + * Sets the sampling rate + * + * @param sensor type + * @param sampling rate + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_set_oversample(sensor_type_t type, uint8_t rate); + +/** + * Gets the current sampling rate for the type of sensor + * + * @param Type of sensor to return sampling rate + * + * @return 0 on success, non-zero on failure + */ +int bme280_get_oversample(sensor_type_t type, uint8_t *rate); + +/** + * Sets the operating mode + * + * @param mode + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_set_mode(uint8_t mode); + +/** + * Gets the operating mode + * + * @param ptr to the mode variable to be filled up + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_get_mode(uint8_t *mode); + +/** + * Resets the BME280 chip + * + * @return 0 on success, non-zero on failure + */ +int +bme280_reset(void); + +/** + * Configure BME280 sensor + * + * @param Sensor device BME280 structure + * @param Sensor device BME280 config + * + * @return 0 on success, and non-zero error code on failure + */ +int bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg); + +/** + * Get the chip id + * + * @param ptr to fill up the chip id + * + * @return 0 on success, and non-zero error code on failure + */ +int +bme280_get_chipid(uint8_t *chipid); + +#if MYNEWT_VAL(BME280_CLI) +int bme280_shell_init(void); +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* __BME280_H__ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/hw/drivers/sensors/bme280/pkg.yml ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bme280/pkg.yml b/hw/drivers/sensors/bme280/pkg.yml new file mode 100644 index 0000000..496e35e --- /dev/null +++ b/hw/drivers/sensors/bme280/pkg.yml @@ -0,0 +1,32 @@ +# +# 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. +# + +pkg.name: hw/drivers/sensors/bme280 +pkg.description: Driver for the BME280 light to digital sensor +pkg.author: "Vipul Rahane <[email protected]>" +pkg.homepage: "http://www.runtime.io/" +pkg.keywords: + - adafruit + - bme280 + - spi + - sensor + +pkg.deps: + - "@apache-mynewt-core/kernel/os" + - "@apache-mynewt-core/hw/hal" http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/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 new file mode 100644 index 0000000..ca5ee0a --- /dev/null +++ b/hw/drivers/sensors/bme280/src/bme280.c @@ -0,0 +1,714 @@ +/* + * 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. + */ + +#include <assert.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> + +#include "defs/error.h" +#include "os/os.h" +#include "sysinit/sysinit.h" +#include "hal/hal_spi.h" +#include "sensor/sensor.h" +#include "bme280/bme280.h" +#include "bme280_priv.h" +#include "hal/hal_gpio.h" + +#if MYNEWT_VAL(BME280_LOG) +#include "log/log.h" +#endif + +#if MYNEWT_VAL(BME280_STATS) +#include "stats/stats.h" +#endif + +static struct hal_spi_settings spi_bme280_settings = { + .data_order = HAL_SPI_MSB_FIRST, + .data_mode = HAL_SPI_MODE0, + .baudrate = 500, + .word_size = HAL_SPI_WORD_SIZE_8BIT, +}; + +#if MYNEWT_VAL(BME280_STATS) +/* Define the stats section and records */ +STATS_SECT_START(bme280_stat_section) + STATS_SECT_ENTRY(errors) +STATS_SECT_END + +/* Define stat names for querying */ +STATS_NAME_START(bme280_stat_section) + STATS_NAME(bme280_stat_section, errors) +STATS_NAME_END(bme280_stat_section) + +/* Global variable used to hold stats data */ +STATS_SECT_DECL(bme280_stat_section) g_bme280stats; +#endif + +#if MYNEWT_VAL(BME280_LOG) +#define LOG_MODULE_BME280 (2561) +#define BME280_INFO(...) LOG_INFO(&_log, LOG_MODULE_BME280, __VA_ARGS__) +#define BME280_ERR(...) LOG_ERROR(&_log, LOG_MODULE_BME280, __VA_ARGS__) +static struct log _log; +#else +#define BME280_INFO(...) +#define BME280_ERR(...) +#endif + +/* Exports for the sensor interface. + */ +static void *bme280_sensor_get_interface(struct sensor *, sensor_type_t); +static int bme280_sensor_read(struct sensor *, sensor_type_t, + sensor_data_func_t, void *, uint32_t); +static int bme280_sensor_get_config(struct sensor *, sensor_type_t, + struct sensor_cfg *); + +static const struct sensor_driver g_bme280_sensor_driver = { + bme280_sensor_get_interface, + bme280_sensor_read, + bme280_sensor_get_config +}; + +static int +bme280_default_cfg(struct bme280_cfg *cfg) +{ + cfg->bc_iir = BME280_FILTER_OFF; + cfg->bc_mode = BME280_MODE_NORMAL; + + cfg->bc_boc[0].boc_type = SENSOR_TYPE_TEMPERATURE; + cfg->bc_boc[0].boc_oversample = BME280_SAMPLING_NONE; + cfg->bc_boc[1].boc_type = SENSOR_TYPE_PRESSURE; + cfg->bc_boc[1].boc_oversample = BME280_SAMPLING_NONE; + cfg->bc_boc[2].boc_type = SENSOR_TYPE_RELATIVE_HUMIDITY; + cfg->bc_boc[2].boc_oversample = BME280_SAMPLING_NONE; + + return 0; +} + +/** + * Expects to be called back through os_dev_create(). + * + * @param The device object associated with bme280 + * @param Argument passed to OS device init, unused + * + * @return 0 on success, non-zero error on failure. + */ +int +bme280_init(struct os_dev *dev, void *arg) +{ + struct bme280 *bme280; + struct sensor *sensor; + int rc; + + bme280 = (struct bme280 *) dev; + + rc = bme280_default_cfg(&bme280->cfg); + if (rc) { + goto err; + } + +#if MYNEWT_VAL(BME280_LOG) + log_register("bme280", &_log, &log_console_handler, NULL, LOG_SYSLEVEL); +#endif + + sensor = &bme280->sensor; + +#if MYNEWT_VAL(BME280_STATS) + /* Initialise the stats entry */ + rc = stats_init( + STATS_HDR(g_bme280stats), + STATS_SIZE_INIT_PARMS(g_bme280stats, STATS_SIZE_32), + STATS_NAME_INIT_PARMS(bme280_stat_section)); + SYSINIT_PANIC_ASSERT(rc == 0); + /* Register the entry with the stats registry */ + rc = stats_register("bme280", STATS_HDR(g_bme280stats)); + SYSINIT_PANIC_ASSERT(rc == 0); +#endif + rc = sensor_init(sensor, dev); + if (rc != 0) { + goto err; + } + + /* Add the driver */ + rc = sensor_set_driver(sensor, SENSOR_TYPE_TEMPERATURE | + SENSOR_TYPE_PRESSURE | + SENSOR_TYPE_RELATIVE_HUMIDITY, + (struct sensor_driver *) &g_bme280_sensor_driver); + if (rc != 0) { + goto err; + } + + rc = sensor_mgr_register(sensor); + if (rc != 0) { + goto err; + } + + rc = hal_spi_config(MYNEWT_VAL(BME280_SPINUM), &spi_bme280_settings); + if (rc) { + goto err; + } + + rc = hal_spi_enable(MYNEWT_VAL(BME280_SPINUM)); + if (rc) { + goto err; + } + + rc = hal_gpio_init_out(MYNEWT_VAL(BME280_CSPIN), 1); + if (rc) { + goto err; + } + + return (0); +err: + return (rc); + +} + +static void * +bme280_sensor_get_interface(struct sensor *sensor, sensor_type_t type) +{ + return (NULL); +} + +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; + int rc; + + if (!(type & SENSOR_TYPE_PRESSURE) || + !(type & SENSOR_TYPE_TEMPERATURE) || + !(type & SENSOR_TYPE_RELATIVE_HUMIDITY)) { + rc = SYS_EINVAL; + goto err; + } + + temp = press = humid = 0; + + /* Get a new pressure sample */ + if (type & SENSOR_TYPE_PRESSURE) { + rc = bme280_get_pressure(&press); + if (rc) { + goto err; + } + + //lux = bme280_calculate_lux(full, ir, &(bme280->cfg)); + + /* Call data function */ + rc = data_func(sensor, data_arg, &press); + if (rc) { + goto err; + } + } + + /* Get a new temperature sample */ + if (type & SENSOR_TYPE_TEMPERATURE) { + rc = bme280_get_temperature(&temp); + if (rc) { + goto err; + } + + //lux = bme280_calculate_lux(full, ir, &(bme280->cfg)); + + /* Call data function */ + rc = data_func(sensor, data_arg, &temp); + if (rc) { + goto err; + } + } + + /* Get a new relative humidity sample */ + if (type & SENSOR_TYPE_RELATIVE_HUMIDITY) { + rc = bme280_get_humidity(&humid); + if (rc) { + goto err; + } + + //lux = bme280_calculate_lux(full, ir, &(bme280->cfg)); + + /* Call data function */ + rc = data_func(sensor, data_arg, &humid); + if (rc) { + goto err; + } + } + + return 0; +err: + return rc; +} + +static int +bme280_sensor_get_config(struct sensor *sensor, sensor_type_t type, + struct sensor_cfg *cfg) +{ + int rc; + + if (!(type & SENSOR_TYPE_PRESSURE) || + !(type & SENSOR_TYPE_TEMPERATURE) || + !(type & SENSOR_TYPE_RELATIVE_HUMIDITY)) { + rc = SYS_EINVAL; + goto err; + } + + cfg->sc_valtype = SENSOR_VALUE_TYPE_INT32; + + return (0); +err: + return (rc); +} + +/** + * Configure BME280 sensor + * + * @param Sensor device BME280 structure + * @param Sensor device BME280 config + * + * @return 0 on success, non-zero on failure + */ +int +bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg) +{ + int rc; + + rc = bme280_set_iir(cfg->bc_iir); + + rc |= bme280_set_mode(cfg->bc_mode); + + rc |= bme280_set_oversample(cfg->bc_boc[0].boc_type, + cfg->bc_boc[0].boc_oversample); + + rc |= bme280_set_oversample(cfg->bc_boc[1].boc_type, + cfg->bc_boc[1].boc_oversample); + + rc |= bme280_set_oversample(cfg->bc_boc[2].boc_type, + cfg->bc_boc[2].boc_oversample); + + if (rc) { + goto err; + } + + /* Overwrite the configuration data. */ + memcpy(&bme280->cfg, cfg, sizeof(*cfg)); + +err: + return (rc); +} + +/** + * Read multiple length data from BME280 sensor over SPI + * + * @param register address + * @param variable length payload + * @param length of the payload to read + * + * @return 0 on success, non-zero on failure + */ +int +bme280_readlen(uint8_t addr, uint8_t *payload, uint8_t len) +{ + int i; + int rc; + uint8_t txdata; + uint8_t rxdata; + + /* Select the device */ + 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); + if (rc) { + goto err; + } + + txdata = 0; + + for (i = 0; i < len; i++) { + /* Read data */ + rc = hal_spi_txrx(MYNEWT_VAL(BME280_SPINUM), &txdata, &rxdata, 1); + if (rc) { + goto err; + } + + payload[i] = rxdata; + } + + /* De-select the device */ + hal_gpio_write(MYNEWT_VAL(BME280_CSPIN), 1); + + return 0; +err: + return rc; +} + +/** + * Write multiple length data to BME280 sensor over SPI + * + * @param register address + * @param variable length payload + * @param length of the payload to write + * + * @return 0 on success, non-zero on failure + */ +int +bme280_writelen(uint8_t addr, uint8_t *payload, uint8_t len) +{ + int i; + int rc; + + /* Select the device */ + 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); + if (rc) { + goto err; + } + + for (i = 0; i < len; i++) { + /* Read data */ + rc = hal_spi_tx_val(MYNEWT_VAL(BME280_SPINUM), payload[i]); + if (rc) { + goto err; + } + } + + /* De-select the device */ + hal_gpio_write(MYNEWT_VAL(BME280_CSPIN), 1); + + return 0; +err: + return rc; +} + +/** + * Gets temperature + * + * @param temperature + * + * @return 0 on success, and non-zero error code on failure + */ +int +bme280_get_temperature(uint32_t *temp) +{ + int rc; + uint8_t tmp[3]; + + rc = bme280_readlen(BME280_REG_ADDR_TEMP, tmp, 3); + if (rc) { + goto err; + } + + *temp = (tmp[1] << 8 | tmp[0]) << 4 | (tmp[2] >> 4); + +err: + return rc; +} + +/** + * Gets humidity + * + * @param humidity + * + * @return 0 on success, and non-zero error code on failure + */ +int +bme280_get_humidity(uint32_t *humid) +{ + int rc; + uint8_t tmp[2]; + + rc = bme280_readlen(BME280_REG_ADDR_HUM, tmp, 2); + if (rc) { + goto err; + } + + *humid = (tmp[1] << 8 | tmp[0]); + +err: + return rc; +} + +/** + * Gets pressure + * + * @param pressure + * + * @return 0 on success, and non-zero error code on failure + */ +int +bme280_get_pressure(uint32_t *press) +{ + int rc; + uint8_t tmp[3]; + + rc = bme280_readlen(BME280_REG_ADDR_PRESS, tmp, 2); + if (rc) { + goto err; + } + + *press = (tmp[1] << 8 | tmp[0]) << 4 | (tmp[2] >> 4); + +err: + return rc; +} + +/** + * Resets the BME280 chip + * + * @return 0 on success, non-zero on failure + */ +int +bme280_reset(void) +{ + uint8_t txdata; + + txdata = 1; + + return bme280_writelen(BME280_REG_ADDR_RESET, &txdata, 1); +} + +/** + * Get IIR filter setting + * + * @param ptr to fill up iir setting into + * + * @return 0 on success, non-zero on failure + */ +int +bme280_get_iir(uint8_t *iir) +{ + int rc; + uint8_t tmp; + + rc = bme280_readlen(BME280_REG_ADDR_CONFIG, &tmp, 1); + if (rc) { + goto err; + } + + *iir = ((tmp & BME280_REG_CONFIG_FILTER) >> 5); + +err: + return rc; +} + +/** + * Sets IIR filter + * + * @param filter setting + * + * @return 0 on success, non-zero on failure + */ +int +bme280_set_iir(uint8_t iir) +{ + int rc; + uint8_t cfg; + + rc = bme280_readlen(BME280_REG_ADDR_CONFIG, &cfg, 1); + if (rc) { + goto err; + } + + iir = cfg | ((iir << 5) & BME280_REG_CONFIG_FILTER); + + rc = bme280_writelen(BME280_REG_ADDR_CONFIG, &iir, 1); + if (rc) { + goto err; + } + + return 0; +err: + return rc; +} + +/** + * Gets the operating mode + * + * @param ptr to the mode variable to be filled up + * + * @return 0 on success, and non-zero error code on failure + */ +int +bme280_get_mode(uint8_t *mode) +{ + int rc; + uint8_t tmp; + + rc = bme280_readlen(BME280_REG_ADDR_CTRL_MEAS, &tmp, 1); + if (rc) { + goto err; + } + + *mode = (tmp & BME280_REG_CTRL_MEAS_MODE); + +err: + return rc; +} + +/** + * Sets the operating mode + * + * @param mode + * + * @return 0 on success, and non-zero error code on failure + */ +int +bme280_set_mode(uint8_t mode) +{ + int rc; + uint8_t cfg; + + rc = bme280_readlen(BME280_REG_ADDR_CTRL_MEAS, &cfg, 1); + if (rc) { + goto err; + } + + mode = cfg | (mode & BME280_REG_CTRL_MEAS_MODE); + + rc = bme280_writelen(BME280_REG_ADDR_CTRL_MEAS, &mode, 1); + if (rc) { + goto err; + } + + return 0; +err: + return rc; +} + +/** + * Gets the current sampling rate for the type of sensor + * + * @param Type of sensor to return sampling rate + * + * @return 0 on success, non-zero on failure + */ +int +bme280_get_oversample(sensor_type_t type, uint8_t *oversample) +{ + int rc; + uint8_t tmp; + + if (type & SENSOR_TYPE_TEMPERATURE || type & SENSOR_TYPE_PRESSURE) { + rc = bme280_readlen(BME280_REG_ADDR_CTRL_MEAS, &tmp, 1); + if (rc) { + goto err; + } + + if (type & SENSOR_TYPE_TEMPERATURE) { + *oversample = ((tmp & BME280_REG_CTRL_MEAS_TOVER) >> 5); + } + + if (type & SENSOR_TYPE_PRESSURE) { + *oversample = ((tmp & BME280_REG_CTRL_MEAS_POVER) >> 3); + } + } + + if (type & SENSOR_TYPE_RELATIVE_HUMIDITY) { + rc = bme280_readlen(BME280_REG_ADDR_CTRL_HUM, &tmp, 1); + if (rc) { + goto err; + } + *oversample = (tmp & BME280_REG_CTRL_HUM_HOVER); + } + + return 0; +err: + return rc; +} + +/** + * Sets the sampling rate + * + * @param sensor type + * @param sampling rate + * + * @return 0 on success, and non-zero error code on failure + */ +int +bme280_set_oversample(sensor_type_t type, uint8_t oversample) +{ + int rc; + uint8_t cfg; + + if (type & SENSOR_TYPE_TEMPERATURE || type & SENSOR_TYPE_PRESSURE) { + rc = bme280_readlen(BME280_REG_ADDR_CTRL_MEAS, &cfg, 1); + if (rc) { + goto err; + } + + if (type & SENSOR_TYPE_TEMPERATURE) { + oversample = cfg | ((oversample << 5) & BME280_REG_CTRL_MEAS_TOVER); + } + + if (type & SENSOR_TYPE_PRESSURE) { + oversample = cfg | ((oversample << 3) & BME280_REG_CTRL_MEAS_POVER); + } + + rc = bme280_writelen(BME280_REG_ADDR_CTRL_MEAS, &oversample, 1); + if (rc) { + goto err; + } + } + + if (type & SENSOR_TYPE_RELATIVE_HUMIDITY) { + rc = bme280_readlen(BME280_REG_ADDR_CTRL_HUM, &cfg, 1); + if (rc) { + goto err; + } + + oversample = cfg | (oversample & BME280_REG_CTRL_HUM_HOVER); + + rc = bme280_writelen(BME280_REG_ADDR_CTRL_HUM, &oversample, 1); + if (rc) { + goto err; + } + } + + return 0; +err: + return rc; +} + +/** + * Get the chip id + * + * @param ptr to fill up the chip id + * + * @return 0 on success, non-zero on failure + */ +int +bme280_get_chipid(uint8_t *chipid) +{ + int rc; + uint8_t tmp; + + rc = bme280_readlen(BME280_REG_ADDR_PRESS, &tmp, 1); + if (rc) { + goto err; + } + + *chipid = tmp; + +err: + return rc; +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/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 new file mode 100644 index 0000000..1c93aeb --- /dev/null +++ b/hw/drivers/sensors/bme280/src/bme280_priv.h @@ -0,0 +1,110 @@ +/* + * 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 __BME280_PRIV_H__ +#define __BME280_PRIV_H__ + +#define BME280_REG_ADDR_DIG_T1 0x88 +#define BME280_REG_ADDR_DIG_T2 0x8A +#define BME280_REG_ADDR_DIG_T3 0x8C + +#define BME280_REG_ADDR_DIG_P1 0x8E +#define BME280_REG_ADDR_DIG_P2 0x90 +#define BME280_REG_ADDR_DIG_P3 0x92 +#define BME280_REG_ADDR_DIG_P4 0x94 +#define BME280_REG_ADDR_DIG_P5 0x96 +#define BME280_REG_ADDR_DIG_P6 0x98 +#define BME280_REG_ADDR_DIG_P7 0x9A +#define BME280_REG_ADDR_DIG_P8 0x9C +#define BME280_REG_ADDR_DIG_P9 0x9E + +#define BME280_REG_ADDR_DIG_H1 0xA1 +#define BME280_REG_ADDR_DIG_H2 0xE1 +#define BME280_REG_ADDR_DIG_H3 0xE3 +#define BME280_REG_ADDR_DIG_H4 0xE4 +#define BME280_REG_ADDR_DIG_H5 0xE5 +#define BME280_REG_ADDR_DIG_H6 0xE7 + +#define BME280_REG_ADDR_CHIPID 0xD0 +#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_CTRL_HUM 0xF2 +#define BME280_REG_CTRL_HUM_NONE (0x1F) +#define BME280_REG_CTRL_HUM_HOVER (0x11) + +#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_ADDR_CONFIG 0xF5 +#define BME280_REG_CONFIG_STANDBY (0x11 << 5) +#define BME280_REG_CONFIG_FILTER (0x11 << 3) +#define BME280_REG_CONFIG_SPI3_EN (0x1) + +#define BME280_REG_ADDR_PRESS 0xF7 +#define BME280_REG_ADDR_PRESS_MSB 0xF7 +#define BME280_REG_ADDR_PRESS_LSB 0xF8 +#define BME280_REG_ADDR_PRESS_XLSB 0xF9 + +#define BME280_REG_ADDR_TEMP 0xFA +#define BME280_REG_ADDR_TEMP_MSB 0xFA +#define BME280_REG_ADDR_TEMP_LSB 0xFB +#define BME280_REG_ADDR_TEMP_XLSB 0xFC + +#define BME280_REG_ADDR_HUM 0xFD + +#define BME280_REG_ADDR_RESET 0xE0 + +#define BME280_REG_CHIPID 0x60 + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Write multiple length data to BME280 sensor over SPI + * + * @param register address + * @param variable length payload + * @param length of the payload to write + */ +int bme280_writelen(uint8_t addr, uint8_t *payload, uint8_t len); + +/** + * Read multiple length data from BME280 sensor over SPI + * + * @param register address + * @param variable length payload + * @param length of the payload to read + */ +int bme280_readlen(uint8_t addr, uint8_t *payload, uint8_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* __BME280_PRIV_H_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/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 new file mode 100644 index 0000000..f9d3f46 --- /dev/null +++ b/hw/drivers/sensors/bme280/src/bme280_shell.c @@ -0,0 +1,364 @@ +/* + * 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. + */ + +#include <string.h> +#include <errno.h> +#include "sysinit/sysinit.h" +#include "console/console.h" +#include "shell/shell.h" +#include "hal/hal_gpio.h" +#include "bme280/bme280.h" +#include "bme280_priv.h" + +#if MYNEWT_VAL(BME280_CLI) + +static int bme280_shell_cmd(int argc, char **argv); + +static struct shell_cmd bme280_shell_cmd_struct = { + .sc_cmd = "bme280", + .sc_cmd_func = bme280_shell_cmd +}; + +static int +bme280_shell_stol(char *param_val, long min, long max, long *output) +{ + char *endptr; + long lval; + + lval = strtol(param_val, &endptr, 10); /* Base 10 */ + if (param_val != '\0' && *endptr == '\0' && + lval >= min && lval <= max) { + *output = lval; + } else { + return EINVAL; + } + + return 0; +} + +static int +bme280_shell_err_too_many_args(char *cmd_name) +{ + console_printf("Error: too many arguments for command \"%s\"\n", + cmd_name); + return EINVAL; +} + +static int +bme280_shell_err_unknown_arg(char *cmd_name) +{ + console_printf("Error: unknown argument \"%s\"\n", + cmd_name); + return EINVAL; +} + +static int +bme280_shell_err_invalid_arg(char *cmd_name) +{ + console_printf("Error: invalid argument \"%s\"\n", + cmd_name); + return EINVAL; +} + +static int +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("\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("\tdump\n"); + + return 0; +} + +static int +bme280_shell_cmd_read_chipid(int argc, char **argv) +{ + int rc; + uint8_t chipid; + + rc = bme280_get_chipid(&chipid); + if (rc) { + goto err; + } + + console_printf("CHIP_ID:%02X", chipid); + + return 0; +err: + return rc; +} + +static int +bme280_shell_cmd_reset(int argc, char **argv) +{ + return bme280_reset(); +} + +static int +bme280_shell_cmd_read(int argc, char **argv) +{ + uint32_t temp; + uint32_t press; + uint32_t humid; + uint16_t samples = 1; + long val; + int rc; + struct bme280 bme280; + + if (argc > 3) { + return bme280_shell_err_too_many_args(argv[1]); + } + + /* Check if more than one sample requested */ + if (argc == 3) { + if (bme280_shell_stol(argv[2], 1, UINT16_MAX, &val)) { + return bme280_shell_err_invalid_arg(argv[2]); + } + samples = (uint16_t)val; + } + + while(samples--) { + + rc = bme280_get_data(&temp, &press, &humid, &bme280); + if (rc != 0) { + console_printf("Read failed: %d\n", rc); + return rc; + } + console_printf("temperature: %u\tpressure: %u\thumidity: %u\n", + temp, press, humid); + } + + return 0; +} + +static int +bme280_shell_cmd_oversample(int argc, char **argv) +{ + long val; + int rc; + uint8_t oversample; + uint32_t type; + + if (argc > 3) { + return bme280_shell_err_too_many_args(argv[1]); + } + + /* Display the oversample */ + if (argc == 3) { + if (bme280_shell_stol(argv[2], 4, 8, &val)) { + return bme280_shell_err_invalid_arg(argv[2]); + } + rc = bme280_get_oversampling(val, &oversample); + if (rc) { + goto err; + } + console_printf("%u\n", oversample); + } + + /* Update the oversampling */ + if (argc == 4) { + if (bme280_shell_stol(argv[2], 4, 8, &val)) { + return bme280_shell_err_invalid_arg(argv[2]); + } + + type = val; + + if (bme280_shell_stol(argv[3], 0, 5, &val)) { + return bme280_shell_err_invalid_arg(argv[2]); + } + + oversample = val; + + rc = bme280_set_oversample(type, oversample); + if (rc) { + goto err; + } + } + + return 0; +err: + return rc; +} + +static int +bme280_shell_cmd_iir(int argc, char **argv) +{ + uint8_t iir; + long val; + + if (argc > 3) { + return bme280_shell_err_too_many_args(argv[1]); + } + + /* Display if iir enabled */ + if (argc == 2) { + rc = bme280_get_iir(&iir); + if (rc) { + goto err; + } + console_printf("IIR: %02X", iir); + } + + /* Enable/disable iir*/ + if (argc == 3) { + if (bme280_shell_stol(argv[2], 0, 1, &val)) { + return bme280_shell_err_invalid_arg(argv[2]); + } + rc = bme280_set_iir(val); + if (rc) { + goto err; + } + } + + return 0; +} + +static int +bme280_shell_cmd_dump(int argc, char **argv) +{ + uint8_t val; + + if (argc > 3) { + return bme280_shell_err_too_many_args(argv[1]); + } + + /* Dump all the register values for debug purposes */ + val = 0; + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_T1, &val, 1)); + console_printf("0x%02X (DIG_T1): 0x%02X\n", BME280_REG_ADDR_DIG_T1, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_T2, &val, 1)); + console_printf("0x%02X (DIG_T2): 0x%02X\n", BME280_REG_ADDR_DIG_T2, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_T3, &val, 1)); + console_printf("0x%02X (DIG_T3): 0x%02X\n", BME280_REG_ADDR_DIG_T3, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_P1, &val, 1)); + console_printf("0x%02X (DIG_P1): 0x%02X\n", BME280_REG_ADDR_DIG_P1, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_P2, &val, 1)); + console_printf("0x%02X (DIG_P2): 0x%02X\n", BME280_REG_ADDR_DIG_P2, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_P3, &val, 1)); + console_printf("0x%02X (DIG_P3): 0x%02X\n", BME280_REG_ADDR_DIG_P3, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_P4, &val, 1)); + console_printf("0x%02X (DIG_P4): 0x%02X\n", BME280_REG_ADDR_DIG_P4, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_P5, &val, 1)); + console_printf("0x%02X (DIG_P5): 0x%02X\n", BME280_REG_ADDR_DIG_P5, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_P6, &val, 1)); + console_printf("0x%02X (DIG_P6): 0x%02X\n", BME280_REG_ADDR_DIG_P6, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_P7, &val, 1)); + console_printf("0x%02X (DIG_P7): 0x%02X\n", BME280_REG_ADDR_DIG_P7, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_P8, &val, 1)); + console_printf("0x%02X (DIG_P8): 0x%02X\n", BME280_REG_ADDR_DIG_P8, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_P9, &val, 1)); + console_printf("0x%02X (DIG_P9): 0x%02X\n", BME280_REG_ADDR_DIG_P9, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_H1, &val, 1)); + console_printf("0x%02X (DIG_H1): 0x%02X\n", BME280_REG_ADDR_DIG_H1, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_H2, &val, 1)); + console_printf("0x%02X (DIG_H2): 0x%02X\n", BME280_REG_ADDR_DIG_H2, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_H3, &val, 1)); + console_printf("0x%02X (DIG_H3): 0x%02X\n", BME280_REG_ADDR_DIG_H3, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_H4, &val, 1)); + console_printf("0x%02X (DIG_H4): 0x%02X\n", BME280_REG_ADDR_DIG_H4, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_H5, &val, 1)); + console_printf("0x%02X (DIG_H5): 0x%02X\n", BME280_REG_ADDR_DIG_H5, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_DIG_H6, &val, 1)); + console_printf("0x%02X (DIG_H6): 0x%02X\n", BME280_REG_ADDR_DIG_H6, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_CHIPID, &val, 1)); + console_printf("0x%02X (CHIPID): 0x%02X\n", BME280_REG_ADDR_CHIPID, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_VERSION, &val, 1)); + console_printf("0x%02X (VER): 0x%02X\n", BME280_REG_ADDR_VERSION, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_CTRL_HUM, &val, 1)); + console_printf("0x%02X (CTRL_HUM): 0x%02X\n", BME280_REG_ADDR_CTRL_HUM, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_STATUS, &val, 1)); + console_printf("0x%02X (STATUS): 0x%02X\n", BME280_REG_ADDR_STATUS, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_CTRL_MEAS, &val, 1)); + console_printf("0x%02X (CTRL_MEAS): 0x%02X\n", BME280_REG_ADDR_CTRL_MEAS, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_CONFIG, &val, 1)); + console_printf("0x%02X (CONFIG): 0x%02X\n", BME280_REG_ADDR_CONFIG, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_PRESS, &val, 1)); + console_printf("0x%02X (PRESS): 0x%02X\n", BME280_REG_ADDR_PRESS, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_TEMP, &val, 1)); + console_printf("0x%02X (TEMP): 0x%02X\n", BME280_REG_ADDR_TEMP, val); + assert(0 == bme280_readlen(BME280_REG_ADDR_HUM, &val, 1)); + console_printf("0x%02X (HUM): 0x%02X\n", BME280_REG_ADDR_HUM, val); + + return 0; +} + +static int +bme280_shell_cmd(int argc, char **argv) +{ + if (argc == 1) { + return bme280_shell_help(); + } + + /* Read command (get a new data sample) */ + if (argc > 1 && strcmp(argv[1], "r") == 0) { + return bme280_shell_cmd_read(argc, argv); + } + + /* Mode command */ + if (argc > 1 && strcmp(argv[1], "mode") == 0) { + return bme280_shell_cmd_mode(argc, argv); + } + + /* IIR */ + if (argc > 1 && strcmp(argv[1], "iir") == 0) { + return bme280_shell_cmd_iir(argc, argv); + } + + /* Oversample */ + if (argc > 1 && strcmp(argv[1], "oversample") == 0) { + return bme280_shell_cmd_oversample(argc, argv); + } + + /* Reset */ + if (argc > 1 && strcmp(argv[1], "reset") == 0) { + return bme280_shell_cmd_reset(argc, argv); + } + + /* Chip ID */ + if (argc > 1 && strcmp(argv[1], "chipid") == 0) { + return bme280_shell_cmd_read_chipid(argc, argv); + } + + /* Dump */ + if (argc > 1 && strcmp(argv[1], "dump") == 0) { + return bme280_shell_cmd_dump(argc, argv); + } + + return bme280_shell_err_unknown_arg(argv[1]); +} + +int +bme280_shell_init(void) +{ + int rc; + + rc = shell_cmd_register(&bme280_shell_cmd_struct); + SYSINIT_PANIC_ASSERT(rc == 0); + + return rc; +} + +#endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/hw/drivers/sensors/bme280/syscfg.yml ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bme280/syscfg.yml b/hw/drivers/sensors/bme280/syscfg.yml new file mode 100644 index 0000000..126ecda --- /dev/null +++ b/hw/drivers/sensors/bme280/syscfg.yml @@ -0,0 +1,38 @@ +# The BSD License (BSD) +# +# Copyright (c) 2016 Adafruit Industries +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +syscfg.defs: + BME280_SPINUM: + description: 'SPI number for the BME280' + value: -1 + BME280_CLI: + description: 'Enable shell support for the BME280' + value: 0 + BME280_LOG: + description: 'Enable BME280 logging' + value: 0 + BME280_STATS: + description: 'Enable BME280 statistics' + value: 0 + BME280_CSPIN: + description: 'CS pin for BME280' + value : 0 http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad7cdf09/hw/sensor/src/sensor_shell.c ---------------------------------------------------------------------- diff --git a/hw/sensor/src/sensor_shell.c b/hw/sensor/src/sensor_shell.c index 481e241..ebe3b1b 100644 --- a/hw/sensor/src/sensor_shell.c +++ b/hw/sensor/src/sensor_shell.c @@ -230,7 +230,6 @@ 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; - int8_t *temperature; char tmpstr[13]; ctx = (struct sensor_shell_read_ctx *) arg; @@ -288,8 +287,7 @@ sensor_shell_read_listener(struct sensor *sensor, void *arg, void *data) } if (ctx->type == SENSOR_TYPE_TEMPERATURE) { - temperature = (int8_t *) data; - console_printf("temprature = %d", *temperature); + console_printf("temprature = %d", *(int *)data); console_printf("\n"); } @@ -366,6 +364,16 @@ sensor_shell_read_listener(struct sensor *sensor, void *arg, void *data) console_printf("\n\n"); } + if (ctx->type == SENSOR_TYPE_PRESSURE) { + console_printf("pressure = %d", *(int *)data); + console_printf("\n"); + } + + if (ctx->type == SENSOR_TYPE_RELATIVE_HUMIDITY) { + console_printf("relative humidity = %d", *(int *)data); + console_printf("\n"); + } + return (0); }
