You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2019/08/30 09:49:38 UTC

[mynewt-core] 01/02: sensors/bno055: Add bus driver support

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 6501a6cda3bf63c141f400ebacecb231ca159e3a
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Mon Aug 26 09:02:52 2019 +0200

    sensors/bno055: Add bus driver support
---
 hw/drivers/sensors/bno055/include/bno055/bno055.h |  9 +++
 hw/drivers/sensors/bno055/src/bno055.c            | 67 ++++++++++++++++++++++-
 hw/sensor/creator/src/sensor_creator.c            | 23 +++++++-
 hw/sensor/creator/syscfg.yml                      |  7 +++
 4 files changed, 102 insertions(+), 4 deletions(-)

diff --git a/hw/drivers/sensors/bno055/include/bno055/bno055.h b/hw/drivers/sensors/bno055/include/bno055/bno055.h
index 32d1eba..b51cae1 100644
--- a/hw/drivers/sensors/bno055/include/bno055/bno055.h
+++ b/hw/drivers/sensors/bno055/include/bno055/bno055.h
@@ -161,7 +161,11 @@ struct bno055_cfg {
 };
 
 struct bno055 {
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    struct bus_i2c_node i2c_node;
+#else
     struct os_dev dev;
+#endif
     struct sensor sensor;
     struct bno055_cfg cfg;
     os_time_t last_read_time;
@@ -397,6 +401,11 @@ bno055_get_sys_status(struct sensor_itf *itf, uint8_t *system_status,
 int
 bno055_get_chip_id(struct sensor_itf *itf, uint8_t *id);
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+int bno055_create_sensor_dev(struct bno055 *dev, const char *name,
+                             const struct bus_i2c_node_cfg *i2c_cfg,
+                             struct sensor_itf *sensor_itf);
+#endif
 
 #ifdef __cplusplus
 }
diff --git a/hw/drivers/sensors/bno055/src/bno055.c b/hw/drivers/sensors/bno055/src/bno055.c
index 48ed77d..6e33e41 100644
--- a/hw/drivers/sensors/bno055/src/bno055.c
+++ b/hw/drivers/sensors/bno055/src/bno055.c
@@ -76,6 +76,13 @@ bno055_write8(struct sensor_itf *itf, uint8_t reg, uint8_t value)
     int rc;
     uint8_t payload[2] = { reg, value};
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    rc = bus_node_simple_write(itf->si_dev, payload, 2);
+    if (rc) {
+        BNO055_LOG_ERROR("Write to bno055 failed %d\n", rc);
+        STATS_INC(g_bno055stats, errors);
+    }
+#else
     struct hal_i2c_master_data data_struct = {
         .address = itf->si_addr,
         .len = 2,
@@ -90,6 +97,7 @@ bno055_write8(struct sensor_itf *itf, uint8_t reg, uint8_t value)
                    data_struct.address, reg, value);
         STATS_INC(g_bno055stats, errors);
     }
+#endif
 
     return rc;
 }
@@ -112,6 +120,19 @@ bno055_writelen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer,
                               0, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0};
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    if (len > (sizeof(payload) - 1)) {
+        return OS_EINVAL;
+    }
+
+    memcpy(payload, buffer, len);
+
+    rc = bus_node_simple_write(itf->si_dev, payload, len + 1);
+    if (rc) {
+        BNO055_LOG_ERROR("Write to bno055 failed %d\n", rc);
+        STATS_INC(g_bno055stats, errors);
+    }
+#else
     struct hal_i2c_master_data data_struct = {
         .address = itf->si_addr,
         .len = 1,
@@ -151,6 +172,7 @@ bno055_writelen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer,
 
 err:
     sensor_itf_unlock(itf);
+#endif
 
     return rc;
 }
@@ -168,8 +190,15 @@ int
 bno055_read8(struct sensor_itf *itf, uint8_t reg, uint8_t *value)
 {
     int rc;
-    uint8_t payload;
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    rc = bus_node_simple_write_read_transact(itf->si_dev, &reg, 1, value, 1);
+    if (rc) {
+        BNO055_LOG_ERROR("Read from bno055 failed %d\n", rc);
+        STATS_INC(g_bno055stats, errors);
+    }
+#else
+    uint8_t payload;
     struct hal_i2c_master_data data_struct = {
         .address = itf->si_addr,
         .len = 1,
@@ -206,6 +235,7 @@ bno055_read8(struct sensor_itf *itf, uint8_t reg, uint8_t *value)
 
 err:
     sensor_itf_unlock(itf);
+#endif
 
     return rc;
 }
@@ -225,6 +255,14 @@ bno055_readlen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer,
                uint8_t len)
 {
     int rc;
+
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    rc = bus_node_simple_write_read_transact(itf->si_dev, &reg, 1, buffer, len);
+    if (rc) {
+        BNO055_LOG_ERROR("Read from bno055 failed %d\n", rc);
+        STATS_INC(g_bno055stats, errors);
+    }
+#else
     uint8_t payload[23] = { reg, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0, 0,
                               0, 0, 0, 0, 0, 0, 0};
@@ -269,6 +307,7 @@ bno055_readlen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer,
 
 err:
     sensor_itf_unlock(itf);
+#endif
 
     return rc;
 }
