You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2018/12/03 12:42:17 UTC

[mynewt-core] 06/15: hw/drivers: Add bus driver support to LPS33HW

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

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

commit 37af8f159251724740cc9f4e486d743307e5c04f
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Fri Nov 23 17:26:02 2018 +0100

    hw/drivers: Add bus driver support to LPS33HW
---
 .../sensors/lps33hw/include/lps33hw/lps33hw.h      | 25 +++++++++++
 hw/drivers/sensors/lps33hw/src/lps33hw.c           | 51 +++++++++++++++++++++-
 2 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/hw/drivers/sensors/lps33hw/include/lps33hw/lps33hw.h b/hw/drivers/sensors/lps33hw/include/lps33hw/lps33hw.h
index 6648e74..baa9a31 100644
--- a/hw/drivers/sensors/lps33hw/include/lps33hw/lps33hw.h
+++ b/hw/drivers/sensors/lps33hw/include/lps33hw/lps33hw.h
@@ -22,6 +22,10 @@
 
 #include "os/mynewt.h"
 #include "sensor/sensor.h"
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+#include "bus/bus_driver.h"
+#include "bus/i2c.h"
+#endif
 #include "hal/hal_gpio.h"
 
 #ifdef __cplusplus
@@ -77,7 +81,11 @@ struct lps33hw_private_driver_data {
 };
 
 struct lps33hw {
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    struct bus_i2c_node i2c_node;
+#else
     struct os_dev dev;
+#endif
     struct sensor sensor;
     struct lps33hw_cfg cfg;
     os_time_t last_read_time;
@@ -212,6 +220,23 @@ int lps33hw_config(struct lps33hw *, struct lps33hw_cfg *);
 int lps33hw_shell_init(void);
 #endif
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+/**
+ * Create I2C bus node for LPS33HW sensor
+ *
+ * @param node        Bus node
+ * @param name        Device name
+ * @param i2c_cfg     I2C node configuration
+ * @param sensor_itf  Sensors interface
+ *
+ * @return 0 on success, non-zero on failure
+ */
+int
+lps33hw_create_i2c_sensor_dev(struct bus_i2c_node *node, const char *name,
+                              const struct bus_i2c_node_cfg *i2c_cfg,
+                              struct sensor_itf *sensor_itf);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/hw/drivers/sensors/lps33hw/src/lps33hw.c b/hw/drivers/sensors/lps33hw/src/lps33hw.c
index 62706b5..7cc221b 100644
--- a/hw/drivers/sensors/lps33hw/src/lps33hw.c
+++ b/hw/drivers/sensors/lps33hw/src/lps33hw.c
@@ -23,10 +23,14 @@
 #include <math.h>
 
 #include "os/mynewt.h"
+#include "hal/hal_gpio.h"
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+#include "bus/bus.h"
+#else
 #include "hal/hal_i2c.h"
 #include "hal/hal_spi.h"
-#include "hal/hal_gpio.h"
 #include "i2cn/i2cn.h"
+#endif
 #include "sensor/sensor.h"
 #include "sensor/pressure.h"
 #include "sensor/temperature.h"
@@ -36,12 +40,14 @@
 #include "stats/stats.h"
 #include <syscfg/syscfg.h>
 
+#if !MYNEWT_VAL(BUS_DRIVER_PRESENT)
 static struct hal_spi_settings spi_lps33hw_settings = {
     .data_order = HAL_SPI_MSB_FIRST,
     .data_mode  = HAL_SPI_MODE3,
     .baudrate   = 4000,
     .word_size  = HAL_SPI_WORD_SIZE_8BIT,
 };
+#endif
 
 /* Define the stats section and records */
 STATS_SECT_START(lps33hw_stat_section)
@@ -154,6 +160,7 @@ lps33hw_reg_to_degc(int16_t reg)
     return reg / LPS33HW_TEMP_OUT_DIV;
 }
 
+#if !MYNEWT_VAL(BUS_DRIVER_PRESENT)
 /**
  * Writes a single byte to the specified register using i2c
  * interface
@@ -237,6 +244,7 @@ err:
 
     return rc;
 }
+#endif
 
 /**
  * Writes a single byte to the specified register using specified
@@ -253,6 +261,11 @@ lps33hw_set_reg(struct sensor_itf *itf, uint8_t reg, uint8_t value)
 {
     int rc;
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    uint8_t data[2] = { reg, value };
+
+    rc = bus_node_simple_write(itf->si_dev, data, 2);
+#else
     rc = sensor_itf_lock(itf, MYNEWT_VAL(LPS33HW_ITF_LOCK_TMO));
     if (rc) {
         return rc;
@@ -265,10 +278,12 @@ lps33hw_set_reg(struct sensor_itf *itf, uint8_t reg, uint8_t value)
     }
 
     sensor_itf_unlock(itf);
+#endif
 
     return rc;
 }
 
+#if !MYNEWT_VAL(BUS_DRIVER_PRESENT)
 /**
  *
  * Read bytes from the specified register using SPI interface
@@ -370,6 +385,7 @@ lps33hw_i2c_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size,
     }
     return rc;
 }
+#endif
 
 /**
  * Read bytes from the specified register using specified interface
@@ -387,6 +403,9 @@ lps33hw_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size,
 {
     int rc;
 
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+    rc = bus_node_simple_write_read_transact(itf->si_dev, &reg, 1, buffer, size);
+#else
     rc = sensor_itf_lock(itf, MYNEWT_VAL(LPS33HW_ITF_LOCK_TMO));
     if (rc) {
         return rc;
@@ -399,6 +418,7 @@ lps33hw_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size,
     }
 
     sensor_itf_unlock(itf);
+#endif
 
     return rc;
 }
@@ -907,6 +927,7 @@ lps33hw_init(struct os_dev *dev, void *arg)
         return rc;
     }
 
+#if !MYNEWT_VAL(BUS_DRIVER_PRESENT)
     if (sensor->s_itf.si_type == SENSOR_ITF_SPI) {
         rc = hal_spi_config(sensor->s_itf.si_num, &spi_lps33hw_settings);
         if (rc == EINVAL) {
@@ -926,6 +947,7 @@ lps33hw_init(struct os_dev *dev, void *arg)
             return rc;
         }
     }
+#endif
 
     return rc;
 }
@@ -1084,3 +1106,30 @@ lps33hw_sensor_get_config(struct sensor *sensor, sensor_type_t type,
 
     return 0;
 }
+
+#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
+static void
+init_node_cb(struct bus_node *bnode, void *arg)
+{
+    struct sensor_itf *itf = arg;
+
+    lps33hw_init((struct os_dev *)bnode, itf);
+}
+
+int
+lps33hw_create_i2c_sensor_dev(struct bus_i2c_node *node, 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;
+
+    bus_node_set_callbacks((struct os_dev *)node, &cbs);
+
+    rc = bus_i2c_node_create(name, node, i2c_cfg, sensor_itf);
+
+    return rc;
+}
+#endif