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/06/08 06:15:05 UTC

[mynewt-core] 10/10: sensor/creator: Add adxl345 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 fa5e4c31b0bea2b107747e3b0d40002d3bef4132
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Fri Jun 7 14:21:33 2019 +0200

    sensor/creator: Add adxl345 bus driver support
    
    Accelerometer can now be used with SPI and I2C bus drivers.
---
 hw/sensor/creator/src/sensor_creator.c | 41 ++++++++++++++++++++++++++++++----
 hw/sensor/creator/syscfg.yml           | 24 ++++++++++++++++++++
 2 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/hw/sensor/creator/src/sensor_creator.c b/hw/sensor/creator/src/sensor_creator.c
index 770801e..c63fe04 100644
--- a/hw/sensor/creator/src/sensor_creator.c
+++ b/hw/sensor/creator/src/sensor_creator.c
@@ -396,16 +396,39 @@ static struct sensor_itf spi2c_0_itf_bmp388= {
 };
 #endif
 
-#if MYNEWT_VAL(I2C_0) && MYNEWT_VAL(ADXL345_OFB)
-static struct sensor_itf i2c_0_itf_adxl = {
+#if MYNEWT_VAL(ADXL345_OFB)
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+#if MYNEWT_VAL(ADXL345_OFB_I2C_NUM) >= 0
+static const struct bus_i2c_node_cfg adxl345_node_cfg = {
+    .node_cfg = {
+        .bus_name = MYNEWT_VAL(ADXL345_OFB_BUS),
+    },
+    .addr = MYNEWT_VAL(ADXL345_ITF_ADDR),
+    .freq = 400,
+};
+#elif MYNEWT_VAL(ADXL345_OFB_SPI_NUM) >= 0
+static const struct bus_spi_node_cfg adxl345_node_cfg = {
+    .node_cfg = {
+        .bus_name = MYNEWT_VAL(ADXL345_OFB_BUS),
+    },
+    .pin_cs = MYNEWT_VAL(ADXL345_OFB_CS),
+    .mode = BUS_SPI_MODE_3,
+    .data_order = BUS_SPI_DATA_ORDER_MSB,
+    .freq = 4000,
+};
+#endif
+static struct sensor_itf adxl345_itf;
+#else
+static struct sensor_itf adxl_itf = {
     .si_type = SENSOR_ITF_I2C,
-    .si_num  = 0,
+    .si_num  = MYNEWT_VAL(ADXL345_OFB_I2C_NUM),
     .si_addr = MYNEWT_VAL(ADXL345_ITF_ADDR),
     .si_ints = {
        { MYNEWT_VAL(ADXL345_INT_PIN_HOST), MYNEWT_VAL(ADXL345_INT_PIN_DEVICE),
          MYNEWT_VAL(ADXL345_INT_CFG_ACTIVE)}}
 };
 #endif
+#endif
 
 #if MYNEWT_VAL(I2C_0) && MYNEWT_VAL(LPS33HW_OFB)
 static struct sensor_itf i2c_0_itf_lps = {
@@ -1559,8 +1582,18 @@ sensor_dev_create(void)
 
 
 #if MYNEWT_VAL(ADXL345_OFB)
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+#if MYNEWT_VAL(ADXL345_OFB_I2C_NUM) >= 0
+    rc = adxl345_create_i2c_sensor_dev(&adxl345.i2c_node, "adxl345_0",
+                                       &adxl345_node_cfg, &adxl345_itf);
+#elif MYNEWT_VAL(ADXL345_OFB_SPI_NUM) >= 0
+    rc = adxl345_create_spi_sensor_dev(&adxl345.spi_node, "adxl345_0",
+                                       &adxl345_node_cfg, &adxl345_itf);
+#endif
+#else
     rc = os_dev_create((struct os_dev *) &adxl345, "adxl345_0",
-      OS_DEV_INIT_PRIMARY, 0, adxl345_init, (void *)&i2c_0_itf_adxl);
+      OS_DEV_INIT_PRIMARY, 0, adxl345_init, (void *)&adxl_itf);
+#endif
     assert(rc == 0);
 
     rc = config_adxl345_sensor();
diff --git a/hw/sensor/creator/syscfg.yml b/hw/sensor/creator/syscfg.yml
index 5ba422e..756504d 100644
--- a/hw/sensor/creator/syscfg.yml
+++ b/hw/sensor/creator/syscfg.yml
@@ -76,6 +76,30 @@ syscfg.defs:
     ADXL345_OFB:
         description: 'ADXL345 is present'
         value : 0
+    ADXL345_OFB_I2C_NUM:
+        description: 'I2C interface used for ADXL345'
+        value:
+        restrictions:
+         - '(ADXL345_OFB == 0) ||
+            ((ADXL345_OFB_I2C_NUM == 0) && (I2C_0 == 1)) ||
+            ((ADXL345_OFB_I2C_NUM == 1) && (I2C_1 == 1)) ||
+            ((ADXL345_OFB_I2C_NUM == 2) && (I2C_2 == 1)) ||
+            ((ADXL345_OFB_I2C_NUM == -1))'
+    ADXL345_OFB_SPI_NUM:
+        description: 'SPI interface used for ADXL345'
+        value: -1
+        restrictions:
+         - '(ADXL345_OFB == 0) ||
+            ((ADXL345_OFB_SPI_NUM == 0) && (SPI_0_MASTER == 1)) ||
+            ((ADXL345_OFB_SPI_NUM == 1) && (SPI_1_MASTER == 1)) ||
+            ((ADXL345_OFB_SPI_NUM == 2) && (SPI_2_MASTER == 1)) ||
+            ((ADXL345_OFB_SPI_NUM == -1))'
+    ADXL345_OFB_BUS:
+        description: 'I2C or SPI interface used for ADXL345'
+        value: '"i2c0"'
+    ADXL345_OFB_CS:
+        description: 'CS pin for ADXL345'
+        value:
     LPS33HW_OFB:
         description: 'LPS33HW is present'
         value : 0