You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by vi...@apache.org on 2017/05/13 00:32:18 UTC

[4/6] incubator-mynewt-core git commit: MYNEWT-748 SensorAPI: Add BME280 support

MYNEWT-748 SensorAPI: Add BME280 support

- SensorAPI pressure, temperature and humidity support
- OIC support
- ruuvi_tag_revb2 bsp, create sensor device
- nrf52840pdk bsp, create sensor device
- Add BMP280 sensor device support since BME280 and BMP280 are pin to
  pin compatible


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/8d98e078
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/8d98e078
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/8d98e078

Branch: refs/heads/master
Commit: 8d98e078c7281265f7f20af1b97042289d2c5a27
Parents: ad7cdf0
Author: Vipul Rahane <vi...@apache.org>
Authored: Thu Apr 27 14:56:20 2017 -0700
Committer: Vipul Rahane <vi...@apache.org>
Committed: Fri May 12 17:08:16 2017 -0700

----------------------------------------------------------------------
 apps/sensors_test/src/main.c                    |   6 ++
 apps/sensors_test/syscfg.yml                    |   2 +-
 hw/bsp/nrf52840pdk/src/hal_bsp.c                |  20 ++++
 hw/bsp/ruuvi_tag_revb2/src/hal_bsp.c            |  31 +++++-
 .../sensors/bme280/include/bme280/bme280.h      |   2 +-
 hw/drivers/sensors/bme280/src/bme280.c          | 105 ++++++++++++++-----
 hw/drivers/sensors/bme280/src/bme280_priv.h     |   4 +-
 hw/drivers/sensors/bme280/src/bme280_shell.c    |  61 ++++++++++-
 hw/sensor/src/sensor_oic.c                      |  29 +++--
 9 files changed, 209 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d98e078/apps/sensors_test/src/main.c
----------------------------------------------------------------------
diff --git a/apps/sensors_test/src/main.c b/apps/sensors_test/src/main.c
index 152d48e..ce5f4bd 100755
--- a/apps/sensors_test/src/main.c
+++ b/apps/sensors_test/src/main.c
@@ -398,6 +398,8 @@ config_sensor(void)
         goto err;
     }
 
+    memset(&bmecfg, 0, sizeof(bmecfg));
+
     rc = bme280_config((struct bme280 *)dev, &bmecfg);
     if (rc) {
         os_dev_close(dev);
@@ -566,6 +568,10 @@ sensors_dev_shell_init(void)
     bno055_shell_init();
 #endif
 
+#if MYNEWT_VAL(BME280_CLI)
+    bme280_shell_init();
+#endif
+
 }
 
 static void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d98e078/apps/sensors_test/syscfg.yml
----------------------------------------------------------------------
diff --git a/apps/sensors_test/syscfg.yml b/apps/sensors_test/syscfg.yml
index 8172a62..fe8b2d7 100644
--- a/apps/sensors_test/syscfg.yml
+++ b/apps/sensors_test/syscfg.yml
@@ -44,7 +44,7 @@ syscfg.vals:
     TSL2561_CLI: 0
     BNO055_CLI: 0
     TCS34725_CLI: 0
-    BME280_CLI: 0
+    BME280_CLI: 1
 
     # Setup Sensor BLE OIC GATT Server
     SENSOR_OIC: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d98e078/hw/bsp/nrf52840pdk/src/hal_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf52840pdk/src/hal_bsp.c b/hw/bsp/nrf52840pdk/src/hal_bsp.c
index bf2dd36..88b42b0 100644
--- a/hw/bsp/nrf52840pdk/src/hal_bsp.c
+++ b/hw/bsp/nrf52840pdk/src/hal_bsp.c
@@ -48,6 +48,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;
@@ -65,6 +68,9 @@ 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;
@@ -198,6 +204,14 @@ color_init(struct os_dev *dev, void *arg)
 }
 #endif
 
