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/01/13 13:29:11 UTC

[GitHub] [incubator-nuttx] pkarashchenko commented on a change in pull request #5219: arch/arm/samv7: implement quadrature encoder driver

pkarashchenko commented on a change in pull request #5219:
URL: https://github.com/apache/incubator-nuttx/pull/5219#discussion_r783956840



##########
File path: arch/arm/src/samv7/sam_qencoder.c
##########
@@ -0,0 +1,378 @@
+/****************************************************************************
+ * arch/arm/src/samv7/sam_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/sensors/qencoder.h>
+
+#include <arch/board/board.h>
+
+#include "chip.h"
+#include "arm_internal.h"
+#include "arm_arch.h"
+
+#include "sam_tc.h"
+#include "sam_qencoder.h"
+
+#ifdef CONFIG_SENSORS_QENCODER
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Debug ********************************************************************/
+
+/* Non-standard debug that may be enabled just for testing the quadrature
+ * encoder
+ */
+
+#ifndef CONFIG_DEBUG_FEATURES
+#  undef CONFIG_DEBUG_SENSORS
+#endif
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* Overall, RAM-based state structure */
+
+struct sam_lowerhalf_s
+{
+  /* The first field of this state structure must be a pointer to the lower-
+   * half callback structure:
+   */
+
+  FAR const struct qe_ops_s *ops;  /* Lower half callback structure */
+
+  /* SAMV7 driver-specific fields: */
+
+  uint8_t          tcid;         /* Timer counter ID {0,1,2,3} */
+  TC_HANDLE        tch;          /* Timer channel {0,3,6,9} */
+
+  bool             inuse;        /* True: The lower-half driver is in-use */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Helper functions */
+
+static FAR struct sam_lowerhalf_s *sam_tc2lower(int tc);
+
+/* Lower-half Quadrature Encoder Driver Methods */
+
+static int sam_setup(FAR struct qe_lowerhalf_s *lower);
+static int sam_shutdown(FAR struct qe_lowerhalf_s *lower);
+static int sam_position(FAR struct qe_lowerhalf_s *lower, FAR int32_t *pos);
+static int sam_reset(FAR struct qe_lowerhalf_s *lower);
+static int sam_ioctl(FAR struct qe_lowerhalf_s *lower, int cmd,
+                     unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* The lower half callback structure */
+
+static const struct qe_ops_s g_qecallbacks =
+{
+  .setup     = sam_setup,
+  .shutdown  = sam_shutdown,
+  .position  = sam_position,
+  .setposmax = NULL,            /* not supported yet */
+  .reset     = sam_reset,
+  .setindex  = NULL,            /* not supported yet */
+  .ioctl     = sam_ioctl,
+};
+
+/* Per-timer state structures */
+
+#ifdef CONFIG_SAMV7_TC0_QE
+static struct sam_lowerhalf_s g_tc0lower =
+{
+  .ops      = &g_qecallbacks,
+  .tcid     = 0,
+  .inuse    = false,
+};
+#endif
+
+#ifdef CONFIG_SAMV7_TC1_QE
+static struct sam_lowerhalf_s g_tc1lower =
+{
+  .ops      = &g_qecallbacks,
+  .timid    = 1,
+  .inuse    = false,
+};
+#endif
+
+#ifdef CONFIG_SAMV7_TC2_QE
+static struct sam_lowerhalf_s g_tc2lower =
+{
+  .ops      = &g_qecallbacks,
+  .tcid     = 2,
+  .inuse    = false,
+};
+#endif
+
+#ifdef CONFIG_SAMV7_TC3_QE
+static struct sam_lowerhalf_s g_tc3lower =
+{
+  .ops      = &g_qecallbacks,
+  .tcid     = 3,
+  .inuse    = false,
+};
+#endif
+
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: sam_tc2lower
+ *
+ * Description:
+ *   Map a timer counter number to a device structure
+ *
+ ****************************************************************************/
+
+static FAR struct sam_lowerhalf_s *sam_tc2lower(int tc)
+{
+  switch (tc)
+    {
+#ifdef CONFIG_SAMV7_TC0_QE
+    case 0:
+      return &g_tc0lower;
+#endif
+#ifdef CONFIG_SAMV7_TC1_QE
+    case 1:
+      return &g_tc1lower;
+#endif
+#ifdef CONFIG_SAMV7_TC2_QE
+    case 2:
+      return &g_tc2lower;
+#endif
+#ifdef CONFIG_SAMV7_TC3_QE
+    case 3:
+      return &g_tc3lower;
+#endif
+    default:
+      return NULL;
+    }
+}
+
+/****************************************************************************
+ * Name: sam_setup
+ *
+ * Description:
+ *   This method is called when the driver is opened.  The lower half driver
+ *   should configure and initialize the device so that it is ready for use.
+ *   The initial position value should be zero.
+ *
+ ****************************************************************************/
+
+static int sam_setup(FAR struct qe_lowerhalf_s *lower)
+{
+  FAR struct sam_lowerhalf_s *priv = (FAR struct sam_lowerhalf_s *)lower;
+
+  /* Start the counter */
+
+  sam_tc_start(priv->tch);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: sam_shutdown
+ *
+ * Description:
+ *   This method is called when the driver is closed.  The lower half driver
+ *   should stop data collection, free any resources, disable timer hardware,
+ *   and put the system into the lowest possible power usage state
+ *
+ ****************************************************************************/
+
+static int sam_shutdown(FAR struct qe_lowerhalf_s *lower)
+{
+  FAR struct sam_lowerhalf_s *priv = (FAR struct sam_lowerhalf_s *)lower;
+
+  sam_tc_stop(priv->tch);
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: sam_position
+ *
+ * Description:
+ *   Return the current position measurement.
+ *
+ ****************************************************************************/
+
+static int sam_position(FAR struct qe_lowerhalf_s *lower, FAR int32_t *pos)
+{
+  FAR struct sam_lowerhalf_s *priv = (FAR struct sam_lowerhalf_s *)lower;
+
+  /* Return the counter value */
+
+  *pos = (int32_t)(int16_t)sam_tc_getcounter(priv->tch);

Review comment:
       The only question is should this be `*pos = (int32_t)sam_tc_getcounter(priv->tch);` or `*pos = (int32_t)(int16_t)sam_tc_getcounter(priv->tch);`?




-- 
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