You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/07/11 12:46:25 UTC

[GitHub] mkiiskila closed pull request #1251: Added stubs for I2C simulation

mkiiskila closed pull request #1251: Added stubs for I2C simulation
URL: https://github.com/apache/mynewt-core/pull/1251
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/hw/bsp/native/src/hal_bsp.c b/hw/bsp/native/src/hal_bsp.c
index f3034233af..84fc69c391 100644
--- a/hw/bsp/native/src/hal_bsp.c
+++ b/hw/bsp/native/src/hal_bsp.c
@@ -29,6 +29,7 @@
 #include "uart_hal/uart_hal.h"
 #include "mcu/native_bsp.h"
 #include "mcu/mcu_hal.h"
+#include "hal/hal_i2c.h"
 
 #if MYNEWT_VAL(SIM_ACCEL_PRESENT)
 #include "sim/sim_accel.h"
@@ -77,6 +78,11 @@ hal_bsp_init(void)
             OS_DEV_INIT_PRIMARY, 0, uart_hal_init, (void *) NULL);
     assert(rc == 0);
 
+#if MYNEWT_VAL(I2C_0)
+    rc = hal_i2c_init(0, NULL);
+    assert(rc == 0);
+#endif
+    
 #if MYNEWT_VAL(SIM_ACCEL_PRESENT)
     rc = os_dev_create((struct os_dev *) &os_bsp_accel0, "simaccel0",
             OS_DEV_INIT_PRIMARY, 0, simaccel_init, (void *) NULL);
