You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/10/17 12:21:16 UTC

[GitHub] [incubator-nuttx] tmedicci commented on a diff in pull request #7328: esp32: Add support to Quadrature Encoder

tmedicci commented on code in PR #7328:
URL: https://github.com/apache/incubator-nuttx/pull/7328#discussion_r996990918


##########
arch/xtensa/src/esp32/esp32_qencoder.c:
##########
@@ -0,0 +1,840 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32/esp32_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 <stdint.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+#include <inttypes.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/sensors/qencoder.h>
+
+#include <arch/board/board.h>
+
+#include "chip.h"
+#include "esp32_gpio.h"
+#include "esp32_irq.h"
+
+#include "xtensa.h"
+#include "hardware/esp32_soc.h"
+#include "hardware/esp32_dport.h"
+#include "hardware/esp32_pinmap.h"
+#include "hardware/esp32_gpio_sigmap.h"
+#include "hardware/esp32_pcnt.h"
+#include "esp32_qencoder.h"
+
+#ifdef CONFIG_SENSORS_QENCODER
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Input filter *************************************************************/
+
+#ifdef CONFIG_ESP32_PCNT_U0_QE
+#  ifndef CONFIG_ESP32_PCNT_U0_FILTER_EN
+#    warning "Glitch Filter is recommended for Quadrature Encoder on PCNT_U0"
+#  endif
+#endif
+
+#ifdef CONFIG_ESP32_PCNT_U1_QE
+#  ifndef CONFIG_ESP32_PCNT_U1_FILTER_EN
+#    warning "Glitch Filter is recommended for Quadrature Encoder on PCNT_U1"
+#  endif
+#endif
+
+#ifdef CONFIG_ESP32_PCNT_U2_QE
+#  ifndef CONFIG_ESP32_PCNT_U2_FILTER_EN
+#    warning "Glitch Filter is recommended for Quadrature Encoder on PCNT_U2"
+#  endif
+#endif
+
+#ifdef CONFIG_ESP32_PCNT_U3_QE
+#  ifndef CONFIG_ESP32_PCNT_U3_FILTER_EN
+#    warning "Glitch Filter is recommended for Quadrature Encoder on PCNT_U3"
+#  endif
+#endif
+
+#ifdef CONFIG_ESP32_PCNT_U4_QE
+#  ifndef CONFIG_ESP32_PCNT_U4_FILTER_EN
+#    warning "Glitch Filter is recommended for Quadrature Encoder on PCNT_U4"
+#  endif
+#endif
+
+#ifdef CONFIG_ESP32_PCNT_U5_QE
+#  ifndef CONFIG_ESP32_PCNT_U5_FILTER_EN
+#    warning "Glitch Filter is recommended for Quadrature Encoder on PCNT_U5"
+#  endif
+#endif
+
+#ifdef CONFIG_ESP32_PCNT_U6_QE
+#  ifndef CONFIG_ESP32_PCNT_U6_FILTER_EN
+#    warning "Glitch Filter is recommended for Quadrature Encoder on PCNT_U6"
+#  endif
+#endif
+
+#ifdef CONFIG_ESP32_PCNT_U7_QE
+#  ifndef CONFIG_ESP32_PCNT_U7_FILTER_EN
+#    warning "Glitch Filter is recommended for Quadrature Encoder on PCNT_U7"
+#  endif
+#endif
+
+/* Debug ********************************************************************/
+
+/* Non-standard debug that may be enabled just for testing the quadrature
+ * encoder
+ */
+
+#ifndef CONFIG_DEBUG_FEATURES
+#  undef CONFIG_DEBUG_SENSORS
+#endif
+
+#ifdef CONFIG_DEBUG_SENSORS
+#  ifdef CONFIG_DEBUG_INFO
+#    define qe_dumpgpio(p,m)    esp32_dumpgpio(p,m)
+#  else
+#    define qe_dumpgpio(p,m)
+#  endif
+#else
+#  define qe_dumpgpio(p,m)
+#endif
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* Constant configuration structure that is retained in FLASH */
+
+struct esp32_qeconfig_s
+{
+  uint8_t   pcntid;        /* PCNT ID {0,1,2,3,4,5,6,7} */
+  uint8_t   ch0_gpio;      /* Channel 0 gpio pin (Edge/Pulse) */
+  uint8_t   ch1_gpio;      /* Channel 1 gpio pin (Level/Ctrl) */
+  uint32_t  ch0_pulse_sig; /* ch0 pulse signal index */
+  uint32_t  ch0_ctrl_sig;  /* ch0 ctrl signal index */
+  uint32_t  ch1_pulse_sig; /* ch1 pulse signal index */
+  uint32_t  ch1_ctrl_sig;  /* ch1 ctrl signal index */
+  uint16_t  filter_thres;  /* Filter threshold for this PCNT Unit */
+};
+
+/* NOTE: we are using Quadrature Encoder in X4 mode on ESP32 PCNT, then
+ * instead of using 'pulse_gpio' and 'ctrl_gpio' names, we only use ch0_gpio
+ * and ch1_gpio names. It avoid confusion, since the same signal that is used
+ * on pin 'pulse' of CH0 is also connected to 'ctrl' pin of the CH1 and
+ * 'ctrl' pin of CH0 is also connected on 'pulse' pin of CH1.
+ */
+
+/* Overall, RAM-based state structure */
+
+struct esp32_lowerhalf_s
+{
+  /* The first field of this state structure must be a pointer to the
+   * lower-half callback structure:
+   */
+
+  const struct qe_ops_s *ops; /* Lower half callback structure */
+
+  /* ESP32 driver-specific fields: */
+
+  const struct esp32_qeconfig_s *config; /* static configuration */
+
+  bool inuse; /* True: The lower-half driver is in-use */
+
+  volatile int32_t position; /* The current position offset */
+
+  spinlock_t lock; /* Device specific lock. */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Helper functions */
+
+#if defined(CONFIG_DEBUG_SENSORS) && defined(CONFIG_DEBUG_INFO)
+static void esp32_dumpregs(struct esp32_lowerhalf_s *priv,
+                           const char *msg);
+#else
+#  define esp32_dumpregs(priv,msg)
+#endif
+
+static struct esp32_lowerhalf_s *esp32_pcnt2lower(int pcnt);
+
+/* Interrupt handling */
+
+#if 0 /* FIXME: To be implement */

Review Comment:
   typo: `/* FIXME: To be implemented */`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org