@@ -2231,3 +2270,29 @@ err:
     return rc;
 }
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+
+void
+init_node_cb(struct bus_node *node, void *arg)
+{
+    bno055_init((struct os_dev *)node, arg);
+}
+
+int
+bno055_create_sensor_dev(struct bno055 *dev, const char *name,
+                         const struct bus_i2c_node_cfg *i2c_cfg,
+                         struct sensor_itf *sensor_itf)
+{
+    struct bus_node_callbacks cbs = {
+        .init = init_node_cb,
+    };
+    int rc;
+
+    sensor_itf->si_dev = &dev->i2c_node.bnode.odev;
+    bus_node_set_callbacks((struct os_dev *)dev, &cbs);
+
+    rc = bus_i2c_node_create(name, &dev->i2c_node, i2c_cfg, sensor_itf);
+
+    return rc;
+}
+#endif
diff --git a/hw/sensor/creator/src/sensor_creator.c b/hw/sensor/creator/src/sensor_creator.c
index 6701102..39681f5 100644
--- a/hw/sensor/creator/src/sensor_creator.c
+++ b/hw/sensor/creator/src/sensor_creator.c
@@ -318,14 +318,25 @@ static struct sensor_itf mpu6050_i2c_itf = {
 #endif
 #endif
 
-#if MYNEWT_VAL(I2C_0) && MYNEWT_VAL(BNO055_OFB)
-static struct sensor_itf i2c_0_itf_bno = {
+#if MYNEWT_VAL(BNO055_OFB)
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+static const struct bus_i2c_node_cfg bno055_i2c_cfg = {
+    .node_cfg = {
+        .bus_name = MYNEWT_VAL(BNO055_OFB_I2C_BUS),
+    },
+    .addr = MYNEWT_VAL(BNO055_OFB_I2C_ADDR),
+    .freq = 400,
+};
+static struct sensor_itf bno055_i2c_itf;
+#elif MYNEWT_VAL(I2C_0)
+static struct sensor_itf bno055_i2c_itf = {
     .si_type = SENSOR_ITF_I2C,
     .si_num  = 0,
     /* HW I2C address for the BNO055 */
     .si_addr = 0x28
 };
 #endif
+#endif
 
 #if MYNEWT_VAL(I2C_0) && MYNEWT_VAL(TSL2561_OFB)
 static struct sensor_itf i2c_0_itf_tsl = {
@@ -1666,9 +1677,15 @@ sensor_dev_create(void)
 #endif
 
 #if MYNEWT_VAL(BNO055_OFB)
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    rc = bno055_create_sensor_dev(&bno055, "bno055_0", &bno055_i2c_cfg,
+                                  &bno055_i2c_itf);
+    assert(rc == 0);
+#else
     rc = os_dev_create((struct os_dev *) &bno055, "bno055_0",
-      OS_DEV_INIT_PRIMARY, 0, bno055_init, (void *)&i2c_0_itf_bno);
+      OS_DEV_INIT_PRIMARY, 0, bno055_init, (void *)&bno055_i2c_itf);
     assert(rc == 0);
+#endif
 
     rc = config_bno055_sensor();
     assert(rc == 0);
diff --git a/hw/sensor/creator/syscfg.yml b/hw/sensor/creator/syscfg.yml
index e977e6c..ff164af 100644
--- a/hw/sensor/creator/syscfg.yml
+++ b/hw/sensor/creator/syscfg.yml
@@ -46,6 +46,13 @@ syscfg.defs:
     BNO055_OFB:
         description: 'BNO055 is present'
         value : 0
+    BNO055_OFB_I2C_BUS:
+        description: 'I2C interface used for BNO055'
+        value : '"i2c0"'
+    BNO055_OFB_I2C_ADDR:
+        description: 'I2C address of BNO055 0x28 or 0x29'
+        value: 0x28
+        range: 0x28,0x29
     BME280_OFB:
         description: 'BME280 is present'
         value : 0