You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/04/15 14:55:36 UTC

[incubator-nuttx] 04/05: boards/risc-v/esp32c3: ESP32C3 TWAI (CAN) controller bringup for DevKitM-1.

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

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

commit 0d9d2f4bf8d45b5380f61947855c46f9dd2fc8ae
Author: Jan Charvat <ja...@gmail.com>
AuthorDate: Wed Apr 6 12:22:18 2022 +0200

    boards/risc-v/esp32c3: ESP32C3 TWAI (CAN) controller bringup for DevKitM-1.
    
    Signed-off-by: Jan Charvat <ja...@gmail.com>
---
 boards/risc-v/esp32c3/esp32c3-devkit/src/Makefile  |  4 +
 .../esp32c3/esp32c3-devkit/src/esp32c3-devkit.h    | 16 ++++
 .../esp32c3/esp32c3-devkit/src/esp32c3_bringup.c   | 11 +++
 .../esp32c3/esp32c3-devkit/src/esp32c3_twai.c      | 93 ++++++++++++++++++++++
 4 files changed, 124 insertions(+)

diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/src/Makefile b/boards/risc-v/esp32c3/esp32c3-devkit/src/Makefile
index 61ae8cff6f..729ba52994 100644
--- a/boards/risc-v/esp32c3/esp32c3-devkit/src/Makefile
+++ b/boards/risc-v/esp32c3/esp32c3-devkit/src/Makefile
@@ -62,6 +62,10 @@ ifeq ($(CONFIG_I2C_DRIVER),y)
 CSRCS += esp32c3_i2c.c
 endif
 
+ifeq ($(CONFIG_CAN),y)
+CSRCS += esp32c3_twai.c
+endif
+
 ifeq ($(CONFIG_SENSORS_BMP180),y)
 CSRCS += esp32c3_bmp180.c
 endif
diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3-devkit.h b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3-devkit.h
index a58b8e4e26..582eba795c 100644
--- a/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3-devkit.h
+++ b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3-devkit.h
@@ -149,6 +149,22 @@ int board_spislavedev_initialize(int bus);
 int board_i2c_init(void);
 #endif
 
+/****************************************************************************
+ * Name: board_twai_setup
+ *
+ * Description:
+ *   Configure the TWAI driver.
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; A negated errno value is returned
+ *   to indicate the nature of any failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_CAN
+int esp32c3_twai_setup(void);
+#endif
+
 /****************************************************************************
  * Name: board_oneshot_init
  *
diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c
index 0ecca7b81a..b7b5f5e8fe 100644
--- a/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c
+++ b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c
@@ -220,6 +220,17 @@ int esp32c3_bringup(void)
     }
 #endif
 
+#ifdef CONFIG_CAN
+
+  /* Initialize TWAI and register the TWAI driver. */
+
+  ret = esp32c3_twai_setup();
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: esp32c3_twai_setup failed: %d\n", ret);
+    }
+#endif
+
 #ifdef CONFIG_SENSORS_BMP180
   /* Try to register BMP180 device in I2C0 */
 
diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_twai.c b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_twai.c
new file mode 100644
index 0000000000..6c284b63cb
--- /dev/null
+++ b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_twai.c
@@ -0,0 +1,93 @@
+/****************************************************************************
+ * boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_twai.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 <errno.h>
+#include <debug.h>
+
+#include <nuttx/can/can.h>
+#include <arch/board/board.h>
+
+#include "chip.h"
+/* #include "arm_arch.h" */
+
+#include "esp32c3_twai.h"
+#include "esp32c3-devkit.h"
+
+#ifdef CONFIG_CAN
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Configuration ************************************************************/
+
+#define TWAI_PORT0 0
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_twai_setup
+ *
+ * Description:
+ *  Initialize TWAI and register the TWAI device
+ *
+ ****************************************************************************/
+
+int esp32c3_twai_setup(void)
+{
+#ifdef CONFIG_ESP32C3_TWAI0
+  struct can_dev_s *twai;
+  int ret;
+
+  /* Call esp32c3_twaiinitialize() to get an instance of the TWAI0
+   * interface
+   * */
+
+  twai = esp32c3_twaiinitialize(TWAI_PORT0);
+  if (twai == NULL)
+    {
+      canerr("ERROR:  Failed to get TWAI0 interface\n");
+      return -ENODEV;
+    }
+
+  /* Register the TWAI0 driver at "/dev/can0" */
+
+  ret = can_register("/dev/can0", twai);
+  if (ret < 0)
+    {
+      canerr("ERROR: TWAI0 register failed: %d\n", ret);
+      return ret;
+    }
+
+  return OK;
+#else
+  return -ENODEV;
+#endif
+}
+
+#endif /* CONFIG_CAN */