You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2021/01/26 16:59:42 UTC

[incubator-nuttx] 01/02: arch: cxd56xx: Add I2C bitbang lower driver

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

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 7723ce46ceb59f8e87fb22630017427b8288aa0d
Author: baggio63446333 <ba...@users.noreply.github.com>
AuthorDate: Tue Jan 26 11:49:15 2021 +0900

    arch: cxd56xx: Add I2C bitbang lower driver
    
    Add I2C bitbang lower driver for cxd56xx.
---
 arch/arm/src/cxd56xx/Make.defs           |   4 +
 arch/arm/src/cxd56xx/cxd56_i2c_bitbang.c | 141 +++++++++++++++++++++++++++++++
 arch/arm/src/cxd56xx/cxd56_i2c_bitbang.h |  51 +++++++++++
 3 files changed, 196 insertions(+)

diff --git a/arch/arm/src/cxd56xx/Make.defs b/arch/arm/src/cxd56xx/Make.defs
index 66d3142..4f56e6e 100644
--- a/arch/arm/src/cxd56xx/Make.defs
+++ b/arch/arm/src/cxd56xx/Make.defs
@@ -149,6 +149,10 @@ ifeq ($(CONFIG_CXD56_I2C),y)
 CHIP_CSRCS += cxd56_i2c.c
 endif
 
+ifeq ($(CONFIG_I2C_BITBANG),y)
+CHIP_CSRCS += cxd56_i2c_bitbang.c
+endif
+
 ifeq ($(CONFIG_CXD56_DMAC),y)
 CHIP_CSRCS += cxd56_dmac.c
 endif
diff --git a/arch/arm/src/cxd56xx/cxd56_i2c_bitbang.c b/arch/arm/src/cxd56xx/cxd56_i2c_bitbang.c
new file mode 100644
index 0000000..e045b43
--- /dev/null
+++ b/arch/arm/src/cxd56xx/cxd56_i2c_bitbang.c
@@ -0,0 +1,141 @@
+/****************************************************************************
+ * arch/arm/src/cxd56/cxd56_i2c_bitbang.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/i2c/i2c_master.h>
+#include <nuttx/i2c/i2c_bitbang.h>
+#include <nuttx/kmalloc.h>
+#include <arch/board/board.h>
+#include <arch/chip/pin.h>
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct cxd56_i2c_bitbang_dev_s
+{
+  struct i2c_bitbang_lower_dev_s lower;
+  uint32_t sda_pin;
+  uint32_t scl_pin;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static void i2c_bb_initialize(FAR struct i2c_bitbang_lower_dev_s *lower);
+
+static void i2c_bb_set_scl(FAR struct i2c_bitbang_lower_dev_s *lower,
+                           bool high);
+static void i2c_bb_set_sda(FAR struct i2c_bitbang_lower_dev_s *lower,
+                           bool high);
+
+static bool i2c_bb_get_scl(FAR struct i2c_bitbang_lower_dev_s *lower);
+static bool i2c_bb_get_sda(FAR struct i2c_bitbang_lower_dev_s *lower);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+const static struct i2c_bitbang_lower_ops_s g_ops =
+{
+  .initialize = i2c_bb_initialize,
+  .set_scl    = i2c_bb_set_scl,
+  .set_sda    = i2c_bb_set_sda,
+  .get_scl    = i2c_bb_get_scl,
+  .get_sda    = i2c_bb_get_sda
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void i2c_bb_initialize(FAR struct i2c_bitbang_lower_dev_s *lower)
+{
+  struct cxd56_i2c_bitbang_dev_s *dev = lower->priv;
+
+  /* Set to input enable and pull-up */
+
+  board_gpio_config(dev->scl_pin, 0, true, true, PIN_PULLUP);
+  board_gpio_config(dev->sda_pin, 0, true, true, PIN_PULLUP);
+}
+
+static void i2c_bb_set_scl(FAR struct i2c_bitbang_lower_dev_s *lower,
+                           bool high)
+{
+  struct cxd56_i2c_bitbang_dev_s *dev = lower->priv;
+  int value;
+
+  /* If set high, pin is pulled up by output disable */
+
+  value = (high) ? -1 : 0;
+  board_gpio_write(dev->scl_pin, value);
+}
+
+static void i2c_bb_set_sda(FAR struct i2c_bitbang_lower_dev_s *lower,
+                           bool high)
+{
+  struct cxd56_i2c_bitbang_dev_s *dev = lower->priv;
+  int value;
+
+  /* If set high, pin is pulled up by output disable */
+
+  value = (high) ? -1 : 0;
+  board_gpio_write(dev->sda_pin, value);
+}
+
+static bool i2c_bb_get_scl(FAR struct i2c_bitbang_lower_dev_s *lower)
+{
+  struct cxd56_i2c_bitbang_dev_s *dev = lower->priv;
+
+  return board_gpio_read(dev->scl_pin);
+}
+
+static bool i2c_bb_get_sda(FAR struct i2c_bitbang_lower_dev_s *lower)
+{
+  struct cxd56_i2c_bitbang_dev_s *dev = lower->priv;
+
+  return board_gpio_read(dev->sda_pin);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+struct i2c_master_s *cxd56_i2c_bitbang_initialize(uint32_t sda_pin,
+                                                  uint32_t scl_pin)
+{
+  struct cxd56_i2c_bitbang_dev_s *dev =
+      (struct cxd56_i2c_bitbang_dev_s *)
+          kmm_zalloc(sizeof(struct cxd56_i2c_bitbang_dev_s));
+
+  DEBUGASSERT(dev);
+
+  dev->lower.ops = &g_ops;
+  dev->lower.priv = dev;
+  dev->scl_pin = scl_pin;
+  dev->sda_pin = sda_pin;
+
+  return i2c_bitbang_initialize(&dev->lower);
+}
diff --git a/arch/arm/src/cxd56xx/cxd56_i2c_bitbang.h b/arch/arm/src/cxd56xx/cxd56_i2c_bitbang.h
new file mode 100644
index 0000000..1311552
--- /dev/null
+++ b/arch/arm/src/cxd56xx/cxd56_i2c_bitbang.h
@@ -0,0 +1,51 @@
+/****************************************************************************
+ * arch/arm/src/cxd56/cxd56_i2c_bitbang.h
+ *
+ * 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 __ARCH_ARM_SRC_CXD56_CXD56_I2C_BITBANG_H
+#define __ARCH_ARM_SRC_CXD56_CXD56_I2C_BITBANG_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/i2c/i2c_master.h>
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+struct i2c_master_s *cxd56_i2c_bitbang_initialize(uint32_t sda_pin,
+                                                  uint32_t scl_pin);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ARCH_ARM_SRC_CXD56_CXD56_I2C_BITBANG_H */