+#if MYNEWT_VAL(BME280_PRESENT)
+static int
+press_init(struct os_dev *dev, void *arg)
+{
+    return (0);
+}
+#endif
+
 static void
 sensor_dev_create(void)
 {
@@ -227,6 +241,12 @@ sensor_dev_create(void)
       OS_DEV_INIT_PRIMARY, 0, color_init, NULL);
     assert(rc == 0);
 #endif
+
+#if MYNEWT_VAL(BME280_PRESENT)
+    rc = os_dev_create((struct os_dev *) &bme280, "bme280",
+      OS_DEV_INIT_PRIMARY, 0, press_init, NULL);
+    assert(rc == 0);
+#endif
 }
 
 void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d98e078/hw/bsp/ruuvi_tag_revb2/src/hal_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/ruuvi_tag_revb2/src/hal_bsp.c b/hw/bsp/ruuvi_tag_revb2/src/hal_bsp.c
index ab960ce..30da796 100644
--- a/hw/bsp/ruuvi_tag_revb2/src/hal_bsp.c
+++ b/hw/bsp/ruuvi_tag_revb2/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 = {
@@ -198,7 +205,15 @@ slinky_light_init(struct os_dev *dev, void *arg)
 
 #if MYNEWT_VAL(TCS34725_PRESENT)
 static int
-slinky_color_init(struct os_dev *dev, void *arg)
+color_init(struct os_dev *dev, void *arg)
+{
+    return (0);
+}
+#endif
+
+#if MYNEWT_VAL(BME280_PRESENT)
+static int
+press_init(struct os_dev *dev, void *arg)
 {
     return (0);
 }
@@ -212,25 +227,31 @@ sensor_dev_create(void)
     (void)rc;
 #if MYNEWT_VAL(LSM303DLHC_PRESENT)
     rc = os_dev_create((struct os_dev *) &lsm303dlhc, "accel0",
-      OS_DEV_INIT_PRIMARY, 0, slinky_accel_init, NULL);
+      OS_DEV_INIT_PRIMARY, 0, accel_init, NULL);
     assert(rc == 0);
 #endif
 
 #if MYNEWT_VAL(BNO055_PRESENT)
     rc = os_dev_create((struct os_dev *) &bno055, "accel1",
-      OS_DEV_INIT_PRIMARY, 0, slinky_accel_init, NULL);
+      OS_DEV_INIT_PRIMARY, 0, accel_init, NULL);
     assert(rc == 0);
 #endif
 
 #if MYNEWT_VAL(TSL2561_PRESENT)
     rc = os_dev_create((struct os_dev *) &tsl2561, "light0",
-      OS_DEV_INIT_PRIMARY, 0, slinky_light_init, NULL);
+      OS_DEV_INIT_PRIMARY, 0, light_init, NULL);
     assert(rc == 0);
 #endif
 
 #if MYNEWT_VAL(TCS34725_PRESENT)
     rc = os_dev_create((struct os_dev *) &tcs34725, "color0",
-      OS_DEV_INIT_PRIMARY, 0, slinky_color_init, NULL);
+      OS_DEV_INIT_PRIMARY, 0, color_init, NULL);
+    assert(rc == 0);
+#endif
+
+#if MYNEWT_VAL(BME280_PRESENT)
+    rc = os_dev_create((struct os_dev *) &bme280, "bme280",
+      OS_DEV_INIT_PRIMARY, 0, press_init, NULL);
     assert(rc == 0);
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d98e078/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 f5ea131..4ef5d78 100644
--- a/hw/drivers/sensors/bme280/include/bme280/bme280.h
+++ b/hw/drivers/sensors/bme280/include/bme280/bme280.h
@@ -167,7 +167,7 @@ int bme280_get_pressure(uint32_t *press);
  *
  * @return 0 on success, and non-zero error code on failure
  */
