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/nuttx.git

commit c5be5c2db4a36479fc9d848348e4cbd8fdc85e2c
Author: Martin Vajnar <[email protected]>
AuthorDate: Thu Sep 12 12:55:00 2024 +0200

    esp32c6-devkitc: Add Quadrature Encoder support and example configuration
    
    Signed-off-by: Martin Vajnar <[email protected]>
---
 .../esp32c6/boards/esp32c6-devkitc/index.rst       | 10 +++
 .../esp32c6/common/include/esp_board_qencoder.h    | 74 +++++++++++++++++
 boards/risc-v/esp32c6/common/src/Make.defs         |  4 +
 .../risc-v/esp32c6/common/src/esp_board_qencoder.c | 97 ++++++++++++++++++++++
 .../esp32c6-devkitc/configs/qencoder/defconfig     | 58 +++++++++++++
 .../esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c  | 14 ++++
 6 files changed, 257 insertions(+)

diff --git 
a/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst 
b/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst
index 0536d88071..c809f71bd3 100644
--- a/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst
+++ b/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst
@@ -194,6 +194,16 @@ To test it, just execute the ``pwm`` application::
     pwm_main: starting output with frequency: 10000 duty: 00008000
     pwm_main: stopping output
 
+qencoder
+---
+
+This configuration demostrates the use of Quadrature Encoder connected to pins
+GPIO10 and GPIO11. You can start measurement of pulses using the following
+command (by default, it will open ``\dev\qe0`` device and print 20 samples
+using 1 second delay)::
+
+    nsh> qe
+
 rmt
 ---
 
diff --git a/boards/risc-v/esp32c6/common/include/esp_board_qencoder.h 
b/boards/risc-v/esp32c6/common/include/esp_board_qencoder.h
new file mode 100644
index 0000000000..15899d3063
--- /dev/null
+++ b/boards/risc-v/esp32c6/common/include/esp_board_qencoder.h
@@ -0,0 +1,74 @@
+/****************************************************************************
+ * boards/risc-v/esp32c6/common/include/esp_board_qencoder.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 __BOARDS_RISC_V_ESP32C6_COMMON_INCLUDE_ESP_BOARD_QENCODER_H
+#define __BOARDS_RISC_V_ESP32C6_COMMON_INCLUDE_ESP_BOARD_QENCODER_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_qencoder_initialize
+ *
+ * Description:
+ *   Initialize the quadrature encoder driver for the given timer
+ *
+ ****************************************************************************/
+
+int board_qencoder_initialize(void);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BOARDS_RISC_V_ESP32C6_COMMON_INCLUDE_ESP_BOARD_QENCODER_H */
+
diff --git a/boards/risc-v/esp32c6/common/src/Make.defs 
b/boards/risc-v/esp32c6/common/src/Make.defs
index 5ca961d384..2d2a487d5a 100644
--- a/boards/risc-v/esp32c6/common/src/Make.defs
+++ b/boards/risc-v/esp32c6/common/src/Make.defs
@@ -68,6 +68,10 @@ ifeq ($(CONFIG_CL_MFRC522),y)
   CSRCS += esp_board_mfrc522.c
 endif
 
+ifeq ($(CONFIG_ESP_PCNT),y)
+  CSRCS += esp_board_qencoder.c
+endif
+
 DEPPATH += --dep-path src
 VPATH += :src
 CFLAGS += 