diff --git a/hw/mcu/native/include/mcu/mcu_sim_i2c.h b/hw/mcu/native/include/mcu/mcu_sim_i2c.h
new file mode 100644
index 0000000000..0b929594d9
--- /dev/null
+++ b/hw/mcu/native/include/mcu/mcu_sim_i2c.h
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+#ifndef H_MCU_SIM_I2C_
+#define H_MCU_SIM_I2C_
+
+#include <inttypes.h>
+#include <hal/hal_i2c.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Sends a start condition and writes <len> bytes of data on the i2c bus.
+ * This API does NOT issue a stop condition unless `last_op` is set to `1`.
+ * You must stop the bus after successful or unsuccessful write attempts.
+ * This API is blocking until an error or NaK occurs. Timeout is platform
+ * dependent.
+ *
+ * @param i2c_num The number of the I2C device being written to
+ * @param pdata The data to write to the I2C bus
+ * @param timeout How long to wait for transaction to complete in ticks
+ * @param last_op Master should send a STOP at the end to signify end of
+ *        transaction.
+ *
+ * @return 0 on success, and non-zero error code on failure
+ */
+typedef int (*hal_i2c_sim_master_write_t)(uint8_t i2c_num,
+             struct hal_i2c_master_data *pdata, uint32_t timeout,
+             uint8_t last_op);
+
+/**
+* Sends a start condition and reads <len> bytes of data on the i2c bus.
+* This API does NOT issue a stop condition unless `last_op` is set to `1`.
+* You must stop the bus after successful or unsuccessful write attempts.
+* This API is blocking until an error or NaK occurs. Timeout is platform
+* dependent.
+*
+* @param i2c_num The number of the I2C device being written to
+* @param pdata The location to place read data
+* @param timeout How long to wait for transaction to complete in ticks
+* @param last_op Master should send a STOP at the end to signify end of
+*        transaction.
+*
+* @return 0 on success, and non-zero error code on failure
+*/
+typedef int (*hal_i2c_sim_master_read_t)(uint8_t i2c_num,
+             struct hal_i2c_master_data *pdata,
+             uint32_t timeout, uint8_t last_op);
+
+struct hal_i2c_sim_driver {
+    hal_i2c_sim_master_write_t sd_write;
+    hal_i2c_sim_master_read_t sd_read;
+
+    /* 7-bit I2C device address */
+    uint8_t addr;
+
+    /* Reserved for future use */
+    uint8_t rsvd[3];
+
+    /* The next simulated sensor in the global sim driver list. */
+    SLIST_ENTRY(hal_i2c_sim_driver) s_next;
+};
+
+/**
+* Register a driver simulator
+*
+* @param drv The simulated driver to register
+*
+* @return 0 on success, non-zero on failure.
+*/
+int hal_i2c_sim_register(struct hal_i2c_sim_driver *drv);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_MCU_SIM_I2C_ */
diff --git a/hw/mcu/native/src/hal_i2c.c b/hw/mcu/native/src/hal_i2c.c
new file mode 100644
index 0000000000..5155fb7eb9
--- /dev/null
+++ b/hw/mcu/native/src/hal_i2c.c
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <stdio.h>
+#include "os/mynewt.h"
+#include "hal/hal_i2c.h"
+#include "mcu/mcu_sim_i2c.h"
+
+struct {
+    struct os_mutex mgr_lock;
+    SLIST_HEAD(, hal_i2c_sim_driver) mgr_sim_list;
+} hal_i2c_sim_mgr;
+
+int
+hal_i2c_init(uint8_t i2c_num, void *cfg)
+{
+    os_mutex_init(&hal_i2c_sim_mgr.mgr_lock);
+
+    return 0;
+}
+
+int
+hal_i2c_master_write(uint8_t i2c_num, struct hal_i2c_master_data *pdata,
+                     uint32_t timeout, uint8_t last_op)
+{
+    struct hal_i2c_sim_driver *cursor;
+
+    cursor = NULL;
+    SLIST_FOREACH(cursor, &hal_i2c_sim_mgr.mgr_sim_list, s_next) {
+        if (cursor->addr == pdata->address) {
+            /* Forward the read request to the sim driver */
+            return cursor->sd_write(i2c_num, pdata, timeout, last_op);
+        }
+    }
+
+    return 0;
+}
+
+int
+hal_i2c_master_read(uint8_t i2c_num, struct hal_i2c_master_data *pdata,
+                    uint32_t timeout, uint8_t last_op)
+{
+    struct hal_i2c_sim_driver *cursor;
+
+    cursor = NULL;
+    SLIST_FOREACH(cursor, &hal_i2c_sim_mgr.mgr_sim_list, s_next) {
+        if (cursor->addr == pdata->address) {
+            /* Forward the read request to the sim driver */
+            return cursor->sd_read(i2c_num, pdata, timeout, last_op);
+        }
+    }
+
+    return 0;
+}
+
+int
+hal_i2c_master_probe(uint8_t i2c_num, uint8_t address,
+                     uint32_t timeout)
+{
+    struct hal_i2c_sim_driver *cursor;
+
+    cursor = NULL;
+    SLIST_FOREACH(cursor, &hal_i2c_sim_mgr.mgr_sim_list, s_next) {
+        if (cursor->addr == address) {
+            return 0;
+        }
+    }
+
+    return -1;
+}
+
+/**
+ * Lock sim manager to access the list of simulated drivers
+ */
+int
+hal_i2c_sim_mgr_lock(void)
+{
+    int rc;
+
+    rc = os_mutex_pend(&hal_i2c_sim_mgr.mgr_lock, OS_TIMEOUT_NEVER);
+    if (rc == 0 || rc == OS_NOT_STARTED) {
+        return (0);
+    }
+    return (rc);
+}
+
+/**
+ * Unlock sim manager once the list of simulated drivers has been accessed
+ */
+void
+hal_i2c_sim_mgr_unlock(void)
+{
+    (void) os_mutex_release(&hal_i2c_sim_mgr.mgr_lock);
+}
+
+/**
+ * Insert a simulated driver into the list
+ */
+static void
+hal_i2c_sim_mgr_insert(struct hal_i2c_sim_driver *driver)
+{
+    SLIST_INSERT_HEAD(&hal_i2c_sim_mgr.mgr_sim_list, driver, s_next);
+}
+
+int
+hal_i2c_sim_register(struct hal_i2c_sim_driver *drv)
+{
+    int rc;
+
+    rc = hal_i2c_sim_mgr_lock();
+    if (rc != 0) {
+        goto err;
+    }
+
+    printf("Registering I2C sim driver for 0x%02X\n", drv->addr);
+    fflush(stdout);
+    hal_i2c_sim_mgr_insert(drv);
+
+    hal_i2c_sim_mgr_unlock();
+
+    return (0);
+err:
+    return (rc);
+}
diff --git a/hw/mcu/native/syscfg.yml b/hw/mcu/native/syscfg.yml
index e3209dd2c8..a7cac8c237 100644
--- a/hw/mcu/native/syscfg.yml
+++ b/hw/mcu/native/syscfg.yml
@@ -19,6 +19,10 @@
 # Package: hw/bsp/nrf52dk
 
 syscfg.defs:
+    I2C_0:
+        description: 'I2C interface 0'
+        value:  0
+
     MCU_FLASH_MIN_WRITE_SIZE:
         description: >
             Specifies the required alignment for internal flash writes.
diff --git a/test/i2c_scan/src/i2c_scan.c b/test/i2c_scan/src/i2c_scan.c
index 41428874c2..8420497a65 100644
--- a/test/i2c_scan/src/i2c_scan.c
+++ b/test/i2c_scan/src/i2c_scan.c
@@ -60,11 +60,7 @@ i2c_scan_cli_cmd(int argc, char **argv)
 
     /* Scan all valid I2C addresses (0x08..0x77) */
     for (addr = 0x08; addr < 0x78; addr++) {
-#ifndef ARCH_sim
         rc = hal_i2c_master_probe(i2cnum, addr, timeout);
-#else
-        (void)timeout;
-#endif
         /* Print addr header every 16 bytes */
         if (!(addr % 16)) {
             console_printf("\n%02x: ", addr);


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services