-int bme280_get_humidity(uint32_t *humidity);
+int bme280_get_humidity(uint32_t *humid);
 
 /**
  * Sets the sampling rate

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d98e078/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 ca5ee0a..e31cffc 100644
--- a/hw/drivers/sensors/bme280/src/bme280.c
+++ b/hw/drivers/sensors/bme280/src/bme280.c
@@ -195,8 +195,8 @@ bme280_sensor_read(struct sensor *sensor, sensor_type_t type,
     uint32_t humid;
     int rc;
 
-    if (!(type & SENSOR_TYPE_PRESSURE)    ||
-        !(type & SENSOR_TYPE_TEMPERATURE) ||
+    if (!(type & SENSOR_TYPE_PRESSURE)    &&
+        !(type & SENSOR_TYPE_TEMPERATURE) &&
         !(type & SENSOR_TYPE_RELATIVE_HUMIDITY)) {
         rc = SYS_EINVAL;
         goto err;
@@ -289,26 +289,74 @@ int
 bme280_config(struct bme280 *bme280, struct bme280_cfg *cfg)
 {
     int rc;
+    uint8_t id;
 
-    rc = bme280_set_iir(cfg->bc_iir);
+    /* Check if we can read the chip address */
+    rc = bme280_get_chipid(&id);
+    if (rc) {
+        goto err;
+    }
 
-    rc |= bme280_set_mode(cfg->bc_mode);
+    if (id != BME280_CHIPID && id != BMP280_CHIPID) {
+        os_time_delay((OS_TICKS_PER_SEC * 100)/1000 + 1);
 
-    rc |= bme280_set_oversample(cfg->bc_boc[0].boc_type,
-                                cfg->bc_boc[0].boc_oversample);
+        rc = bme280_get_chipid(&id);
+        if (rc) {
+            goto err;
+        }
 
-    rc |= bme280_set_oversample(cfg->bc_boc[1].boc_type,
-                                cfg->bc_boc[1].boc_oversample);
+        if(id != BME280_CHIPID && id != BMP280_CHIPID) {
+            rc = SYS_EINVAL;
+            goto err;
+        }
+    }
 
-    rc |= bme280_set_oversample(cfg->bc_boc[2].boc_type,
-                                cfg->bc_boc[2].boc_oversample);
+    rc = bme280_set_iir(cfg->bc_iir);
+    if (rc) {
+        goto err;
+    }
 
+    bme280->cfg.bc_iir = cfg->bc_iir;
+
+    rc = bme280_set_mode(cfg->bc_mode);
     if (rc) {
         goto err;
     }
 
-    /* Overwrite the configuration data. */
-    memcpy(&bme280->cfg, cfg, sizeof(*cfg));
+    bme280->cfg.bc_mode = cfg->bc_mode;
+
+    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) {
+            goto err;
+        }
+    }
+
+    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) {
+        rc = bme280_set_oversample(cfg->bc_boc[1].boc_type,
+                                   cfg->bc_boc[1].boc_oversample);
+        if (rc) {
+            goto err;
+        }
+    }
+
+    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) {
+        rc = bme280_set_oversample(cfg->bc_boc[2].boc_type,
+                                   cfg->bc_boc[2].boc_oversample);
+        if (rc) {
+            goto err;
+        }
+    }
+
+    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;
 
 err:
     return (rc);
@@ -327,36 +375,35 @@ int
 bme280_readlen(uint8_t addr, uint8_t *payload, uint8_t len)
 {
     int i;
+    uint16_t retval;
     int rc;
-    uint8_t txdata;
-    uint8_t rxdata;
+
+    rc = 0;
 
     /* 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) {
+    retval = hal_spi_tx_val(MYNEWT_VAL(BME280_SPINUM),
+                            addr | BME280_SPI_READ_CMD_BIT);
+    if (retval == 0xFFFF) {
+        rc = SYS_EINVAL;
         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) {
+        retval = hal_spi_tx_val(MYNEWT_VAL(BME280_SPINUM), 0);
+        if (retval == 0xFFFF) {
+            rc = SYS_EINVAL;
             goto err;
         }
-
-        payload[i] = rxdata;
+        payload[i] = retval;
     }
 
+err:
     /* De-select the device */
     hal_gpio_write(MYNEWT_VAL(BME280_CSPIN), 1);
-
-    return 0;
-err:
     return rc;
 }
 