${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src
diff --git a/boards/risc-v/esp32c6/common/src/esp_board_qencoder.c 
b/boards/risc-v/esp32c6/common/src/esp_board_qencoder.c
new file mode 100644
index 0000000000..b7e17b0069
--- /dev/null
+++ b/boards/risc-v/esp32c6/common/src/esp_board_qencoder.c
@@ -0,0 +1,97 @@
+/****************************************************************************
+ * boards/risc-v/esp32c6/common/src/esp_board_qencoder.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 <stdio.h>
+
+#include <nuttx/sensors/qencoder.h>
+#include <arch/board/board.h>
+
+#include "espressif/esp_qencoder.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_qencoder_initialize
+ *
+ * Description:
+ *   Initialize the quadrature encoder driver
+ *
+ ****************************************************************************/
+
+int board_qencoder_initialize(void)
+{
+  int ret;
+  char devpath[12];
+  int devno = 0;
+
+  /* Initialize a quadrature encoder interface. */
+#ifdef CONFIG_ESP_PCNT_U0_QE
+  snprintf(devpath, sizeof(devpath), "/dev/qe%d", devno++);
+  ret = esp_qeinitialize(devpath, 0);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: esp_qeinitialize failed: %d\n", ret);
+      return ret;
+    }
+#endif
+
+#ifdef CONFIG_ESP_PCNT_U1_QE
+  snprintf(devpath, sizeof(devpath), "/dev/qe%d", devno++);
+  ret = esp_qeinitialize(devpath, 1);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: esp_qeinitialize failed: %d\n", ret);
+      return ret;
+    }
+#endif
+
+#ifdef CONFIG_ESP_PCNT_U2_QE
+  snprintf(devpath, sizeof(devpath), "/dev/qe%d", devno++);
+  ret = esp_qeinitialize(devpath, 2);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: esp_qeinitialize failed: %d\n", ret);
+      return ret;
+    }
+#endif
+
+#ifdef CONFIG_ESP_PCNT_U3_QE
+  snprintf(devpath, sizeof(devpath), "/dev/qe%d", devno++);
+  ret = esp_qeinitialize(devpath, 3);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: esp_qeinitialize failed: %d\n", ret);
+      return ret;
+    }
+#endif
+
+  return ret;
+}
+
diff --git a/boards/risc-v/esp32c6/esp32c6-devkitc/configs/qencoder/defconfig 
b/boards/risc-v/esp32c6/esp32c6-devkitc/configs/qencoder/defconfig
new file mode 100644
index 0000000000..e93c8625c4
--- /dev/null
+++ b/boards/risc-v/esp32c6/esp32c6-devkitc/configs/qencoder/defconfig
@@ -0,0 +1,58 @@
+#
+# This file is autogenerated: PLEASE DO NOT EDIT IT.
+#
+# You can use "make menuconfig" to make any modifications to the installed 
.config file.
+# You can then do "make savedefconfig" to generate a new defconfig file that 
includes your
+# modifications.
+#
+# CONFIG_NSH_ARGCAT is not set
+# CONFIG_NSH_CMDOPT_HEXDUMP is not set
+CONFIG_ARCH="risc-v"
+CONFIG_ARCH_BOARD="esp32c6-devkitc"
+CONFIG_ARCH_BOARD_COMMON=y
+CONFIG_ARCH_BOARD_ESP32C6_DEVKITC=y
+CONFIG_ARCH_CHIP="esp32c6"
+CONFIG_ARCH_CHIP_ESP32C6=y
+CONFIG_ARCH_CHIP_ESP32C6WROOM1=y
+CONFIG_ARCH_INTERRUPTSTACK=2048
+CONFIG_ARCH_RISCV=y
+CONFIG_ARCH_STACKDUMP=y
+CONFIG_BOARDCTL_RESET=y
+CONFIG_BOARD_LOOPSPERMSEC=15000
+CONFIG_BUILTIN=y
+CONFIG_DEV_ZERO=y
+CONFIG_ESPRESSIF_ESP32C6=y
+CONFIG_ESP_PCNT=y
+CONFIG_ESP_PCNT_U0=y
+CONFIG_ESP_PCNT_U0_CH0_EDGE_PIN=10
+CONFIG_ESP_PCNT_U0_CH1_LEVEL_PIN=11
+CONFIG_ESP_PCNT_U0_FILTER_EN=y
+CONFIG_EXAMPLES_QENCODER=y
+CONFIG_EXAMPLES_QENCODER_DELAY=1000
+CONFIG_EXAMPLES_QENCODER_NSAMPLES=20
+CONFIG_FS_PROCFS=y
+CONFIG_IDLETHREAD_STACKSIZE=2048
+CONFIG_INIT_ENTRYPOINT="nsh_main"
+CONFIG_INTELHEX_BINARY=y
+CONFIG_LIBC_PERROR_STDOUT=y
+CONFIG_LIBC_STRERROR=y
+CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6
+CONFIG_NSH_ARCHINIT=y
+CONFIG_NSH_BUILTIN_APPS=y
+CONFIG_NSH_FILEIOSIZE=512
+CONFIG_NSH_READLINE=y
+CONFIG_NSH_STRERROR=y
+CONFIG_PREALLOC_TIMERS=0
+CONFIG_RR_INTERVAL=200
+CONFIG_SCHED_BACKTRACE=y
+CONFIG_SCHED_WAITPID=y
+CONFIG_SENSORS=y
+CONFIG_SENSORS_QENCODER=y
+CONFIG_START_DAY=29
+CONFIG_START_MONTH=11
+CONFIG_START_YEAR=2019
+CONFIG_SYSTEM_DUMPSTACK=y
+CONFIG_SYSTEM_NSH=y
+CONFIG_TESTING_GETPRIME=y
+CONFIG_TESTING_OSTEST=y
+CONFIG_UART0_SERIAL_CONSOLE=y
diff --git a/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c 
b/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c
index b197add1f9..fc7c9fde7d 100644
--- a/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c
+++ b/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c
@@ -93,6 +93,10 @@
 #  include "esp_board_mcpwm.h"
 #endif
 
+#ifdef CONFIG_ESP_PCNT_AS_QE
+#  include "esp_board_qencoder.h"
+#endif
+
 #include "esp32c6-devkitc.h"
 
 /****************************************************************************
@@ -356,6 +360,16 @@ int esp_bringup(void)
     }
 #endif
 
+#ifdef CONFIG_SENSORS_QENCODER
+  /* Initialize and register the qencoder driver */
+
+  ret = board_qencoder_initialize();
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: board_qencoder_initialize failed: %d\n", ret);
+    }
+#endif
+
   /* If we got here then perhaps not all initialization was successful, but
    * at least enough succeeded to bring-up NSH with perhaps reduced
    * capabilities.

Reply via email to