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 2019/01/08 13:11:19 UTC

[mynewt-core] 04/06: hw/drivers/lps33xhw: Fix sensor register read

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 64720421f25669f4d08c3561f377a730d152506e
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Jan 7 12:00:09 2019 +0100

    hw/drivers/lps33xhw: Fix sensor register read
    
    With i2cn, we need to make sure that before retrying read we also
    perform write as otherwise we may read subsequent registers instead of
    those we wanted to read thus invalid values will be reported.
---
 hw/drivers/sensors/lps33hw/src/lps33hw.c   | 28 ++++++++++----------------
 hw/drivers/sensors/lps33thw/src/lps33thw.c | 32 +++++++++---------------------
 2 files changed, 19 insertions(+), 41 deletions(-)

diff --git a/hw/drivers/sensors/lps33hw/src/lps33hw.c b/hw/drivers/sensors/lps33hw/src/lps33hw.c
index a55a7a6..b54527b 100644
--- a/hw/drivers/sensors/lps33hw/src/lps33hw.c
+++ b/hw/drivers/sensors/lps33hw/src/lps33hw.c
@@ -354,35 +354,27 @@ lps33hw_i2c_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size,
     uint8_t *buffer)
 {
     int rc;
-
-    struct hal_i2c_master_data data_struct = {
+    struct hal_i2c_master_data wdata = {
         .address = itf->si_addr,
         .len = 1,
         .buffer = &reg
     };
+    struct hal_i2c_master_data rdata = {
+        .address = itf->si_addr,
+        .len = size,
+        .buffer = buffer,
+    };
 
-    /* Register write */
-    rc = i2cn_master_write(itf->si_num, &data_struct, MYNEWT_VAL(LPS33HW_I2C_TIMEOUT_TICKS), 0,
-                           MYNEWT_VAL(LPS33HW_I2C_RETRIES));
+    rc = i2cn_master_write_read_transact(itf->si_num, &wdata, &rdata,
+                                         MYNEWT_VAL(LPS33HW_I2C_TIMEOUT_TICKS) * (size + 1),
+                                         1, MYNEWT_VAL(LPS33HW_I2C_RETRIES));
     if (rc) {
         LPS33HW_LOG(ERROR, "I2C access failed at address 0x%02X\n",
                     itf->si_addr);
-        STATS_INC(g_lps33hwstats, write_errors);
+        STATS_INC(g_lps33hwstats, read_errors);
         return rc;
     }
 
-    /* Read */
-    data_struct.len = size;
-    data_struct.buffer = buffer;
-    rc = i2cn_master_read(itf->si_num, &data_struct,
-                          (MYNEWT_VAL(LPS33HW_I2C_TIMEOUT_TICKS)) * size, 1,
-                          MYNEWT_VAL(LPS33HW_I2C_RETRIES));
-
-    if (rc) {
-        LPS33HW_LOG(ERROR, "Failed to read from 0x%02X:0x%02X\n",
-                    itf->si_addr, reg);
-        STATS_INC(g_lps33hwstats, read_errors);
-    }
     return rc;
 }
 #endif
diff --git a/hw/drivers/sensors/lps33thw/src/lps33thw.c b/hw/drivers/sensors/lps33thw/src/lps33thw.c
index f80f050..9919929 100644
--- a/hw/drivers/sensors/lps33thw/src/lps33thw.c
+++ b/hw/drivers/sensors/lps33thw/src/lps33thw.c
@@ -361,34 +361,20 @@ lps33thw_i2c_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size,
 
     rc = bus_node_simple_write_read_transact(odev, &reg, 1, buffer, size);
 #else
-    struct hal_i2c_master_data data_struct = {
+    struct hal_i2c_master_data wdata = {
         .address = itf->si_addr,
         .len = 1,
         .buffer = &reg
     };
+    struct hal_i2c_master_data rdata = {
+        .address = itf->si_addr,
+        .len = size,
+        .buffer = buffer,
+    };
 
-    /* Register write */
-    rc = i2cn_master_write(itf->si_num, &data_struct, MYNEWT_VAL(LPS33THW_I2C_TIMEOUT_TICKS), 0,
-                           MYNEWT_VAL(LPS33THW_I2C_RETRIES));
-    if (rc) {
-        LPS33THW_LOG(ERROR, "I2C access failed at address 0x%02X\n",
-                    itf->si_addr);
-        STATS_INC(g_lps33thwstats, write_errors);
-        return rc;
-    }
-
-    /* Read */
-    data_struct.len = size;
-    data_struct.buffer = buffer;
-    rc = i2cn_master_read(itf->si_num, &data_struct,
-                          (MYNEWT_VAL(LPS33THW_I2C_TIMEOUT_TICKS)) * size, 1,
-                          MYNEWT_VAL(LPS33THW_I2C_RETRIES));
-
-    if (rc) {
-        LPS33THW_LOG(ERROR, "Failed to read from 0x%02X:0x%02X\n",
-                    itf->si_addr, reg);
-        STATS_INC(g_lps33thwstats, read_errors);
-    }
+    rc = i2cn_master_write_read_transact(itf->si_num, &wdata, &rdata,
+                                         MYNEWT_VAL(LPS33THW_I2C_TIMEOUT_TICKS) * (size + 1),
+                                         1, MYNEWT_VAL(LPS33THW_I2C_RETRIES));
 #endif
 
     return rc;