@@ -380,14 +427,16 @@ bme280_writelen(uint8_t addr, uint8_t *payload, uint8_t len)
 
     /* Send the address */
     rc = hal_spi_tx_val(MYNEWT_VAL(BME280_SPINUM), addr | BME280_SPI_READ_CMD_BIT);
-    if (rc) {
+    if (rc == 0xFFFF) {
+        rc = SYS_EINVAL;
         goto err;
     }
 
     for (i = 0; i < len; i++) {
         /* Read data */
         rc = hal_spi_tx_val(MYNEWT_VAL(BME280_SPINUM), payload[i]);
-        if (rc) {
+        if (rc == 0xFFFF) {
+            rc = SYS_EINVAL;
             goto err;
         }
     }
@@ -702,7 +751,7 @@ bme280_get_chipid(uint8_t *chipid)
     int rc;
     uint8_t tmp;
 
-    rc = bme280_readlen(BME280_REG_ADDR_PRESS, &tmp, 1);
+    rc = bme280_readlen(BME280_REG_ADDR_CHIPID, &tmp, 1);
     if (rc) {
         goto err;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d98e078/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 1c93aeb..23d0aaf 100644
--- a/hw/drivers/sensors/bme280/src/bme280_priv.h
+++ b/hw/drivers/sensors/bme280/src/bme280_priv.h
@@ -79,7 +79,9 @@
 
 #define BME280_REG_ADDR_RESET             0xE0
 
-#define BME280_REG_CHIPID                 0x60
+#define BME280_CHIPID                     0x60
+
+#define BMP280_CHIPID                     0x58
 
 #ifdef __cplusplus
 extern "C" {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d98e078/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 f9d3f46..78f5d2e 100644
--- a/hw/drivers/sensors/bme280/src/bme280_shell.c
+++ b/hw/drivers/sensors/bme280/src/bme280_shell.c
@@ -126,7 +126,6 @@ bme280_shell_cmd_read(int argc, char **argv)
     uint16_t samples = 1;
     long val;
     int rc;
-    struct bme280 bme280;
 
     if (argc > 3) {
         return bme280_shell_err_too_many_args(argv[1]);
@@ -142,13 +141,27 @@ bme280_shell_cmd_read(int argc, char **argv)
 
     while(samples--) {
 
-        rc = bme280_get_data(&temp, &press, &humid, &bme280);
-        if (rc != 0) {
+        rc = bme280_get_pressure(&press);
+        if (rc) {
+            console_printf("Read failed: %d\n", rc);
+            return rc;
+        }
+
+        rc = bme280_get_temperature(&temp);
+        if (rc) {
+            console_printf("Read failed: %d\n", rc);
+            return rc;
+        }
+
+        rc = bme280_get_humidity(&humid);
+        if (rc) {
             console_printf("Read failed: %d\n", rc);
             return rc;
         }
+
         console_printf("temperature: %u\tpressure: %u\thumidity: %u\n",
-                       temp, press, humid);
+                       (unsigned int)temp, (unsigned int)press,
+                       (unsigned int)humid);
     }
 
     return 0;
@@ -171,7 +184,7 @@ bme280_shell_cmd_oversample(int argc, char **argv)
         if (bme280_shell_stol(argv[2], 4, 8, &val)) {
             return bme280_shell_err_invalid_arg(argv[2]);
         }
-        rc = bme280_get_oversampling(val, &oversample);
+        rc = bme280_get_oversample(val, &oversample);
         if (rc) {
             goto err;
         }
@@ -204,10 +217,46 @@ err:
 }
 
 static int
+bme280_shell_cmd_mode(int argc, char **argv)
+{
+    uint8_t mode;
+    long val;
+    int rc;
+
+    if (argc > 3) {
+        return bme280_shell_err_too_many_args(argv[1]);
+    }
+
+    if (argc == 2) {
+        rc = bme280_get_mode(&mode);
+        if (rc) {
+            goto err;
+        }
+        console_printf("mode: %u", mode);
+    }
+
+    /* Chaneg mode */
+    if (argc == 3) {
+        if (bme280_shell_stol(argv[2], 0, 1, &val)) {
+            return bme280_shell_err_invalid_arg(argv[2]);
+        }
+        rc = bme280_set_mode(val);
+        if (rc) {
+            goto err;
+        }
+    }
+
+    return 0;
+err:
+    return rc;
+}
+
+static int
 bme280_shell_cmd_iir(int argc, char **argv)
 {
     uint8_t iir;
     long val;
+    int rc;
 
     if (argc > 3) {
         return bme280_shell_err_too_many_args(argv[1]);
@@ -234,6 +283,8 @@ bme280_shell_cmd_iir(int argc, char **argv)
     }
 
     return 0;
+err:
+    return rc;
 }
 
 static int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8d98e078/hw/sensor/src/sensor_oic.c
----------------------------------------------------------------------
diff --git a/hw/sensor/src/sensor_oic.c b/hw/sensor/src/sensor_oic.c
index de9e65a..879da5d 100644
--- a/hw/sensor/src/sensor_oic.c
+++ b/hw/sensor/src/sensor_oic.c
@@ -134,17 +134,19 @@ sensor_oic_encode(struct sensor* sensor, void *arg, void *databuf)
             oc_rep_set_double(root, ambient_temp, *(double *)databuf);
             break;
 
-#if 0
         /* Pressure sensor supported */
-        SENSOR_TYPE_PRESSURE:
-            oc_rep_set_double(root, pressure, (double *)databuf);
-
+        case SENSOR_TYPE_PRESSURE:
+            oc_rep_set_uint(root, pressure, *(uint32_t *)databuf);
+            break;
+#if 0
         /* Proximity sensor supported */
         SENSOR_TYPE_PROXIMITY:
-
-        /* Relative humidity supported */
-        SENSOR_TYPE_RELATIVE_HUMIDITY:
 #endif
+        /* Relative humidity supported */
+        case SENSOR_TYPE_RELATIVE_HUMIDITY:
+            oc_rep_set_uint(root, humidity, *(uint32_t *)databuf);
+            break;
+
         /* Rotation vector (quaternion) supported */
         case SENSOR_TYPE_ROTATION_VECTOR:
             if (((struct sensor_quat_data *)(databuf))->sqd_x_is_valid) {
@@ -457,13 +459,20 @@ sensor_oic_get_data(oc_request_t *request, oc_interface_mask_t interface)
     int rc;
     struct sensor *sensor;
     struct sensor_listener listener;
-    char devname[COAP_MAX_URI] = {0};
+    char *devname;
     char *typename;
     sensor_type_t type;
+    char tmpstr[COAP_MAX_URI] = {0};
+    const char s[2] = "/";
 
-    memcpy(devname, (char *)&(request->resource->uri.os_str[1]),
+    memcpy(tmpstr, (char *)&(request->resource->uri.os_str[1]),
            request->resource->uri.os_sz - 1);
 
+    /* Parse the sensor device name from the uri  */
+    devname = strtok(tmpstr, s);
+
+    typename = strtok(NULL, s);
+
     /* Look up sensor by name */
     sensor = sensor_mgr_find_next_bydevname(devname, NULL);
     if (!sensor) {
@@ -563,7 +572,7 @@ sensor_oic_init(void)
                 }
 
                 memset(tmpstr, 0, sizeof(tmpstr));
-                snprintf(tmpstr, sizeof(tmpstr), "/%s", sensor->s_dev->od_name);
+                snprintf(tmpstr, sizeof(tmpstr), "/%s/%s", sensor->s_dev->od_name, typename);
 
                 res = oc_new_resource(tmpstr, 1, 0);