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/11/23 16:16:35 UTC
[mynewt-core] 03/26: hw/bus: Add initial I2C bus driver
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 1b26ea7f2bf9ce0d2d6c709164a2799b4cd34f41
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Nov 5 17:10:43 2018 +0100
hw/bus: Add initial I2C bus driver
---
hw/bus/i2c/include/bus/i2c.h | 162 ++++++++++++++++++++++++++++++++++
hw/bus/i2c/pkg.yml | 25 ++++++
hw/bus/i2c/src/i2c.c | 202 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 389 insertions(+)
diff --git a/hw/bus/i2c/include/bus/i2c.h b/hw/bus/i2c/include/bus/i2c.h
new file mode 100644
index 0000000..47050f2
--- /dev/null
+++ b/hw/bus/i2c/include/bus/i2c.h
@@ -0,0 +1,162 @@
+/*
+ * 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 HW_BUS_I2C_H_
+#define HW_BUS_I2C_H_
+
+#include <stddef.h>
+#include <stdint.h>
+#include "bus/bus.h"
+#include "bus/bus_driver.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define BUS_I2C_QUIRK_NEED_RESET_ON_TMO 0x0001
+
+/**
+ * Bus I2C device configuration
+ */
+struct bus_i2c_dev_cfg {
+ /** I2C interface number */
+ int i2c_num;
+ /** GPIO number of SDA line */
+ int pin_sda;
+ /** GPIO number of SCL line */
+ int pin_scl;
+};
+
+/**
+ * Bus I2C node configuration
+ */
+struct bus_i2c_node_cfg {
+ /** General node configuration */
+ struct bus_node_cfg node_cfg;
+ /** I2C address of node */
+ uint8_t addr;
+ /** I2C frequency to be used for node */
+ uint16_t freq;
+ /** Quirks to be applied for device */
+ uint16_t quirks;
+};
+
+/**
+ * Bus I2C device object state
+ *
+ * Contents of these objects are managed internally by bus driver and shall not
+ * be accessed directly.
+ */
+struct bus_i2c_dev {
+ struct bus_dev bdev;
+ struct bus_i2c_dev_cfg cfg;
+
+#if MYNEWT_VAL(BUS_DEBUG)
+ uint32_t devmagic;
+#endif
+};
+
+/**
+ * Bus I2C node object state
+ *
+ * Contents of these objects are managed internally by bus driver and shall not
+ * be accessed directly.
+ */
+struct bus_i2c_node {
+ struct bus_node bnode;
+ uint16_t freq;
+ uint16_t quirks;
+ uint8_t addr;
+
+#if MYNEWT_VAL(BUS_DEBUG)
+ uint32_t nodemagic;
+#endif
+};
+
+/**
+ * Initialize os_dev as bus I2C device
+ *
+ * This can be passed as a parameter to os_dev_create() when creating os_dev
+ * object for I2C device, however it's recommended to create devices using helper
+ * like bus_i2c_dev_create().
+ *
+ * @param node Node device object
+ * @param arg Node configuration struct (struct bus_node_cfg)
+ */
+int
+bus_i2c_dev_init_func(struct os_dev *odev, void *arg);
+
+/**
+ * Create bus I2C device
+ *
+ * This is a convenient helper and recommended way to create os_dev for bus I2C
+ * device instead of calling os_dev_create() directly.
+ *
+ * @param name Name of device
+ * @param dev Device state object
+ * @param cfg Configuration
+ */
+static inline int
+bus_i2c_dev_create(const char *name, struct bus_i2c_dev *dev,
+ struct bus_i2c_dev_cfg *cfg)
+{
+ struct os_dev *odev = (struct os_dev *)dev;
+
+ return os_dev_create(odev, name, OS_DEV_INIT_PRIMARY, 0,
+ bus_i2c_dev_init_func, cfg);
+}
+
+/**
+ * Initialize os_dev as bus I2C node
+ *
+ * This can be passed as a parameter to os_dev_create() when creating os_dev
+ * object for I2C node, however it's recommended to create devices using helper
+ * like bus_i2c_node_create().
+ *
+ * @param node Node device object
+ * @param arg Node configuration struct (struct bus_node_cfg)
+ */
+int
+bus_i2c_node_init_func(struct os_dev *odev, void *arg);
+
+/**
+ * Create bus I2C node
+ *
+ * This is a convenient helper and recommended way to create os_dev for bus I2C
+ * node instead of calling os_dev_create() directly.
+ *
+ * @param name Name of device
+ * @param node Node state object
+ * @param cfg Configuration
+ */
+static inline int
+bus_i2c_node_create(const char *name, struct bus_i2c_node *node,
+ const struct bus_i2c_node_cfg *cfg)
+{
+ struct os_dev *odev = (struct os_dev *)node;
+
+ return os_dev_create(odev, name, OS_DEV_INIT_PRIMARY, 1,
+ bus_i2c_node_init_func, (void *)cfg);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HW_BUS_I2C_H_ */
diff --git a/hw/bus/i2c/pkg.yml b/hw/bus/i2c/pkg.yml
new file mode 100644
index 0000000..2e2e0ac
--- /dev/null
+++ b/hw/bus/i2c/pkg.yml
@@ -0,0 +1,25 @@
+#
+# 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.
+#
+
+pkg.name: hw/bus/i2c
+pkg.description: I2C bus implementation
+pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+pkg.deps:
diff --git a/hw/bus/i2c/src/i2c.c b/hw/bus/i2c/src/i2c.c
new file mode 100644
index 0000000..bba3a85
--- /dev/null
+++ b/hw/bus/i2c/src/i2c.c
@@ -0,0 +1,202 @@
+/*
+ * 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 <assert.h>
+#include "defs/error.h"
+#include "hal/hal_i2c.h"
+#include "bus/bus.h"
+#include "bus/bus_debug.h"
+#include "bus/i2c.h"
+
+static int
+bus_i2c_enable(struct bus_dev *bdev)
+{
+ struct bus_i2c_dev *dev = (struct bus_i2c_dev *)bdev;
+ int rc;
+
+ BUS_DEBUG_VERIFY_DEV(dev);
+
+ rc = hal_i2c_enable(dev->cfg.i2c_num);
+ if (rc) {
+ return SYS_EINVAL;
+ }
+
+ return 0;
+}
+
+static int
+bus_i2c_configure(struct bus_dev *bdev, struct bus_node *bnode)
+{
+ struct bus_i2c_dev *dev = (struct bus_i2c_dev *)bdev;
+ struct bus_i2c_node *node = (struct bus_i2c_node *)bnode;
+ struct bus_i2c_node *current_node = (struct bus_i2c_node *)bdev->configured_for;
+ struct hal_i2c_settings i2c_cfg;
+ int rc;
+
+ BUS_DEBUG_VERIFY_DEV(dev);
+ BUS_DEBUG_VERIFY_NODE(node);
+
+ if (current_node && (current_node->freq == node->freq)) {
+ return 0;
+ }
+
+ rc = hal_i2c_disable(dev->cfg.i2c_num);
+ if (rc) {
+ goto done;
+ }
+
+ i2c_cfg.frequency = node->freq;
+
+ rc = hal_i2c_config(dev->cfg.i2c_num, &i2c_cfg);
+ if (rc) {
+ goto done;
+ }
+
+ rc = hal_i2c_enable(dev->cfg.i2c_num);
+
+done:
+ if (rc) {
+ rc = SYS_EIO;
+ }
+
+ return rc;
+}
+
+static int
+bus_i2c_read(struct bus_dev *bdev, struct bus_node *bnode, uint8_t *buf,
+ uint16_t length, os_time_t timeout, uint16_t flags)
+{
+ struct bus_i2c_dev *dev = (struct bus_i2c_dev *)bdev;
+ struct bus_i2c_node *node = (struct bus_i2c_node *)bnode;
+ struct hal_i2c_master_data i2c_data;
+ uint8_t last_op;
+ int rc;
+
+ BUS_DEBUG_VERIFY_DEV(dev);
+ BUS_DEBUG_VERIFY_NODE(node);
+
+ i2c_data.address = node->addr;
+ i2c_data.buffer = buf;
+ i2c_data.len = length;
+
+ last_op = !(flags & BUS_F_NOSTOP);
+
+ rc = hal_i2c_master_read(dev->cfg.i2c_num, &i2c_data,
+ os_time_ticks_to_ms32(timeout), last_op);
+
+ return rc;
+}
+
+static int
+bus_i2c_write(struct bus_dev *bdev, struct bus_node *bnode, uint8_t *buf,
+ uint16_t length, os_time_t timeout, uint16_t flags)
+{
+ struct bus_i2c_dev *dev = (struct bus_i2c_dev *)bdev;
+ struct bus_i2c_node *node = (struct bus_i2c_node *)bnode;
+ struct hal_i2c_master_data i2c_data;
+ uint8_t last_op;
+ int rc;
+
+ BUS_DEBUG_VERIFY_DEV(dev);
+ BUS_DEBUG_VERIFY_NODE(node);
+
+ i2c_data.address = node->addr;
+ i2c_data.buffer = buf;
+ i2c_data.len = length;
+
+ last_op = !(flags & BUS_F_NOSTOP);
+
+ rc = hal_i2c_master_write(dev->cfg.i2c_num, &i2c_data,
+ os_time_ticks_to_ms32(timeout), last_op);
+
+ return rc;
+}
+
+static int bus_i2c_disable(struct bus_dev *bdev)
+{
+ struct bus_i2c_dev *dev = (struct bus_i2c_dev *)bdev;
+ int rc;
+
+ BUS_DEBUG_VERIFY_DEV(dev);
+
+ rc = hal_i2c_disable(dev->cfg.i2c_num);
+ if (rc) {
+ return SYS_EINVAL;
+ }
+
+ return 0;
+}
+
+static const struct bus_dev_ops bus_i2c_ops = {
+ .enable = bus_i2c_enable,
+ .configure = bus_i2c_configure,
+ .read = bus_i2c_read,
+ .write = bus_i2c_write,
+ .disable = bus_i2c_disable,
+};
+
+int
+bus_i2c_dev_init_func(struct os_dev *odev, void *arg)
+{
+ struct bus_i2c_dev *dev = (struct bus_i2c_dev *)odev;
+ struct bus_i2c_dev_cfg *cfg = arg;
+ struct hal_i2c_hw_settings hal_cfg;
+ int rc;
+
+ hal_cfg.pin_scl = cfg->pin_scl;
+ hal_cfg.pin_sda = cfg->pin_sda;
+ rc = hal_i2c_init_hw(cfg->i2c_num, &hal_cfg);
+ if (rc) {
+ return SYS_EINVAL;
+ }
+
+ rc = bus_dev_init_func(odev, (void*)&bus_i2c_ops);
+ assert(rc == 0);
+
+ BUS_DEBUG_POISON_DEV(dev);
+
+ dev->cfg = *cfg;
+
+ rc = hal_i2c_enable(dev->cfg.i2c_num);
+ assert(rc == 0);
+
+ return 0;
+}
+
+int
+bus_i2c_node_init_func(struct os_dev *odev, void *arg)
+{
+ struct bus_i2c_node *node = (struct bus_i2c_node *)odev;
+ struct bus_i2c_node_cfg *cfg = arg;
+ struct bus_node_cfg *node_cfg = &cfg->node_cfg;
+ int rc;
+
+ rc = bus_node_init_func(odev, node_cfg);
+ if (rc) {
+ return rc;
+ }
+
+ BUS_DEBUG_POISON_NODE(node);
+
+ node->freq = cfg->freq;
+ node->addr = cfg->addr;
+ node->quirks = cfg->quirks;
+
+ return 0;
+}